CheckSourceRuns.cmake 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 that the source supplied in ``<code>`` can be compiled as a source
  14. file for the requested language, linked as an executable and then run.
  15. If the ``<code>`` could be built and run successfully, the internal cache variable
  16. specified by ``<resultVar>`` will be set to 1, otherwise it will be set to
  17. a value that evaluates to boolean false (e.g. an empty string or an error
  18. message).
  19. By default, the test source file will be given a file extension that matches
  20. the requested language. The ``SRC_EXT`` option can be used to override this
  21. with ``.<extension>`` instead.
  22. The ``<code>`` must contain a valid main program. For example:
  23. .. code-block:: cmake
  24. check_source_runs(C
  25. "#include <stdlib.h>
  26. #include <stdnoreturn.h>
  27. noreturn void f(){ exit(0); }
  28. int main(void) { f(); return 1; }"
  29. HAVE_NORETURN)
  30. check_source_runs(Fortran
  31. "program test
  32. real :: x[*]
  33. call co_sum(x)
  34. end program"
  35. HAVE_COARRAY)
  36. The check is only performed once, with the result cached in the variable named
  37. by ``<resultVar>``. Every subsequent CMake run will reuse this cached value
  38. rather than performing the check again, even if the ``<code>`` changes. In
  39. order to force the check to be re-evaluated, the variable named by
  40. ``<resultVar>`` must be manually removed from the cache.
  41. The compile and link commands can be influenced by setting any of the
  42. following variables prior to calling ``check_source_runs()``
  43. .. include:: /module/CMAKE_REQUIRED_FLAGS.txt
  44. .. include:: /module/CMAKE_REQUIRED_DEFINITIONS.txt
  45. .. include:: /module/CMAKE_REQUIRED_INCLUDES.txt
  46. .. include:: /module/CMAKE_REQUIRED_LINK_OPTIONS.txt
  47. .. include:: /module/CMAKE_REQUIRED_LIBRARIES.txt
  48. .. include:: /module/CMAKE_REQUIRED_QUIET.txt
  49. #]=======================================================================]
  50. include_guard(GLOBAL)
  51. include(Internal/CheckSourceRuns)
  52. function(CHECK_SOURCE_RUNS _lang _source _var)
  53. cmake_check_source_runs(${_lang} "${_source}" ${_var} ${ARGN})
  54. endfunction()