cmake/Modules/CheckSymbolExists.cmake

165 lines
5.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.
2017-07-20 19:35:53 +02:00
#[=======================================================================[.rst:
CheckSymbolExists
-----------------
Provides a macro to check if a symbol exists as a function, variable,
or macro in ``C``.
.. command:: check_symbol_exists
2019-11-11 23:01:05 +01:00
.. code-block:: cmake
2017-07-20 19:35:53 +02:00
check_symbol_exists(<symbol> <files> <variable>)
Check that the ``<symbol>`` is available after including given header
``<files>`` and store the result in a ``<variable>``. Specify the list
of files in one argument as a semicolon-separated list.
``<variable>`` will be created as an internal cache variable.
If the header files define the symbol as a macro it is considered
available and assumed to work. If the header files declare the symbol
as a function or variable then the symbol must also be available for
linking (so intrinsics may not be detected).
If the symbol is a type, enum value, or intrinsic it will not be recognized
2021-11-20 13:41:27 +01:00
(consider using :module:`CheckTypeSize` or :module:`CheckSourceCompiles`).
2017-07-20 19:35:53 +02:00
If the check needs to be done in C++, consider using
:module:`CheckCXXSymbolExists` instead.
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_INCLUDES.txt
.. include:: /module/CMAKE_REQUIRED_LINK_OPTIONS.txt
.. include:: /module/CMAKE_REQUIRED_LIBRARIES.txt
.. include:: /module/CMAKE_REQUIRED_QUIET.txt
2020-02-01 23:06:01 +01:00
For example:
.. code-block:: cmake
include(CheckSymbolExists)
# Check for macro SEEK_SET
check_symbol_exists(SEEK_SET "stdio.h" HAVE_SEEK_SET)
# Check for function fopen
check_symbol_exists(fopen "stdio.h" HAVE_FOPEN)
2017-07-20 19:35:53 +02:00
#]=======================================================================]
2018-04-23 21:13:27 +02:00
include_guard(GLOBAL)
2023-07-02 19:51:09 +02:00
block(SCOPE_FOR POLICIES)
2018-08-09 18:06:22 +02:00
cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced
2013-03-16 19:13:01 +02:00
macro(CHECK_SYMBOL_EXISTS SYMBOL FILES VARIABLE)
2015-11-17 17:22:37 +01:00
if(CMAKE_C_COMPILER_LOADED)
2022-03-29 21:10:50 +02:00
__CHECK_SYMBOL_EXISTS_FILTER_FLAGS(C)
2022-11-16 20:14:03 +01:00
__CHECK_SYMBOL_EXISTS_IMPL(CheckSymbolExists.c "${SYMBOL}" "${FILES}" "${VARIABLE}" )
2022-03-29 21:10:50 +02:00
__CHECK_SYMBOL_EXISTS_RESTORE_FLAGS(C)
2015-11-17 17:22:37 +01:00
elseif(CMAKE_CXX_COMPILER_LOADED)
2022-03-29 21:10:50 +02:00
__CHECK_SYMBOL_EXISTS_FILTER_FLAGS(CXX)
2022-11-16 20:14:03 +01:00
__CHECK_SYMBOL_EXISTS_IMPL(CheckSymbolExists.cxx "${SYMBOL}" "${FILES}" "${VARIABLE}" )
2022-03-29 21:10:50 +02:00
__CHECK_SYMBOL_EXISTS_RESTORE_FLAGS(CXX)
2015-11-17 17:22:37 +01:00
else()
message(FATAL_ERROR "CHECK_SYMBOL_EXISTS needs either C or CXX language enabled")
endif()
2013-03-16 19:13:01 +02:00
endmacro()
2012-02-18 12:40:36 +02:00
2022-03-29 21:10:50 +02:00
macro(__CHECK_SYMBOL_EXISTS_FILTER_FLAGS LANG)
set(__CMAKE_${LANG}_FLAGS_SAVED "${CMAKE_${LANG}_FLAGS}")
string(REGEX REPLACE "(^| )-Werror([= ][^ ]*)?( |$)" " " CMAKE_${LANG}_FLAGS "${CMAKE_${LANG}_FLAGS}")
string(REGEX REPLACE "(^| )-pedantic-errors( |$)" " " CMAKE_${LANG}_FLAGS "${CMAKE_${LANG}_FLAGS}")
endmacro()
macro(__CHECK_SYMBOL_EXISTS_RESTORE_FLAGS LANG)
set(CMAKE_${LANG}_FLAGS "${__CMAKE_${LANG}_FLAGS_SAVED}")
unset(__CMAKE_${LANG}_FLAGS_SAVED)
endmacro()
2018-01-26 17:06:56 +01:00
macro(__CHECK_SYMBOL_EXISTS_IMPL SOURCEFILE SYMBOL FILES VARIABLE)
2015-04-27 22:25:09 +02:00
if(NOT DEFINED "${VARIABLE}" OR "x${${VARIABLE}}" STREQUAL "x${VARIABLE}")
2022-11-16 20:14:03 +01:00
set(_CSE_SOURCE "/* */\n")
2013-03-16 19:13:01 +02:00
set(MACRO_CHECK_SYMBOL_EXISTS_FLAGS ${CMAKE_REQUIRED_FLAGS})
2019-11-11 23:01:05 +01:00
if(CMAKE_REQUIRED_LINK_OPTIONS)
set(CHECK_SYMBOL_EXISTS_LINK_OPTIONS
LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
else()
set(CHECK_SYMBOL_EXISTS_LINK_OPTIONS)
endif()
2013-03-16 19:13:01 +02:00
if(CMAKE_REQUIRED_LIBRARIES)
set(CHECK_SYMBOL_EXISTS_LIBS
LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
else()
set(CHECK_SYMBOL_EXISTS_LIBS)
endif()
if(CMAKE_REQUIRED_INCLUDES)
set(CMAKE_SYMBOL_EXISTS_INCLUDES
"-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
2013-03-16 19:13:01 +02:00
else()
set(CMAKE_SYMBOL_EXISTS_INCLUDES)
endif()
foreach(FILE ${FILES})
2022-11-16 20:14:03 +01:00
string(APPEND _CSE_SOURCE
2016-10-30 18:24:19 +01:00
"#include <${FILE}>\n")
2013-03-16 19:13:01 +02:00
endforeach()
2022-11-16 20:14:03 +01:00
string(APPEND _CSE_SOURCE "
2020-02-01 23:06:01 +01:00
int main(int argc, char** argv)
{
(void)argv;")
set(_CSE_CHECK_NON_MACRO "return ((int*)(&${SYMBOL}))[argc];")
if("${SYMBOL}" MATCHES "^[a-zA-Z_][a-zA-Z0-9_]*$")
# The SYMBOL has a legal macro name. Test whether it exists as a macro.
2022-11-16 20:14:03 +01:00
string(APPEND _CSE_SOURCE "
2020-02-01 23:06:01 +01:00
#ifndef ${SYMBOL}
${_CSE_CHECK_NON_MACRO}
#else
(void)argc;
return 0;
#endif")
else()
# The SYMBOL cannot be a macro (e.g., a template function).
2022-11-16 20:14:03 +01:00
string(APPEND _CSE_SOURCE "
2020-02-01 23:06:01 +01:00
${_CSE_CHECK_NON_MACRO}")
endif()
2022-11-16 20:14:03 +01:00
string(APPEND _CSE_SOURCE "
2023-05-23 16:38:00 +02:00
}\n")
2020-02-01 23:06:01 +01:00
unset(_CSE_CHECK_NON_MACRO)
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 ${SYMBOL}")
2015-04-27 22:25:09 +02:00
endif()
2013-03-16 19:13:01 +02:00
try_compile(${VARIABLE}
2022-11-16 20:14:03 +01:00
SOURCE_FROM_VAR "${SOURCEFILE}" _CSE_SOURCE
COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
2019-11-11 23:01:05 +01:00
${CHECK_SYMBOL_EXISTS_LINK_OPTIONS}
2013-03-16 19:13:01 +02:00
${CHECK_SYMBOL_EXISTS_LIBS}
2012-02-18 12:40:36 +02:00
CMAKE_FLAGS
-DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_SYMBOL_EXISTS_FLAGS}
"${CMAKE_SYMBOL_EXISTS_INCLUDES}"
2023-05-23 16:38:00 +02:00
)
2013-03-16 19:13:01 +02:00
if(${VARIABLE})
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
set(${VARIABLE} 1 CACHE INTERNAL "Have symbol ${SYMBOL}")
else()
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
set(${VARIABLE} "" CACHE INTERNAL "Have symbol ${SYMBOL}")
endif()
2022-11-16 20:14:03 +01:00
unset(_CSE_SOURCE)
2013-03-16 19:13:01 +02:00
endif()
endmacro()
2018-08-09 18:06:22 +02:00
2023-07-02 19:51:09 +02:00
endblock()