Michael Hirsch, Ph.D | 10a1477 | 2018-12-06 15:09:54 -0500 | [diff] [blame] | 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: |
| 5 | CheckFortranSourceRuns |
| 6 | ---------------------- |
| 7 | |
Kitware Robot | 496ec60 | 2020-07-06 10:14:38 -0400 | [diff] [blame] | 8 | .. versionadded:: 3.14 |
| 9 | |
Michael Hirsch, Ph.D | 10a1477 | 2018-12-06 15:09:54 -0500 | [diff] [blame] | 10 | Check if given Fortran source compiles and links into an executable and can |
| 11 | subsequently 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 |
scivision | b19036d | 2023-02-04 20:25:58 -0500 | [diff] [blame] | 21 | file, linked as an executable and then run. The ``<code>`` must be a Fortran |
| 22 | ``program``. |
Michael Hirsch, Ph.D | 3d63d3c | 2019-01-22 22:24:03 -0500 | [diff] [blame] | 23 | |
| 24 | .. code-block:: cmake |
| 25 | |
scivision | b19036d | 2023-02-04 20:25:58 -0500 | [diff] [blame] | 26 | check_fortran_source_runs("program test |
| 27 | real :: x[*] |
| 28 | call co_sum(x) |
| 29 | end program" |
| 30 | HAVE_COARRAY) |
Michael Hirsch, Ph.D | 3d63d3c | 2019-01-22 22:24:03 -0500 | [diff] [blame] | 31 | |
| 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.D | 10a1477 | 2018-12-06 15:09:54 -0500 | [diff] [blame] | 39 | 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 | |
scivision | cf48022 | 2023-03-08 00:23:49 -0500 | [diff] [blame] | 46 | 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.D | 10a1477 | 2018-12-06 15:09:54 -0500 | [diff] [blame] | 50 | ``<resultVar>`` must be manually removed from the cache. |
| 51 | |
scivision | cf48022 | 2023-03-08 00:23:49 -0500 | [diff] [blame] | 52 | 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.D | 10a1477 | 2018-12-06 15:09:54 -0500 | [diff] [blame] | 67 | #]=======================================================================] |
| 68 | |
| 69 | include_guard(GLOBAL) |
Robert Maynard | d192918 | 2020-10-25 12:26:42 -0400 | [diff] [blame] | 70 | include(Internal/CheckSourceRuns) |
Michael Hirsch, Ph.D | 10a1477 | 2018-12-06 15:09:54 -0500 | [diff] [blame] | 71 | |
| 72 | macro(CHECK_Fortran_SOURCE_RUNS SOURCE VAR) |
Michael Hirsch | 1e519df | 2020-10-15 08:46:03 -0400 | [diff] [blame] | 73 | # Pass the SRC_EXT we used by default historically. |
| 74 | # A user-provided SRC_EXT argument in ARGN will override ours. |
Robert Maynard | d192918 | 2020-10-25 12:26:42 -0400 | [diff] [blame] | 75 | cmake_check_source_runs(Fortran "${SOURCE}" ${VAR} SRC_EXT "F90" ${ARGN}) |
Michael Hirsch, Ph.D | 10a1477 | 2018-12-06 15:09:54 -0500 | [diff] [blame] | 76 | endmacro() |