CheckFortranFunctionExists.cmake 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst 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. .. include:: /module/include/CMAKE_REQUIRED_LINK_OPTIONS.rst
  22. .. include:: /module/include/CMAKE_REQUIRED_LIBRARIES.rst
  23. .. include:: /module/include/CMAKE_REQUIRED_LINK_DIRECTORIES.rst
  24. #]=======================================================================]
  25. include_guard(GLOBAL)
  26. macro(CHECK_FORTRAN_FUNCTION_EXISTS FUNCTION VARIABLE)
  27. if(NOT DEFINED ${VARIABLE})
  28. message(CHECK_START "Looking for Fortran ${FUNCTION}")
  29. if(CMAKE_REQUIRED_LINK_OPTIONS)
  30. set(CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS
  31. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  32. else()
  33. set(CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS)
  34. endif()
  35. if(CMAKE_REQUIRED_LIBRARIES)
  36. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
  37. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  38. else()
  39. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
  40. endif()
  41. if(CMAKE_REQUIRED_LINK_DIRECTORIES)
  42. set(_CFFE_LINK_DIRECTORIES
  43. "-DLINK_DIRECTORIES:STRING=${CMAKE_REQUIRED_LINK_DIRECTORIES}")
  44. else()
  45. set(_CFFE_LINK_DIRECTORIES)
  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. CMAKE_FLAGS
  60. "${_CFFE_LINK_DIRECTORIES}"
  61. )
  62. unset(__CheckFunction_testFortranCompilerSource)
  63. unset(_CFFE_LINK_DIRECTORIES)
  64. if(${VARIABLE})
  65. set(${VARIABLE} 1 CACHE INTERNAL "Have Fortran function ${FUNCTION}")
  66. message(CHECK_PASS "found")
  67. else()
  68. message(CHECK_FAIL "not found")
  69. set(${VARIABLE} "" CACHE INTERNAL "Have Fortran function ${FUNCTION}")
  70. endif()
  71. endif()
  72. endmacro()