CheckSourceCompiles.cmake 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. Check once if source code can be built for a given language.
  8. .. command:: check_source_compiles
  9. .. code-block:: cmake
  10. check_source_compiles(<lang> <code> <resultVar>
  11. [FAIL_REGEX <regex1> [<regex2>...]]
  12. [SRC_EXT <extension>])
  13. Check once that the source supplied in ``<code>`` can be built for code
  14. language ``<lang>``. The result is stored in the internal cache variable
  15. specified by ``<resultVar>``, with boolean ``true`` for success and
  16. boolean ``false`` for failure.
  17. If ``FAIL_REGEX`` is provided, then failure is determined by checking
  18. if anything in the compiler output matches any of the specified regular
  19. expressions.
  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 C example checks if the compiler supports the ``noreturn`` attribute:
  24. .. code-block:: cmake
  25. set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
  26. check_source_compiles(C
  27. "#if !__has_c_attribute(noreturn)
  28. #error \"No noreturn attribute\"
  29. #endif"
  30. HAVE_NORETURN)
  31. The Fortran example checks if the compiler supports the ``pure`` procedure
  32. attribute:
  33. .. code-block:: cmake
  34. set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
  35. check_source_compiles(Fortran
  36. "pure subroutine foo()
  37. end subroutine"
  38. HAVE_PURE)
  39. Internally, :command:`try_compile` is used to compile the source. If
  40. :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` is set to ``EXECUTABLE`` (default),
  41. the source is compiled and linked as an executable program. If set to
  42. ``STATIC_LIBRARY``, the source is compiled but not linked. In any case, all
  43. functions must be declared as usual.
  44. See also :command:`check_source_runs` to run compiled source.
  45. The compile and link commands can be influenced by setting any of the
  46. following variables prior to calling ``check_source_compiles()``:
  47. .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst
  48. .. include:: /module/include/CMAKE_REQUIRED_DEFINITIONS.rst
  49. .. include:: /module/include/CMAKE_REQUIRED_INCLUDES.rst
  50. .. include:: /module/include/CMAKE_REQUIRED_LINK_OPTIONS.rst
  51. .. include:: /module/include/CMAKE_REQUIRED_LIBRARIES.rst
  52. .. include:: /module/include/CMAKE_REQUIRED_LINK_DIRECTORIES.rst
  53. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst
  54. #]=======================================================================]
  55. include_guard(GLOBAL)
  56. include(Internal/CheckSourceCompiles)
  57. function(CHECK_SOURCE_COMPILES _lang _source _var)
  58. cmake_check_source_compiles(${_lang} "${_source}" ${_var} ${ARGN})
  59. endfunction()