CheckIncludeFiles.cmake 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. ::
  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. list of macros to define (-DFOO=bar)
  25. ``CMAKE_REQUIRED_INCLUDES``
  26. list of include directories
  27. ``CMAKE_REQUIRED_LIBRARIES``
  28. A list of libraries to link. See policy :policy:`CMP0075`.
  29. ``CMAKE_REQUIRED_QUIET``
  30. execute quietly without messages
  31. See modules :module:`CheckIncludeFile` and :module:`CheckIncludeFileCXX`
  32. to check for a single header file in ``C`` or ``CXX`` languages.
  33. #]=======================================================================]
  34. include_guard(GLOBAL)
  35. macro(CHECK_INCLUDE_FILES INCLUDE VARIABLE)
  36. if(NOT DEFINED "${VARIABLE}")
  37. set(CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n")
  38. if("x${ARGN}" STREQUAL "x")
  39. if(CMAKE_C_COMPILER_LOADED)
  40. set(_lang C)
  41. elseif(CMAKE_CXX_COMPILER_LOADED)
  42. set(_lang CXX)
  43. else()
  44. message(FATAL_ERROR "CHECK_INCLUDE_FILES needs either C or CXX language enabled.\n")
  45. endif()
  46. elseif("x${ARGN}" MATCHES "^xLANGUAGE;([a-zA-Z]+)$")
  47. set(_lang "${CMAKE_MATCH_1}")
  48. elseif("x${ARGN}" MATCHES "^xLANGUAGE$")
  49. message(FATAL_ERROR "No languages listed for LANGUAGE option.\nSupported languages: C, CXX.\n")
  50. else()
  51. message(FATAL_ERROR "Unknown arguments:\n ${ARGN}\n")
  52. endif()
  53. if(_lang STREQUAL "C")
  54. set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckIncludeFiles/${VARIABLE}.c)
  55. elseif(_lang STREQUAL "CXX")
  56. set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckIncludeFiles/${VARIABLE}.cpp)
  57. else()
  58. message(FATAL_ERROR "Unknown language:\n ${_lang}\nSupported languages: C, CXX.\n")
  59. endif()
  60. if(CMAKE_REQUIRED_INCLUDES)
  61. set(CHECK_INCLUDE_FILES_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
  62. else()
  63. set(CHECK_INCLUDE_FILES_INCLUDE_DIRS)
  64. endif()
  65. set(CHECK_INCLUDE_FILES_CONTENT "/* */\n")
  66. set(MACRO_CHECK_INCLUDE_FILES_FLAGS ${CMAKE_REQUIRED_FLAGS})
  67. foreach(FILE ${INCLUDE})
  68. string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT
  69. "#include <${FILE}>\n")
  70. endforeach()
  71. string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT
  72. "\n\nint main(void){return 0;}\n")
  73. configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
  74. "${src}" @ONLY)
  75. set(_INCLUDE ${INCLUDE}) # remove empty elements
  76. if("${_INCLUDE}" MATCHES "^([^;]+);.+;([^;]+)$")
  77. list(LENGTH _INCLUDE _INCLUDE_LEN)
  78. set(_description "${_INCLUDE_LEN} include files ${CMAKE_MATCH_1}, ..., ${CMAKE_MATCH_2}")
  79. elseif("${_INCLUDE}" MATCHES "^([^;]+);([^;]+)$")
  80. set(_description "include files ${CMAKE_MATCH_1}, ${CMAKE_MATCH_2}")
  81. else()
  82. set(_description "include file ${_INCLUDE}")
  83. endif()
  84. set(_CIF_LINK_LIBRARIES "")
  85. if(CMAKE_REQUIRED_LIBRARIES)
  86. cmake_policy(GET CMP0075 _CIF_CMP0075
  87. PARENT_SCOPE # undocumented, do not use outside of CMake
  88. )
  89. if("x${_CIF_CMP0075}x" STREQUAL "xNEWx")
  90. set(_CIF_LINK_LIBRARIES LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  91. elseif("x${_CIF_CMP0075}x" STREQUAL "xOLDx")
  92. elseif(NOT _CIF_CMP0075_WARNED)
  93. set(_CIF_CMP0075_WARNED 1)
  94. message(AUTHOR_WARNING
  95. "Policy CMP0075 is not set: Include file check macros honor CMAKE_REQUIRED_LIBRARIES. "
  96. "Run \"cmake --help-policy CMP0075\" for policy details. "
  97. "Use the cmake_policy command to set the policy and suppress this warning."
  98. "\n"
  99. "CMAKE_REQUIRED_LIBRARIES is set to:\n"
  100. " ${CMAKE_REQUIRED_LIBRARIES}\n"
  101. "For compatibility with CMake 3.11 and below this check is ignoring it."
  102. )
  103. endif()
  104. unset(_CIF_CMP0075)
  105. endif()
  106. if(NOT CMAKE_REQUIRED_QUIET)
  107. message(STATUS "Looking for ${_description}")
  108. endif()
  109. try_compile(${VARIABLE}
  110. ${CMAKE_BINARY_DIR}
  111. ${src}
  112. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  113. ${_CIF_LINK_LIBRARIES}
  114. CMAKE_FLAGS
  115. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILES_FLAGS}
  116. "${CHECK_INCLUDE_FILES_INCLUDE_DIRS}"
  117. OUTPUT_VARIABLE OUTPUT)
  118. unset(_CIF_LINK_LIBRARIES)
  119. if(${VARIABLE})
  120. if(NOT CMAKE_REQUIRED_QUIET)
  121. message(STATUS "Looking for ${_description} - found")
  122. endif()
  123. set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
  124. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  125. "Determining if files ${INCLUDE} "
  126. "exist passed with the following output:\n"
  127. "${OUTPUT}\n\n")
  128. else()
  129. if(NOT CMAKE_REQUIRED_QUIET)
  130. message(STATUS "Looking for ${_description} - not found")
  131. endif()
  132. set(${VARIABLE} "" CACHE INTERNAL "Have includes ${INCLUDE}")
  133. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  134. "Determining if files ${INCLUDE} "
  135. "exist failed with the following output:\n"
  136. "${OUTPUT}\nSource:\n${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
  137. endif()
  138. endif()
  139. endmacro()