1 # - Check if the variable exists.
2 # CHECK_VARIABLE_EXISTS(VAR VARIABLE)
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}
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)
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"
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"
47 ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
48 ENDMACRO(CHECK_VARIABLE_EXISTS)