CheckFortranSourceCompiles.cmake 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. Check if given Fortran source compiles and links into an executable.
  7. .. command:: check_fortran_source_compiles
  8. .. code-block:: cmake
  9. check_fortran_source_compiles(<code> <resultVar>
  10. [FAIL_REGEX <regex>...]
  11. [SRC_EXT <extension>]
  12. )
  13. Checks that the source supplied in ``<code>`` can be compiled as a Fortran
  14. source file and linked as an executable. The ``<code>`` must be a Fortran program
  15. containing at least an ``end`` statement--for example:
  16. .. code-block:: cmake
  17. check_fortran_source_compiles("character :: b; error stop b; end" F2018ESTOPOK SRC_EXT F90)
  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. This generate-time check may advise the
  21. user of such before the main build process. See also the
  22. :command:`check_fortran_source_runs` command to actually run the compiled code.
  23. The result will be stored in the internal cache
  24. variable ``<resultVar>``, with a boolean true value for success and boolean
  25. false for failure.
  26. If ``FAIL_REGEX`` is provided, then failure is determined by checking
  27. if anything in the output matches any of the specified regular expressions.
  28. By default, the test source file will be given a ``.F`` file extension. The
  29. ``SRC_EXT`` option can be used to override this with ``.<extension>`` instead--
  30. ``.F90`` is a typical choice.
  31. The underlying check is performed by the :command:`try_compile` command. The
  32. compile and link commands can be influenced by setting any of the following
  33. variables prior to calling ``check_fortran_source_compiles()``:
  34. ``CMAKE_REQUIRED_FLAGS``
  35. Additional flags to pass to the compiler. Note that the contents of
  36. :variable:`CMAKE_Fortran_FLAGS <CMAKE_<LANG>_FLAGS>` and its associated
  37. configuration-specific variable are automatically added to the compiler
  38. command before the contents of ``CMAKE_REQUIRED_FLAGS``.
  39. ``CMAKE_REQUIRED_DEFINITIONS``
  40. A :ref:`;-list <CMake Language Lists>` of compiler definitions of the form
  41. ``-DFOO`` or ``-DFOO=bar``. A definition for the name specified by
  42. ``<resultVar>`` will also be added automatically.
  43. ``CMAKE_REQUIRED_INCLUDES``
  44. A :ref:`;-list <CMake Language Lists>` of header search paths to pass to
  45. the compiler. These will be the only header search paths used by
  46. ``try_compile()``, i.e. the contents of the :prop_dir:`INCLUDE_DIRECTORIES`
  47. directory property will be ignored.
  48. ``CMAKE_REQUIRED_LINK_OPTIONS``
  49. A :ref:`;-list <CMake Language Lists>` of options to add to the link
  50. command (see :command:`try_compile` for further details).
  51. ``CMAKE_REQUIRED_LIBRARIES``
  52. A :ref:`;-list <CMake Language Lists>` of libraries to add to the link
  53. command. These can be the name of system libraries or they can be
  54. :ref:`Imported Targets <Imported Targets>` (see :command:`try_compile` for
  55. further details).
  56. ``CMAKE_REQUIRED_QUIET``
  57. If this variable evaluates to a boolean true value, all status messages
  58. associated with the check will be suppressed.
  59. The check is only performed once, with the result cached in the variable
  60. named by ``<resultVar>``. Every subsequent CMake run will re-use this cached
  61. value rather than performing the check again, even if the ``<code>`` changes.
  62. In order to force the check to be re-evaluated, the variable named by
  63. ``<resultVar>`` must be manually removed from the cache.
  64. #]=======================================================================]
  65. include_guard(GLOBAL)
  66. macro(CHECK_Fortran_SOURCE_COMPILES SOURCE VAR)
  67. if(NOT DEFINED "${VAR}")
  68. set(_FAIL_REGEX)
  69. set(_SRC_EXT)
  70. set(_key)
  71. foreach(arg ${ARGN})
  72. if("${arg}" MATCHES "^(FAIL_REGEX|SRC_EXT)$")
  73. set(_key "${arg}")
  74. elseif(_key)
  75. list(APPEND _${_key} "${arg}")
  76. else()
  77. message(FATAL_ERROR "Unknown argument:\n ${arg}\n")
  78. endif()
  79. endforeach()
  80. if(NOT _SRC_EXT)
  81. set(_SRC_EXT F)
  82. endif()
  83. set(MACRO_CHECK_FUNCTION_DEFINITIONS
  84. "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
  85. if(CMAKE_REQUIRED_LINK_OPTIONS)
  86. set(CHECK_Fortran_SOURCE_COMPILES_ADD_LINK_OPTIONS
  87. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  88. else()
  89. set(CHECK_Fortran_SOURCE_COMPILES_ADD_LINK_OPTIONS)
  90. endif()
  91. if(CMAKE_REQUIRED_LIBRARIES)
  92. set(CHECK_Fortran_SOURCE_COMPILES_ADD_LIBRARIES
  93. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  94. else()
  95. set(CHECK_Fortran_SOURCE_COMPILES_ADD_LIBRARIES)
  96. endif()
  97. if(CMAKE_REQUIRED_INCLUDES)
  98. set(CHECK_Fortran_SOURCE_COMPILES_ADD_INCLUDES
  99. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  100. else()
  101. set(CHECK_Fortran_SOURCE_COMPILES_ADD_INCLUDES)
  102. endif()
  103. file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.${_SRC_EXT}"
  104. "${SOURCE}\n")
  105. if(NOT CMAKE_REQUIRED_QUIET)
  106. message(STATUS "Performing Test ${VAR}")
  107. endif()
  108. try_compile(${VAR}
  109. ${CMAKE_BINARY_DIR}
  110. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.${_SRC_EXT}
  111. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  112. ${CHECK_Fortran_SOURCE_COMPILES_ADD_LINK_OPTIONS}
  113. ${CHECK_Fortran_SOURCE_COMPILES_ADD_LIBRARIES}
  114. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  115. "${CHECK_Fortran_SOURCE_COMPILES_ADD_INCLUDES}"
  116. OUTPUT_VARIABLE OUTPUT)
  117. foreach(_regex ${_FAIL_REGEX})
  118. if("${OUTPUT}" MATCHES "${_regex}")
  119. set(${VAR} 0)
  120. endif()
  121. endforeach()
  122. if(${VAR})
  123. set(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
  124. if(NOT CMAKE_REQUIRED_QUIET)
  125. message(STATUS "Performing Test ${VAR} - Success")
  126. endif()
  127. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  128. "Performing Fortran SOURCE FILE Test ${VAR} succeeded with the following output:\n"
  129. "${OUTPUT}\n"
  130. "Source file was:\n${SOURCE}\n")
  131. else()
  132. if(NOT CMAKE_REQUIRED_QUIET)
  133. message(STATUS "Performing Test ${VAR} - Failed")
  134. endif()
  135. set(${VAR} "" CACHE INTERNAL "Test ${VAR}")
  136. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  137. "Performing Fortran SOURCE FILE Test ${VAR} failed with the following output:\n"
  138. "${OUTPUT}\n"
  139. "Source file was:\n${SOURCE}\n")
  140. endif()
  141. endif()
  142. endmacro()