Changed: #1111 Create a CMake macro to check if we should link to an external lib or not

This commit is contained in:
kervala 2010-10-03 10:04:36 +02:00
parent 31a990fa60
commit 04ba071785

View file

@ -1,20 +1,5 @@
# Macros to check if a library needs to be manually linked to another one # Macros to check if a library needs to be manually linked to another one
# because it's using a symbol from it but it's not linked to # because it's using a symbol from it but it's not linked to
#
# Syntax:
# CHECK_DEPENDS(MYLIBRARY OTHERLIBRARY SYMBOL MUSTLINK)
#
# Sets the following variables:
# XYZ_LIBRARY_MUSTLINK
#
# Example:
# Check if we need to explicitly link to libz with libpng
# CHECK_DEPENDS(PNG_LIBRARY ZLIB_LIBRARY inflate)
# objdump -p /usr/lib/libwwwxml.so | grep expat
# objdump -T /usr/lib/libwwwxml.so | grep "*UND*" | grep XML_ParserCreate
# objdump -p <lib> | grep <libray>
# objdump -T <lib> | grep "*UND*" | grep <function>
#
# CHECK_UNDEFINED_SYMBOL # CHECK_UNDEFINED_SYMBOL
# Macro to check if a library is calling an undefined symbol # Macro to check if a library is calling an undefined symbol
@ -35,15 +20,24 @@ MACRO(CHECK_UNDEFINED_SYMBOL MYLIBRARY SYMBOL SYMBOL_FOUND)
ELSEIF(UNIX) ELSEIF(UNIX)
IF(CMAKE_OBJDUMP) IF(CMAKE_OBJDUMP)
# Use objdump to check if a library is using an external symbol # Use objdump to check if a library is using an external symbol
EXEC_PROGRAM(${CMAKE_OBJDUMP} ARGS "-T ${MYLIBRARY} | grep ${SYMBOL}" OUTPUT_VARIABLE OBJDUMP_SYMBOL) EXEC_PROGRAM(${CMAKE_OBJDUMP} ARGS "-T ${${MYLIBRARY}} | grep ${SYMBOL}" OUTPUT_VARIABLE OBJDUMP_SYMBOL)
message(STATUS "OBJDUMP_SYMBOL = ${OBJDUMP_SYMBOL}")
IF(NOT OBJDUMP_SYMBOL MATCHES "UND") IF(NOT OBJDUMP_SYMBOL MATCHES "UND")
SET(${SYMBOL_FOUND} FALSE) SET(${SYMBOL_FOUND} FALSE)
ENDIF(OBJDUMP_SYMBOL MATCHES "UND") ENDIF(NOT OBJDUMP_SYMBOL MATCHES "UND")
ENDIF(CMAKE_OBJDUMP) ENDIF(CMAKE_OBJDUMP)
ENDIF(WIN32) ENDIF(WIN32)
ENDMACRO(CHECK_UNDEFINED_SYMBOL) ENDMACRO(CHECK_UNDEFINED_SYMBOL)
# CHECK_LINKED_LIBRARY
# Macro to check if a library is linked to another one
#
# Syntax:
# CHECK_LINKED_LIBRARY(MYLIBRARY OTHERLIBRARY LIBRARY_FOUND)
# LIBRARY_FOUND will be set to TRUE or FALSE
#
# Example:
# CHECK_LINKED_LIBRARY(PNG_LIBRARY ZLIB_LIBRARY ZLIB_FOUND)
#
MACRO(CHECK_LINKED_LIBRARY MYLIBRARY OTHERLIBRARY LIBRARY_FOUND) MACRO(CHECK_LINKED_LIBRARY MYLIBRARY OTHERLIBRARY LIBRARY_FOUND)
SET(${LIBRARY_FOUND} FALSE) SET(${LIBRARY_FOUND} FALSE)
IF(WIN32) IF(WIN32)
@ -52,8 +46,9 @@ MACRO(CHECK_LINKED_LIBRARY MYLIBRARY OTHERLIBRARY LIBRARY_FOUND)
# TODO: use otool to detect if a library is using a symbol # TODO: use otool to detect if a library is using a symbol
ELSEIF(UNIX) ELSEIF(UNIX)
IF(CMAKE_OBJDUMP) IF(CMAKE_OBJDUMP)
GET_FILENAME_COMPONENT(LIBNAME ${${OTHERLIBRARY}} NAME)
# Use objdump to check if a library is linked to another library # Use objdump to check if a library is linked to another library
EXEC_PROGRAM(${CMAKE_OBJDUMP} ARGS "-p ${MYLIBRARY} | grep ${OTHERLIBRARY}" OUTPUT_VARIABLE OBJDUMP_LIBRARY) EXEC_PROGRAM(${CMAKE_OBJDUMP} ARGS "-p ${${MYLIBRARY}} | grep ${LIBNAME}" OUTPUT_VARIABLE OBJDUMP_LIBRARY)
IF(OBJDUMP_LIBRARY MATCHES "NEEDED") IF(OBJDUMP_LIBRARY MATCHES "NEEDED")
SET(${LIBRARY_FOUND} TRUE) SET(${LIBRARY_FOUND} TRUE)
ENDIF(OBJDUMP_LIBRARY MATCHES "NEEDED") ENDIF(OBJDUMP_LIBRARY MATCHES "NEEDED")
@ -75,6 +70,16 @@ MACRO(CHECK_DEPENDS MYLIBRARY OTHERLIBRARY SYMBOL MUSTLINK)
ENDIF(SYMBOL_FOUND AND NOT LIBRARY_FOUND) ENDIF(SYMBOL_FOUND AND NOT LIBRARY_FOUND)
ENDMACRO(CHECK_DEPENDS) ENDMACRO(CHECK_DEPENDS)
# LINK_DEPENDS
# Macro to link a library if a symbol is used but is not already linked to it
#
# Syntax:
# LINK_DEPENDS(LIBRARIES MYLIBRARY OTHERLIBRARY SYMBOL)
# OTHERLIBRARY_LINKED will be set to TRUE or FALSE
#
# Example:
# LINK_DEPENDS(PNG_LIBRARIES PNG_LIBRARY ZLIB_LIBRARY inflate)
#
MACRO(LINK_DEPENDS LIBRARIES MYLIBRARY OTHERLIBRARY SYMBOL) MACRO(LINK_DEPENDS LIBRARIES MYLIBRARY OTHERLIBRARY SYMBOL)
SET(MUST_LINK FALSE) SET(MUST_LINK FALSE)
IF(${MYLIBRARY} AND ${OTHERLIBRARY} AND NOT ${OTHERLIBRARY}_LINKED) IF(${MYLIBRARY} AND ${OTHERLIBRARY} AND NOT ${OTHERLIBRARY}_LINKED)
@ -88,7 +93,6 @@ MACRO(LINK_DEPENDS LIBRARIES MYLIBRARY OTHERLIBRARY SYMBOL)
CHECK_LINKED_LIBRARY(${MYLIBRARY} ${OTHERLIBRARY} LIBRARY_FOUND) CHECK_LINKED_LIBRARY(${MYLIBRARY} ${OTHERLIBRARY} LIBRARY_FOUND)
ENDIF(SYMBOL_FOUND) ENDIF(SYMBOL_FOUND)
# CHECK_DEPENDS(${MYLIBRARY} ${${OTHERLIBRARY}} ${SYMBOL} MUST_LINK)
IF(SYMBOL_FOUND AND NOT LIBRARY_FOUND) IF(SYMBOL_FOUND AND NOT LIBRARY_FOUND)
MESSAGE(STATUS "Underlinking found: ${${MYLIBRARY}} needs ${${OTHERLIBRARY}} but is not linked to, manually linking...") MESSAGE(STATUS "Underlinking found: ${${MYLIBRARY}} needs ${${OTHERLIBRARY}} but is not linked to, manually linking...")
SET(MUST_LINK TRUE) SET(MUST_LINK TRUE)
@ -97,7 +101,9 @@ MACRO(LINK_DEPENDS LIBRARIES MYLIBRARY OTHERLIBRARY SYMBOL)
ENDIF(${MYLIBRARY} AND ${OTHERLIBRARY} AND NOT ${OTHERLIBRARY}_LINKED) ENDIF(${MYLIBRARY} AND ${OTHERLIBRARY} AND NOT ${OTHERLIBRARY}_LINKED)
IF(MUST_LINK) IF(MUST_LINK)
MESSAGE(STATUS "Linking with ${${OTHERLIBRARY}}") MESSAGE(STATUS "Linking with ${${OTHERLIBRARY}}")
SET(${OTHERLIBRARY}_LINKED TRUE)
SET(${LIBRARIES} ${${LIBRARIES}} ${${OTHERLIBRARY}}) SET(${LIBRARIES} ${${LIBRARIES}} ${${OTHERLIBRARY}})
ENDIF(MUST_LINK) ENDIF(MUST_LINK)
IF(LIBRARY_FOUND)
SET(${OTHERLIBRARY}_LINKED TRUE)
ENDIF(LIBRARY_FOUND)
ENDMACRO(LINK_DEPENDS) ENDMACRO(LINK_DEPENDS)