CheckIncludeFiles.cmake 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. CheckIncludeFiles
  5. -----------------
  6. Provides a macro to check if a list of one or more header files can
  7. be included together.
  8. .. command:: CHECK_INCLUDE_FILES
  9. .. code-block:: cmake
  10. CHECK_INCLUDE_FILES("<includes>" <variable> [LANGUAGE <language>])
  11. Check if the given ``<includes>`` list may be included together
  12. in a source file and store the result in an internal cache
  13. entry named ``<variable>``. Specify the ``<includes>`` argument
  14. as a :ref:`;-list <CMake Language Lists>` of header file names.
  15. If ``LANGUAGE`` is set, the specified compiler will be used to perform the
  16. check. Acceptable values are ``C`` and ``CXX``. If not set, the C compiler
  17. will be used if enabled. If the C compiler is not enabled, the C++
  18. compiler will be used if enabled.
  19. The following variables may be set before calling this macro to modify
  20. the way the check is run:
  21. ``CMAKE_REQUIRED_FLAGS``
  22. string of compile command line flags.
  23. ``CMAKE_REQUIRED_DEFINITIONS``
  24. a :ref:`;-list <CMake Language Lists>` of macros to define (-DFOO=bar).
  25. ``CMAKE_REQUIRED_INCLUDES``
  26. a :ref:`;-list <CMake Language Lists>` of header search paths to pass to
  27. the compiler.
  28. ``CMAKE_REQUIRED_LINK_OPTIONS``
  29. .. versionadded:: 3.14
  30. a :ref:`;-list <CMake Language Lists>` of options to add to the link command.
  31. ``CMAKE_REQUIRED_LIBRARIES``
  32. a :ref:`;-list <CMake Language Lists>` of libraries to add to the link
  33. command. See policy :policy:`CMP0075`.
  34. ``CMAKE_REQUIRED_QUIET``
  35. .. versionadded:: 3.1
  36. execute quietly without messages.
  37. See modules :module:`CheckIncludeFile` and :module:`CheckIncludeFileCXX`
  38. to check for a single header file in ``C`` or ``CXX`` languages.
  39. #]=======================================================================]
  40. include_guard(GLOBAL)
  41. macro(CHECK_INCLUDE_FILES INCLUDE VARIABLE)
  42. if(NOT DEFINED "${VARIABLE}")
  43. set(CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n")
  44. if("x${ARGN}" STREQUAL "x")
  45. if(CMAKE_C_COMPILER_LOADED)
  46. set(_lang C)
  47. elseif(CMAKE_CXX_COMPILER_LOADED)
  48. set(_lang CXX)
  49. else()
  50. message(FATAL_ERROR "CHECK_INCLUDE_FILES needs either C or CXX language enabled.\n")
  51. endif()
  52. elseif("x${ARGN}" MATCHES "^xLANGUAGE;([a-zA-Z]+)$")
  53. set(_lang "${CMAKE_MATCH_1}")
  54. elseif("x${ARGN}" MATCHES "^xLANGUAGE$")
  55. message(FATAL_ERROR "No languages listed for LANGUAGE option.\nSupported languages: C, CXX.\n")
  56. else()
  57. message(FATAL_ERROR "Unknown arguments:\n ${ARGN}\n")
  58. endif()
  59. if(_lang STREQUAL "C")
  60. set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckIncludeFiles/${VARIABLE}.c)
  61. elseif(_lang STREQUAL "CXX")
  62. set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckIncludeFiles/${VARIABLE}.cpp)
  63. else()
  64. message(FATAL_ERROR "Unknown language:\n ${_lang}\nSupported languages: C, CXX.\n")
  65. endif()
  66. if(CMAKE_REQUIRED_INCLUDES)
  67. set(CHECK_INCLUDE_FILES_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
  68. else()
  69. set(CHECK_INCLUDE_FILES_INCLUDE_DIRS)
  70. endif()
  71. set(CHECK_INCLUDE_FILES_CONTENT "/* */\n")
  72. set(MACRO_CHECK_INCLUDE_FILES_FLAGS ${CMAKE_REQUIRED_FLAGS})
  73. foreach(FILE ${INCLUDE})
  74. string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT
  75. "#include <${FILE}>\n")
  76. endforeach()
  77. string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT
  78. "\n\nint main(void){return 0;}\n")
  79. configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
  80. "${src}" @ONLY)
  81. set(_INCLUDE ${INCLUDE}) # remove empty elements
  82. if("${_INCLUDE}" MATCHES "^([^;]+);.+;([^;]+)$")
  83. list(LENGTH _INCLUDE _INCLUDE_LEN)
  84. set(_description "${_INCLUDE_LEN} include files ${CMAKE_MATCH_1}, ..., ${CMAKE_MATCH_2}")
  85. elseif("${_INCLUDE}" MATCHES "^([^;]+);([^;]+)$")
  86. set(_description "include files ${CMAKE_MATCH_1}, ${CMAKE_MATCH_2}")
  87. else()
  88. set(_description "include file ${_INCLUDE}")
  89. endif()
  90. set(_CIF_LINK_OPTIONS)
  91. if(CMAKE_REQUIRED_LINK_OPTIONS)
  92. set(_CIF_LINK_OPTIONS LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  93. endif()
  94. set(_CIF_LINK_LIBRARIES "")
  95. if(CMAKE_REQUIRED_LIBRARIES)
  96. cmake_policy(GET CMP0075 _CIF_CMP0075
  97. PARENT_SCOPE # undocumented, do not use outside of CMake
  98. )
  99. if("x${_CIF_CMP0075}x" STREQUAL "xNEWx")
  100. set(_CIF_LINK_LIBRARIES LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  101. elseif("x${_CIF_CMP0075}x" STREQUAL "xOLDx")
  102. elseif(NOT _CIF_CMP0075_WARNED)
  103. set(_CIF_CMP0075_WARNED 1)
  104. message(AUTHOR_WARNING
  105. "Policy CMP0075 is not set: Include file check macros honor CMAKE_REQUIRED_LIBRARIES. "
  106. "Run \"cmake --help-policy CMP0075\" for policy details. "
  107. "Use the cmake_policy command to set the policy and suppress this warning."
  108. "\n"
  109. "CMAKE_REQUIRED_LIBRARIES is set to:\n"
  110. " ${CMAKE_REQUIRED_LIBRARIES}\n"
  111. "For compatibility with CMake 3.11 and below this check is ignoring it."
  112. )
  113. endif()
  114. unset(_CIF_CMP0075)
  115. endif()
  116. if(NOT CMAKE_REQUIRED_QUIET)
  117. message(CHECK_START "Looking for ${_description}")
  118. endif()
  119. try_compile(${VARIABLE}
  120. ${CMAKE_BINARY_DIR}
  121. ${src}
  122. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  123. ${_CIF_LINK_OPTIONS}
  124. ${_CIF_LINK_LIBRARIES}
  125. CMAKE_FLAGS
  126. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILES_FLAGS}
  127. "${CHECK_INCLUDE_FILES_INCLUDE_DIRS}"
  128. OUTPUT_VARIABLE OUTPUT)
  129. unset(_CIF_LINK_OPTIONS)
  130. unset(_CIF_LINK_LIBRARIES)
  131. if(${VARIABLE})
  132. if(NOT CMAKE_REQUIRED_QUIET)
  133. message(CHECK_PASS "found")
  134. endif()
  135. set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
  136. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  137. "Determining if files ${INCLUDE} "
  138. "exist passed with the following output:\n"
  139. "${OUTPUT}\n\n")
  140. else()
  141. if(NOT CMAKE_REQUIRED_QUIET)
  142. message(CHECK_FAIL "not found")
  143. endif()
  144. set(${VARIABLE} "" CACHE INTERNAL "Have includes ${INCLUDE}")
  145. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  146. "Determining if files ${INCLUDE} "
  147. "exist failed with the following output:\n"
  148. "${OUTPUT}\nSource:\n${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
  149. endif()
  150. endif()
  151. endmacro()