CheckCSourceRuns.cmake 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # - Check if the given C source code compiles and runs.
  2. # CHECK_C_SOURCE_RUNS(<code> <var>)
  3. # <code> - source code to try to compile
  4. # <var> - variable to store the result
  5. # (1 for success, empty for failure)
  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 2006-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. INCLUDE("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
  26. MACRO(CHECK_C_SOURCE_RUNS SOURCE VAR)
  27. IF("${VAR}" MATCHES "^${VAR}$")
  28. SET(MACRO_CHECK_FUNCTION_DEFINITIONS
  29. "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
  30. IF(CMAKE_REQUIRED_LIBRARIES)
  31. # this one translates potentially used imported library targets to their files on disk
  32. CMAKE_EXPAND_IMPORTED_TARGETS(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
  33. SET(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES
  34. "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
  35. ELSE(CMAKE_REQUIRED_LIBRARIES)
  36. SET(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES)
  37. ENDIF(CMAKE_REQUIRED_LIBRARIES)
  38. IF(CMAKE_REQUIRED_INCLUDES)
  39. SET(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES
  40. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  41. ELSE(CMAKE_REQUIRED_INCLUDES)
  42. SET(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES)
  43. ENDIF(CMAKE_REQUIRED_INCLUDES)
  44. FILE(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c"
  45. "${SOURCE}\n")
  46. MESSAGE(STATUS "Performing Test ${VAR}")
  47. TRY_RUN(${VAR}_EXITCODE ${VAR}_COMPILED
  48. ${CMAKE_BINARY_DIR}
  49. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c
  50. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  51. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  52. -DCMAKE_SKIP_RPATH:BOOL=${CMAKE_SKIP_RPATH}
  53. "${CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES}"
  54. "${CHECK_C_SOURCE_COMPILES_ADD_INCLUDES}"
  55. COMPILE_OUTPUT_VARIABLE OUTPUT)
  56. # if it did not compile make the return value fail code of 1
  57. IF(NOT ${VAR}_COMPILED)
  58. SET(${VAR}_EXITCODE 1)
  59. ENDIF(NOT ${VAR}_COMPILED)
  60. # if the return value was 0 then it worked
  61. IF("${${VAR}_EXITCODE}" EQUAL 0)
  62. SET(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
  63. MESSAGE(STATUS "Performing Test ${VAR} - Success")
  64. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  65. "Performing C SOURCE FILE Test ${VAR} succeded with the following output:\n"
  66. "${OUTPUT}\n"
  67. "Return value: ${${VAR}}\n"
  68. "Source file was:\n${SOURCE}\n")
  69. ELSE("${${VAR}_EXITCODE}" EQUAL 0)
  70. IF(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES "FAILED_TO_RUN")
  71. SET(${VAR} "${${VAR}_EXITCODE}")
  72. ELSE(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES "FAILED_TO_RUN")
  73. SET(${VAR} "" CACHE INTERNAL "Test ${VAR}")
  74. ENDIF(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES "FAILED_TO_RUN")
  75. MESSAGE(STATUS "Performing Test ${VAR} - Failed")
  76. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  77. "Performing C SOURCE FILE Test ${VAR} failed with the following output:\n"
  78. "${OUTPUT}\n"
  79. "Return value: ${${VAR}_EXITCODE}\n"
  80. "Source file was:\n${SOURCE}\n")
  81. ENDIF("${${VAR}_EXITCODE}" EQUAL 0)
  82. ENDIF("${VAR}" MATCHES "^${VAR}$")
  83. ENDMACRO(CHECK_C_SOURCE_RUNS)