CheckSymbolExists.cmake 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. ::
  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:`CheckCSourceCompiles`).
  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. list of macros to define (-DFOO=bar)
  29. ``CMAKE_REQUIRED_INCLUDES``
  30. list of include directories
  31. ``CMAKE_REQUIRED_LIBRARIES``
  32. list of libraries to link
  33. ``CMAKE_REQUIRED_QUIET``
  34. execute quietly without messages
  35. #]=======================================================================]
  36. macro(CHECK_SYMBOL_EXISTS SYMBOL FILES VARIABLE)
  37. if(CMAKE_C_COMPILER_LOADED)
  38. _CHECK_SYMBOL_EXISTS("${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckSymbolExists.c" "${SYMBOL}" "${FILES}" "${VARIABLE}" )
  39. elseif(CMAKE_CXX_COMPILER_LOADED)
  40. _CHECK_SYMBOL_EXISTS("${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckSymbolExists.cxx" "${SYMBOL}" "${FILES}" "${VARIABLE}" )
  41. else()
  42. message(FATAL_ERROR "CHECK_SYMBOL_EXISTS needs either C or CXX language enabled")
  43. endif()
  44. endmacro()
  45. macro(_CHECK_SYMBOL_EXISTS SOURCEFILE SYMBOL FILES VARIABLE)
  46. if(NOT DEFINED "${VARIABLE}" OR "x${${VARIABLE}}" STREQUAL "x${VARIABLE}")
  47. set(CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n")
  48. set(MACRO_CHECK_SYMBOL_EXISTS_FLAGS ${CMAKE_REQUIRED_FLAGS})
  49. if(CMAKE_REQUIRED_LIBRARIES)
  50. set(CHECK_SYMBOL_EXISTS_LIBS
  51. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  52. else()
  53. set(CHECK_SYMBOL_EXISTS_LIBS)
  54. endif()
  55. if(CMAKE_REQUIRED_INCLUDES)
  56. set(CMAKE_SYMBOL_EXISTS_INCLUDES
  57. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  58. else()
  59. set(CMAKE_SYMBOL_EXISTS_INCLUDES)
  60. endif()
  61. foreach(FILE ${FILES})
  62. string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT
  63. "#include <${FILE}>\n")
  64. endforeach()
  65. string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT
  66. "\nint main(int argc, char** argv)\n{\n (void)argv;\n#ifndef ${SYMBOL}\n return ((int*)(&${SYMBOL}))[argc];\n#else\n (void)argc;\n return 0;\n#endif\n}\n")
  67. configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
  68. "${SOURCEFILE}" @ONLY)
  69. if(NOT CMAKE_REQUIRED_QUIET)
  70. message(STATUS "Looking for ${SYMBOL}")
  71. endif()
  72. try_compile(${VARIABLE}
  73. ${CMAKE_BINARY_DIR}
  74. "${SOURCEFILE}"
  75. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  76. ${CHECK_SYMBOL_EXISTS_LIBS}
  77. CMAKE_FLAGS
  78. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_SYMBOL_EXISTS_FLAGS}
  79. "${CMAKE_SYMBOL_EXISTS_INCLUDES}"
  80. OUTPUT_VARIABLE OUTPUT)
  81. if(${VARIABLE})
  82. if(NOT CMAKE_REQUIRED_QUIET)
  83. message(STATUS "Looking for ${SYMBOL} - found")
  84. endif()
  85. set(${VARIABLE} 1 CACHE INTERNAL "Have symbol ${SYMBOL}")
  86. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  87. "Determining if the ${SYMBOL} "
  88. "exist passed with the following output:\n"
  89. "${OUTPUT}\nFile ${SOURCEFILE}:\n"
  90. "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
  91. else()
  92. if(NOT CMAKE_REQUIRED_QUIET)
  93. message(STATUS "Looking for ${SYMBOL} - not found")
  94. endif()
  95. set(${VARIABLE} "" CACHE INTERNAL "Have symbol ${SYMBOL}")
  96. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  97. "Determining if the ${SYMBOL} "
  98. "exist failed with the following output:\n"
  99. "${OUTPUT}\nFile ${SOURCEFILE}:\n"
  100. "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
  101. endif()
  102. endif()
  103. endmacro()