CheckFortranFunctionExists.cmake 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. The following variables may be set before calling this macro to modify
  16. the way the check is run:
  17. ``CMAKE_REQUIRED_LINK_OPTIONS``
  18. A :ref:`;-list <CMake Language Lists>` of options to add to the link
  19. command (see :command:`try_compile` for further details).
  20. ``CMAKE_REQUIRED_LIBRARIES``
  21. A :ref:`;-list <CMake Language Lists>` of libraries to add to the link
  22. command. These can be the name of system libraries or they can be
  23. :ref:`Imported Targets <Imported Targets>` (see :command:`try_compile` for
  24. further details).
  25. #]=======================================================================]
  26. include_guard(GLOBAL)
  27. macro(CHECK_FORTRAN_FUNCTION_EXISTS FUNCTION VARIABLE)
  28. if(NOT DEFINED ${VARIABLE})
  29. message(STATUS "Looking for Fortran ${FUNCTION}")
  30. if(CMAKE_REQUIRED_LINK_OPTIONS)
  31. set(CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS
  32. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  33. else()
  34. set(CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS)
  35. endif()
  36. if(CMAKE_REQUIRED_LIBRARIES)
  37. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
  38. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  39. else()
  40. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
  41. endif()
  42. file(WRITE
  43. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompiler.f
  44. "
  45. program TESTFortran
  46. external ${FUNCTION}
  47. call ${FUNCTION}()
  48. end program TESTFortran
  49. "
  50. )
  51. try_compile(${VARIABLE}
  52. ${CMAKE_BINARY_DIR}
  53. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompiler.f
  54. ${CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS}
  55. ${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}
  56. OUTPUT_VARIABLE OUTPUT
  57. )
  58. # message(STATUS "${OUTPUT}")
  59. if(${VARIABLE})
  60. set(${VARIABLE} 1 CACHE INTERNAL "Have Fortran function ${FUNCTION}")
  61. message(STATUS "Looking for Fortran ${FUNCTION} - found")
  62. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  63. "Determining if the Fortran ${FUNCTION} exists passed with the following output:\n"
  64. "${OUTPUT}\n\n")
  65. else()
  66. message(STATUS "Looking for Fortran ${FUNCTION} - not found")
  67. set(${VARIABLE} "" CACHE INTERNAL "Have Fortran function ${FUNCTION}")
  68. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  69. "Determining if the Fortran ${FUNCTION} exists failed with the following output:\n"
  70. "${OUTPUT}\n\n")
  71. endif()
  72. endif()
  73. endmacro()