CheckSymbolExists.cmake 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CheckSymbolExists
  5. -----------------
  6. Provides a macro to check if a symbol exists as a function, variable,
  7. or macro in ``C``.
  8. .. command:: check_symbol_exists
  9. .. code-block:: cmake
  10. check_symbol_exists(<symbol> <files> <variable>)
  11. Check that the ``<symbol>`` is available after including given header
  12. ``<files>`` and store the result in a ``<variable>``. Specify the list
  13. of files in one argument as a semicolon-separated list.
  14. ``<variable>`` will be created as an internal cache variable.
  15. If the header files define the symbol as a macro it is considered
  16. available and assumed to work. If the header files declare the symbol
  17. as a function or variable then the symbol must also be available for
  18. linking (so intrinsics may not be detected).
  19. If the symbol is a type, enum value, or intrinsic it will not be recognized
  20. (consider using :module:`CheckTypeSize` or :module:`CheckSourceCompiles`).
  21. If the check needs to be done in C++, consider using
  22. :module:`CheckCXXSymbolExists` instead.
  23. The following variables may be set before calling this macro to modify
  24. the way the check is run:
  25. ``CMAKE_REQUIRED_FLAGS``
  26. string of compile command line flags.
  27. ``CMAKE_REQUIRED_DEFINITIONS``
  28. a :ref:`;-list <CMake Language Lists>` of macros to define (-DFOO=bar).
  29. ``CMAKE_REQUIRED_INCLUDES``
  30. a :ref:`;-list <CMake Language Lists>` of header search paths to pass to
  31. the compiler.
  32. ``CMAKE_REQUIRED_LINK_OPTIONS``
  33. .. versionadded:: 3.14
  34. a :ref:`;-list <CMake Language Lists>` of options to add to the link command.
  35. ``CMAKE_REQUIRED_LIBRARIES``
  36. a :ref:`;-list <CMake Language Lists>` of libraries to add to the link
  37. command. See policy :policy:`CMP0075`.
  38. ``CMAKE_REQUIRED_QUIET``
  39. .. versionadded:: 3.1
  40. execute quietly without messages.
  41. For example:
  42. .. code-block:: cmake
  43. include(CheckSymbolExists)
  44. # Check for macro SEEK_SET
  45. check_symbol_exists(SEEK_SET "stdio.h" HAVE_SEEK_SET)
  46. # Check for function fopen
  47. check_symbol_exists(fopen "stdio.h" HAVE_FOPEN)
  48. #]=======================================================================]
  49. include_guard(GLOBAL)
  50. cmake_policy(PUSH)
  51. cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced
  52. macro(CHECK_SYMBOL_EXISTS SYMBOL FILES VARIABLE)
  53. if(CMAKE_C_COMPILER_LOADED)
  54. __CHECK_SYMBOL_EXISTS_IMPL("${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckSymbolExists.c" "${SYMBOL}" "${FILES}" "${VARIABLE}" )
  55. elseif(CMAKE_CXX_COMPILER_LOADED)
  56. __CHECK_SYMBOL_EXISTS_IMPL("${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckSymbolExists.cxx" "${SYMBOL}" "${FILES}" "${VARIABLE}" )
  57. else()
  58. message(FATAL_ERROR "CHECK_SYMBOL_EXISTS needs either C or CXX language enabled")
  59. endif()
  60. endmacro()
  61. macro(__CHECK_SYMBOL_EXISTS_IMPL SOURCEFILE SYMBOL FILES VARIABLE)
  62. if(NOT DEFINED "${VARIABLE}" OR "x${${VARIABLE}}" STREQUAL "x${VARIABLE}")
  63. set(CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n")
  64. set(MACRO_CHECK_SYMBOL_EXISTS_FLAGS ${CMAKE_REQUIRED_FLAGS})
  65. if(CMAKE_REQUIRED_LINK_OPTIONS)
  66. set(CHECK_SYMBOL_EXISTS_LINK_OPTIONS
  67. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  68. else()
  69. set(CHECK_SYMBOL_EXISTS_LINK_OPTIONS)
  70. endif()
  71. if(CMAKE_REQUIRED_LIBRARIES)
  72. set(CHECK_SYMBOL_EXISTS_LIBS
  73. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  74. else()
  75. set(CHECK_SYMBOL_EXISTS_LIBS)
  76. endif()
  77. if(CMAKE_REQUIRED_INCLUDES)
  78. set(CMAKE_SYMBOL_EXISTS_INCLUDES
  79. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  80. else()
  81. set(CMAKE_SYMBOL_EXISTS_INCLUDES)
  82. endif()
  83. foreach(FILE ${FILES})
  84. string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT
  85. "#include <${FILE}>\n")
  86. endforeach()
  87. string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT "
  88. int main(int argc, char** argv)
  89. {
  90. (void)argv;")
  91. set(_CSE_CHECK_NON_MACRO "return ((int*)(&${SYMBOL}))[argc];")
  92. if("${SYMBOL}" MATCHES "^[a-zA-Z_][a-zA-Z0-9_]*$")
  93. # The SYMBOL has a legal macro name. Test whether it exists as a macro.
  94. string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT "
  95. #ifndef ${SYMBOL}
  96. ${_CSE_CHECK_NON_MACRO}
  97. #else
  98. (void)argc;
  99. return 0;
  100. #endif")
  101. else()
  102. # The SYMBOL cannot be a macro (e.g., a template function).
  103. string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT "
  104. ${_CSE_CHECK_NON_MACRO}")
  105. endif()
  106. string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT "
  107. }")
  108. unset(_CSE_CHECK_NON_MACRO)
  109. configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
  110. "${SOURCEFILE}" @ONLY)
  111. if(NOT CMAKE_REQUIRED_QUIET)
  112. message(CHECK_START "Looking for ${SYMBOL}")
  113. endif()
  114. try_compile(${VARIABLE}
  115. ${CMAKE_BINARY_DIR}
  116. "${SOURCEFILE}"
  117. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  118. ${CHECK_SYMBOL_EXISTS_LINK_OPTIONS}
  119. ${CHECK_SYMBOL_EXISTS_LIBS}
  120. CMAKE_FLAGS
  121. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_SYMBOL_EXISTS_FLAGS}
  122. "${CMAKE_SYMBOL_EXISTS_INCLUDES}"
  123. OUTPUT_VARIABLE OUTPUT)
  124. if(${VARIABLE})
  125. if(NOT CMAKE_REQUIRED_QUIET)
  126. message(CHECK_PASS "found")
  127. endif()
  128. set(${VARIABLE} 1 CACHE INTERNAL "Have symbol ${SYMBOL}")
  129. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  130. "Determining if the ${SYMBOL} "
  131. "exist passed with the following output:\n"
  132. "${OUTPUT}\nFile ${SOURCEFILE}:\n"
  133. "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
  134. else()
  135. if(NOT CMAKE_REQUIRED_QUIET)
  136. message(CHECK_FAIL "not found")
  137. endif()
  138. set(${VARIABLE} "" CACHE INTERNAL "Have symbol ${SYMBOL}")
  139. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  140. "Determining if the ${SYMBOL} "
  141. "exist failed with the following output:\n"
  142. "${OUTPUT}\nFile ${SOURCEFILE}:\n"
  143. "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
  144. endif()
  145. unset(CMAKE_CONFIGURABLE_FILE_CONTENT)
  146. endif()
  147. endmacro()
  148. cmake_policy(POP)