CheckIncludeFiles.cmake 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. 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. .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst
  22. .. include:: /module/include/CMAKE_REQUIRED_DEFINITIONS.rst
  23. .. include:: /module/include/CMAKE_REQUIRED_INCLUDES.rst
  24. .. include:: /module/include/CMAKE_REQUIRED_LINK_OPTIONS.rst
  25. .. include:: /module/include/CMAKE_REQUIRED_LIBRARIES.rst
  26. .. include:: /module/include/CMAKE_REQUIRED_LINK_DIRECTORIES.rst
  27. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst
  28. Examples
  29. ^^^^^^^^
  30. Checking whether one or more ``C`` headers exist and storing the check result
  31. in cache variables:
  32. .. code-block:: cmake
  33. include(CheckIncludeFiles)
  34. check_include_files(sys/socket.h HAVE_SYS_SOCKET_H)
  35. if(HAVE_SYS_SOCKET_H)
  36. # The <net/if.h> header on Darwin and BSD-like systems is not self-contained
  37. # and also requires <sys/socket.h>
  38. check_include_files("sys/socket.h;net/if.h" HAVE_NET_IF_H)
  39. else()
  40. check_include_files(net/if.h HAVE_NET_IF_H)
  41. endif()
  42. The ``LANGUAGE`` option can be used to specify which compiler to use. For
  43. example, checking multiple ``C++`` headers, when both ``C`` and ``CXX``
  44. languages are enabled in the project:
  45. .. code-block:: cmake
  46. include(CheckIncludeFiles)
  47. check_include_files("header_1.hpp;header_2.hpp" HAVE_HEADERS LANGUAGE CXX)
  48. See Also
  49. ^^^^^^^^
  50. * The :module:`CheckIncludeFile` module to check for single ``C`` header.
  51. * The :module:`CheckIncludeFileCXX` module to check for single ``C++`` header.
  52. #]=======================================================================]
  53. include_guard(GLOBAL)
  54. macro(CHECK_INCLUDE_FILES INCLUDE VARIABLE)
  55. if(NOT DEFINED "${VARIABLE}")
  56. set(_src_content "/* */\n")
  57. if("x${ARGN}" STREQUAL "x")
  58. if(CMAKE_C_COMPILER_LOADED)
  59. set(_lang C)
  60. elseif(CMAKE_CXX_COMPILER_LOADED)
  61. set(_lang CXX)
  62. else()
  63. message(FATAL_ERROR "CHECK_INCLUDE_FILES needs either C or CXX language enabled.\n")
  64. endif()
  65. elseif("x${ARGN}" MATCHES "^xLANGUAGE;([a-zA-Z]+)$")
  66. set(_lang "${CMAKE_MATCH_1}")
  67. elseif("x${ARGN}" MATCHES "^xLANGUAGE$")
  68. message(FATAL_ERROR "No languages listed for LANGUAGE option.\nSupported languages: C, CXX.\n")
  69. else()
  70. message(FATAL_ERROR "Unknown arguments:\n ${ARGN}\n")
  71. endif()
  72. string(MAKE_C_IDENTIFIER ${VARIABLE} _variable_escaped)
  73. if(_lang STREQUAL "C")
  74. set(src ${_variable_escaped}.c)
  75. elseif(_lang STREQUAL "CXX")
  76. set(src ${_variable_escaped}.cpp)
  77. else()
  78. message(FATAL_ERROR "Unknown language:\n ${_lang}\nSupported languages: C, CXX.\n")
  79. endif()
  80. if(CMAKE_REQUIRED_INCLUDES)
  81. set(CHECK_INCLUDE_FILES_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
  82. else()
  83. set(CHECK_INCLUDE_FILES_INCLUDE_DIRS)
  84. endif()
  85. set(CHECK_INCLUDE_FILES_CONTENT "/* */\n")
  86. set(MACRO_CHECK_INCLUDE_FILES_FLAGS ${CMAKE_REQUIRED_FLAGS})
  87. foreach(FILE ${INCLUDE})
  88. string(APPEND _src_content
  89. "#include <${FILE}>\n")
  90. endforeach()
  91. string(APPEND _src_content
  92. "\n\nint main(void){return 0;}\n")
  93. set(_INCLUDE ${INCLUDE}) # remove empty elements
  94. if("${_INCLUDE}" MATCHES "^([^;]+);.+;([^;]+)$")
  95. list(LENGTH _INCLUDE _INCLUDE_LEN)
  96. set(_description "${_INCLUDE_LEN} include files ${CMAKE_MATCH_1}, ..., ${CMAKE_MATCH_2}")
  97. elseif("${_INCLUDE}" MATCHES "^([^;]+);([^;]+)$")
  98. set(_description "include files ${CMAKE_MATCH_1}, ${CMAKE_MATCH_2}")
  99. else()
  100. set(_description "include file ${_INCLUDE}")
  101. endif()
  102. set(_CIF_LINK_OPTIONS)
  103. if(CMAKE_REQUIRED_LINK_OPTIONS)
  104. set(_CIF_LINK_OPTIONS LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  105. endif()
  106. set(_CIF_LINK_LIBRARIES "")
  107. if(CMAKE_REQUIRED_LIBRARIES)
  108. cmake_policy(GET CMP0075 _CIF_CMP0075
  109. PARENT_SCOPE # undocumented, do not use outside of CMake
  110. )
  111. if("x${_CIF_CMP0075}x" STREQUAL "xNEWx")
  112. set(_CIF_LINK_LIBRARIES LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  113. elseif("x${_CIF_CMP0075}x" STREQUAL "xOLDx")
  114. elseif(NOT _CIF_CMP0075_WARNED)
  115. set(_CIF_CMP0075_WARNED 1)
  116. message(AUTHOR_WARNING
  117. "Policy CMP0075 is not set: Include file check macros honor CMAKE_REQUIRED_LIBRARIES. "
  118. "Run \"cmake --help-policy CMP0075\" for policy details. "
  119. "Use the cmake_policy command to set the policy and suppress this warning."
  120. "\n"
  121. "CMAKE_REQUIRED_LIBRARIES is set to:\n"
  122. " ${CMAKE_REQUIRED_LIBRARIES}\n"
  123. "For compatibility with CMake 3.11 and below this check is ignoring it."
  124. )
  125. endif()
  126. unset(_CIF_CMP0075)
  127. endif()
  128. if(CMAKE_REQUIRED_LINK_DIRECTORIES)
  129. set(_CIF_LINK_DIRECTORIES
  130. "-DLINK_DIRECTORIES:STRING=${CMAKE_REQUIRED_LINK_DIRECTORIES}")
  131. else()
  132. set(_CIF_LINK_DIRECTORIES)
  133. endif()
  134. if(NOT CMAKE_REQUIRED_QUIET)
  135. message(CHECK_START "Looking for ${_description}")
  136. endif()
  137. try_compile(${VARIABLE}
  138. SOURCE_FROM_VAR "${src}" _src_content
  139. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  140. ${_CIF_LINK_OPTIONS}
  141. ${_CIF_LINK_LIBRARIES}
  142. CMAKE_FLAGS
  143. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILES_FLAGS}
  144. "${CHECK_INCLUDE_FILES_INCLUDE_DIRS}"
  145. "${_CIF_LINK_DIRECTORIES}"
  146. )
  147. unset(_CIF_LINK_OPTIONS)
  148. unset(_CIF_LINK_LIBRARIES)
  149. unset(_CIF_LINK_DIRECTORIES)
  150. if(${VARIABLE})
  151. if(NOT CMAKE_REQUIRED_QUIET)
  152. message(CHECK_PASS "found")
  153. endif()
  154. set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
  155. else()
  156. if(NOT CMAKE_REQUIRED_QUIET)
  157. message(CHECK_FAIL "not found")
  158. endif()
  159. set(${VARIABLE} "" CACHE INTERNAL "Have includes ${INCLUDE}")
  160. endif()
  161. endif()
  162. endmacro()