CheckSourceRuns.cmake 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst 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>``. If the code builds and runs with exit code ``0``, success is
  18. indicated by boolean ``true``. Failure to build or run is indicated by boolean
  19. ``false``, such as an empty 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/include/CMAKE_REQUIRED_FLAGS.rst
  40. .. include:: /module/include/CMAKE_REQUIRED_DEFINITIONS.rst
  41. .. include:: /module/include/CMAKE_REQUIRED_INCLUDES.rst
  42. .. include:: /module/include/CMAKE_REQUIRED_LINK_OPTIONS.rst
  43. .. include:: /module/include/CMAKE_REQUIRED_LIBRARIES.rst
  44. .. include:: /module/include/CMAKE_REQUIRED_LINK_DIRECTORIES.rst
  45. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst
  46. #]=======================================================================]
  47. include_guard(GLOBAL)
  48. include(Internal/CheckSourceRuns)
  49. function(CHECK_SOURCE_RUNS _lang _source _var)
  50. cmake_check_source_runs(${_lang} "${_source}" ${_var} ${ARGN})
  51. endfunction()