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#[=======================================================================[.rst: 5CheckCSourceRuns 6---------------- 7 8Check if given C source compiles and links into an executable and can 9subsequently be run. 10 11.. command:: check_c_source_runs 12 13 .. code-block:: cmake 14 15 check_c_source_runs(<code> <resultVar>) 16 17 Check that the source supplied in ``<code>`` can be compiled as a C source 18 file, linked as an executable and then run. The ``<code>`` must contain at 19 least a ``main()`` function. If the ``<code>`` could be built and run 20 successfully, the internal cache variable specified by ``<resultVar>`` will 21 be set to 1, otherwise it will be set to an value that evaluates to boolean 22 false (e.g. an empty string or an error message). 23 24 The underlying check is performed by the :command:`try_run` command. The 25 compile and link commands can be influenced by setting any of the following 26 variables prior to calling ``check_c_source_runs()``: 27 28 ``CMAKE_REQUIRED_FLAGS`` 29 Additional flags to pass to the compiler. Note that the contents of 30 :variable:`CMAKE_C_FLAGS <CMAKE_<LANG>_FLAGS>` and its associated 31 configuration-specific variable are automatically added to the compiler 32 command before the contents of ``CMAKE_REQUIRED_FLAGS``. 33 34 ``CMAKE_REQUIRED_DEFINITIONS`` 35 A :ref:`;-list <CMake Language Lists>` of compiler definitions of the form 36 ``-DFOO`` or ``-DFOO=bar``. A definition for the name specified by 37 ``<resultVar>`` will also be added automatically. 38 39 ``CMAKE_REQUIRED_INCLUDES`` 40 A :ref:`;-list <CMake Language Lists>` of header search paths to pass to 41 the compiler. These will be the only header search paths used by 42 ``try_run()``, i.e. the contents of the :prop_dir:`INCLUDE_DIRECTORIES` 43 directory property will be ignored. 44 45 ``CMAKE_REQUIRED_LINK_OPTIONS`` 46 .. versionadded:: 3.14 47 48 A :ref:`;-list <CMake Language Lists>` of options to add to the link 49 command (see :command:`try_run` for further details). 50 51 ``CMAKE_REQUIRED_LIBRARIES`` 52 A :ref:`;-list <CMake Language Lists>` of libraries to add to the link 53 command. These can be the name of system libraries or they can be 54 :ref:`Imported Targets <Imported Targets>` (see :command:`try_run` for 55 further details). 56 57 ``CMAKE_REQUIRED_QUIET`` 58 .. versionadded:: 3.1 59 60 If this variable evaluates to a boolean true value, all status messages 61 associated with the check will be suppressed. 62 63 The check is only performed once, with the result cached in the variable 64 named by ``<resultVar>``. Every subsequent CMake run will re-use this cached 65 value rather than performing the check again, even if the ``<code>`` changes. 66 In order to force the check to be re-evaluated, the variable named by 67 ``<resultVar>`` must be manually removed from the cache. 68 69#]=======================================================================] 70 71include_guard(GLOBAL) 72include(Internal/CheckSourceRuns) 73 74macro(CHECK_C_SOURCE_RUNS SOURCE VAR) 75 set(_CheckSourceRuns_old_signature 1) 76 cmake_check_source_runs(C "${SOURCE}" ${VAR} ${ARGN}) 77 unset(_CheckSourceRuns_old_signature) 78endmacro() 79