CheckIncludeFiles.cmake 7.2 KB

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