CheckIncludeFileCXX.cmake 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. Provides a macro to check if a header file can be included in ``CXX``.
  7. .. command:: check_include_file_cxx
  8. .. code-block:: cmake
  9. check_include_file_cxx(<include> <variable> [<flags>])
  10. Check if the given ``<include>`` file may be included in a ``CXX``
  11. source file and store the result in an internal cache entry named
  12. ``<variable>``. The optional third argument may be used to add
  13. compilation flags to the check (or use ``CMAKE_REQUIRED_FLAGS`` below).
  14. The following variables may be set before calling this macro to modify
  15. the way the check is run:
  16. .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst
  17. .. include:: /module/include/CMAKE_REQUIRED_DEFINITIONS.rst
  18. .. include:: /module/include/CMAKE_REQUIRED_INCLUDES.rst
  19. .. include:: /module/include/CMAKE_REQUIRED_LINK_OPTIONS.rst
  20. .. include:: /module/include/CMAKE_REQUIRED_LIBRARIES.rst
  21. .. include:: /module/include/CMAKE_REQUIRED_LINK_DIRECTORIES.rst
  22. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst
  23. Examples
  24. ^^^^^^^^
  25. Checking whether the ``C++23`` header ``<stdfloat>`` exists and storing the
  26. check result in the ``HAVE_STDFLOAT_HEADER`` cache variable:
  27. .. code-block:: cmake
  28. include(CheckIncludeFileCXX)
  29. check_include_file_cxx(stdfloat HAVE_STDFLOAT_HEADER)
  30. See Also
  31. ^^^^^^^^
  32. * The :module:`CheckIncludeFile` module to check for single ``C`` header.
  33. * The :module:`CheckIncludeFiles` module to check for one or more ``C`` or
  34. ``C++`` headers at once.
  35. #]=======================================================================]
  36. include_guard(GLOBAL)
  37. macro(CHECK_INCLUDE_FILE_CXX INCLUDE VARIABLE)
  38. if(NOT DEFINED "${VARIABLE}" OR "x${${VARIABLE}}" STREQUAL "x${VARIABLE}")
  39. if(CMAKE_REQUIRED_INCLUDES)
  40. set(CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
  41. else()
  42. set(CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS)
  43. endif()
  44. set(MACRO_CHECK_INCLUDE_FILE_FLAGS ${CMAKE_REQUIRED_FLAGS})
  45. set(CHECK_INCLUDE_FILE_VAR ${INCLUDE})
  46. file(READ ${CMAKE_ROOT}/Modules/CheckIncludeFile.cxx.in _CIF_SOURCE_CONTENT)
  47. string(CONFIGURE "${_CIF_SOURCE_CONTENT}" _CIF_SOURCE_CONTENT)
  48. if(NOT CMAKE_REQUIRED_QUIET)
  49. message(CHECK_START "Looking for C++ include ${INCLUDE}")
  50. endif()
  51. if(${ARGC} EQUAL 3)
  52. set(CMAKE_CXX_FLAGS_SAVE ${CMAKE_CXX_FLAGS})
  53. string(APPEND CMAKE_CXX_FLAGS " ${ARGV2}")
  54. endif()
  55. set(_CIF_LINK_OPTIONS)
  56. if(CMAKE_REQUIRED_LINK_OPTIONS)
  57. set(_CIF_LINK_OPTIONS LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  58. endif()
  59. set(_CIF_LINK_LIBRARIES "")
  60. if(CMAKE_REQUIRED_LIBRARIES)
  61. cmake_policy(GET CMP0075 _CIF_CMP0075
  62. PARENT_SCOPE # undocumented, do not use outside of CMake
  63. )
  64. if("x${_CIF_CMP0075}x" STREQUAL "xNEWx")
  65. set(_CIF_LINK_LIBRARIES LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  66. elseif("x${_CIF_CMP0075}x" STREQUAL "xOLDx")
  67. elseif(NOT _CIF_CMP0075_WARNED)
  68. set(_CIF_CMP0075_WARNED 1)
  69. message(AUTHOR_WARNING
  70. "Policy CMP0075 is not set: Include file check macros honor CMAKE_REQUIRED_LIBRARIES. "
  71. "Run \"cmake --help-policy CMP0075\" for policy details. "
  72. "Use the cmake_policy command to set the policy and suppress this warning."
  73. "\n"
  74. "CMAKE_REQUIRED_LIBRARIES is set to:\n"
  75. " ${CMAKE_REQUIRED_LIBRARIES}\n"
  76. "For compatibility with CMake 3.11 and below this check is ignoring it."
  77. )
  78. endif()
  79. unset(_CIF_CMP0075)
  80. endif()
  81. if(CMAKE_REQUIRED_LINK_DIRECTORIES)
  82. set(_CIF_LINK_DIRECTORIES
  83. "-DLINK_DIRECTORIES:STRING=${CMAKE_REQUIRED_LINK_DIRECTORIES}")
  84. else()
  85. set(_CIF_LINK_DIRECTORIES)
  86. endif()
  87. try_compile(${VARIABLE}
  88. SOURCE_FROM_VAR CheckIncludeFile.cxx _CIF_SOURCE_CONTENT
  89. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  90. ${_CIF_LINK_OPTIONS}
  91. ${_CIF_LINK_LIBRARIES}
  92. CMAKE_FLAGS
  93. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILE_FLAGS}
  94. "${CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS}"
  95. "${_CIF_LINK_DIRECTORIES}"
  96. )
  97. unset(_CIF_LINK_OPTIONS)
  98. unset(_CIF_LINK_LIBRARIES)
  99. unset(_CIF_LINK_DIRECTORIES)
  100. if(${ARGC} EQUAL 3)
  101. set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_SAVE})
  102. endif()
  103. if(${VARIABLE})
  104. if(NOT CMAKE_REQUIRED_QUIET)
  105. message(CHECK_PASS "found")
  106. endif()
  107. set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
  108. else()
  109. if(NOT CMAKE_REQUIRED_QUIET)
  110. message(CHECK_FAIL "not found")
  111. endif()
  112. set(${VARIABLE} "" CACHE INTERNAL "Have include ${INCLUDE}")
  113. endif()
  114. endif()
  115. endmacro()