CheckCSourceCompiles.cmake 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # - Check if the given C source code compiles.
  2. # CHECK_C_SOURCE_COMPILES(<code> <var> [FAIL_REGEX <fail-regex>])
  3. # <code> - source code to try to compile
  4. # <var> - variable to store whether the source code compiled
  5. # <fail-regex> - fail if test output matches this regex
  6. # The following variables may be set before calling this macro to
  7. # modify the way the check is run:
  8. #
  9. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  10. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  11. # CMAKE_REQUIRED_INCLUDES = list of include directories
  12. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  13. #=============================================================================
  14. # Copyright 2005-2009 Kitware, Inc.
  15. #
  16. # Distributed under the OSI-approved BSD License (the "License");
  17. # see accompanying file Copyright.txt for details.
  18. #
  19. # This software is distributed WITHOUT ANY WARRANTY; without even the
  20. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  21. # See the License for more information.
  22. #=============================================================================
  23. # (To distribute this file outside of CMake, substitute the full
  24. # License text for the above reference.)
  25. MACRO(CHECK_C_SOURCE_COMPILES SOURCE VAR)
  26. IF("${VAR}" MATCHES "^${VAR}$")
  27. SET(_FAIL_REGEX)
  28. SET(_key)
  29. FOREACH(arg ${ARGN})
  30. IF("${arg}" MATCHES "^(FAIL_REGEX)$")
  31. SET(_key "${arg}")
  32. ELSEIF(_key)
  33. LIST(APPEND _${_key} "${arg}")
  34. ELSE()
  35. MESSAGE(FATAL_ERROR "Unknown argument:\n ${arg}\n")
  36. ENDIF()
  37. ENDFOREACH()
  38. SET(MACRO_CHECK_FUNCTION_DEFINITIONS
  39. "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
  40. IF(CMAKE_REQUIRED_LIBRARIES)
  41. SET(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES
  42. "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
  43. ELSE(CMAKE_REQUIRED_LIBRARIES)
  44. SET(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES)
  45. ENDIF(CMAKE_REQUIRED_LIBRARIES)
  46. IF(CMAKE_REQUIRED_INCLUDES)
  47. SET(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES
  48. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  49. ELSE(CMAKE_REQUIRED_INCLUDES)
  50. SET(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES)
  51. ENDIF(CMAKE_REQUIRED_INCLUDES)
  52. FILE(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c"
  53. "${SOURCE}\n")
  54. MESSAGE(STATUS "Performing Test ${VAR}")
  55. TRY_COMPILE(${VAR}
  56. ${CMAKE_BINARY_DIR}
  57. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c
  58. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  59. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  60. "${CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES}"
  61. "${CHECK_C_SOURCE_COMPILES_ADD_INCLUDES}"
  62. OUTPUT_VARIABLE OUTPUT)
  63. FOREACH(_regex ${_FAIL_REGEX})
  64. IF("${OUTPUT}" MATCHES "${_regex}")
  65. SET(${VAR} 0)
  66. ENDIF()
  67. ENDFOREACH()
  68. IF(${VAR})
  69. SET(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
  70. MESSAGE(STATUS "Performing Test ${VAR} - Success")
  71. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  72. "Performing C SOURCE FILE Test ${VAR} succeded with the following output:\n"
  73. "${OUTPUT}\n"
  74. "Source file was:\n${SOURCE}\n")
  75. ELSE(${VAR})
  76. MESSAGE(STATUS "Performing Test ${VAR} - Failed")
  77. SET(${VAR} "" CACHE INTERNAL "Test ${VAR}")
  78. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  79. "Performing C SOURCE FILE Test ${VAR} failed with the following output:\n"
  80. "${OUTPUT}\n"
  81. "Source file was:\n${SOURCE}\n")
  82. ENDIF(${VAR})
  83. ENDIF("${VAR}" MATCHES "^${VAR}$")
  84. ENDMACRO(CHECK_C_SOURCE_COMPILES)