Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Modules / CheckVariableExists.cmake
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:
5 CheckVariableExists
6 -------------------
7
8 Check 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
26 The following variables may be set before calling this macro to modify
27 the 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
43 include_guard(GLOBAL)
44
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}")
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       SOURCES ${CMAKE_ROOT}/Modules/CheckVariableExists.c
66       COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
67       ${CHECK_VARIABLE_EXISTS_ADD_LINK_OPTIONS}
68       ${CHECK_VARIABLE_EXISTS_ADD_LIBRARIES}
69       CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_VARIABLE_DEFINITIONS}
70       OUTPUT_VARIABLE OUTPUT)
71     if(${VARIABLE})
72       set(${VARIABLE} 1 CACHE INTERNAL "Have variable ${VAR}")
73       if(NOT CMAKE_REQUIRED_QUIET)
74         message(CHECK_PASS "found")
75       endif()
76       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
77         "Determining if the variable ${VAR} exists passed with the following output:\n"
78         "${OUTPUT}\n\n")
79     else()
80       set(${VARIABLE} "" CACHE INTERNAL "Have variable ${VAR}")
81       if(NOT CMAKE_REQUIRED_QUIET)
82         message(CHECK_FAIL "not found")
83       endif()
84       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
85         "Determining if the variable ${VAR} exists failed with the following output:\n"
86         "${OUTPUT}\n\n")
87     endif()
88   endif()
89 endmacro()