CheckIncludeFile.cmake 4.8 KB

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