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:
5CheckFortranSourceRuns
6----------------------
7
8.. versionadded:: 3.14
9
10Check if given Fortran source compiles and links into an executable and can
11subsequently be run.
12
13.. command:: check_fortran_source_runs
14
15  .. code-block:: cmake
16
17    check_fortran_source_runs(<code> <resultVar>
18        [SRC_EXT <extension>])
19
20  Check that the source supplied in ``<code>`` can be compiled as a Fortran source
21  file, linked as an executable and then run. The ``<code>`` must be a Fortran program
22  containing at least an ``end`` statement--for example:
23
24  .. code-block:: cmake
25
26    check_fortran_source_runs("real :: x[*]; call co_sum(x); end" F2018coarrayOK)
27
28  This command can help avoid costly build processes when a compiler lacks support
29  for a necessary feature, or a particular vendor library is not compatible with
30  the Fortran compiler version being used. Some of these failures only occur at runtime
31  instead of linktime, and a trivial runtime example can catch the issue before the
32  main build process.
33
34  If the ``<code>`` could be built and run
35  successfully, the internal cache variable specified by ``<resultVar>`` will
36  be set to 1, otherwise it will be set to an value that evaluates to boolean
37  false (e.g. an empty string or an error message).
38
39  By default, the test source file will be given a ``.F90`` file extension. The
40  ``SRC_EXT`` option can be used to override this with ``.<extension>`` instead.
41
42  The underlying check is performed by the :command:`try_run` command. The
43  compile and link commands can be influenced by setting any of the following
44  variables prior to calling ``check_fortran_source_runs()``:
45
46  ``CMAKE_REQUIRED_FLAGS``
47    Additional flags to pass to the compiler. Note that the contents of
48    :variable:`CMAKE_Fortran_FLAGS <CMAKE_<LANG>_FLAGS>` and its associated
49    configuration-specific variable are automatically added to the compiler
50    command before the contents of ``CMAKE_REQUIRED_FLAGS``.
51
52  ``CMAKE_REQUIRED_DEFINITIONS``
53    A :ref:`;-list <CMake Language Lists>` of compiler definitions of the form
54    ``-DFOO`` or ``-DFOO=bar``. A definition for the name specified by
55    ``<resultVar>`` will also be added automatically.
56
57  ``CMAKE_REQUIRED_INCLUDES``
58    A :ref:`;-list <CMake Language Lists>` of header search paths to pass to
59    the compiler. These will be the only header search paths used by
60    ``try_run()``, i.e. the contents of the :prop_dir:`INCLUDE_DIRECTORIES`
61    directory property will be ignored.
62
63  ``CMAKE_REQUIRED_LINK_OPTIONS``
64    A :ref:`;-list <CMake Language Lists>` of options to add to the link
65    command (see :command:`try_run` for further details).
66
67  ``CMAKE_REQUIRED_LIBRARIES``
68    A :ref:`;-list <CMake Language Lists>` of libraries to add to the link
69    command. These can be the name of system libraries or they can be
70    :ref:`Imported Targets <Imported Targets>` (see :command:`try_run` for
71    further details).
72
73  ``CMAKE_REQUIRED_QUIET``
74    If this variable evaluates to a boolean true value, all status messages
75    associated with the check will be suppressed.
76
77  The check is only performed once, with the result cached in the variable
78  named by ``<resultVar>``. Every subsequent CMake run will re-use this cached
79  value rather than performing the check again, even if the ``<code>`` changes.
80  In order to force the check to be re-evaluated, the variable named by
81  ``<resultVar>`` must be manually removed from the cache.
82
83#]=======================================================================]
84
85include_guard(GLOBAL)
86include(Internal/CheckSourceRuns)
87
88macro(CHECK_Fortran_SOURCE_RUNS SOURCE VAR)
89  # Pass the SRC_EXT we used by default historically.
90  # A user-provided SRC_EXT argument in ARGN will override ours.
91  cmake_check_source_runs(Fortran "${SOURCE}" ${VAR} SRC_EXT "F90" ${ARGN})
92endmacro()
93