CheckFortranSourceRuns.cmake 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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
  15. ``program``.
  16. .. code-block:: cmake
  17. check_fortran_source_runs("program test
  18. real :: x[*]
  19. call co_sum(x)
  20. end program"
  21. HAVE_COARRAY)
  22. This command can help avoid costly build processes when a compiler lacks support
  23. for a necessary feature, or a particular vendor library is not compatible with
  24. the Fortran compiler version being used. Some of these failures only occur at runtime
  25. instead of linktime, and a trivial runtime example can catch the issue before the
  26. main build process.
  27. If the ``<code>`` could be built and run
  28. successfully, the internal cache variable specified by ``<resultVar>`` will
  29. be set to 1, otherwise it will be set to an value that evaluates to boolean
  30. false (e.g. an empty string or an error message).
  31. By default, the test source file will be given a ``.F90`` file extension. The
  32. ``SRC_EXT`` option can be used to override this with ``.<extension>`` instead.
  33. The check is only performed once, with the result cached in the variable named
  34. by ``<resultVar>``. Every subsequent CMake run will reuse this cached value
  35. rather than performing the check again, even if the ``<code>`` changes. In
  36. order to force the check to be re-evaluated, the variable named by
  37. ``<resultVar>`` must be manually removed from the cache.
  38. The compile and link commands can be influenced by setting any of the
  39. following variables prior to calling ``check_fortran_source_runs()``:
  40. .. include:: /module/CMAKE_REQUIRED_FLAGS.txt
  41. .. include:: /module/CMAKE_REQUIRED_DEFINITIONS.txt
  42. .. include:: /module/CMAKE_REQUIRED_INCLUDES.txt
  43. .. include:: /module/CMAKE_REQUIRED_LINK_OPTIONS.txt
  44. .. include:: /module/CMAKE_REQUIRED_LIBRARIES.txt
  45. .. include:: /module/CMAKE_REQUIRED_QUIET.txt
  46. #]=======================================================================]
  47. include_guard(GLOBAL)
  48. include(Internal/CheckSourceRuns)
  49. macro(CHECK_Fortran_SOURCE_RUNS SOURCE VAR)
  50. # Pass the SRC_EXT we used by default historically.
  51. # A user-provided SRC_EXT argument in ARGN will override ours.
  52. cmake_check_source_runs(Fortran "${SOURCE}" ${VAR} SRC_EXT "F90" ${ARGN})
  53. endmacro()