CheckCSourceCompiles.cmake 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # - Check if given C source compiles and links into an executable
  2. # CHECK_C_SOURCE_COMPILES(<code> <var> [FAIL_REGEX <fail-regex>])
  3. # <code> - source code to try to compile, must define 'main'
  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. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  43. else()
  44. set(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES)
  45. endif()
  46. if(CMAKE_REQUIRED_INCLUDES)
  47. set(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES
  48. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  49. else()
  50. set(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES)
  51. endif()
  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. ${CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES}
  60. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  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()
  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()
  83. endif()
  84. endmacro()