2010-10-02 12:21:45 +00:00
|
|
|
# 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
|
|
|
|
|
|
|
|
# CHECK_UNDEFINED_SYMBOL
|
|
|
|
# Macro to check if a library is calling an undefined symbol
|
|
|
|
#
|
|
|
|
# Syntax:
|
|
|
|
# CHECK_UNDEFINED_SYMBOL(MYLIBRARY SYMBOL SYMBOL_FOUND)
|
2010-11-02 11:51:35 +00:00
|
|
|
# SYMBOL_FOUND will be set to TRUE if UNDEFINED
|
2010-10-02 12:21:45 +00:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# CHECK_UNDEFINED_SYMBOL(PNG_LIBRARY inflate INFLATE_FOUND)
|
|
|
|
#
|
|
|
|
MACRO(CHECK_UNDEFINED_SYMBOL MYLIBRARY SYMBOL SYMBOL_FOUND)
|
|
|
|
SET(${SYMBOL_FOUND} TRUE)
|
|
|
|
IF(WIN32)
|
|
|
|
# Always TRUE under Windows because we are using static libraries
|
|
|
|
ELSEIF(APPLE)
|
2010-10-31 21:15:49 +00:00
|
|
|
SET(CMAKE_NM nm)
|
|
|
|
IF(CMAKE_NM)
|
|
|
|
# Use nm to check if a library is using an external symbol
|
2010-11-02 09:45:37 +00:00
|
|
|
EXEC_PROGRAM(${CMAKE_NM} ARGS "-gu ${${MYLIBRARY}} | grep ${SYMBOL}" OUTPUT_VARIABLE NM_SYMBOL)
|
2010-11-02 12:04:10 +00:00
|
|
|
# MESSAGE(STATUS "Checking for undefined symbol ${SYMBOL} in ${${MYLIBRARY}}")
|
2010-11-02 10:00:17 +00:00
|
|
|
IF(NOT NM_SYMBOL MATCHES ${SYMBOL})
|
2010-10-31 21:15:49 +00:00
|
|
|
SET(${SYMBOL_FOUND} FALSE)
|
2010-11-02 12:04:10 +00:00
|
|
|
# MESSAGE(STATUS "Defined symbol ${SYMBOL} detected in ${${MYLIBRARY}}")
|
2010-11-02 10:00:17 +00:00
|
|
|
ENDIF(NOT NM_SYMBOL MATCHES ${SYMBOL})
|
2010-10-31 21:15:49 +00:00
|
|
|
ENDIF(CMAKE_NM)
|
2010-10-02 12:21:45 +00:00
|
|
|
ELSEIF(UNIX)
|
2011-04-13 17:58:41 +00:00
|
|
|
SET(CMAKE_OBJDUMP objdump)
|
2010-10-02 12:21:45 +00:00
|
|
|
IF(CMAKE_OBJDUMP)
|
|
|
|
# Use objdump to check if a library is using an external symbol
|
2011-04-13 17:58:41 +00:00
|
|
|
#MESSAGE(STATUS "exec ${CMAKE_OBJDUMP} -T ${${MYLIBRARY}} | grep ${SYMBOL}")
|
2010-10-03 08:04:36 +00:00
|
|
|
EXEC_PROGRAM(${CMAKE_OBJDUMP} ARGS "-T ${${MYLIBRARY}} | grep ${SYMBOL}" OUTPUT_VARIABLE OBJDUMP_SYMBOL)
|
2011-04-12 18:08:35 +00:00
|
|
|
IF(NOT OBJDUMP_SYMBOL MATCHES "UND")
|
2011-04-13 17:58:41 +00:00
|
|
|
#MESSAGE(STATUS "${${MYLIBRARY}} does not use symbol ${SYMBOL}")
|
2010-10-02 12:21:45 +00:00
|
|
|
SET(${SYMBOL_FOUND} FALSE)
|
2011-04-13 17:58:41 +00:00
|
|
|
ELSE(NOT OBJDUMP_SYMBOL MATCHES "UND")
|
|
|
|
#MESSAGE(STATUS "${${MYLIBRARY}} uses symbol ${SYMBOL}")
|
2011-04-12 18:08:35 +00:00
|
|
|
ENDIF(NOT OBJDUMP_SYMBOL MATCHES "UND")
|
2010-10-02 12:21:45 +00:00
|
|
|
ENDIF(CMAKE_OBJDUMP)
|
|
|
|
ENDIF(WIN32)
|
|
|
|
ENDMACRO(CHECK_UNDEFINED_SYMBOL)
|
|
|
|
|
2010-10-03 08:04:36 +00:00
|
|
|
# CHECK_LINKED_LIBRARY
|
|
|
|
# Macro to check if a library is linked to another one
|
|
|
|
#
|
|
|
|
# Syntax:
|
|
|
|
# CHECK_LINKED_LIBRARY(MYLIBRARY OTHERLIBRARY LIBRARY_FOUND)
|
2010-11-02 11:51:35 +00:00
|
|
|
# LIBRARY_FOUND will be set to TRUE if LINKED
|
2010-10-03 08:04:36 +00:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# CHECK_LINKED_LIBRARY(PNG_LIBRARY ZLIB_LIBRARY ZLIB_FOUND)
|
|
|
|
#
|
2010-10-02 12:21:45 +00:00
|
|
|
MACRO(CHECK_LINKED_LIBRARY MYLIBRARY OTHERLIBRARY LIBRARY_FOUND)
|
|
|
|
SET(${LIBRARY_FOUND} FALSE)
|
|
|
|
IF(WIN32)
|
|
|
|
# Always FALSE under Windows because we are using static libraries
|
|
|
|
ELSEIF(APPLE)
|
2010-10-03 16:23:38 +00:00
|
|
|
SET(CMAKE_OTOOL otool)
|
|
|
|
IF(CMAKE_OTOOL)
|
|
|
|
# Use otool to check if a library is linked to another library
|
2011-01-27 17:00:21 +00:00
|
|
|
GET_FILENAME_COMPONENT(LIBNAME "${${OTHERLIBRARY}}" NAME_WE)
|
2010-11-02 09:45:37 +00:00
|
|
|
EXEC_PROGRAM(${CMAKE_OTOOL} ARGS "-L ${${MYLIBRARY}} | grep ${LIBNAME}" OUTPUT_VARIABLE OTOOL_LIBRARY)
|
2010-11-02 12:04:10 +00:00
|
|
|
# MESSAGE(STATUS "Checking if ${LIBNAME} is linked to ${${MYLIBRARY}}")
|
2010-11-02 11:22:08 +00:00
|
|
|
IF(OTOOL_LIBRARY MATCHES "${LIBNAME}")
|
2010-11-02 10:16:03 +00:00
|
|
|
SET(${LIBRARY_FOUND} TRUE)
|
2010-11-02 12:04:10 +00:00
|
|
|
# MESSAGE(STATUS "Library ${LIBNAME} already linked to ${${MYLIBRARY}}")
|
2010-11-02 11:26:57 +00:00
|
|
|
ENDIF(OTOOL_LIBRARY MATCHES "${LIBNAME}")
|
2010-10-03 16:23:38 +00:00
|
|
|
ENDIF(CMAKE_OTOOL)
|
2010-10-02 12:21:45 +00:00
|
|
|
ELSEIF(UNIX)
|
2011-04-13 17:58:41 +00:00
|
|
|
SET(CMAKE_OBJDUMP objdump)
|
2010-10-02 12:21:45 +00:00
|
|
|
IF(CMAKE_OBJDUMP)
|
2011-01-27 17:00:21 +00:00
|
|
|
GET_FILENAME_COMPONENT(LIBNAME "${${OTHERLIBRARY}}" NAME)
|
2010-10-03 16:23:38 +00:00
|
|
|
# TODO: under Solaris use dump -Lv <lib>
|
2010-10-02 12:21:45 +00:00
|
|
|
# Use objdump to check if a library is linked to another library
|
2011-04-13 17:58:41 +00:00
|
|
|
#MESSAGE(STATUS "exec ${CMAKE_OBJDUMP} -p ${${MYLIBRARY}} | grep ${LIBNAME}")
|
2010-10-03 08:04:36 +00:00
|
|
|
EXEC_PROGRAM(${CMAKE_OBJDUMP} ARGS "-p ${${MYLIBRARY}} | grep ${LIBNAME}" OUTPUT_VARIABLE OBJDUMP_LIBRARY)
|
2011-04-12 18:08:35 +00:00
|
|
|
IF(OBJDUMP_LIBRARY MATCHES "NEEDED")
|
2011-04-13 17:58:41 +00:00
|
|
|
#MESSAGE(STATUS "${${MYLIBRARY}} references to ${LIBNAME}.")
|
2010-10-02 12:21:45 +00:00
|
|
|
SET(${LIBRARY_FOUND} TRUE)
|
2011-04-13 17:58:41 +00:00
|
|
|
ELSE(OBJDUMP_LIBRARY MATCHES "NEEDED")
|
|
|
|
#MESSAGE(STATUS "${${MYLIBRARY}} does not reference to ${LIBNAME}!")
|
2011-04-12 18:08:35 +00:00
|
|
|
ENDIF(OBJDUMP_LIBRARY MATCHES "NEEDED")
|
2010-10-02 12:21:45 +00:00
|
|
|
ENDIF(CMAKE_OBJDUMP)
|
|
|
|
ENDIF(WIN32)
|
|
|
|
ENDMACRO(CHECK_LINKED_LIBRARY)
|
|
|
|
|
|
|
|
MACRO(CHECK_DEPENDS MYLIBRARY OTHERLIBRARY SYMBOL MUSTLINK)
|
|
|
|
CHECK_UNDEFINED_SYMBOL(MYLIBRARY SYMBOL SYMBOL_FOUND)
|
|
|
|
|
|
|
|
IF(SYMBOL_FOUND)
|
|
|
|
CHECK_LINKED_LIBRARY(MYLIBRARY OTHERLIBRARY LIBRARY_FOUND)
|
|
|
|
ENDIF(SYMBOL_FOUND)
|
|
|
|
|
|
|
|
IF(SYMBOL_FOUND AND NOT LIBRARY_FOUND)
|
|
|
|
SET(${MUSTLINK} YES)
|
|
|
|
ELSE(SYMBOL_FOUND AND NOT LIBRARY_FOUND)
|
|
|
|
SET(${MUSTLINK} NO)
|
|
|
|
ENDIF(SYMBOL_FOUND AND NOT LIBRARY_FOUND)
|
|
|
|
ENDMACRO(CHECK_DEPENDS)
|
|
|
|
|
2010-10-03 08:04:36 +00:00
|
|
|
# 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)
|
|
|
|
#
|
2010-10-02 12:21:45 +00:00
|
|
|
MACRO(LINK_DEPENDS LIBRARIES MYLIBRARY OTHERLIBRARY SYMBOL)
|
|
|
|
SET(MUST_LINK FALSE)
|
|
|
|
IF(${MYLIBRARY} AND ${OTHERLIBRARY} AND NOT ${OTHERLIBRARY}_LINKED)
|
|
|
|
IF(WIN32 OR WITH_STATIC)
|
|
|
|
# In static, we link all libraries because it will keep only used symbols
|
|
|
|
SET(MUST_LINK TRUE)
|
|
|
|
ELSE(WIN32 OR WITH_STATIC)
|
|
|
|
CHECK_UNDEFINED_SYMBOL(${MYLIBRARY} ${SYMBOL} SYMBOL_FOUND)
|
|
|
|
|
|
|
|
IF(SYMBOL_FOUND)
|
|
|
|
CHECK_LINKED_LIBRARY(${MYLIBRARY} ${OTHERLIBRARY} LIBRARY_FOUND)
|
|
|
|
ENDIF(SYMBOL_FOUND)
|
|
|
|
|
|
|
|
IF(SYMBOL_FOUND AND NOT LIBRARY_FOUND)
|
|
|
|
MESSAGE(STATUS "Underlinking found: ${${MYLIBRARY}} needs ${${OTHERLIBRARY}} but is not linked to, manually linking...")
|
|
|
|
SET(MUST_LINK TRUE)
|
|
|
|
ENDIF(SYMBOL_FOUND AND NOT LIBRARY_FOUND)
|
|
|
|
ENDIF(WIN32 OR WITH_STATIC)
|
|
|
|
ENDIF(${MYLIBRARY} AND ${OTHERLIBRARY} AND NOT ${OTHERLIBRARY}_LINKED)
|
|
|
|
IF(MUST_LINK)
|
|
|
|
MESSAGE(STATUS "Linking with ${${OTHERLIBRARY}}")
|
|
|
|
SET(${LIBRARIES} ${${LIBRARIES}} ${${OTHERLIBRARY}})
|
2010-11-02 12:04:10 +00:00
|
|
|
SET(${OTHERLIBRARY}_LINKED TRUE)
|
2010-10-02 12:21:45 +00:00
|
|
|
ENDIF(MUST_LINK)
|
2010-11-02 12:36:12 +00:00
|
|
|
ENDMACRO(LINK_DEPENDS)
|
2011-04-13 17:58:41 +00:00
|
|
|
|