CheckFortranFunctionExists.cmake 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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(CHECK_START "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. if(${VARIABLE})
  59. set(${VARIABLE} 1 CACHE INTERNAL "Have Fortran function ${FUNCTION}")
  60. message(CHECK_PASS "found")
  61. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  62. "Determining if the Fortran ${FUNCTION} exists passed with the following output:\n"
  63. "${OUTPUT}\n\n")
  64. else()
  65. message(CHECK_FAIL "not found")
  66. set(${VARIABLE} "" CACHE INTERNAL "Have Fortran function ${FUNCTION}")
  67. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  68. "Determining if the Fortran ${FUNCTION} exists failed with the following output:\n"
  69. "${OUTPUT}\n\n")
  70. endif()
  71. endif()
  72. endmacro()