CheckIncludeFiles.cmake 3.6 KB

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