CheckIncludeFile.cmake 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #.rst:
  2. # CheckIncludeFile
  3. # ----------------
  4. #
  5. # macro which checks the include file exists.
  6. #
  7. # CHECK_INCLUDE_FILE(INCLUDE VARIABLE)
  8. #
  9. # ::
  10. #
  11. # INCLUDE - name of include file
  12. # VARIABLE - variable to return result
  13. #
  14. #
  15. #
  16. # an optional third argument is the CFlags to add to the compile line or
  17. # you can use CMAKE_REQUIRED_FLAGS
  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 2002-2009 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_FILE INCLUDE VARIABLE)
  41. if("${VARIABLE}" MATCHES "^${VARIABLE}$")
  42. if(CMAKE_REQUIRED_INCLUDES)
  43. set(CHECK_INCLUDE_FILE_C_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
  44. else()
  45. set(CHECK_INCLUDE_FILE_C_INCLUDE_DIRS)
  46. endif()
  47. set(MACRO_CHECK_INCLUDE_FILE_FLAGS ${CMAKE_REQUIRED_FLAGS})
  48. set(CHECK_INCLUDE_FILE_VAR ${INCLUDE})
  49. configure_file(${CMAKE_ROOT}/Modules/CheckIncludeFile.c.in
  50. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFile.c)
  51. if(NOT CMAKE_REQUIRED_QUIET)
  52. message(STATUS "Looking for ${INCLUDE}")
  53. endif()
  54. if(${ARGC} EQUAL 3)
  55. set(CMAKE_C_FLAGS_SAVE ${CMAKE_C_FLAGS})
  56. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${ARGV2}")
  57. endif()
  58. try_compile(${VARIABLE}
  59. ${CMAKE_BINARY_DIR}
  60. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFile.c
  61. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  62. CMAKE_FLAGS
  63. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILE_FLAGS}
  64. "${CHECK_INCLUDE_FILE_C_INCLUDE_DIRS}"
  65. OUTPUT_VARIABLE OUTPUT)
  66. if(${ARGC} EQUAL 3)
  67. set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_SAVE})
  68. endif()
  69. if(${VARIABLE})
  70. if(NOT CMAKE_REQUIRED_QUIET)
  71. message(STATUS "Looking for ${INCLUDE} - found")
  72. endif()
  73. set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
  74. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  75. "Determining if the include file ${INCLUDE} "
  76. "exists passed with the following output:\n"
  77. "${OUTPUT}\n\n")
  78. else()
  79. if(NOT CMAKE_REQUIRED_QUIET)
  80. message(STATUS "Looking for ${INCLUDE} - not found")
  81. endif()
  82. set(${VARIABLE} "" CACHE INTERNAL "Have include ${INCLUDE}")
  83. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  84. "Determining if the include file ${INCLUDE} "
  85. "exists failed with the following output:\n"
  86. "${OUTPUT}\n\n")
  87. endif()
  88. endif()
  89. endmacro()