CheckIncludeFiles.cmake 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #.rst:
  2. # CheckIncludeFiles
  3. # -----------------
  4. #
  5. # Check if the files can be included
  6. #
  7. #
  8. #
  9. # CHECK_INCLUDE_FILES(INCLUDE VARIABLE)
  10. #
  11. # ::
  12. #
  13. # INCLUDE - list of files to include
  14. # VARIABLE - variable to return result
  15. # Will be created as an internal cache variable.
  16. #
  17. #
  18. #
  19. # The following variables may be set before calling this macro to modify
  20. # the way the check is run:
  21. #
  22. # ::
  23. #
  24. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  25. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  26. # CMAKE_REQUIRED_INCLUDES = list of include directories
  27. # CMAKE_REQUIRED_QUIET = execute quietly without messages
  28. #=============================================================================
  29. # Copyright 2003-2012 Kitware, Inc.
  30. #
  31. # Distributed under the OSI-approved BSD License (the "License");
  32. # see accompanying file Copyright.txt for details.
  33. #
  34. # This software is distributed WITHOUT ANY WARRANTY; without even the
  35. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  36. # See the License for more information.
  37. #=============================================================================
  38. # (To distribute this file outside of CMake, substitute the full
  39. # License text for the above reference.)
  40. macro(CHECK_INCLUDE_FILES INCLUDE VARIABLE)
  41. if(NOT DEFINED "${VARIABLE}")
  42. set(CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n")
  43. if(CMAKE_REQUIRED_INCLUDES)
  44. set(CHECK_INCLUDE_FILES_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
  45. else()
  46. set(CHECK_INCLUDE_FILES_INCLUDE_DIRS)
  47. endif()
  48. set(CHECK_INCLUDE_FILES_CONTENT "/* */\n")
  49. set(MACRO_CHECK_INCLUDE_FILES_FLAGS ${CMAKE_REQUIRED_FLAGS})
  50. foreach(FILE ${INCLUDE})
  51. set(CMAKE_CONFIGURABLE_FILE_CONTENT
  52. "${CMAKE_CONFIGURABLE_FILE_CONTENT}#include <${FILE}>\n")
  53. endforeach()
  54. set(CMAKE_CONFIGURABLE_FILE_CONTENT
  55. "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n\nint main(void){return 0;}\n")
  56. configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
  57. "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFiles.c" @ONLY)
  58. set(_INCLUDE ${INCLUDE}) # remove empty elements
  59. if("${_INCLUDE}" MATCHES "^([^;]+);.+;([^;]+)$")
  60. list(LENGTH _INCLUDE _INCLUDE_LEN)
  61. set(_description "${_INCLUDE_LEN} include files ${CMAKE_MATCH_1}, ..., ${CMAKE_MATCH_2}")
  62. elseif("${_INCLUDE}" MATCHES "^([^;]+);([^;]+)$")
  63. set(_description "include files ${CMAKE_MATCH_1}, ${CMAKE_MATCH_2}")
  64. else()
  65. set(_description "include file ${_INCLUDE}")
  66. endif()
  67. if(NOT CMAKE_REQUIRED_QUIET)
  68. message(STATUS "Looking for ${_description}")
  69. endif()
  70. try_compile(${VARIABLE}
  71. ${CMAKE_BINARY_DIR}
  72. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFiles.c
  73. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  74. CMAKE_FLAGS
  75. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILES_FLAGS}
  76. "${CHECK_INCLUDE_FILES_INCLUDE_DIRS}"
  77. OUTPUT_VARIABLE OUTPUT)
  78. if(${VARIABLE})
  79. if(NOT CMAKE_REQUIRED_QUIET)
  80. message(STATUS "Looking for ${_description} - found")
  81. endif()
  82. set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
  83. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  84. "Determining if files ${INCLUDE} "
  85. "exist passed with the following output:\n"
  86. "${OUTPUT}\n\n")
  87. else()
  88. if(NOT CMAKE_REQUIRED_QUIET)
  89. message(STATUS "Looking for ${_description} - not found")
  90. endif()
  91. set(${VARIABLE} "" CACHE INTERNAL "Have includes ${INCLUDE}")
  92. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  93. "Determining if files ${INCLUDE} "
  94. "exist failed with the following output:\n"
  95. "${OUTPUT}\nSource:\n${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
  96. endif()
  97. endif()
  98. endmacro()