CheckIncludeFileCXX.cmake 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. # list of libraries to link
  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. try_compile(${VARIABLE}
  56. ${CMAKE_BINARY_DIR}
  57. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFile.cxx
  58. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  59. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}
  60. CMAKE_FLAGS
  61. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILE_FLAGS}
  62. "${CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS}"
  63. OUTPUT_VARIABLE OUTPUT)
  64. if(${ARGC} EQUAL 3)
  65. set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_SAVE})
  66. endif()
  67. if(${VARIABLE})
  68. if(NOT CMAKE_REQUIRED_QUIET)
  69. message(STATUS "Looking for C++ include ${INCLUDE} - found")
  70. endif()
  71. set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
  72. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  73. "Determining if the include file ${INCLUDE} "
  74. "exists passed with the following output:\n"
  75. "${OUTPUT}\n\n")
  76. else()
  77. if(NOT CMAKE_REQUIRED_QUIET)
  78. message(STATUS "Looking for C++ include ${INCLUDE} - not found")
  79. endif()
  80. set(${VARIABLE} "" CACHE INTERNAL "Have include ${INCLUDE}")
  81. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  82. "Determining if the include file ${INCLUDE} "
  83. "exists failed with the following output:\n"
  84. "${OUTPUT}\n\n")
  85. endif()
  86. endif()
  87. endmacro()