CheckIncludeFiles.cmake 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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> [LANGUAGE <language>])
  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. # If LANGUAGE is set, the specified compiler will be used to perform the
  22. # check. Acceptable values are C and CXX.
  23. #
  24. # The following variables may be set before calling this macro to modify
  25. # the way the check is run:
  26. #
  27. # ``CMAKE_REQUIRED_FLAGS``
  28. # string of compile command line flags
  29. # ``CMAKE_REQUIRED_DEFINITIONS``
  30. # list of macros to define (-DFOO=bar)
  31. # ``CMAKE_REQUIRED_INCLUDES``
  32. # list of include directories
  33. # ``CMAKE_REQUIRED_QUIET``
  34. # execute quietly without messages
  35. #
  36. # See modules :module:`CheckIncludeFile` and :module:`CheckIncludeFileCXX`
  37. # to check for a single header file in ``C`` or ``CXX`` languages.
  38. macro(CHECK_INCLUDE_FILES INCLUDE VARIABLE)
  39. if(NOT DEFINED "${VARIABLE}")
  40. set(CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n")
  41. if("x${ARGN}" STREQUAL "x")
  42. if(CMAKE_C_COMPILER_LOADED)
  43. set(_lang C)
  44. elseif(CMAKE_CXX_COMPILER_LOADED)
  45. set(_lang CXX)
  46. else()
  47. message(FATAL_ERROR "CHECK_INCLUDE_FILES needs either C or CXX language enabled")
  48. endif()
  49. elseif("x${ARGN}" MATCHES "^xLANGUAGE;([a-zA-Z]+)$")
  50. set(_lang "${CMAKE_MATCH_1}")
  51. else()
  52. message(FATAL_ERROR "Unknown arguments:\n ${ARGN}\n")
  53. endif()
  54. if(_lang STREQUAL "C")
  55. set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckIncludeFiles/${var}.c)
  56. elseif(_lang STREQUAL "CXX")
  57. set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckIncludeFiles/${var}.cpp)
  58. else()
  59. message(FATAL_ERROR "Unknown language:\n ${_lang}\nSupported languages: C, CXX.\n")
  60. endif()
  61. if(CMAKE_REQUIRED_INCLUDES)
  62. set(CHECK_INCLUDE_FILES_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
  63. else()
  64. set(CHECK_INCLUDE_FILES_INCLUDE_DIRS)
  65. endif()
  66. set(CHECK_INCLUDE_FILES_CONTENT "/* */\n")
  67. set(MACRO_CHECK_INCLUDE_FILES_FLAGS ${CMAKE_REQUIRED_FLAGS})
  68. foreach(FILE ${INCLUDE})
  69. string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT
  70. "#include <${FILE}>\n")
  71. endforeach()
  72. string(APPEND CMAKE_CONFIGURABLE_FILE_CONTENT
  73. "\n\nint main(void){return 0;}\n")
  74. configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
  75. "${src}" @ONLY)
  76. set(_INCLUDE ${INCLUDE}) # remove empty elements
  77. if("${_INCLUDE}" MATCHES "^([^;]+);.+;([^;]+)$")
  78. list(LENGTH _INCLUDE _INCLUDE_LEN)
  79. set(_description "${_INCLUDE_LEN} include files ${CMAKE_MATCH_1}, ..., ${CMAKE_MATCH_2}")
  80. elseif("${_INCLUDE}" MATCHES "^([^;]+);([^;]+)$")
  81. set(_description "include files ${CMAKE_MATCH_1}, ${CMAKE_MATCH_2}")
  82. else()
  83. set(_description "include file ${_INCLUDE}")
  84. endif()
  85. if(NOT CMAKE_REQUIRED_QUIET)
  86. message(STATUS "Looking for ${_description}")
  87. endif()
  88. try_compile(${VARIABLE}
  89. ${CMAKE_BINARY_DIR}
  90. ${src}
  91. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  92. CMAKE_FLAGS
  93. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILES_FLAGS}
  94. "${CHECK_INCLUDE_FILES_INCLUDE_DIRS}"
  95. OUTPUT_VARIABLE OUTPUT)
  96. if(${VARIABLE})
  97. if(NOT CMAKE_REQUIRED_QUIET)
  98. message(STATUS "Looking for ${_description} - 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 files ${INCLUDE} "
  103. "exist passed with the following output:\n"
  104. "${OUTPUT}\n\n")
  105. else()
  106. if(NOT CMAKE_REQUIRED_QUIET)
  107. message(STATUS "Looking for ${_description} - not found")
  108. endif()
  109. set(${VARIABLE} "" CACHE INTERNAL "Have includes ${INCLUDE}")
  110. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  111. "Determining if files ${INCLUDE} "
  112. "exist failed with the following output:\n"
  113. "${OUTPUT}\nSource:\n${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
  114. endif()
  115. endif()
  116. endmacro()