CheckIncludeFile.cmake 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. CheckIncludeFile
  5. ----------------
  6. Provides a macro to check if a header file can be included in ``C``.
  7. .. command:: CHECK_INCLUDE_FILE
  8. .. code-block:: cmake
  9. CHECK_INCLUDE_FILE(<include> <variable> [<flags>])
  10. Check if the given ``<include>`` file may be included in a ``C``
  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. a :ref:`;-list <CMake Language Lists>` of macros to define (-DFOO=bar).
  20. ``CMAKE_REQUIRED_INCLUDES``
  21. a :ref:`;-list <CMake Language Lists>` of header search paths to pass to
  22. the compiler.
  23. ``CMAKE_REQUIRED_LINK_OPTIONS``
  24. a :ref:`;-list <CMake Language Lists>` of options to add to the link command.
  25. ``CMAKE_REQUIRED_LIBRARIES``
  26. a :ref:`;-list <CMake Language Lists>` of libraries to add to the link
  27. command. See policy :policy:`CMP0075`.
  28. ``CMAKE_REQUIRED_QUIET``
  29. execute quietly without messages.
  30. See the :module:`CheckIncludeFiles` module to check for multiple headers
  31. at once. See the :module:`CheckIncludeFileCXX` module to check for headers
  32. using the ``CXX`` language.
  33. #]=======================================================================]
  34. include_guard(GLOBAL)
  35. macro(CHECK_INCLUDE_FILE INCLUDE VARIABLE)
  36. if(NOT DEFINED "${VARIABLE}")
  37. if(CMAKE_REQUIRED_INCLUDES)
  38. set(CHECK_INCLUDE_FILE_C_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
  39. else()
  40. set(CHECK_INCLUDE_FILE_C_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.c.in
  45. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFile.c)
  46. if(NOT CMAKE_REQUIRED_QUIET)
  47. message(STATUS "Looking for ${INCLUDE}")
  48. endif()
  49. if(${ARGC} EQUAL 3)
  50. set(CMAKE_C_FLAGS_SAVE ${CMAKE_C_FLAGS})
  51. string(APPEND CMAKE_C_FLAGS " ${ARGV2}")
  52. endif()
  53. set(_CIF_LINK_OPTIONS)
  54. if(CMAKE_REQUIRED_LINK_OPTIONS)
  55. set(_CIF_LINK_OPTIONS LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  56. endif()
  57. set(_CIF_LINK_LIBRARIES "")
  58. if(CMAKE_REQUIRED_LIBRARIES)
  59. cmake_policy(GET CMP0075 _CIF_CMP0075
  60. PARENT_SCOPE # undocumented, do not use outside of CMake
  61. )
  62. if("x${_CIF_CMP0075}x" STREQUAL "xNEWx")
  63. set(_CIF_LINK_LIBRARIES LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  64. elseif("x${_CIF_CMP0075}x" STREQUAL "xOLDx")
  65. elseif(NOT _CIF_CMP0075_WARNED)
  66. set(_CIF_CMP0075_WARNED 1)
  67. message(AUTHOR_WARNING
  68. "Policy CMP0075 is not set: Include file check macros honor CMAKE_REQUIRED_LIBRARIES. "
  69. "Run \"cmake --help-policy CMP0075\" for policy details. "
  70. "Use the cmake_policy command to set the policy and suppress this warning."
  71. "\n"
  72. "CMAKE_REQUIRED_LIBRARIES is set to:\n"
  73. " ${CMAKE_REQUIRED_LIBRARIES}\n"
  74. "For compatibility with CMake 3.11 and below this check is ignoring it."
  75. )
  76. endif()
  77. unset(_CIF_CMP0075)
  78. endif()
  79. try_compile(${VARIABLE}
  80. ${CMAKE_BINARY_DIR}
  81. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFile.c
  82. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  83. ${_CIF_LINK_OPTIONS}
  84. ${_CIF_LINK_LIBRARIES}
  85. CMAKE_FLAGS
  86. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILE_FLAGS}
  87. "${CHECK_INCLUDE_FILE_C_INCLUDE_DIRS}"
  88. OUTPUT_VARIABLE OUTPUT)
  89. unset(_CIF_LINK_OPTIONS)
  90. unset(_CIF_LINK_LIBRARIES)
  91. if(${ARGC} EQUAL 3)
  92. set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_SAVE})
  93. endif()
  94. if(${VARIABLE})
  95. if(NOT CMAKE_REQUIRED_QUIET)
  96. message(STATUS "Looking for ${INCLUDE} - found")
  97. endif()
  98. set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
  99. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  100. "Determining if the include file ${INCLUDE} "
  101. "exists passed with the following output:\n"
  102. "${OUTPUT}\n\n")
  103. else()
  104. if(NOT CMAKE_REQUIRED_QUIET)
  105. message(STATUS "Looking for ${INCLUDE} - not found")
  106. endif()
  107. set(${VARIABLE} "" CACHE INTERNAL "Have include ${INCLUDE}")
  108. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  109. "Determining if the include file ${INCLUDE} "
  110. "exists failed with the following output:\n"
  111. "${OUTPUT}\n\n")
  112. endif()
  113. endif()
  114. endmacro()