CheckIncludeFileCXX.cmake 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_QUIET``
  30. # execute quietly without messages
  31. #
  32. # See modules :module:`CheckIncludeFile` and :module:`CheckIncludeFiles`
  33. # to check for one or more ``C`` headers.
  34. include_guard(GLOBAL)
  35. macro(CHECK_INCLUDE_FILE_CXX INCLUDE VARIABLE)
  36. if(NOT DEFINED "${VARIABLE}" OR "x${${VARIABLE}}" STREQUAL "x${VARIABLE}")
  37. if(CMAKE_REQUIRED_INCLUDES)
  38. set(CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
  39. else()
  40. set(CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS)
  41. endif()
  42. set(MACRO_CHECK_INCLUDE_FILE_FLAGS ${CMAKE_REQUIRED_FLAGS})
  43. set(CHECK_INCLUDE_FILE_VAR ${INCLUDE})
  44. configure_file(${CMAKE_ROOT}/Modules/CheckIncludeFile.cxx.in
  45. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFile.cxx)
  46. if(NOT CMAKE_REQUIRED_QUIET)
  47. message(STATUS "Looking for C++ include ${INCLUDE}")
  48. endif()
  49. if(${ARGC} EQUAL 3)
  50. set(CMAKE_CXX_FLAGS_SAVE ${CMAKE_CXX_FLAGS})
  51. string(APPEND CMAKE_CXX_FLAGS " ${ARGV2}")
  52. endif()
  53. try_compile(${VARIABLE}
  54. ${CMAKE_BINARY_DIR}
  55. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFile.cxx
  56. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  57. CMAKE_FLAGS
  58. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILE_FLAGS}
  59. "${CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS}"
  60. OUTPUT_VARIABLE OUTPUT)
  61. if(${ARGC} EQUAL 3)
  62. set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_SAVE})
  63. endif()
  64. if(${VARIABLE})
  65. if(NOT CMAKE_REQUIRED_QUIET)
  66. message(STATUS "Looking for C++ include ${INCLUDE} - found")
  67. endif()
  68. set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
  69. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  70. "Determining if the include file ${INCLUDE} "
  71. "exists passed with the following output:\n"
  72. "${OUTPUT}\n\n")
  73. else()
  74. if(NOT CMAKE_REQUIRED_QUIET)
  75. message(STATUS "Looking for C++ include ${INCLUDE} - not found")
  76. endif()
  77. set(${VARIABLE} "" CACHE INTERNAL "Have include ${INCLUDE}")
  78. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  79. "Determining if the include file ${INCLUDE} "
  80. "exists failed with the following output:\n"
  81. "${OUTPUT}\n\n")
  82. endif()
  83. endif()
  84. endmacro()