CheckIncludeFiles.cmake 3.5 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()
  32. set(CHECK_INCLUDE_FILES_INCLUDE_DIRS)
  33. endif()
  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()
  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()
  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()
  77. endif()
  78. endmacro()