1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file LICENSE.rst or https://cmake.org/licensing for details.
#[=======================================================================[.rst:
CheckVariableExists
-------------------
This module provides a command to check whether a C variable exists.
Load this module in a CMake project with:
.. code-block:: cmake
include(CheckVariableExists)
Commands
^^^^^^^^
This module provides the following command:
.. command:: check_variable_exists
Checks once if a C variable exists:
.. code-block:: cmake
check_variable_exists(<var> <variable>)
This command attempts to compile and link a test C program that references
the specified C variable ``<var>``. A boolean result of whether
the check was successful is stored in an internal cache variable
``<variable>``.
.. note::
Prefer using :module:`CheckSymbolExists` or :module:`CheckSourceCompiles`
instead of this command for more robust detection. This command performs
a link-only check and doesn't detect whether a variable is also declared
in system or library headers. Neither can it detect variables that might
be defined as preprocessor macros.
.. rubric:: Variables Affecting the Check
The following variables may be set before calling this command to modify
the way the check is run:
.. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst
.. include:: /module/include/CMAKE_REQUIRED_DEFINITIONS.rst
.. include:: /module/include/CMAKE_REQUIRED_LINK_OPTIONS.rst
.. include:: /module/include/CMAKE_REQUIRED_LIBRARIES.rst
.. include:: /module/include/CMAKE_REQUIRED_LINK_DIRECTORIES.rst
.. include:: /module/include/CMAKE_REQUIRED_QUIET.rst
Examples
^^^^^^^^
Example: Basic Usage
""""""""""""""""""""
In the following example, a check is performed whether the linker sees the
C variable ``tzname`` and stores the check result in the
``PROJECT_HAVE_TZNAME`` internal cache variable:
.. code-block:: cmake
include(CheckVariableExists)
check_variable_exists(tzname PROJECT_HAVE_TZNAME)
Example: Isolated Check With Linked Libraries
"""""""""""""""""""""""""""""""""""""""""""""
In the following example, this module is used in combination with the
:module:`CMakePushCheckState` module to link additional required library
using the ``CMAKE_REQUIRED_LIBRARIES`` variable. For example, in a find
module, to check whether the Net-SNMP library has the
``usmHMAC192SHA256AuthProtocol`` array:
.. code-block:: cmake
include(CheckVariableExists)
include(CMakePushCheckState)
find_library(SNMP_LIBRARY NAMES netsnmp)
if(SNMP_LIBRARY)
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_LIBRARIES ${SNMP_LIBRARY})
check_variable_exists(usmHMAC192SHA256AuthProtocol SNMP_HAVE_SHA256)
cmake_pop_check_state()
endif()
See Also
^^^^^^^^
* The :module:`CheckSymbolExists` module to check whether a C symbol exists.
#]=======================================================================]
include_guard(GLOBAL)
macro(CHECK_VARIABLE_EXISTS VAR VARIABLE)
if(NOT DEFINED "${VARIABLE}")
set(MACRO_CHECK_VARIABLE_DEFINITIONS
"-DCHECK_VARIABLE_EXISTS=${VAR} ${CMAKE_REQUIRED_FLAGS}")
if(NOT CMAKE_REQUIRED_QUIET)
message(CHECK_START "Looking for ${VAR}")
endif()
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()
if(CMAKE_REQUIRED_LIBRARIES)
set(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES
LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
else()
set(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES)
endif()
if(CMAKE_REQUIRED_LINK_DIRECTORIES)
set(_CVE_LINK_DIRECTORIES
"-DLINK_DIRECTORIES:STRING=${CMAKE_REQUIRED_LINK_DIRECTORIES}")
else()
set(_CVE_LINK_DIRECTORIES)
endif()
try_compile(${VARIABLE}
SOURCES ${CMAKE_ROOT}/Modules/CheckVariableExists.c
COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
${CHECK_VARIABLE_EXISTS_ADD_LINK_OPTIONS}
${CHECK_VARIABLE_EXISTS_ADD_LIBRARIES}
CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_VARIABLE_DEFINITIONS}
"${_CVE_LINK_DIRECTORIES}"
)
unset(_CVE_LINK_DIRECTORIES)
if(${VARIABLE})
set(${VARIABLE} 1 CACHE INTERNAL "Have variable ${VAR}")
if(NOT CMAKE_REQUIRED_QUIET)
message(CHECK_PASS "found")
endif()
else()
set(${VARIABLE} "" CACHE INTERNAL "Have variable ${VAR}")
if(NOT CMAKE_REQUIRED_QUIET)
message(CHECK_FAIL "not found")
endif()
endif()
endif()
endmacro()
|