CheckSourceRuns.cmake 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. This module provides a command to check whether a source code can be built
  8. and run.
  9. Load this module in a CMake project with:
  10. .. code-block:: cmake
  11. include(CheckSourceRuns)
  12. Commands
  13. ^^^^^^^^
  14. This module provides the following command:
  15. .. command:: check_source_runs
  16. Checks once whether the given source code compiles and links into an
  17. executable that can subsequently be run:
  18. .. code-block:: cmake
  19. check_source_runs(<lang> <code> <variable> [SRC_EXT <extension>])
  20. This command checks once that the ``<lang>`` source code supplied in
  21. ``<code>`` can be built, linked as an executable, and then run. The
  22. result of the check is stored in the internal cache variable specified by
  23. ``<variable>``.
  24. The arguments are:
  25. ``<lang>``
  26. The programming language of the source ``<code>`` to check. Supported
  27. languages are: ``C``, ``CXX``, ``CUDA``, ``Fortran``, ``HIP``, ``OBJC``,
  28. and ``OBJCXX``.
  29. .. versionadded:: 3.21
  30. Support for ``HIP`` language.
  31. ``<code>``
  32. The source code to be tested. It must contain a valid source program.
  33. For example, it must contain at least a ``main()`` function (in C/C++),
  34. or a ``program`` unit (in Fortran).
  35. ``<variable>``
  36. Name of the internal cache variable with the result of the check. If
  37. the code builds and runs with exit code ``0``, success is indicated by
  38. a boolean true value. Failure to build or run is indicated by a boolean
  39. false value, such as an empty string or an error message.
  40. ``SRC_EXT <extension>``
  41. By default, the internal test source file used for the check will be
  42. given a file extension that matches the requested language (e.g., ``.c``
  43. for C, ``.cxx`` for C++, ``.F90`` for Fortran, etc.). This option can
  44. be used to override this with the ``.<extension>`` instead.
  45. .. rubric:: Variables Affecting the Check
  46. The following variables may be set before calling this command to modify
  47. the way the check is run:
  48. .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst
  49. .. include:: /module/include/CMAKE_REQUIRED_DEFINITIONS.rst
  50. .. include:: /module/include/CMAKE_REQUIRED_INCLUDES.rst
  51. .. include:: /module/include/CMAKE_REQUIRED_LINK_OPTIONS.rst
  52. .. include:: /module/include/CMAKE_REQUIRED_LIBRARIES.rst
  53. .. include:: /module/include/CMAKE_REQUIRED_LINK_DIRECTORIES.rst
  54. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst
  55. Examples
  56. ^^^^^^^^
  57. Example: Basic Usage
  58. """"""""""""""""""""
  59. The following example demonstrates how to use this module to check whether
  60. the C source code is supported and operational at runtime. The result of
  61. the check is stored in the internal cache variable ``HAVE_NORETURN``.
  62. .. code-block:: cmake
  63. include(CheckSourceRuns)
  64. check_source_runs(C [[
  65. #include <stdlib.h>
  66. #include <stdnoreturn.h>
  67. noreturn void f(){ exit(0); }
  68. int main(void) { f(); return 1; }
  69. ]] HAVE_NORETURN)
  70. Example: Checking Fortran Code
  71. """"""""""""""""""""""""""""""
  72. Checking if Fortran source code runs successfully:
  73. .. code-block:: cmake
  74. include(CheckSourceRuns)
  75. check_source_runs(Fortran [[
  76. program test
  77. real :: x[*]
  78. call co_sum(x)
  79. end program
  80. ]] HAVE_COARRAY)
  81. Example: Checking C++ Code With Bracket Argument
  82. """"""""""""""""""""""""""""""""""""""""""""""""
  83. The following example demonstrates how to check whether the C++ standard
  84. library is functional and ``std::vector`` works at runtime. If the source
  85. compiles, links, and runs successfully, internal cache variable
  86. ``HAVE_WORKING_STD_VECTOR`` will be set to boolean true value. Code is
  87. supplied using :ref:`Bracket Argument` for easier embedded quotes handling:
  88. .. code-block:: cmake
  89. :force:
  90. include(CheckSourceRuns)
  91. check_source_runs(CXX [[
  92. #include <iostream>
  93. #include <vector>
  94. int main()
  95. {
  96. std::vector<int> v = {1, 2, 3};
  97. if (v.size() != 3) return 1;
  98. std::cout << "Vector works correctly." << std::endl;
  99. return 0;
  100. }
  101. ]] HAVE_WORKING_STD_VECTOR)
  102. Example: Isolated Check
  103. """""""""""""""""""""""
  104. In the following example, this module is used in combination with the
  105. :module:`CMakePushCheckState` module to modify required compile definitions
  106. and libraries when checking whether the C function ``sched_getcpu()`` is
  107. supported and operational at runtime. For example, on some systems, the
  108. ``sched_getcpu()`` function may be available at compile time but not actually
  109. implemented by the kernel. In such cases, it returns ``-1`` and sets
  110. ``errno`` to ``ENOSYS``. This check verifies that ``sched_getcpu()`` runs
  111. successfully and stores a boolean result in the internal cache variable
  112. ``HAVE_SCHED_GETCPU``.
  113. .. code-block:: cmake
  114. include(CheckSourceRuns)
  115. include(CMakePushCheckState)
  116. cmake_push_check_state(RESET)
  117. set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
  118. if(CMAKE_SYSTEM_NAME STREQUAL "Haiku")
  119. set(CMAKE_REQUIRED_LIBRARIES gnu)
  120. endif()
  121. check_source_runs(C [[
  122. #include <sched.h>
  123. int main(void)
  124. {
  125. if (sched_getcpu() == -1) {
  126. return 1;
  127. }
  128. return 0;
  129. }
  130. ]] HAVE_SCHED_GETCPU)
  131. cmake_pop_check_state()
  132. See Also
  133. ^^^^^^^^
  134. * The :module:`CheckSourceCompiles` module to check whether a source code
  135. can be built.
  136. #]=======================================================================]
  137. include_guard(GLOBAL)
  138. include(Internal/CheckSourceRuns)
  139. function(CHECK_SOURCE_RUNS _lang _source _var)
  140. cmake_check_source_runs(${_lang} "${_source}" ${_var} ${ARGN})
  141. endfunction()