CheckIncludeFile.cmake 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. CheckIncludeFile
  5. ----------------
  6. This module provides a command to check C header file.
  7. Load this module in a CMake project with:
  8. .. code-block:: cmake
  9. include(CheckIncludeFile)
  10. Commands
  11. ^^^^^^^^
  12. This module provides the following command:
  13. .. command:: check_include_file
  14. Checks once whether a header file exists and can be included in C code:
  15. .. code-block:: cmake
  16. check_include_file(<include> <variable> [<flags>])
  17. .. rubric:: The arguments are:
  18. ``<include>``
  19. A header file to be checked.
  20. ``<variable>``
  21. The name of the variable to store the result of the check. This
  22. variable will be created as an internal cache variable.
  23. ``<flags>``
  24. (Optional) A :ref:`semicolon-separated list <CMake Language Lists>` of
  25. additional compilation flags to be added to the check. Alternatively,
  26. flags can be also specified with the ``CMAKE_REQUIRED_FLAGS`` variable
  27. below.
  28. .. rubric:: Variables Affecting the Check
  29. The following variables may be set before calling this command to modify
  30. the way the check is run:
  31. .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst
  32. .. include:: /module/include/CMAKE_REQUIRED_DEFINITIONS.rst
  33. .. include:: /module/include/CMAKE_REQUIRED_INCLUDES.rst
  34. .. include:: /module/include/CMAKE_REQUIRED_LINK_OPTIONS.rst
  35. .. include:: /module/include/CMAKE_REQUIRED_LIBRARIES.rst
  36. .. include:: /module/include/CMAKE_REQUIRED_LINK_DIRECTORIES.rst
  37. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst
  38. .. versionadded:: 3.12
  39. The ``CMAKE_REQUIRED_LIBRARIES`` variable, if policy :policy:`CMP0075` is
  40. set to ``NEW``.
  41. Examples
  42. ^^^^^^^^
  43. Example: Checking C Header
  44. """"""""""""""""""""""""""
  45. Checking whether the C header ``<unistd.h>`` exists and storing the check
  46. result in the ``HAVE_UNISTD_H`` cache variable:
  47. .. code-block:: cmake
  48. include(CheckIncludeFile)
  49. check_include_file(unistd.h HAVE_UNISTD_H)
  50. Example: Isolated Check
  51. """""""""""""""""""""""
  52. In the following example, this module is used in combination with the
  53. :module:`CMakePushCheckState` module to temporarily modify the required
  54. compile definitions (via ``CMAKE_REQUIRED_DEFINITIONS``) and verify whether
  55. the C header ``<ucontext.h>`` is available. The result is stored
  56. in the internal cache variable ``HAVE_UCONTEXT_H``.
  57. For example, on macOS, the ``ucontext`` API is deprecated, and headers may
  58. be hidden unless certain feature macros are defined. In particular,
  59. defining ``_XOPEN_SOURCE`` (without a value) can expose the necessary
  60. symbols without enabling broader POSIX or SUS (Single Unix Specification)
  61. features (values 500 or greater).
  62. .. code-block:: cmake
  63. include(CheckIncludeFile)
  64. include(CMakePushCheckState)
  65. cmake_push_check_state(RESET)
  66. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  67. set(CMAKE_REQUIRED_DEFINITIONS -D_XOPEN_SOURCE)
  68. endif()
  69. check_include_file(ucontext.h HAVE_UCONTEXT_H)
  70. cmake_pop_check_state()
  71. See Also
  72. ^^^^^^^^
  73. * The :module:`CheckIncludeFileCXX` module to check for single C++ header.
  74. * The :module:`CheckIncludeFiles` module to check for one or more C or
  75. C++ headers at once.
  76. #]=======================================================================]
  77. include_guard(GLOBAL)
  78. macro(CHECK_INCLUDE_FILE INCLUDE VARIABLE)
  79. if(NOT DEFINED "${VARIABLE}")
  80. if(CMAKE_REQUIRED_INCLUDES)
  81. set(CHECK_INCLUDE_FILE_C_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
  82. else()
  83. set(CHECK_INCLUDE_FILE_C_INCLUDE_DIRS)
  84. endif()
  85. set(MACRO_CHECK_INCLUDE_FILE_FLAGS ${CMAKE_REQUIRED_FLAGS})
  86. set(CHECK_INCLUDE_FILE_VAR ${INCLUDE})
  87. file(READ ${CMAKE_ROOT}/Modules/CheckIncludeFile.c.in _CIF_SOURCE_CONTENT)
  88. string(CONFIGURE "${_CIF_SOURCE_CONTENT}" _CIF_SOURCE_CONTENT)
  89. if(NOT CMAKE_REQUIRED_QUIET)
  90. message(CHECK_START "Looking for ${INCLUDE}")
  91. endif()
  92. if(${ARGC} EQUAL 3)
  93. set(CMAKE_C_FLAGS_SAVE ${CMAKE_C_FLAGS})
  94. string(APPEND CMAKE_C_FLAGS " ${ARGV2}")
  95. endif()
  96. set(_CIF_LINK_OPTIONS)
  97. if(CMAKE_REQUIRED_LINK_OPTIONS)
  98. set(_CIF_LINK_OPTIONS LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  99. endif()
  100. set(_CIF_LINK_LIBRARIES "")
  101. if(CMAKE_REQUIRED_LIBRARIES)
  102. cmake_policy(GET CMP0075 _CIF_CMP0075
  103. PARENT_SCOPE # undocumented, do not use outside of CMake
  104. )
  105. if("x${_CIF_CMP0075}x" STREQUAL "xNEWx")
  106. set(_CIF_LINK_LIBRARIES LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  107. elseif("x${_CIF_CMP0075}x" STREQUAL "xOLDx")
  108. elseif(NOT _CIF_CMP0075_WARNED)
  109. set(_CIF_CMP0075_WARNED 1)
  110. message(AUTHOR_WARNING
  111. "Policy CMP0075 is not set: Include file check macros honor CMAKE_REQUIRED_LIBRARIES. "
  112. "Run \"cmake --help-policy CMP0075\" for policy details. "
  113. "Use the cmake_policy command to set the policy and suppress this warning."
  114. "\n"
  115. "CMAKE_REQUIRED_LIBRARIES is set to:\n"
  116. " ${CMAKE_REQUIRED_LIBRARIES}\n"
  117. "For compatibility with CMake 3.11 and below this check is ignoring it."
  118. )
  119. endif()
  120. unset(_CIF_CMP0075)
  121. endif()
  122. if(CMAKE_REQUIRED_LINK_DIRECTORIES)
  123. set(_CIF_LINK_DIRECTORIES
  124. "-DLINK_DIRECTORIES:STRING=${CMAKE_REQUIRED_LINK_DIRECTORIES}")
  125. else()
  126. set(_CIF_LINK_DIRECTORIES)
  127. endif()
  128. try_compile(${VARIABLE}
  129. SOURCE_FROM_VAR CheckIncludeFile.c _CIF_SOURCE_CONTENT
  130. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  131. ${_CIF_LINK_OPTIONS}
  132. ${_CIF_LINK_LIBRARIES}
  133. CMAKE_FLAGS
  134. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILE_FLAGS}
  135. "${CHECK_INCLUDE_FILE_C_INCLUDE_DIRS}"
  136. "${_CIF_LINK_DIRECTORIES}"
  137. )
  138. unset(_CIF_LINK_OPTIONS)
  139. unset(_CIF_LINK_LIBRARIES)
  140. unset(_CIF_LINK_DIRECTORIES)
  141. if(${ARGC} EQUAL 3)
  142. set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_SAVE})
  143. endif()
  144. if(${VARIABLE})
  145. if(NOT CMAKE_REQUIRED_QUIET)
  146. message(CHECK_PASS "found")
  147. endif()
  148. set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
  149. else()
  150. if(NOT CMAKE_REQUIRED_QUIET)
  151. message(CHECK_FAIL "not found")
  152. endif()
  153. set(${VARIABLE} "" CACHE INTERNAL "Have include ${INCLUDE}")
  154. endif()
  155. endif()
  156. endmacro()