CheckIncludeFiles.cmake 4.7 KB

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