CheckSourceCompiles.cmake 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. CheckSourceCompiles
  5. ----------------------
  6. .. versionadded:: 3.19
  7. This module provides a command that checks whether a source code can be
  8. built for a given language.
  9. Load this module in a CMake project with:
  10. .. code-block:: cmake
  11. include(CheckSourceCompiles)
  12. Commands
  13. ^^^^^^^^
  14. This module provides the following command:
  15. .. command:: check_source_compiles
  16. Checks once whether the given source code can be built for the given
  17. language:
  18. .. code-block:: cmake
  19. check_source_compiles(
  20. <lang>
  21. <code>
  22. <variable>
  23. [FAIL_REGEX <regexes>...]
  24. [SRC_EXT <extension>]
  25. )
  26. This command checks once that the source supplied in ``<code>`` can be
  27. compiled (and linked into an executable) for code language ``<lang>``.
  28. The result of the check is stored in the internal cache variable specified
  29. by ``<variable>``.
  30. The arguments are:
  31. ``<lang>``
  32. Language of the source code to check. Supported languages are:
  33. ``C``, ``CXX``, ``CUDA``, ``Fortran``, ``HIP``, ``ISPC``, ``OBJC``,
  34. ``OBJCXX``, and ``Swift``.
  35. .. versionadded:: 3.21
  36. Support for ``HIP`` language.
  37. .. versionadded:: 3.26
  38. Support for ``Swift`` language.
  39. ``<code>``
  40. The source code to check. This must be an entire program, as written
  41. in a file containing the body block. All symbols used in the source code
  42. are expected to be declared as usual in their corresponding headers.
  43. ``<variable>``
  44. Variable name of an internal cache variable to store the result of the
  45. check, with boolean true for success and boolean false for failure.
  46. ``FAIL_REGEX <regexes>...``
  47. If one or more regular expression patterns are provided, then failure is
  48. determined by checking if anything in the compiler output matches any of
  49. the specified regular expressions.
  50. ``SRC_EXT <extension>``
  51. By default, the internal test source file used for the check will be
  52. given a file extension that matches the requested language (e.g., ``.c``
  53. for C, ``.cxx`` for C++, ``.F90`` for Fortran, etc.). This option can
  54. be used to override this with the ``.<extension>`` instead.
  55. .. rubric:: Variables Affecting the Check
  56. The following variables may be set before calling this command to modify
  57. the way the check is run:
  58. .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst
  59. .. include:: /module/include/CMAKE_REQUIRED_DEFINITIONS.rst
  60. .. include:: /module/include/CMAKE_REQUIRED_INCLUDES.rst
  61. .. include:: /module/include/CMAKE_REQUIRED_LINK_OPTIONS.rst
  62. .. include:: /module/include/CMAKE_REQUIRED_LIBRARIES.rst
  63. .. include:: /module/include/CMAKE_REQUIRED_LINK_DIRECTORIES.rst
  64. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst
  65. .. include:: /module/include/CMAKE_TRY_COMPILE_TARGET_TYPE.rst
  66. Examples
  67. ^^^^^^^^
  68. Example: Basic Usage
  69. """"""""""""""""""""
  70. The following example demonstrates how to check whether the C++ compiler
  71. supports a specific language feature using this module. In this case, the
  72. check verifies if the compiler supports ``C++11`` lambda expressions. The
  73. result is stored in the internal cache variable ``HAVE_CXX11_LAMBDAS``:
  74. .. code-block:: cmake
  75. include(CheckSourceCompiles)
  76. check_source_compiles(CXX "
  77. int main()
  78. {
  79. auto lambda = []() { return 42; };
  80. return lambda();
  81. }
  82. " HAVE_CXX11_LAMBDAS)
  83. Example: Checking Code With Bracket Argument
  84. """"""""""""""""""""""""""""""""""""""""""""
  85. The following example shows how to check whether the C compiler supports the
  86. ``noreturn`` attribute. Code is supplied using the :ref:`Bracket Argument`
  87. for easier embedded quotes handling:
  88. .. code-block:: cmake
  89. :force:
  90. include(CheckSourceCompiles)
  91. check_source_compiles(C [[
  92. #if !__has_c_attribute(noreturn)
  93. # error "No noreturn attribute"
  94. #endif
  95. int main(void) { return 0; }
  96. ]] HAVE_NORETURN)
  97. Example: Performing a Check Without Linking
  98. """""""""""""""""""""""""""""""""""""""""""
  99. In the following example, this module is used to perform a compile-only
  100. check of Fortran source code, whether the compiler supports the ``pure``
  101. procedure attribute:
  102. .. code-block:: cmake
  103. include(CheckSourceCompiles)
  104. block()
  105. set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
  106. check_source_compiles(
  107. Fortran
  108. "pure subroutine foo()
  109. end subroutine"
  110. HAVE_PURE
  111. )
  112. endblock()
  113. Example: Isolated Check
  114. """""""""""""""""""""""
  115. In the following example, this module is used in combination with the
  116. :module:`CMakePushCheckState` module to modify required libraries when
  117. checking whether the PostgreSQL ``PGVerbosity`` enum contains
  118. ``PQERRORS_SQLSTATE`` (available as of PostgreSQL version 12):
  119. .. code-block:: cmake
  120. include(CheckSourceCompiles)
  121. include(CMakePushCheckState)
  122. find_package(PostgreSQL)
  123. if(TARGET PostgreSQL::PostgreSQL)
  124. cmake_push_check_state(RESET)
  125. set(CMAKE_REQUIRED_LIBRARIES PostgreSQL::PostgreSQL)
  126. check_source_compiles(C "
  127. #include <libpq-fe.h>
  128. int main(void)
  129. {
  130. PGVerbosity e = PQERRORS_SQLSTATE;
  131. (void)e;
  132. return 0;
  133. }
  134. " HAVE_PQERRORS_SQLSTATE)
  135. cmake_pop_check_state()
  136. endif()
  137. See Also
  138. ^^^^^^^^
  139. * The :module:`CheckSourceRuns` module to check whether the source code can
  140. be built and also run.
  141. #]=======================================================================]
  142. include_guard(GLOBAL)
  143. include(Internal/CheckSourceCompiles)
  144. function(CHECK_SOURCE_COMPILES _lang _source _var)
  145. cmake_check_source_compiles(${_lang} "${_source}" ${_var} ${ARGN})
  146. endfunction()