CheckFortranSourceCompiles.cmake 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. CheckFortranSourceCompiles
  5. --------------------------
  6. .. versionadded:: 3.1
  7. Check if given Fortran source compiles and links into an executable.
  8. .. command:: check_fortran_source_compiles
  9. .. code-block:: cmake
  10. check_fortran_source_compiles(<code> <resultVar>
  11. [FAIL_REGEX <regex>...]
  12. [SRC_EXT <extension>]
  13. )
  14. Checks that the source supplied in ``<code>`` can be compiled as a Fortran
  15. source file and linked as an executable. The ``<code>`` must be a Fortran program
  16. containing at least an ``end`` statement--for example:
  17. .. code-block:: cmake
  18. check_fortran_source_compiles("character :: b; error stop b; end" F2018ESTOPOK SRC_EXT F90)
  19. This command can help avoid costly build processes when a compiler lacks support
  20. for a necessary feature, or a particular vendor library is not compatible with
  21. the Fortran compiler version being used. This generate-time check may advise the
  22. user of such before the main build process. See also the
  23. :command:`check_fortran_source_runs` command to actually run the compiled code.
  24. The result will be stored in the internal cache
  25. variable ``<resultVar>``, with a boolean true value for success and boolean
  26. false for failure.
  27. If ``FAIL_REGEX`` is provided, then failure is determined by checking
  28. if anything in the output matches any of the specified regular expressions.
  29. By default, the test source file will be given a ``.F`` file extension. The
  30. ``SRC_EXT`` option can be used to override this with ``.<extension>`` instead--
  31. ``.F90`` is a typical choice.
  32. The underlying check is performed by the :command:`try_compile` command. The
  33. compile and link commands can be influenced by setting any of the following
  34. variables prior to calling ``check_fortran_source_compiles()``:
  35. ``CMAKE_REQUIRED_FLAGS``
  36. Additional flags to pass to the compiler. Note that the contents of
  37. :variable:`CMAKE_Fortran_FLAGS <CMAKE_<LANG>_FLAGS>` and its associated
  38. configuration-specific variable are automatically added to the compiler
  39. command before the contents of ``CMAKE_REQUIRED_FLAGS``.
  40. ``CMAKE_REQUIRED_DEFINITIONS``
  41. A :ref:`;-list <CMake Language Lists>` of compiler definitions of the form
  42. ``-DFOO`` or ``-DFOO=bar``. A definition for the name specified by
  43. ``<resultVar>`` will also be added automatically.
  44. ``CMAKE_REQUIRED_INCLUDES``
  45. A :ref:`;-list <CMake Language Lists>` of header search paths to pass to
  46. the compiler. These will be the only header search paths used by
  47. ``try_compile()``, i.e. the contents of the :prop_dir:`INCLUDE_DIRECTORIES`
  48. directory property will be ignored.
  49. ``CMAKE_REQUIRED_LINK_OPTIONS``
  50. A :ref:`;-list <CMake Language Lists>` of options to add to the link
  51. command (see :command:`try_compile` for further details).
  52. ``CMAKE_REQUIRED_LIBRARIES``
  53. A :ref:`;-list <CMake Language Lists>` of libraries to add to the link
  54. command. These can be the name of system libraries or they can be
  55. :ref:`Imported Targets <Imported Targets>` (see :command:`try_compile` for
  56. further details).
  57. ``CMAKE_REQUIRED_QUIET``
  58. If this variable evaluates to a boolean true value, all status messages
  59. associated with the check will be suppressed.
  60. The check is only performed once, with the result cached in the variable
  61. named by ``<resultVar>``. Every subsequent CMake run will re-use this cached
  62. value rather than performing the check again, even if the ``<code>`` changes.
  63. In order to force the check to be re-evaluated, the variable named by
  64. ``<resultVar>`` must be manually removed from the cache.
  65. #]=======================================================================]
  66. include_guard(GLOBAL)
  67. include(CheckSourceCompiles)
  68. macro(CHECK_Fortran_SOURCE_COMPILES SOURCE VAR)
  69. # Pass the SRC_EXT we used by default historically.
  70. # A user-provided SRC_EXT argument in ARGN will override ours.
  71. check_source_compiles(Fortran "${SOURCE}" ${VAR} SRC_EXT "F" ${ARGN})
  72. endmacro()