CheckSymbolExists.cmake 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst 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. .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst
  26. .. include:: /module/include/CMAKE_REQUIRED_DEFINITIONS.rst
  27. .. include:: /module/include/CMAKE_REQUIRED_INCLUDES.rst
  28. .. include:: /module/include/CMAKE_REQUIRED_LINK_OPTIONS.rst
  29. .. include:: /module/include/CMAKE_REQUIRED_LIBRARIES.rst
  30. .. include:: /module/include/CMAKE_REQUIRED_LINK_DIRECTORIES.rst
  31. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst
  32. For example:
  33. .. code-block:: cmake
  34. include(CheckSymbolExists)
  35. # Check for macro SEEK_SET
  36. check_symbol_exists(SEEK_SET "stdio.h" HAVE_SEEK_SET)
  37. # Check for function fopen
  38. check_symbol_exists(fopen "stdio.h" HAVE_FOPEN)
  39. #]=======================================================================]
  40. include_guard(GLOBAL)
  41. macro(CHECK_SYMBOL_EXISTS SYMBOL FILES VARIABLE)
  42. if(CMAKE_C_COMPILER_LOADED)
  43. __CHECK_SYMBOL_EXISTS_FILTER_FLAGS(C)
  44. __CHECK_SYMBOL_EXISTS_IMPL(CheckSymbolExists.c "${SYMBOL}" "${FILES}" "${VARIABLE}" )
  45. __CHECK_SYMBOL_EXISTS_RESTORE_FLAGS(C)
  46. elseif(CMAKE_CXX_COMPILER_LOADED)
  47. __CHECK_SYMBOL_EXISTS_FILTER_FLAGS(CXX)
  48. __CHECK_SYMBOL_EXISTS_IMPL(CheckSymbolExists.cxx "${SYMBOL}" "${FILES}" "${VARIABLE}" )
  49. __CHECK_SYMBOL_EXISTS_RESTORE_FLAGS(CXX)
  50. else()
  51. message(FATAL_ERROR "CHECK_SYMBOL_EXISTS needs either C or CXX language enabled")
  52. endif()
  53. endmacro()
  54. macro(__CHECK_SYMBOL_EXISTS_FILTER_FLAGS LANG)
  55. if(CMAKE_TRY_COMPILE_CONFIGURATION)
  56. string(TOUPPER "${CMAKE_TRY_COMPILE_CONFIGURATION}" _tc_config)
  57. else()
  58. set(_tc_config "DEBUG")
  59. endif()
  60. foreach(v CMAKE_${LANG}_FLAGS CMAKE_${LANG}_FLAGS_${_tc_config})
  61. set(__${v}_SAVED "${${v}}")
  62. string(REGEX REPLACE "(^| )-Werror([= ][^-][^ ]*)?( |$)" " " ${v} "${${v}}")
  63. string(REGEX REPLACE "(^| )-pedantic-errors( |$)" " " ${v} "${${v}}")
  64. endforeach()
  65. endmacro()
  66. macro(__CHECK_SYMBOL_EXISTS_RESTORE_FLAGS LANG)
  67. if(CMAKE_TRY_COMPILE_CONFIGURATION)
  68. string(TOUPPER "${CMAKE_TRY_COMPILE_CONFIGURATION}" _tc_config)
  69. else()
  70. set(_tc_config "DEBUG")
  71. endif()
  72. foreach(v CMAKE_${LANG}_FLAGS CMAKE_${LANG}_FLAGS_${_tc_config})
  73. set(${v} "${__${v}_SAVED}")
  74. unset(__${v}_SAVED)
  75. endforeach()
  76. endmacro()
  77. macro(__CHECK_SYMBOL_EXISTS_IMPL SOURCEFILE SYMBOL FILES VARIABLE)
  78. if(NOT DEFINED "${VARIABLE}" OR "x${${VARIABLE}}" STREQUAL "x${VARIABLE}")
  79. set(_CSE_SOURCE "/* */\n")
  80. set(MACRO_CHECK_SYMBOL_EXISTS_FLAGS ${CMAKE_REQUIRED_FLAGS})
  81. if(CMAKE_REQUIRED_LINK_OPTIONS)
  82. set(CHECK_SYMBOL_EXISTS_LINK_OPTIONS
  83. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  84. else()
  85. set(CHECK_SYMBOL_EXISTS_LINK_OPTIONS)
  86. endif()
  87. if(CMAKE_REQUIRED_LIBRARIES)
  88. set(CHECK_SYMBOL_EXISTS_LIBS
  89. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  90. else()
  91. set(CHECK_SYMBOL_EXISTS_LIBS)
  92. endif()
  93. if(CMAKE_REQUIRED_INCLUDES)
  94. set(CMAKE_SYMBOL_EXISTS_INCLUDES
  95. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  96. else()
  97. set(CMAKE_SYMBOL_EXISTS_INCLUDES)
  98. endif()
  99. if(CMAKE_REQUIRED_LINK_DIRECTORIES)
  100. set(_CSE_LINK_DIRECTORIES
  101. "-DLINK_DIRECTORIES:STRING=${CMAKE_REQUIRED_LINK_DIRECTORIES}")
  102. else()
  103. set(_CSE_LINK_DIRECTORIES)
  104. endif()
  105. foreach(FILE ${FILES})
  106. string(APPEND _CSE_SOURCE
  107. "#include <${FILE}>\n")
  108. endforeach()
  109. string(APPEND _CSE_SOURCE "
  110. int main(int argc, char** argv)
  111. {
  112. (void)argv;")
  113. set(_CSE_CHECK_NON_MACRO "return ((int*)(&${SYMBOL}))[argc];")
  114. if("${SYMBOL}" MATCHES "^[a-zA-Z_][a-zA-Z0-9_]*$")
  115. # The SYMBOL has a legal macro name. Test whether it exists as a macro.
  116. string(APPEND _CSE_SOURCE "
  117. #ifndef ${SYMBOL}
  118. ${_CSE_CHECK_NON_MACRO}
  119. #else
  120. (void)argc;
  121. return 0;
  122. #endif")
  123. else()
  124. # The SYMBOL cannot be a macro (e.g., a template function).
  125. string(APPEND _CSE_SOURCE "
  126. ${_CSE_CHECK_NON_MACRO}")
  127. endif()
  128. string(APPEND _CSE_SOURCE "
  129. }\n")
  130. unset(_CSE_CHECK_NON_MACRO)
  131. if(NOT CMAKE_REQUIRED_QUIET)
  132. message(CHECK_START "Looking for ${SYMBOL}")
  133. endif()
  134. try_compile(${VARIABLE}
  135. SOURCE_FROM_VAR "${SOURCEFILE}" _CSE_SOURCE
  136. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  137. ${CHECK_SYMBOL_EXISTS_LINK_OPTIONS}
  138. ${CHECK_SYMBOL_EXISTS_LIBS}
  139. CMAKE_FLAGS
  140. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_SYMBOL_EXISTS_FLAGS}
  141. "${CMAKE_SYMBOL_EXISTS_INCLUDES}"
  142. "${_CSE_LINK_DIRECTORIES}"
  143. )
  144. unset(_CSE_LINK_DIRECTORIES)
  145. if(${VARIABLE})
  146. if(NOT CMAKE_REQUIRED_QUIET)
  147. message(CHECK_PASS "found")
  148. endif()
  149. set(${VARIABLE} 1 CACHE INTERNAL "Have symbol ${SYMBOL}")
  150. else()
  151. if(NOT CMAKE_REQUIRED_QUIET)
  152. message(CHECK_FAIL "not found")
  153. endif()
  154. set(${VARIABLE} "" CACHE INTERNAL "Have symbol ${SYMBOL}")
  155. endif()
  156. unset(_CSE_SOURCE)
  157. endif()
  158. endmacro()