Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Modules / CheckVariableExists.cmake
bloba6cdc01fc3468caba2dc45b67c0980a6fe3da651
1 # - Check if the variable exists.
2 #  CHECK_VARIABLE_EXISTS(VAR VARIABLE)
3 #  
4 #  VAR      - the name of the variable
5 #  VARIABLE - variable to store the result
7 # This macro is only for C variables.
9 # The following variables may be set before calling this macro to
10 # modify the way the check is run:
12 #  CMAKE_REQUIRED_FLAGS = string of compile command line flags
13 #  CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
14 #  CMAKE_REQUIRED_LIBRARIES = list of libraries to link
16 MACRO(CHECK_VARIABLE_EXISTS VAR VARIABLE)
17   IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
18     SET(MACRO_CHECK_VARIABLE_DEFINITIONS 
19       "-DCHECK_VARIABLE_EXISTS=${VAR} ${CMAKE_REQUIRED_FLAGS}")
20     MESSAGE(STATUS "Looking for ${VAR}")
21     IF(CMAKE_REQUIRED_LIBRARIES)
22       SET(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES
23         "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
24     ELSE(CMAKE_REQUIRED_LIBRARIES)
25       SET(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES)
26     ENDIF(CMAKE_REQUIRED_LIBRARIES)
27     TRY_COMPILE(${VARIABLE}
28       ${CMAKE_BINARY_DIR}
29       ${CMAKE_ROOT}/Modules/CheckVariableExists.c
30       COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
31       CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_VARIABLE_DEFINITIONS}
32       "${CHECK_VARIABLE_EXISTS_ADD_LIBRARIES}"
33       OUTPUT_VARIABLE OUTPUT)
34     IF(${VARIABLE})
35       SET(${VARIABLE} 1 CACHE INTERNAL "Have variable ${VAR}")
36       MESSAGE(STATUS "Looking for ${VAR} - found")
37       FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log 
38         "Determining if the variable ${VAR} exists passed with the following output:\n"
39         "${OUTPUT}\n\n")
40     ELSE(${VARIABLE})
41       SET(${VARIABLE} "" CACHE INTERNAL "Have variable ${VAR}")
42       MESSAGE(STATUS "Looking for ${VAR} - not found")
43       FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log 
44         "Determining if the variable ${VAR} exists failed with the following output:\n"
45         "${OUTPUT}\n\n")
46     ENDIF(${VARIABLE})
47   ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
48 ENDMACRO(CHECK_VARIABLE_EXISTS)