CheckIncludeFiles.cmake 3.7 KB

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