CheckIncludeFile.cmake 4.3 KB

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