CheckCXXSourceRuns.cmake 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # - Check if the given C++ source code compiles and runs.
  2. # CHECK_CXX_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. macro(CHECK_CXX_SOURCE_RUNS SOURCE VAR)
  26. if("${VAR}" MATCHES "^${VAR}$")
  27. set(MACRO_CHECK_FUNCTION_DEFINITIONS
  28. "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
  29. if(CMAKE_REQUIRED_LIBRARIES)
  30. set(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES
  31. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  32. else()
  33. set(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES)
  34. endif()
  35. if(CMAKE_REQUIRED_INCLUDES)
  36. set(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES
  37. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  38. else()
  39. set(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES)
  40. endif()
  41. file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx"
  42. "${SOURCE}\n")
  43. message(STATUS "Performing Test ${VAR}")
  44. try_run(${VAR}_EXITCODE ${VAR}_COMPILED
  45. ${CMAKE_BINARY_DIR}
  46. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx
  47. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  48. ${CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES}
  49. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  50. -DCMAKE_SKIP_RPATH:BOOL=${CMAKE_SKIP_RPATH}
  51. "${CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES}"
  52. COMPILE_OUTPUT_VARIABLE OUTPUT)
  53. # if it did not compile make the return value fail code of 1
  54. if(NOT ${VAR}_COMPILED)
  55. set(${VAR}_EXITCODE 1)
  56. endif()
  57. # if the return value was 0 then it worked
  58. if("${${VAR}_EXITCODE}" EQUAL 0)
  59. set(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
  60. message(STATUS "Performing Test ${VAR} - Success")
  61. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  62. "Performing C++ SOURCE FILE Test ${VAR} succeded with the following output:\n"
  63. "${OUTPUT}\n"
  64. "Return value: ${${VAR}}\n"
  65. "Source file was:\n${SOURCE}\n")
  66. else()
  67. if(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES "FAILED_TO_RUN")
  68. set(${VAR} "${${VAR}_EXITCODE}")
  69. else()
  70. set(${VAR} "" CACHE INTERNAL "Test ${VAR}")
  71. endif()
  72. message(STATUS "Performing Test ${VAR} - Failed")
  73. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  74. "Performing C++ SOURCE FILE Test ${VAR} failed with the following output:\n"
  75. "${OUTPUT}\n"
  76. "Return value: ${${VAR}_EXITCODE}\n"
  77. "Source file was:\n${SOURCE}\n")
  78. endif()
  79. endif()
  80. endmacro()