CheckFortranSourceCompiles.cmake 6.0 KB

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