CheckIncludeFileCXX.cmake 4.9 KB

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