1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 Check if the variable exists.
10 .. command:: CHECK_VARIABLE_EXISTS
14 CHECK_VARIABLE_EXISTS(VAR VARIABLE)
19 VAR - the name of the variable
20 VARIABLE - variable to store the result
21 Will be created as an internal cache variable.
24 This macro is only for ``C`` variables.
26 The following variables may be set before calling this macro to modify
27 the way the check is run:
29 ``CMAKE_REQUIRED_FLAGS``
30 string of compile command line flags.
31 ``CMAKE_REQUIRED_DEFINITIONS``
32 list of macros to define (-DFOO=bar).
33 ``CMAKE_REQUIRED_LINK_OPTIONS``
34 .. versionadded:: 3.14
35 list of options to pass to link command.
36 ``CMAKE_REQUIRED_LIBRARIES``
37 list of libraries to link.
38 ``CMAKE_REQUIRED_QUIET``
40 execute quietly without messages.
41 #]=======================================================================]
45 macro(CHECK_VARIABLE_EXISTS VAR VARIABLE)
46 if(NOT DEFINED "${VARIABLE}")
47 set(MACRO_CHECK_VARIABLE_DEFINITIONS
48 "-DCHECK_VARIABLE_EXISTS=${VAR} ${CMAKE_REQUIRED_FLAGS}")
49 if(NOT CMAKE_REQUIRED_QUIET)
50 message(CHECK_START "Looking for ${VAR}")
52 if(CMAKE_REQUIRED_LINK_OPTIONS)
53 set(CHECK_VARIABLE_EXISTS_ADD_LINK_OPTIONS
54 LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
56 set(CHECK_VARIABLE_EXISTS_ADD_LINK_OPTIONS)
58 if(CMAKE_REQUIRED_LIBRARIES)
59 set(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES
60 LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
62 set(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES)
64 try_compile(${VARIABLE}
66 ${CMAKE_ROOT}/Modules/CheckVariableExists.c
67 COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
68 ${CHECK_VARIABLE_EXISTS_ADD_LINK_OPTIONS}
69 ${CHECK_VARIABLE_EXISTS_ADD_LIBRARIES}
70 CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_VARIABLE_DEFINITIONS}
71 OUTPUT_VARIABLE OUTPUT)
73 set(${VARIABLE} 1 CACHE INTERNAL "Have variable ${VAR}")
74 if(NOT CMAKE_REQUIRED_QUIET)
75 message(CHECK_PASS "found")
77 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
78 "Determining if the variable ${VAR} exists passed with the following output:\n"
81 set(${VARIABLE} "" CACHE INTERNAL "Have variable ${VAR}")
82 if(NOT CMAKE_REQUIRED_QUIET)
83 message(CHECK_FAIL "not found")
85 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
86 "Determining if the variable ${VAR} exists failed with the following output:\n"