CheckSourceRuns.cmake 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. CheckSourceRuns
  5. -------------------
  6. .. versionadded:: 3.19
  7. Check if given source compiles and links into an executable and can
  8. subsequently be run.
  9. .. command:: check_source_runs
  10. .. code-block:: cmake
  11. check_source_runs(<lang> <code> <resultVar>
  12. [SRC_EXT <extension>])
  13. Check once that the ``<lang>`` source supplied in ``<code>`` can be built,
  14. linked as an executable, and then run. The ``<code>`` must contain at least
  15. a ``main()`` function, or in Fortran a ``program``.
  16. The result is stored in the internal cache variable specified by
  17. ``<resultVar>``. Success of build and run is indicated by boolean ``true``.
  18. Failure to build or run is indicated by boolean ``false`` such as an empty
  19. string or an error message.
  20. By default, the test source file will be given a file extension that matches
  21. the requested language. The ``SRC_EXT`` option can be used to override this
  22. with ``.<extension>`` instead.
  23. The ``<code>`` must contain a valid main program. For example:
  24. .. code-block:: cmake
  25. check_source_runs(C
  26. "#include <stdlib.h>
  27. #include <stdnoreturn.h>
  28. noreturn void f(){ exit(0); }
  29. int main(void) { f(); return 1; }"
  30. HAVE_NORETURN)
  31. check_source_runs(Fortran
  32. "program test
  33. real :: x[*]
  34. call co_sum(x)
  35. end program"
  36. HAVE_COARRAY)
  37. The compile and link commands can be influenced by setting any of the
  38. following variables prior to calling ``check_source_runs()``
  39. .. include:: /module/CMAKE_REQUIRED_FLAGS.txt
  40. .. include:: /module/CMAKE_REQUIRED_DEFINITIONS.txt
  41. .. include:: /module/CMAKE_REQUIRED_INCLUDES.txt
  42. .. include:: /module/CMAKE_REQUIRED_LINK_OPTIONS.txt
  43. .. include:: /module/CMAKE_REQUIRED_LIBRARIES.txt
  44. .. include:: /module/CMAKE_REQUIRED_QUIET.txt
  45. #]=======================================================================]
  46. include_guard(GLOBAL)
  47. include(Internal/CheckSourceRuns)
  48. function(CHECK_SOURCE_RUNS _lang _source _var)
  49. cmake_check_source_runs(${_lang} "${_source}" ${_var} ${ARGN})
  50. endfunction()