CheckIncludeFileCXX.cmake 4.4 KB

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