CheckSourceCompiles.cmake 2.6 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. CheckSourceCompiles
  5. ----------------------
  6. .. versionadded:: 3.19
  7. Check if given source compiles and links into an executable.
  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 that the source supplied in ``<code>`` can be compiled as a source
  14. file for the requested language and linked as an executable. The result
  15. will be stored in the internal cache variable specified by ``<resultVar>``,
  16. with a boolean true value for success and boolean false for failure. If
  17. ``FAIL_REGEX`` is provided, then failure is determined by checking if
  18. 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 ``<code>`` must contain a valid main program. For example:
  24. .. code-block:: cmake
  25. check_source_compiles(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_compiles(Fortran
  32. "program test
  33. error stop
  34. end program"
  35. HAVE_ERROR_STOP)
  36. The check is only performed once, with the result cached in the variable
  37. named by ``<resultVar>``. Every subsequent CMake run will reuse this cached
  38. value rather than performing the check again, even if the ``<code>`` changes.
  39. In 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_compiles()``:
  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/CheckSourceCompiles)
  52. function(CHECK_SOURCE_COMPILES _lang _source _var)
  53. cmake_check_source_compiles(${_lang} "${_source}" ${_var} ${ARGN})
  54. endfunction()