CheckIncludeFiles.cmake 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. # CheckIncludeFiles
  5. # -----------------
  6. #
  7. # Provides a macro to check if a list of one or more header files can
  8. # be included together in ``C``.
  9. #
  10. # .. command:: CHECK_INCLUDE_FILES
  11. #
  12. # ::
  13. #
  14. # CHECK_INCLUDE_FILES("<includes>" <variable>)
  15. #
  16. # Check if the given ``<includes>`` list may be included together
  17. # in a ``C`` source file and store the result in an internal cache
  18. # entry named ``<variable>``. Specify the ``<includes>`` argument
  19. # as a :ref:`;-list <CMake Language Lists>` of header file names.
  20. #
  21. # The following variables may be set before calling this macro to modify
  22. # the way the check is run:
  23. #
  24. # ``CMAKE_REQUIRED_FLAGS``
  25. # string of compile command line flags
  26. # ``CMAKE_REQUIRED_DEFINITIONS``
  27. # list of macros to define (-DFOO=bar)
  28. # ``CMAKE_REQUIRED_INCLUDES``
  29. # list of include directories
  30. # ``CMAKE_REQUIRED_QUIET``
  31. # execute quietly without messages
  32. #
  33. # See modules :module:`CheckIncludeFile` and :module:`CheckIncludeFileCXX`
  34. # to check for a single header file in ``C`` or ``CXX`` languages.
  35. include_guard(GLOBAL)
  36. macro(CHECK_INCLUDE_FILES INCLUDE VARIABLE)
  37. if(NOT DEFINED "${VARIABLE}")
  38. set(CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n")
  39. if(CMAKE_REQUIRED_INCLUDES)
  40. set(CHECK_INCLUDE_FILES_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
  41. else()
  42. set(CHECK_INCLUDE_FILES_INCLUDE_DIRS)
  43. endif()
  44. set(CHECK_INCLUDE_FILES_CONTENT "/* */\n")
  45. set(MACRO_CHECK_INCLUDE_FILES_FLAGS ${CMAKE_REQUIRED_FLAGS})
  46. foreach(FILE ${INCLUDE})
  47. string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT
  48. "#include <${FILE}>\n")
  49. endforeach()
  50. string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT
  51. "\n\nint main(void){return 0;}\n")
  52. configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
  53. "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFiles.c" @ONLY)
  54. set(_INCLUDE ${INCLUDE}) # remove empty elements
  55. if("${_INCLUDE}" MATCHES "^([^;]+);.+;([^;]+)$")
  56. list(LENGTH _INCLUDE _INCLUDE_LEN)
  57. set(_description "${_INCLUDE_LEN} include files ${CMAKE_MATCH_1}, ..., ${CMAKE_MATCH_2}")
  58. elseif("${_INCLUDE}" MATCHES "^([^;]+);([^;]+)$")
  59. set(_description "include files ${CMAKE_MATCH_1}, ${CMAKE_MATCH_2}")
  60. else()
  61. set(_description "include file ${_INCLUDE}")
  62. endif()
  63. if(NOT CMAKE_REQUIRED_QUIET)
  64. message(STATUS "Looking for ${_description}")
  65. endif()
  66. try_compile(${VARIABLE}
  67. ${CMAKE_BINARY_DIR}
  68. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFiles.c
  69. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  70. CMAKE_FLAGS
  71. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILES_FLAGS}
  72. "${CHECK_INCLUDE_FILES_INCLUDE_DIRS}"
  73. OUTPUT_VARIABLE OUTPUT)
  74. if(${VARIABLE})
  75. if(NOT CMAKE_REQUIRED_QUIET)
  76. message(STATUS "Looking for ${_description} - found")
  77. endif()
  78. set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
  79. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  80. "Determining if files ${INCLUDE} "
  81. "exist passed with the following output:\n"
  82. "${OUTPUT}\n\n")
  83. else()
  84. if(NOT CMAKE_REQUIRED_QUIET)
  85. message(STATUS "Looking for ${_description} - not found")
  86. endif()
  87. set(${VARIABLE} "" CACHE INTERNAL "Have includes ${INCLUDE}")
  88. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  89. "Determining if files ${INCLUDE} "
  90. "exist failed with the following output:\n"
  91. "${OUTPUT}\nSource:\n${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
  92. endif()
  93. endif()
  94. endmacro()