CheckFortranFunctionExists.cmake 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. file(WRITE
  48. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompiler.f
  49. "
  50. program TESTFortran
  51. external ${FUNCTION}
  52. call ${FUNCTION}()
  53. end program TESTFortran
  54. "
  55. )
  56. try_compile(${VARIABLE}
  57. ${CMAKE_BINARY_DIR}
  58. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompiler.f
  59. ${CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS}
  60. ${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}
  61. OUTPUT_VARIABLE OUTPUT
  62. )
  63. if(${VARIABLE})
  64. set(${VARIABLE} 1 CACHE INTERNAL "Have Fortran function ${FUNCTION}")
  65. message(CHECK_PASS "found")
  66. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  67. "Determining if the Fortran ${FUNCTION} exists passed with the following output:\n"
  68. "${OUTPUT}\n\n")
  69. else()
  70. message(CHECK_FAIL "not found")
  71. set(${VARIABLE} "" CACHE INTERNAL "Have Fortran function ${FUNCTION}")
  72. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  73. "Determining if the Fortran ${FUNCTION} exists failed with the following output:\n"
  74. "${OUTPUT}\n\n")
  75. endif()
  76. endif()
  77. endmacro()