CheckIncludeFileCXX.cmake 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. CheckIncludeFileCXX
  5. -------------------
  6. This module provides a command to check a C++ header file.
  7. Load this module in a CMake project with:
  8. .. code-block:: cmake
  9. include(CheckIncludeFileCXX)
  10. Commands
  11. ^^^^^^^^
  12. This module provides the following command:
  13. .. command:: check_include_file_cxx
  14. Checks once whether a header file exists and can be included in C++ code:
  15. .. code-block:: cmake
  16. check_include_file_cxx(<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. Checking whether the ``C++23`` header ``<stdfloat>`` exists and storing the
  44. check result in the ``HAVE_STDFLOAT_HEADER`` cache variable:
  45. .. code-block:: cmake
  46. include(CheckIncludeFileCXX)
  47. check_include_file_cxx(stdfloat HAVE_STDFLOAT_HEADER)
  48. See Also
  49. ^^^^^^^^
  50. * The :module:`CheckIncludeFile` module to check for single C header.
  51. * The :module:`CheckIncludeFiles` module to check for one or more C or
  52. C++ headers at once.
  53. #]=======================================================================]
  54. include_guard(GLOBAL)
  55. macro(CHECK_INCLUDE_FILE_CXX INCLUDE VARIABLE)
  56. if(NOT DEFINED "${VARIABLE}" OR "x${${VARIABLE}}" STREQUAL "x${VARIABLE}")
  57. if(CMAKE_REQUIRED_INCLUDES)
  58. set(CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
  59. else()
  60. set(CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS)
  61. endif()
  62. set(MACRO_CHECK_INCLUDE_FILE_FLAGS ${CMAKE_REQUIRED_FLAGS})
  63. set(CHECK_INCLUDE_FILE_VAR ${INCLUDE})
  64. file(READ ${CMAKE_ROOT}/Modules/CheckIncludeFile.cxx.in _CIF_SOURCE_CONTENT)
  65. string(CONFIGURE "${_CIF_SOURCE_CONTENT}" _CIF_SOURCE_CONTENT)
  66. if(NOT CMAKE_REQUIRED_QUIET)
  67. message(CHECK_START "Looking for C++ include ${INCLUDE}")
  68. endif()
  69. if(${ARGC} EQUAL 3)
  70. set(CMAKE_CXX_FLAGS_SAVE ${CMAKE_CXX_FLAGS})
  71. string(APPEND CMAKE_CXX_FLAGS " ${ARGV2}")
  72. endif()
  73. set(_CIF_LINK_OPTIONS)
  74. if(CMAKE_REQUIRED_LINK_OPTIONS)
  75. set(_CIF_LINK_OPTIONS LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  76. endif()
  77. set(_CIF_LINK_LIBRARIES "")
  78. if(CMAKE_REQUIRED_LIBRARIES)
  79. cmake_policy(GET CMP0075 _CIF_CMP0075
  80. PARENT_SCOPE # undocumented, do not use outside of CMake
  81. )
  82. if("x${_CIF_CMP0075}x" STREQUAL "xNEWx")
  83. set(_CIF_LINK_LIBRARIES LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  84. elseif("x${_CIF_CMP0075}x" STREQUAL "xOLDx")
  85. elseif(NOT _CIF_CMP0075_WARNED)
  86. set(_CIF_CMP0075_WARNED 1)
  87. message(AUTHOR_WARNING
  88. "Policy CMP0075 is not set: Include file check macros honor CMAKE_REQUIRED_LIBRARIES. "
  89. "Run \"cmake --help-policy CMP0075\" for policy details. "
  90. "Use the cmake_policy command to set the policy and suppress this warning."
  91. "\n"
  92. "CMAKE_REQUIRED_LIBRARIES is set to:\n"
  93. " ${CMAKE_REQUIRED_LIBRARIES}\n"
  94. "For compatibility with CMake 3.11 and below this check is ignoring it."
  95. )
  96. endif()
  97. unset(_CIF_CMP0075)
  98. endif()
  99. if(CMAKE_REQUIRED_LINK_DIRECTORIES)
  100. set(_CIF_LINK_DIRECTORIES
  101. "-DLINK_DIRECTORIES:STRING=${CMAKE_REQUIRED_LINK_DIRECTORIES}")
  102. else()
  103. set(_CIF_LINK_DIRECTORIES)
  104. endif()
  105. try_compile(${VARIABLE}
  106. SOURCE_FROM_VAR CheckIncludeFile.cxx _CIF_SOURCE_CONTENT
  107. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  108. ${_CIF_LINK_OPTIONS}
  109. ${_CIF_LINK_LIBRARIES}
  110. CMAKE_FLAGS
  111. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILE_FLAGS}
  112. "${CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS}"
  113. "${_CIF_LINK_DIRECTORIES}"
  114. )
  115. unset(_CIF_LINK_OPTIONS)
  116. unset(_CIF_LINK_LIBRARIES)
  117. unset(_CIF_LINK_DIRECTORIES)
  118. if(${ARGC} EQUAL 3)
  119. set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_SAVE})
  120. endif()
  121. if(${VARIABLE})
  122. if(NOT CMAKE_REQUIRED_QUIET)
  123. message(CHECK_PASS "found")
  124. endif()
  125. set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
  126. else()
  127. if(NOT CMAKE_REQUIRED_QUIET)
  128. message(CHECK_FAIL "not found")
  129. endif()
  130. set(${VARIABLE} "" CACHE INTERNAL "Have include ${INCLUDE}")
  131. endif()
  132. endif()
  133. endmacro()