CheckIncludeFileCXX.cmake 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt 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. ``CMAKE_REQUIRED_FLAGS``
  17. string of compile command line flags.
  18. ``CMAKE_REQUIRED_DEFINITIONS``
  19. a :ref:`;-list <CMake Language Lists>` of macros to define (-DFOO=bar).
  20. ``CMAKE_REQUIRED_INCLUDES``
  21. a :ref:`;-list <CMake Language Lists>` of header search paths to pass to
  22. the compiler.
  23. ``CMAKE_REQUIRED_LINK_OPTIONS``
  24. .. versionadded:: 3.14
  25. a :ref:`;-list <CMake Language Lists>` of options to add to the link command.
  26. ``CMAKE_REQUIRED_LIBRARIES``
  27. a :ref:`;-list <CMake Language Lists>` of libraries to add to the link
  28. command. See policy :policy:`CMP0075`.
  29. ``CMAKE_REQUIRED_QUIET``
  30. .. versionadded:: 3.1
  31. execute quietly without messages.
  32. See modules :module:`CheckIncludeFile` and :module:`CheckIncludeFiles`
  33. to check for one or more ``C`` headers.
  34. #]=======================================================================]
  35. include_guard(GLOBAL)
  36. macro(CHECK_INCLUDE_FILE_CXX INCLUDE VARIABLE)
  37. if(NOT DEFINED "${VARIABLE}" OR "x${${VARIABLE}}" STREQUAL "x${VARIABLE}")
  38. if(CMAKE_REQUIRED_INCLUDES)
  39. set(CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
  40. else()
  41. set(CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS)
  42. endif()
  43. set(MACRO_CHECK_INCLUDE_FILE_FLAGS ${CMAKE_REQUIRED_FLAGS})
  44. set(CHECK_INCLUDE_FILE_VAR ${INCLUDE})
  45. configure_file(${CMAKE_ROOT}/Modules/CheckIncludeFile.cxx.in
  46. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFile.cxx)
  47. if(NOT CMAKE_REQUIRED_QUIET)
  48. message(CHECK_START "Looking for C++ include ${INCLUDE}")
  49. endif()
  50. if(${ARGC} EQUAL 3)
  51. set(CMAKE_CXX_FLAGS_SAVE ${CMAKE_CXX_FLAGS})
  52. string(APPEND CMAKE_CXX_FLAGS " ${ARGV2}")
  53. endif()
  54. set(_CIF_LINK_OPTIONS)
  55. if(CMAKE_REQUIRED_LINK_OPTIONS)
  56. set(_CIF_LINK_OPTIONS LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  57. endif()
  58. set(_CIF_LINK_LIBRARIES "")
  59. if(CMAKE_REQUIRED_LIBRARIES)
  60. cmake_policy(GET CMP0075 _CIF_CMP0075
  61. PARENT_SCOPE # undocumented, do not use outside of CMake
  62. )
  63. if("x${_CIF_CMP0075}x" STREQUAL "xNEWx")
  64. set(_CIF_LINK_LIBRARIES LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  65. elseif("x${_CIF_CMP0075}x" STREQUAL "xOLDx")
  66. elseif(NOT _CIF_CMP0075_WARNED)
  67. set(_CIF_CMP0075_WARNED 1)
  68. message(AUTHOR_WARNING
  69. "Policy CMP0075 is not set: Include file check macros honor CMAKE_REQUIRED_LIBRARIES. "
  70. "Run \"cmake --help-policy CMP0075\" for policy details. "
  71. "Use the cmake_policy command to set the policy and suppress this warning."
  72. "\n"
  73. "CMAKE_REQUIRED_LIBRARIES is set to:\n"
  74. " ${CMAKE_REQUIRED_LIBRARIES}\n"
  75. "For compatibility with CMake 3.11 and below this check is ignoring it."
  76. )
  77. endif()
  78. unset(_CIF_CMP0075)
  79. endif()
  80. try_compile(${VARIABLE}
  81. SOURCES ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFile.cxx
  82. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  83. ${_CIF_LINK_OPTIONS}
  84. ${_CIF_LINK_LIBRARIES}
  85. CMAKE_FLAGS
  86. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILE_FLAGS}
  87. "${CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS}"
  88. OUTPUT_VARIABLE OUTPUT)
  89. unset(_CIF_LINK_OPTIONS)
  90. unset(_CIF_LINK_LIBRARIES)
  91. if(${ARGC} EQUAL 3)
  92. set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_SAVE})
  93. endif()
  94. if(${VARIABLE})
  95. if(NOT CMAKE_REQUIRED_QUIET)
  96. message(CHECK_PASS "found")
  97. endif()
  98. set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
  99. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  100. "Determining if the include file ${INCLUDE} "
  101. "exists passed with the following output:\n"
  102. "${OUTPUT}\n\n")
  103. else()
  104. if(NOT CMAKE_REQUIRED_QUIET)
  105. message(CHECK_FAIL "not found")
  106. endif()
  107. set(${VARIABLE} "" CACHE INTERNAL "Have include ${INCLUDE}")
  108. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  109. "Determining if the include file ${INCLUDE} "
  110. "exists failed with the following output:\n"
  111. "${OUTPUT}\n\n")
  112. endif()
  113. endif()
  114. endmacro()