CheckFortranSourceRuns.cmake 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. Check if given Fortran source compiles and links into an executable and can
  7. subsequently be run.
  8. .. command:: check_fortran_source_runs
  9. .. code-block:: cmake
  10. check_fortran_source_runs(<code> <resultVar>
  11. [SRC_EXT <extension>])
  12. Check that the source supplied in ``<code>`` can be compiled as a Fortran source
  13. file, linked as an executable and then run. The ``<code>`` must be a Fortran program
  14. containing at least an ``end`` statement--for example:
  15. .. code-block:: cmake
  16. check_fortran_source_runs("real :: x[*]; call co_sum(x); end" F2018coarrayOK)
  17. This command can help avoid costly build processes when a compiler lacks support
  18. for a necessary feature, or a particular vendor library is not compatible with
  19. the Fortran compiler version being used. Some of these failures only occur at runtime
  20. instead of linktime, and a trivial runtime example can catch the issue before the
  21. main build process.
  22. If the ``<code>`` could be built and run
  23. successfully, the internal cache variable specified by ``<resultVar>`` will
  24. be set to 1, otherwise it will be set to an value that evaluates to boolean
  25. false (e.g. an empty string or an error message).
  26. By default, the test source file will be given a ``.F90`` file extension. The
  27. ``SRC_EXT`` option can be used to override this with ``.<extension>`` instead.
  28. The underlying check is performed by the :command:`try_run` command. The
  29. compile and link commands can be influenced by setting any of the following
  30. variables prior to calling ``check_fortran_source_runs()``:
  31. ``CMAKE_REQUIRED_FLAGS``
  32. Additional flags to pass to the compiler. Note that the contents of
  33. :variable:`CMAKE_Fortran_FLAGS <CMAKE_<LANG>_FLAGS>` and its associated
  34. configuration-specific variable are automatically added to the compiler
  35. command before the contents of ``CMAKE_REQUIRED_FLAGS``.
  36. ``CMAKE_REQUIRED_DEFINITIONS``
  37. A :ref:`;-list <CMake Language Lists>` of compiler definitions of the form
  38. ``-DFOO`` or ``-DFOO=bar``. A definition for the name specified by
  39. ``<resultVar>`` will also be added automatically.
  40. ``CMAKE_REQUIRED_INCLUDES``
  41. A :ref:`;-list <CMake Language Lists>` of header search paths to pass to
  42. the compiler. These will be the only header search paths used by
  43. ``try_run()``, i.e. the contents of the :prop_dir:`INCLUDE_DIRECTORIES`
  44. directory property will be ignored.
  45. ``CMAKE_REQUIRED_LINK_OPTIONS``
  46. A :ref:`;-list <CMake Language Lists>` of options to add to the link
  47. command (see :command:`try_run` for further details).
  48. ``CMAKE_REQUIRED_LIBRARIES``
  49. A :ref:`;-list <CMake Language Lists>` of libraries to add to the link
  50. command. These can be the name of system libraries or they can be
  51. :ref:`Imported Targets <Imported Targets>` (see :command:`try_run` for
  52. further details).
  53. ``CMAKE_REQUIRED_QUIET``
  54. If this variable evaluates to a boolean true value, all status messages
  55. associated with the check will be suppressed.
  56. The check is only performed once, with the result cached in the variable
  57. named by ``<resultVar>``. Every subsequent CMake run will re-use this cached
  58. value rather than performing the check again, even if the ``<code>`` changes.
  59. In order to force the check to be re-evaluated, the variable named by
  60. ``<resultVar>`` must be manually removed from the cache.
  61. #]=======================================================================]
  62. include_guard(GLOBAL)
  63. macro(CHECK_Fortran_SOURCE_RUNS SOURCE VAR)
  64. if(NOT DEFINED "${VAR}")
  65. set(_SRC_EXT)
  66. set(_key)
  67. foreach(arg ${ARGN})
  68. if("${arg}" MATCHES "^(SRC_EXT)$")
  69. set(_key "${arg}")
  70. elseif(_key)
  71. list(APPEND _${_key} "${arg}")
  72. else()
  73. message(FATAL_ERROR "Unknown argument:\n ${arg}\n")
  74. endif()
  75. endforeach()
  76. if(NOT _SRC_EXT)
  77. set(_SRC_EXT F90)
  78. endif()
  79. set(MACRO_CHECK_FUNCTION_DEFINITIONS
  80. "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
  81. if(CMAKE_REQUIRED_LINK_OPTIONS)
  82. set(CHECK_Fortran_SOURCE_COMPILES_ADD_LINK_OPTIONS
  83. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  84. else()
  85. set(CHECK_Fortran_SOURCE_COMPILES_ADD_LINK_OPTIONS)
  86. endif()
  87. if(CMAKE_REQUIRED_LIBRARIES)
  88. set(CHECK_Fortran_SOURCE_COMPILES_ADD_LIBRARIES
  89. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  90. else()
  91. set(CHECK_Fortran_SOURCE_COMPILES_ADD_LIBRARIES)
  92. endif()
  93. if(CMAKE_REQUIRED_INCLUDES)
  94. set(CHECK_Fortran_SOURCE_COMPILES_ADD_INCLUDES
  95. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  96. else()
  97. set(CHECK_Fortran_SOURCE_COMPILES_ADD_INCLUDES)
  98. endif()
  99. file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.${_SRC_EXT}"
  100. "${SOURCE}\n")
  101. if(NOT CMAKE_REQUIRED_QUIET)
  102. message(STATUS "Performing Test ${VAR}")
  103. endif()
  104. try_run(${VAR}_EXITCODE ${VAR}_COMPILED
  105. ${CMAKE_BINARY_DIR}
  106. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.${_SRC_EXT}
  107. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  108. ${CHECK_Fortran_SOURCE_COMPILES_ADD_LINK_OPTIONS}
  109. ${CHECK_Fortran_SOURCE_COMPILES_ADD_LIBRARIES}
  110. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  111. -DCMAKE_SKIP_RPATH:BOOL=${CMAKE_SKIP_RPATH}
  112. "${CHECK_Fortran_SOURCE_COMPILES_ADD_INCLUDES}"
  113. COMPILE_OUTPUT_VARIABLE OUTPUT
  114. RUN_OUTPUT_VARIABLE RUN_OUTPUT)
  115. # if it did not compile make the return value fail code of 1
  116. if(NOT ${VAR}_COMPILED)
  117. set(${VAR}_EXITCODE 1)
  118. endif()
  119. # if the return value was 0 then it worked
  120. if("${${VAR}_EXITCODE}" EQUAL 0)
  121. set(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
  122. if(NOT CMAKE_REQUIRED_QUIET)
  123. message(STATUS "Performing Test ${VAR} - Success")
  124. endif()
  125. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  126. "Performing Fortran SOURCE FILE Test ${VAR} succeeded with the following output:\n"
  127. "${OUTPUT}\n"
  128. "...and run output:\n"
  129. "${RUN_OUTPUT}\n"
  130. "Return value: ${${VAR}}\n"
  131. "Source file was:\n${SOURCE}\n")
  132. else()
  133. if(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES "FAILED_TO_RUN")
  134. set(${VAR} "${${VAR}_EXITCODE}")
  135. else()
  136. set(${VAR} "" CACHE INTERNAL "Test ${VAR}")
  137. endif()
  138. if(NOT CMAKE_REQUIRED_QUIET)
  139. message(STATUS "Performing Test ${VAR} - Failed")
  140. endif()
  141. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  142. "Performing Fortran SOURCE FILE Test ${VAR} failed with the following output:\n"
  143. "${OUTPUT}\n"
  144. "...and run output:\n"
  145. "${RUN_OUTPUT}\n"
  146. "Return value: ${${VAR}_EXITCODE}\n"
  147. "Source file was:\n${SOURCE}\n")
  148. endif()
  149. endif()
  150. endmacro()