1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4#[=======================================================================[.rst:
5CheckFortranFunctionExists
6--------------------------
7
8Check if a Fortran function exists.
9
10.. command:: CHECK_FORTRAN_FUNCTION_EXISTS
11
12  .. code-block:: cmake
13
14    CHECK_FORTRAN_FUNCTION_EXISTS(<function> <result>)
15
16  where
17
18  ``<function>``
19    the name of the Fortran function
20  ``<result>``
21    variable to store the result; will be created as an internal cache variable.
22
23.. note::
24
25  This command does not detect functions in Fortran modules. In general it is
26  recommended to use :module:`CheckSourceCompiles` instead to determine if a
27  Fortran function or subroutine is available.
28
29The following variables may be set before calling this macro to modify
30the way the check is run:
31
32``CMAKE_REQUIRED_LINK_OPTIONS``
33  .. versionadded:: 3.14
34    A :ref:`;-list <CMake Language Lists>` of options to add to the link
35    command (see :command:`try_compile` for further details).
36
37``CMAKE_REQUIRED_LIBRARIES``
38  A :ref:`;-list <CMake Language Lists>` of libraries to add to the link
39  command. These can be the name of system libraries or they can be
40  :ref:`Imported Targets <Imported Targets>` (see :command:`try_compile` for
41  further details).
42#]=======================================================================]
43
44include_guard(GLOBAL)
45
46macro(CHECK_FORTRAN_FUNCTION_EXISTS FUNCTION VARIABLE)
47  if(NOT DEFINED ${VARIABLE})
48    message(CHECK_START "Looking for Fortran ${FUNCTION}")
49    if(CMAKE_REQUIRED_LINK_OPTIONS)
50      set(CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS
51        LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
52    else()
53      set(CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS)
54    endif()
55    if(CMAKE_REQUIRED_LIBRARIES)
56      set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
57        LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
58    else()
59      set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
60    endif()
61    file(WRITE
62    ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompiler.f
63    "
64      program TESTFortran
65      external ${FUNCTION}
66      call ${FUNCTION}()
67      end program TESTFortran
68    "
69    )
70    try_compile(${VARIABLE}
71      ${CMAKE_BINARY_DIR}
72      ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompiler.f
73      ${CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS}
74      ${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}
75      OUTPUT_VARIABLE OUTPUT
76    )
77    if(${VARIABLE})
78      set(${VARIABLE} 1 CACHE INTERNAL "Have Fortran function ${FUNCTION}")
79      message(CHECK_PASS "found")
80      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
81        "Determining if the Fortran ${FUNCTION} exists passed with the following output:\n"
82        "${OUTPUT}\n\n")
83    else()
84      message(CHECK_FAIL "not found")
85      set(${VARIABLE} "" CACHE INTERNAL "Have Fortran function ${FUNCTION}")
86      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
87        "Determining if the Fortran ${FUNCTION} exists failed with the following output:\n"
88        "${OUTPUT}\n\n")
89    endif()
90  endif()
91endmacro()
92