1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4
5#[=======================================================================[.rst:
6CheckSourceCompiles
7----------------------
8
9.. versionadded:: 3.19
10
11Check if given source compiles and links into an executable.
12
13.. command:: check_source_compiles
14
15  .. code-block:: cmake
16
17    check_source_compiles(<lang> <code> <resultVar>
18                          [FAIL_REGEX <regex1> [<regex2>...]]
19                          [SRC_EXT <extension>])
20
21  Check that the source supplied in ``<code>`` can be compiled as a source
22  file for the requested language and linked as an executable (so it must
23  contain at least a ``main()`` function). The result will be stored in the
24  internal cache variable specified by ``<resultVar>``, with a boolean true
25  value for success and boolean false for failure. If ``FAIL_REGEX`` is
26  provided, then failure is determined by checking if anything in the output
27  matches any of the specified regular expressions.
28
29  By default, the test source file will be given a file extension that matches
30  the requested language. The ``SRC_EXT`` option can be used to override this
31  with ``.<extension>`` instead.
32
33  The underlying check is performed by the :command:`try_compile` command. The
34  compile and link commands can be influenced by setting any of the following
35  variables prior to calling ``check_source_compiles()``:
36
37  ``CMAKE_REQUIRED_FLAGS``
38    Additional flags to pass to the compiler. Note that the contents of
39    :variable:`CMAKE_<LANG>_FLAGS <CMAKE_<LANG>_FLAGS>` and its associated
40    configuration-specific variable are automatically added to the compiler
41    command before the contents of ``CMAKE_REQUIRED_FLAGS``.
42
43  ``CMAKE_REQUIRED_DEFINITIONS``
44    A :ref:`;-list <CMake Language Lists>` of compiler definitions of the form
45    ``-DFOO`` or ``-DFOO=bar``. A definition for the name specified by
46    ``<resultVar>`` will also be added automatically.
47
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
54  ``CMAKE_REQUIRED_LINK_OPTIONS``
55    A :ref:`;-list <CMake Language Lists>` of options to add to the link
56    command (see :command:`try_compile` for further details).
57
58  ``CMAKE_REQUIRED_LIBRARIES``
59    A :ref:`;-list <CMake Language Lists>` of libraries to add to the link
60    command. These can be the name of system libraries or they can be
61    :ref:`Imported Targets <Imported Targets>` (see :command:`try_compile` for
62    further details).
63
64  ``CMAKE_REQUIRED_QUIET``
65    If this variable evaluates to a boolean true value, all status messages
66    associated with the check will be suppressed.
67
68  The check is only performed once, with the result cached in the variable
69  named by ``<resultVar>``. Every subsequent CMake run will re-use this cached
70  value rather than performing the check again, even if the ``<code>`` changes.
71  In order to force the check to be re-evaluated, the variable named by
72  ``<resultVar>`` must be manually removed from the cache.
73
74#]=======================================================================]
75
76
77include_guard(GLOBAL)
78include(Internal/CheckSourceCompiles)
79
80function(CHECK_SOURCE_COMPILES _lang _source _var)
81  cmake_check_source_compiles(${_lang} "${_source}" ${_var} ${ARGN})
82endfunction()
83