CheckFortranSourceRuns.cmake 3.9 KB

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