CheckIncludeFileCXX.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. .. include:: /module/CMAKE_REQUIRED_FLAGS.txt
  17. .. include:: /module/CMAKE_REQUIRED_DEFINITIONS.txt
  18. .. include:: /module/CMAKE_REQUIRED_INCLUDES.txt
  19. .. include:: /module/CMAKE_REQUIRED_LINK_OPTIONS.txt
  20. .. include:: /module/CMAKE_REQUIRED_LIBRARIES.txt
  21. .. include:: /module/CMAKE_REQUIRED_LINK_DIRECTORIES.txt
  22. .. include:: /module/CMAKE_REQUIRED_QUIET.txt
  23. See modules :module:`CheckIncludeFile` and :module:`CheckIncludeFiles`
  24. to check for one or more ``C`` headers.
  25. #]=======================================================================]
  26. include_guard(GLOBAL)
  27. macro(CHECK_INCLUDE_FILE_CXX INCLUDE VARIABLE)
  28. if(NOT DEFINED "${VARIABLE}" OR "x${${VARIABLE}}" STREQUAL "x${VARIABLE}")
  29. if(CMAKE_REQUIRED_INCLUDES)
  30. set(CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
  31. else()
  32. set(CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS)
  33. endif()
  34. set(MACRO_CHECK_INCLUDE_FILE_FLAGS ${CMAKE_REQUIRED_FLAGS})
  35. set(CHECK_INCLUDE_FILE_VAR ${INCLUDE})
  36. file(READ ${CMAKE_ROOT}/Modules/CheckIncludeFile.cxx.in _CIF_SOURCE_CONTENT)
  37. string(CONFIGURE "${_CIF_SOURCE_CONTENT}" _CIF_SOURCE_CONTENT)
  38. if(NOT CMAKE_REQUIRED_QUIET)
  39. message(CHECK_START "Looking for C++ include ${INCLUDE}")
  40. endif()
  41. if(${ARGC} EQUAL 3)
  42. set(CMAKE_CXX_FLAGS_SAVE ${CMAKE_CXX_FLAGS})
  43. string(APPEND CMAKE_CXX_FLAGS " ${ARGV2}")
  44. endif()
  45. set(_CIF_LINK_OPTIONS)
  46. if(CMAKE_REQUIRED_LINK_OPTIONS)
  47. set(_CIF_LINK_OPTIONS LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  48. endif()
  49. set(_CIF_LINK_LIBRARIES "")
  50. if(CMAKE_REQUIRED_LIBRARIES)
  51. cmake_policy(GET CMP0075 _CIF_CMP0075
  52. PARENT_SCOPE # undocumented, do not use outside of CMake
  53. )
  54. if("x${_CIF_CMP0075}x" STREQUAL "xNEWx")
  55. set(_CIF_LINK_LIBRARIES LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  56. elseif("x${_CIF_CMP0075}x" STREQUAL "xOLDx")
  57. elseif(NOT _CIF_CMP0075_WARNED)
  58. set(_CIF_CMP0075_WARNED 1)
  59. message(AUTHOR_WARNING
  60. "Policy CMP0075 is not set: Include file check macros honor CMAKE_REQUIRED_LIBRARIES. "
  61. "Run \"cmake --help-policy CMP0075\" for policy details. "
  62. "Use the cmake_policy command to set the policy and suppress this warning."
  63. "\n"
  64. "CMAKE_REQUIRED_LIBRARIES is set to:\n"
  65. " ${CMAKE_REQUIRED_LIBRARIES}\n"
  66. "For compatibility with CMake 3.11 and below this check is ignoring it."
  67. )
  68. endif()
  69. unset(_CIF_CMP0075)
  70. endif()
  71. if(CMAKE_REQUIRED_LINK_DIRECTORIES)
  72. set(_CIF_LINK_DIRECTORIES
  73. "-DLINK_DIRECTORIES:STRING=${CMAKE_REQUIRED_LINK_DIRECTORIES}")
  74. else()
  75. set(_CIF_LINK_DIRECTORIES)
  76. endif()
  77. try_compile(${VARIABLE}
  78. SOURCE_FROM_VAR CheckIncludeFile.cxx _CIF_SOURCE_CONTENT
  79. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  80. ${_CIF_LINK_OPTIONS}
  81. ${_CIF_LINK_LIBRARIES}
  82. CMAKE_FLAGS
  83. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILE_FLAGS}
  84. "${CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS}"
  85. "${_CIF_LINK_DIRECTORIES}"
  86. )
  87. unset(_CIF_LINK_OPTIONS)
  88. unset(_CIF_LINK_LIBRARIES)
  89. unset(_CIF_LINK_DIRECTORIES)
  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(CHECK_PASS "found")
  96. endif()
  97. set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
  98. else()
  99. if(NOT CMAKE_REQUIRED_QUIET)
  100. message(CHECK_FAIL "not found")
  101. endif()
  102. set(${VARIABLE} "" CACHE INTERNAL "Have include ${INCLUDE}")
  103. endif()
  104. endif()
  105. endmacro()