CheckIncludeFile.cmake 3.3 KB

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