ENH: fix advanced bug
[cmake.git] / Modules / CheckSymbolExists.cmake
blob99b89189172b4e64989c8e7dee8daaa66529555a
1 # - Check if the symbol exists in include files
2 # CHECK_SYMBOL_EXISTS(SYMBOL FILES VARIABLE)
4 #  SYMBOL   - symbol
5 #  FILES    - include files to check
6 #  VARIABLE - variable to return result
8 # The following variables may be set before calling this macro to
9 # modify the way the check is run:
11 #  CMAKE_REQUIRED_FLAGS = string of compile command line flags
12 #  CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
13 #  CMAKE_REQUIRED_INCLUDES = list of include directories
14 #  CMAKE_REQUIRED_LIBRARIES = list of libraries to link
16 MACRO(CHECK_SYMBOL_EXISTS SYMBOL FILES VARIABLE)
17   IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
18     SET(CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n")
19     SET(MACRO_CHECK_SYMBOL_EXISTS_FLAGS ${CMAKE_REQUIRED_FLAGS})
20     IF(CMAKE_REQUIRED_LIBRARIES)
21       SET(CHECK_SYMBOL_EXISTS_LIBS 
22         "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
23     ELSE(CMAKE_REQUIRED_LIBRARIES)
24       SET(CHECK_SYMBOL_EXISTS_LIBS)
25     ENDIF(CMAKE_REQUIRED_LIBRARIES)
26     IF(CMAKE_REQUIRED_INCLUDES)
27       SET(CMAKE_SYMBOL_EXISTS_INCLUDES
28         "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
29     ELSE(CMAKE_REQUIRED_INCLUDES)
30       SET(CMAKE_SYMBOL_EXISTS_INCLUDES)
31     ENDIF(CMAKE_REQUIRED_INCLUDES)
32     FOREACH(FILE ${FILES})
33       SET(CMAKE_CONFIGURABLE_FILE_CONTENT
34         "${CMAKE_CONFIGURABLE_FILE_CONTENT}#include <${FILE}>\n")
35     ENDFOREACH(FILE)
36     SET(CMAKE_CONFIGURABLE_FILE_CONTENT
37       "${CMAKE_CONFIGURABLE_FILE_CONTENT}\nvoid cmakeRequireSymbol(int dummy,...){(void)dummy;}\nint main()\n{\n#ifndef ${SYMBOL}\n  cmakeRequireSymbol(0,&${SYMBOL});\n#endif\n  return 0;\n}\n")
39     CONFIGURE_FILE("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
40       "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckSymbolExists.c" @ONLY)
42     MESSAGE(STATUS "Looking for ${SYMBOL}")
43     TRY_COMPILE(${VARIABLE}
44       ${CMAKE_BINARY_DIR}
45       ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckSymbolExists.c
46       COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
47       CMAKE_FLAGS 
48       -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_SYMBOL_EXISTS_FLAGS}
49       "${CHECK_SYMBOL_EXISTS_LIBS}"
50       "${CMAKE_SYMBOL_EXISTS_INCLUDES}"
51       OUTPUT_VARIABLE OUTPUT)
52     IF(${VARIABLE})
53       MESSAGE(STATUS "Looking for ${SYMBOL} - found")
54       SET(${VARIABLE} 1 CACHE INTERNAL "Have symbol ${SYMBOL}")
55       FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log 
56         "Determining if the ${SYMBOL} "
57         "exist passed with the following output:\n"
58         "${OUTPUT}\nFile ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckSymbolExists.c:\n"
59         "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
60     ELSE(${VARIABLE})
61       MESSAGE(STATUS "Looking for ${SYMBOL} - not found.")
62       SET(${VARIABLE} "" CACHE INTERNAL "Have symbol ${SYMBOL}")
63       FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log 
64         "Determining if the ${SYMBOL} "
65         "exist failed with the following output:\n"
66         "${OUTPUT}\nFile ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckSymbolExists.c:\n"
67         "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
68     ENDIF(${VARIABLE})
69   ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
70 ENDMACRO(CHECK_SYMBOL_EXISTS)