CheckIncludeFileCXX.cmake 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #
  7. # Provides a macro to check if a header file can be included in ``CXX``.
  8. #
  9. # .. command:: CHECK_INCLUDE_FILE_CXX
  10. #
  11. # ::
  12. #
  13. # CHECK_INCLUDE_FILE_CXX(<include> <variable> [<flags>])
  14. #
  15. # Check if the given ``<include>`` file may be included in a ``CXX``
  16. # source file and store the result in an internal cache entry named
  17. # ``<variable>``. The optional third argument may be used to add
  18. # compilation flags to the check (or use ``CMAKE_REQUIRED_FLAGS`` below).
  19. #
  20. # The following variables may be set before calling this macro to modify
  21. # the way the check is run:
  22. #
  23. # ``CMAKE_REQUIRED_FLAGS``
  24. # string of compile command line flags
  25. # ``CMAKE_REQUIRED_DEFINITIONS``
  26. # list of macros to define (-DFOO=bar)
  27. # ``CMAKE_REQUIRED_INCLUDES``
  28. # list of include directories
  29. # ``CMAKE_REQUIRED_LIBRARIES``
  30. # A list of libraries to link. See policy :policy:`CMP0075`.
  31. # ``CMAKE_REQUIRED_QUIET``
  32. # execute quietly without messages
  33. #
  34. # See modules :module:`CheckIncludeFile` and :module:`CheckIncludeFiles`
  35. # to check for one or more ``C`` headers.
  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. configure_file(${CMAKE_ROOT}/Modules/CheckIncludeFile.cxx.in
  47. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFile.cxx)
  48. if(NOT CMAKE_REQUIRED_QUIET)
  49. message(STATUS "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_LIBRARIES "")
  56. if(CMAKE_REQUIRED_LIBRARIES)
  57. cmake_policy(GET CMP0075 _CIF_CMP0075
  58. PARENT_SCOPE # undocumented, do not use outside of CMake
  59. )
  60. if("x${_CIF_CMP0075}x" STREQUAL "xNEWx")
  61. set(_CIF_LINK_LIBRARIES LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  62. elseif("x${_CIF_CMP0075}x" STREQUAL "xOLDx")
  63. elseif(NOT _CIF_CMP0075_WARNED)
  64. set(_CIF_CMP0075_WARNED 1)
  65. message(AUTHOR_WARNING
  66. "Policy CMP0075 is not set: Include file check macros honor CMAKE_REQUIRED_LIBRARIES. "
  67. "Run \"cmake --help-policy CMP0075\" for policy details. "
  68. "Use the cmake_policy command to set the policy and suppress this warning."
  69. "\n"
  70. "CMAKE_REQUIRED_LIBRARIES is set to:\n"
  71. " ${CMAKE_REQUIRED_LIBRARIES}\n"
  72. "For compatibility with CMake 3.11 and below this check is ignoring it."
  73. )
  74. endif()
  75. unset(_CIF_CMP0075)
  76. endif()
  77. try_compile(${VARIABLE}
  78. ${CMAKE_BINARY_DIR}
  79. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFile.cxx
  80. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  81. ${_CIF_LINK_LIBRARIES}
  82. CMAKE_FLAGS
  83. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILE_FLAGS}
  84. "${CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS}"
  85. OUTPUT_VARIABLE OUTPUT)
  86. unset(_CIF_LINK_LIBRARIES)
  87. if(${ARGC} EQUAL 3)
  88. set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_SAVE})
  89. endif()
  90. if(${VARIABLE})
  91. if(NOT CMAKE_REQUIRED_QUIET)
  92. message(STATUS "Looking for C++ include ${INCLUDE} - found")
  93. endif()
  94. set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
  95. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  96. "Determining if the include file ${INCLUDE} "
  97. "exists passed with the following output:\n"
  98. "${OUTPUT}\n\n")
  99. else()
  100. if(NOT CMAKE_REQUIRED_QUIET)
  101. message(STATUS "Looking for C++ include ${INCLUDE} - not found")
  102. endif()
  103. set(${VARIABLE} "" CACHE INTERNAL "Have include ${INCLUDE}")
  104. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  105. "Determining if the include file ${INCLUDE} "
  106. "exists failed with the following output:\n"
  107. "${OUTPUT}\n\n")
  108. endif()
  109. endif()
  110. endmacro()