1 # Macros to check if a library needs to be manually linked to another one
\r
2 # because it's using a symbol from it but it's not linked to
\r
4 # CHECK_UNDEFINED_SYMBOL
\r
5 # Macro to check if a library is calling an undefined symbol
\r
8 # CHECK_UNDEFINED_SYMBOL(MYLIBRARY SYMBOL SYMBOL_FOUND)
\r
9 # SYMBOL_FOUND will be set to TRUE if UNDEFINED
\r
12 # CHECK_UNDEFINED_SYMBOL(PNG_LIBRARY inflate INFLATE_FOUND)
\r
14 MACRO(CHECK_UNDEFINED_SYMBOL MYLIBRARY SYMBOL SYMBOL_FOUND)
\r
15 SET(${SYMBOL_FOUND} TRUE)
\r
17 # Always TRUE under Windows because we are using static libraries
\r
21 # Use nm to check if a library is using an external symbol
\r
22 EXEC_PROGRAM(${CMAKE_NM} ARGS "-gu ${${MYLIBRARY}} | grep ${SYMBOL}" OUTPUT_VARIABLE NM_SYMBOL)
\r
23 # MESSAGE(STATUS "Checking for undefined symbol ${SYMBOL} in ${${MYLIBRARY}}")
\r
24 IF(NOT NM_SYMBOL MATCHES ${SYMBOL})
\r
25 SET(${SYMBOL_FOUND} FALSE)
\r
26 # MESSAGE(STATUS "Defined symbol ${SYMBOL} detected in ${${MYLIBRARY}}")
\r
30 SET(CMAKE_OBJDUMP objdump)
\r
32 # Use objdump to check if a library is using an external symbol
\r
33 #MESSAGE(STATUS "exec ${CMAKE_OBJDUMP} -T ${${MYLIBRARY}} | grep ${SYMBOL}")
\r
34 EXEC_PROGRAM(${CMAKE_OBJDUMP} ARGS "-T ${${MYLIBRARY}} | grep ${SYMBOL}" OUTPUT_VARIABLE OBJDUMP_SYMBOL)
\r
35 IF(NOT OBJDUMP_SYMBOL MATCHES "UND")
\r
36 #MESSAGE(STATUS "${${MYLIBRARY}} does not use symbol ${SYMBOL}")
\r
37 SET(${SYMBOL_FOUND} FALSE)
\r
39 #MESSAGE(STATUS "${${MYLIBRARY}} uses symbol ${SYMBOL}")
\r
43 ENDMACRO(CHECK_UNDEFINED_SYMBOL)
\r
45 # CHECK_LINKED_LIBRARY
\r
46 # Macro to check if a library is linked to another one
\r
49 # CHECK_LINKED_LIBRARY(MYLIBRARY OTHERLIBRARY LIBRARY_FOUND)
\r
50 # LIBRARY_FOUND will be set to TRUE if LINKED
\r
53 # CHECK_LINKED_LIBRARY(PNG_LIBRARY ZLIB_LIBRARY ZLIB_FOUND)
\r
55 MACRO(CHECK_LINKED_LIBRARY MYLIBRARY OTHERLIBRARY LIBRARY_FOUND)
\r
56 SET(${LIBRARY_FOUND} FALSE)
\r
58 # Always FALSE under Windows because we are using static libraries
\r
60 SET(CMAKE_OTOOL otool)
\r
62 # Use otool to check if a library is linked to another library
\r
63 GET_FILENAME_COMPONENT(LIBNAME "${${OTHERLIBRARY}}" NAME_WE)
\r
64 EXEC_PROGRAM(${CMAKE_OTOOL} ARGS "-L ${${MYLIBRARY}} | grep ${LIBNAME}" OUTPUT_VARIABLE OTOOL_LIBRARY)
\r
65 # MESSAGE(STATUS "Checking if ${LIBNAME} is linked to ${${MYLIBRARY}}")
\r
66 IF(OTOOL_LIBRARY MATCHES "${LIBNAME}")
\r
67 SET(${LIBRARY_FOUND} TRUE)
\r
68 # MESSAGE(STATUS "Library ${LIBNAME} already linked to ${${MYLIBRARY}}")
\r
72 SET(CMAKE_OBJDUMP objdump)
\r
74 GET_FILENAME_COMPONENT(LIBNAME "${${OTHERLIBRARY}}" NAME)
\r
75 # TODO: under Solaris use dump -Lv <lib>
\r
76 # Use objdump to check if a library is linked to another library
\r
77 #MESSAGE(STATUS "exec ${CMAKE_OBJDUMP} -p ${${MYLIBRARY}} | grep ${LIBNAME}")
\r
78 EXEC_PROGRAM(${CMAKE_OBJDUMP} ARGS "-p ${${MYLIBRARY}} | grep ${LIBNAME}" OUTPUT_VARIABLE OBJDUMP_LIBRARY)
\r
79 IF(OBJDUMP_LIBRARY MATCHES "NEEDED")
\r
80 #MESSAGE(STATUS "${${MYLIBRARY}} references to ${LIBNAME}.")
\r
81 SET(${LIBRARY_FOUND} TRUE)
\r
83 #MESSAGE(STATUS "${${MYLIBRARY}} does not reference to ${LIBNAME}!")
\r
87 ENDMACRO(CHECK_LINKED_LIBRARY)
\r
89 MACRO(CHECK_DEPENDS MYLIBRARY OTHERLIBRARY SYMBOL MUSTLINK)
\r
90 CHECK_UNDEFINED_SYMBOL(MYLIBRARY SYMBOL SYMBOL_FOUND)
\r
93 CHECK_LINKED_LIBRARY(MYLIBRARY OTHERLIBRARY LIBRARY_FOUND)
\r
96 IF(SYMBOL_FOUND AND NOT LIBRARY_FOUND)
\r
97 SET(${MUSTLINK} YES)
\r
101 ENDMACRO(CHECK_DEPENDS)
\r
104 # Macro to link a library if a symbol is used but is not already linked to it
\r
107 # LINK_DEPENDS(LIBRARIES MYLIBRARY OTHERLIBRARY SYMBOL)
\r
108 # OTHERLIBRARY_LINKED will be set to TRUE or FALSE
\r
111 # LINK_DEPENDS(PNG_LIBRARIES PNG_LIBRARY ZLIB_LIBRARY inflate)
\r
113 MACRO(LINK_DEPENDS LIBRARIES MYLIBRARY OTHERLIBRARY SYMBOL)
\r
114 SET(MUST_LINK FALSE)
\r
115 IF(${MYLIBRARY} AND ${OTHERLIBRARY} AND NOT ${OTHERLIBRARY}_LINKED)
\r
116 IF(WIN32 OR WITH_STATIC)
\r
117 # In static, we link all libraries because it will keep only used symbols
\r
118 SET(MUST_LINK TRUE)
\r
120 CHECK_UNDEFINED_SYMBOL(${MYLIBRARY} ${SYMBOL} SYMBOL_FOUND)
\r
123 CHECK_LINKED_LIBRARY(${MYLIBRARY} ${OTHERLIBRARY} LIBRARY_FOUND)
\r
126 IF(SYMBOL_FOUND AND NOT LIBRARY_FOUND)
\r
127 MESSAGE(STATUS "Underlinking found: ${${MYLIBRARY}} needs ${${OTHERLIBRARY}} but is not linked to, manually linking...")
\r
128 SET(MUST_LINK TRUE)
\r
133 MESSAGE(STATUS "Linking with ${${OTHERLIBRARY}}")
\r
134 SET(${LIBRARIES} ${${LIBRARIES}} ${${OTHERLIBRARY}})
\r
135 SET(${OTHERLIBRARY}_LINKED TRUE)
\r
137 ENDMACRO(LINK_DEPENDS)
\r