CheckCXXSourceRuns.cmake 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CheckCXXSourceRuns
  5. ------------------
  6. Check if given C++ source compiles and links into an executable and can
  7. subsequently be run.
  8. .. command:: check_cxx_source_runs
  9. ::
  10. check_cxx_source_runs(code resultVar)
  11. Check that the source supplied in ``code`` can be compiled as a C++ source
  12. file, linked as an executable and then run. The ``code`` must contain at
  13. least a ``main()`` function. If the code could be built and run successfully,
  14. the internal cache variable specified by ``resultVar`` will be set to 1,
  15. otherwise it will be set to an value that evaluates to boolean false (e.g.
  16. an empty string or an error message).
  17. The underlying check is performed by the :command:`try_run` command. The
  18. compile and link commands can be influenced by setting any of the following
  19. variables prior to calling ``check_cxx_source_runs()``:
  20. ``CMAKE_REQUIRED_FLAGS``
  21. Additional flags to pass to the compiler. Note that the contents of
  22. :variable:`CMAKE_CXX_FLAGS <CMAKE_<LANG>_FLAGS>` and its associated
  23. configuration-specific variable are automatically added to the compiler
  24. command before the contents of ``CMAKE_REQUIRED_FLAGS``.
  25. ``CMAKE_REQUIRED_DEFINITIONS``
  26. A :ref:`;-list <CMake Language Lists>` of compiler definitions of the form
  27. ``-DFOO`` or ``-DFOO=bar``. A definition for the name specified by
  28. ``resultVar`` will also be added automatically.
  29. ``CMAKE_REQUIRED_INCLUDES``
  30. A :ref:`;-list <CMake Language Lists>` of header search paths to pass to
  31. the compiler. These will be the only header search paths used by
  32. ``try_run()``, i.e. the contents of the :prop_dir:`INCLUDE_DIRECTORIES`
  33. directory property will be ignored.
  34. ``CMAKE_REQUIRED_LIBRARIES``
  35. A :ref:`;-list <CMake Language Lists>` of libraries to add to the link
  36. command. These can be the name of system libraries or they can be
  37. :ref:`Imported Targets <Imported Targets>` (see :command:`try_run` for
  38. further details).
  39. ``CMAKE_REQUIRED_QUIET``
  40. If this variable evaluates to a boolean true value, all status messages
  41. associated with the check will be suppressed.
  42. The check is only performed once, with the result cached in the variable
  43. named by ``resultVar``. Every subsequent CMake run will re-use this cached
  44. value rather than performing the check again, even if the ``code`` changes.
  45. In order to force the check to be re-evaluated, the variable named by
  46. ``resultVar`` must be manually removed from the cache.
  47. #]=======================================================================]
  48. include_guard(GLOBAL)
  49. macro(CHECK_CXX_SOURCE_RUNS SOURCE VAR)
  50. if(NOT DEFINED "${VAR}")
  51. set(MACRO_CHECK_FUNCTION_DEFINITIONS
  52. "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
  53. if(CMAKE_REQUIRED_LIBRARIES)
  54. set(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES
  55. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  56. else()
  57. set(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES)
  58. endif()
  59. if(CMAKE_REQUIRED_INCLUDES)
  60. set(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES
  61. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  62. else()
  63. set(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES)
  64. endif()
  65. file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx"
  66. "${SOURCE}\n")
  67. if(NOT CMAKE_REQUIRED_QUIET)
  68. message(STATUS "Performing Test ${VAR}")
  69. endif()
  70. try_run(${VAR}_EXITCODE ${VAR}_COMPILED
  71. ${CMAKE_BINARY_DIR}
  72. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx
  73. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  74. ${CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES}
  75. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  76. -DCMAKE_SKIP_RPATH:BOOL=${CMAKE_SKIP_RPATH}
  77. "${CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES}"
  78. COMPILE_OUTPUT_VARIABLE OUTPUT)
  79. # if it did not compile make the return value fail code of 1
  80. if(NOT ${VAR}_COMPILED)
  81. set(${VAR}_EXITCODE 1)
  82. endif()
  83. # if the return value was 0 then it worked
  84. if("${${VAR}_EXITCODE}" EQUAL 0)
  85. set(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
  86. if(NOT CMAKE_REQUIRED_QUIET)
  87. message(STATUS "Performing Test ${VAR} - Success")
  88. endif()
  89. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  90. "Performing C++ SOURCE FILE Test ${VAR} succeeded with the following output:\n"
  91. "${OUTPUT}\n"
  92. "Return value: ${${VAR}}\n"
  93. "Source file was:\n${SOURCE}\n")
  94. else()
  95. if(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES "FAILED_TO_RUN")
  96. set(${VAR} "${${VAR}_EXITCODE}")
  97. else()
  98. set(${VAR} "" CACHE INTERNAL "Test ${VAR}")
  99. endif()
  100. if(NOT CMAKE_REQUIRED_QUIET)
  101. message(STATUS "Performing Test ${VAR} - Failed")
  102. endif()
  103. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  104. "Performing C++ SOURCE FILE Test ${VAR} failed with the following output:\n"
  105. "${OUTPUT}\n"
  106. "Return value: ${${VAR}_EXITCODE}\n"
  107. "Source file was:\n${SOURCE}\n")
  108. endif()
  109. endif()
  110. endmacro()