CheckSourceCompiles.cmake 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 underlying check is performed by the :command:`try_compile` command. The
  37. compile and link commands can be influenced by setting any of the following
  38. variables prior to calling ``check_source_compiles()``:
  39. ``CMAKE_REQUIRED_FLAGS``
  40. Additional flags to pass to the compiler. Note that the contents of
  41. :variable:`CMAKE_<LANG>_FLAGS <CMAKE_<LANG>_FLAGS>` and its associated
  42. configuration-specific variable are automatically added to the compiler
  43. command before the contents of ``CMAKE_REQUIRED_FLAGS``.
  44. ``CMAKE_REQUIRED_DEFINITIONS``
  45. A :ref:`;-list <CMake Language Lists>` of compiler definitions of the form
  46. ``-DFOO`` or ``-DFOO=bar``. A definition for the name specified by
  47. ``<resultVar>`` will also be added automatically.
  48. ``CMAKE_REQUIRED_INCLUDES``
  49. A :ref:`;-list <CMake Language Lists>` of header search paths to pass to
  50. the compiler. These will be the only header search paths used by
  51. ``try_compile()``, i.e. the contents of the :prop_dir:`INCLUDE_DIRECTORIES`
  52. directory property will be ignored.
  53. ``CMAKE_REQUIRED_LINK_OPTIONS``
  54. A :ref:`;-list <CMake Language Lists>` of options to add to the link
  55. command (see :command:`try_compile` for further details).
  56. ``CMAKE_REQUIRED_LIBRARIES``
  57. A :ref:`;-list <CMake Language Lists>` of libraries to add to the link
  58. command. These can be the name of system libraries or they can be
  59. :ref:`Imported Targets <Imported Targets>` (see :command:`try_compile` for
  60. further details).
  61. ``CMAKE_REQUIRED_QUIET``
  62. If this variable evaluates to a boolean true value, all status messages
  63. associated with the check will be suppressed.
  64. The check is only performed once, with the result cached in the variable
  65. named by ``<resultVar>``. Every subsequent CMake run will re-use this cached
  66. value rather than performing the check again, even if the ``<code>`` changes.
  67. In order to force the check to be re-evaluated, the variable named by
  68. ``<resultVar>`` must be manually removed from the cache.
  69. #]=======================================================================]
  70. include_guard(GLOBAL)
  71. include(Internal/CheckSourceCompiles)
  72. function(CHECK_SOURCE_COMPILES _lang _source _var)
  73. cmake_check_source_compiles(${_lang} "${_source}" ${_var} ${ARGN})
  74. endfunction()