cmake/Modules/CheckVariableExists.cmake

82 lines
2.4 KiB
CMake
Raw Normal View History

2016-10-30 18:24:19 +01:00
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
2019-11-11 23:01:05 +01:00
#[=======================================================================[.rst:
CheckVariableExists
-------------------
Check if the variable exists.
.. command:: CHECK_VARIABLE_EXISTS
.. code-block:: cmake
CHECK_VARIABLE_EXISTS(VAR VARIABLE)
::
VAR - the name of the variable
VARIABLE - variable to store the result
Will be created as an internal cache variable.
This macro is only for ``C`` variables.
The following variables may be set before calling this macro to modify
the way the check is run:
2023-07-02 19:51:09 +02:00
.. include:: /module/CMAKE_REQUIRED_FLAGS.txt
.. include:: /module/CMAKE_REQUIRED_DEFINITIONS.txt
.. include:: /module/CMAKE_REQUIRED_LINK_OPTIONS.txt
.. include:: /module/CMAKE_REQUIRED_LIBRARIES.txt
.. include:: /module/CMAKE_REQUIRED_QUIET.txt
2019-11-11 23:01:05 +01:00
#]=======================================================================]
2018-04-23 21:13:27 +02:00
include_guard(GLOBAL)
2013-03-16 19:13:01 +02:00
macro(CHECK_VARIABLE_EXISTS VAR VARIABLE)
2015-04-27 22:25:09 +02:00
if(NOT DEFINED "${VARIABLE}")
2013-03-16 19:13:01 +02:00
set(MACRO_CHECK_VARIABLE_DEFINITIONS
"-DCHECK_VARIABLE_EXISTS=${VAR} ${CMAKE_REQUIRED_FLAGS}")
2015-04-27 22:25:09 +02:00
if(NOT CMAKE_REQUIRED_QUIET)
2020-08-30 11:54:41 +02:00
message(CHECK_START "Looking for ${VAR}")
2015-04-27 22:25:09 +02:00
endif()
2019-11-11 23:01:05 +01:00
if(CMAKE_REQUIRED_LINK_OPTIONS)
set(CHECK_VARIABLE_EXISTS_ADD_LINK_OPTIONS
LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
else()
set(CHECK_VARIABLE_EXISTS_ADD_LINK_OPTIONS)
endif()
2013-03-16 19:13:01 +02:00
if(CMAKE_REQUIRED_LIBRARIES)
set(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES
LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
else()
set(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES)
endif()
try_compile(${VARIABLE}
2022-11-16 20:14:03 +01:00
SOURCES ${CMAKE_ROOT}/Modules/CheckVariableExists.c
COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
2019-11-11 23:01:05 +01:00
${CHECK_VARIABLE_EXISTS_ADD_LINK_OPTIONS}
2013-03-16 19:13:01 +02:00
${CHECK_VARIABLE_EXISTS_ADD_LIBRARIES}
CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_VARIABLE_DEFINITIONS}
2023-05-23 16:38:00 +02:00
)
2013-03-16 19:13:01 +02:00
if(${VARIABLE})
set(${VARIABLE} 1 CACHE INTERNAL "Have variable ${VAR}")
2015-04-27 22:25:09 +02:00
if(NOT CMAKE_REQUIRED_QUIET)
2020-08-30 11:54:41 +02:00
message(CHECK_PASS "found")
2015-04-27 22:25:09 +02:00
endif()
2013-03-16 19:13:01 +02:00
else()
set(${VARIABLE} "" CACHE INTERNAL "Have variable ${VAR}")
2015-04-27 22:25:09 +02:00
if(NOT CMAKE_REQUIRED_QUIET)
2020-08-30 11:54:41 +02:00
message(CHECK_FAIL "not found")
2015-04-27 22:25:09 +02:00
endif()
2013-03-16 19:13:01 +02:00
endif()
endif()
endmacro()