blob: f9967498c4f91dadcf786c81f8cc88915c797e1a [file] [log] [blame]
Michael Hirsch, Ph.D10a14772018-12-06 15:09:54 -05001# 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
Kitware Robot496ec602020-07-06 10:14:38 -04008.. versionadded:: 3.14
9
Michael Hirsch, Ph.D10a14772018-12-06 15:09:54 -050010Check 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
scivisionb19036d2023-02-04 20:25:58 -050021 file, linked as an executable and then run. The ``<code>`` must be a Fortran
22 ``program``.
Michael Hirsch, Ph.D3d63d3c2019-01-22 22:24:03 -050023
24 .. code-block:: cmake
25
scivisionb19036d2023-02-04 20:25:58 -050026 check_fortran_source_runs("program test
27 real :: x[*]
28 call co_sum(x)
29 end program"
30 HAVE_COARRAY)
Michael Hirsch, Ph.D3d63d3c2019-01-22 22:24:03 -050031
32 This command can help avoid costly build processes when a compiler lacks support
33 for a necessary feature, or a particular vendor library is not compatible with
34 the Fortran compiler version being used. Some of these failures only occur at runtime
35 instead of linktime, and a trivial runtime example can catch the issue before the
36 main build process.
37
38 If the ``<code>`` could be built and run
Michael Hirsch, Ph.D10a14772018-12-06 15:09:54 -050039 successfully, the internal cache variable specified by ``<resultVar>`` will
40 be set to 1, otherwise it will be set to an value that evaluates to boolean
41 false (e.g. an empty string or an error message).
42
43 By default, the test source file will be given a ``.F90`` file extension. The
44 ``SRC_EXT`` option can be used to override this with ``.<extension>`` instead.
45
scivisioncf480222023-03-08 00:23:49 -050046 The check is only performed once, with the result cached in the variable named
47 by ``<resultVar>``. Every subsequent CMake run will re-use this cached value
48 rather than performing the check again, even if the ``<code>`` changes. In
49 order to force the check to be re-evaluated, the variable named by
Michael Hirsch, Ph.D10a14772018-12-06 15:09:54 -050050 ``<resultVar>`` must be manually removed from the cache.
51
scivisioncf480222023-03-08 00:23:49 -050052 The compile and link commands can be influenced by setting any of the
53 following variables prior to calling ``check_fortran_source_runs()``:
54
55.. include:: /module/CMAKE_REQUIRED_FLAGS.txt
56
57.. include:: /module/CMAKE_REQUIRED_DEFINITIONS.txt
58
59.. include:: /module/CMAKE_REQUIRED_INCLUDES.txt
60
61.. include:: /module/CMAKE_REQUIRED_LINK_OPTIONS.txt
62
63.. include:: /module/CMAKE_REQUIRED_LIBRARIES.txt
64
65.. include:: /module/CMAKE_REQUIRED_QUIET.txt
66
Michael Hirsch, Ph.D10a14772018-12-06 15:09:54 -050067#]=======================================================================]
68
69include_guard(GLOBAL)
Robert Maynardd1929182020-10-25 12:26:42 -040070include(Internal/CheckSourceRuns)
Michael Hirsch, Ph.D10a14772018-12-06 15:09:54 -050071
72macro(CHECK_Fortran_SOURCE_RUNS SOURCE VAR)
Michael Hirsch1e519df2020-10-15 08:46:03 -040073 # Pass the SRC_EXT we used by default historically.
74 # A user-provided SRC_EXT argument in ARGN will override ours.
Robert Maynardd1929182020-10-25 12:26:42 -040075 cmake_check_source_runs(Fortran "${SOURCE}" ${VAR} SRC_EXT "F90" ${ARGN})
Michael Hirsch, Ph.D10a14772018-12-06 15:09:54 -050076endmacro()