CheckFortranFunctionExists.cmake 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. CheckFortranFunctionExists
  5. --------------------------
  6. Check if a Fortran function exists.
  7. .. command:: CHECK_FORTRAN_FUNCTION_EXISTS
  8. .. code-block:: cmake
  9. CHECK_FORTRAN_FUNCTION_EXISTS(<function> <result>)
  10. where
  11. ``<function>``
  12. the name of the Fortran function
  13. ``<result>``
  14. variable to store the result; will be created as an internal cache variable.
  15. .. note::
  16. This command does not detect functions in Fortran modules. In general it is
  17. recommended to use :module:`CheckSourceCompiles` instead to determine if a
  18. Fortran function or subroutine is available.
  19. The following variables may be set before calling this macro to modify
  20. the way the check is run:
  21. ``CMAKE_REQUIRED_LINK_OPTIONS``
  22. .. versionadded:: 3.14
  23. A :ref:`;-list <CMake Language Lists>` of options to add to the link
  24. command (see :command:`try_compile` for further details).
  25. ``CMAKE_REQUIRED_LIBRARIES``
  26. A :ref:`;-list <CMake Language Lists>` of libraries to add to the link
  27. command. These can be the name of system libraries or they can be
  28. :ref:`Imported Targets <Imported Targets>` (see :command:`try_compile` for
  29. further details).
  30. #]=======================================================================]
  31. include_guard(GLOBAL)
  32. macro(CHECK_FORTRAN_FUNCTION_EXISTS FUNCTION VARIABLE)
  33. if(NOT DEFINED ${VARIABLE})
  34. message(CHECK_START "Looking for Fortran ${FUNCTION}")
  35. if(CMAKE_REQUIRED_LINK_OPTIONS)
  36. set(CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS
  37. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  38. else()
  39. set(CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS)
  40. endif()
  41. if(CMAKE_REQUIRED_LIBRARIES)
  42. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
  43. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  44. else()
  45. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
  46. endif()
  47. set(__CheckFunction_testFortranCompilerSource
  48. "
  49. program TESTFortran
  50. external ${FUNCTION}
  51. call ${FUNCTION}()
  52. end program TESTFortran
  53. "
  54. )
  55. try_compile(${VARIABLE}
  56. SOURCE_FROM_VAR testFortranCompiler.f __CheckFunction_testFortranCompilerSource
  57. ${CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS}
  58. ${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}
  59. )
  60. unset(__CheckFunction_testFortranCompilerSource)
  61. if(${VARIABLE})
  62. set(${VARIABLE} 1 CACHE INTERNAL "Have Fortran function ${FUNCTION}")
  63. message(CHECK_PASS "found")
  64. else()
  65. message(CHECK_FAIL "not found")
  66. set(${VARIABLE} "" CACHE INTERNAL "Have Fortran function ${FUNCTION}")
  67. endif()
  68. endif()
  69. endmacro()