ENH: move EstimateFormatLength to kwsys
[cmake.git] / Modules / CheckVariableExists.cmake
blob93da0045d594d63fa149debeca567839f73bf900
2 # Check if the variable exists.
3 # # CHECK_VARIABLE_EXISTS - macro which checks if the variable exists
4 # VAR - the name of the variable
5 # VARIABLE - variable to store the result
8 MACRO(CHECK_VARIABLE_EXISTS VAR VARIABLE)
9   IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
10     SET(MACRO_CHECK_VARIABLE_DEFINITIONS 
11       "-DCHECK_VARIABLE_EXISTS=${VAR} ${CMAKE_REQUIRED_FLAGS}")
12     MESSAGE(STATUS "Looking for ${VAR}")
13     IF(CMAKE_REQUIRED_LIBRARIES)
14       SET(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES 
15         "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
16     ENDIF(CMAKE_REQUIRED_LIBRARIES)
17     TRY_COMPILE(${VARIABLE}
18       ${CMAKE_BINARY_DIR}
19       ${CMAKE_ROOT}/Modules/CheckVariableExists.c
20       CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_VARIABLE_DEFINITIONS}
21       "${CHECK_VARIABLE_EXISTS_ADD_LIBRARIES}"
22       OUTPUT_VARIABLE OUTPUT)
23     IF(${VARIABLE})
24       SET(${VARIABLE} 1 CACHE INTERNAL "Have variable ${VAR}")
25       MESSAGE(STATUS "Looking for ${VAR} - found")
26       FILE(APPEND ${CMAKE_BINARY_DIR}/CMakeOutput.log 
27         "Determining if the variable ${VAR} exists passed with the following output:\n"
28         "${OUTPUT}\n\n")
29     ELSE(${VARIABLE})
30       SET(${VARIABLE} "" CACHE INTERNAL "Have variable ${VAR}")
31       MESSAGE(STATUS "Looking for ${VAR} - not found")
32       FILE(APPEND ${CMAKE_BINARY_DIR}/CMakeError.log 
33         "Determining if the variable ${VAR} exists failed with the following output:\n"
34         "${OUTPUT}\n\n")
35     ENDIF(${VARIABLE})
36   ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
37 ENDMACRO(CHECK_VARIABLE_EXISTS)