1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4#[=======================================================================[.rst:
5CheckVariableExists
6-------------------
7
8Check if the variable exists.
9
10.. command:: CHECK_VARIABLE_EXISTS
11
12  .. code-block:: cmake
13
14    CHECK_VARIABLE_EXISTS(VAR VARIABLE)
15
16
17  ::
18
19    VAR      - the name of the variable
20    VARIABLE - variable to store the result
21               Will be created as an internal cache variable.
22
23
24  This macro is only for ``C`` variables.
25
26The following variables may be set before calling this macro to modify
27the way the check is run:
28
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``
39  .. versionadded:: 3.1
40    execute quietly without messages.
41#]=======================================================================]
42
43include_guard(GLOBAL)
44
45macro(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}")
51    endif()
52    if(CMAKE_REQUIRED_LINK_OPTIONS)
53      set(CHECK_VARIABLE_EXISTS_ADD_LINK_OPTIONS
54        LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
55    else()
56      set(CHECK_VARIABLE_EXISTS_ADD_LINK_OPTIONS)
57    endif()
58    if(CMAKE_REQUIRED_LIBRARIES)
59      set(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES
60        LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
61    else()
62      set(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES)
63    endif()
64    try_compile(${VARIABLE}
65      ${CMAKE_BINARY_DIR}
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)
72    if(${VARIABLE})
73      set(${VARIABLE} 1 CACHE INTERNAL "Have variable ${VAR}")
74      if(NOT CMAKE_REQUIRED_QUIET)
75        message(CHECK_PASS "found")
76      endif()
77      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
78        "Determining if the variable ${VAR} exists passed with the following output:\n"
79        "${OUTPUT}\n\n")
80    else()
81      set(${VARIABLE} "" CACHE INTERNAL "Have variable ${VAR}")
82      if(NOT CMAKE_REQUIRED_QUIET)
83        message(CHECK_FAIL "not found")
84      endif()
85      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
86        "Determining if the variable ${VAR} exists failed with the following output:\n"
87        "${OUTPUT}\n\n")
88    endif()
89  endif()
90endmacro()
91