CheckFortranFunctionExists.cmake 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. :command:`Macro <macro>` which checks if a Fortran function exists.
  7. .. code-block:: cmake
  8. CHECK_FORTRAN_FUNCTION_EXISTS(<function> <result>)
  9. where
  10. ``<function>``
  11. the name of the Fortran function
  12. ``<result>``
  13. variable to store the result; will be created as an internal cache variable.
  14. The following variables may be set before calling this macro to modify
  15. the way the check is run:
  16. ``CMAKE_REQUIRED_LIBRARIES``
  17. list of libraries to link
  18. #]=======================================================================]
  19. include_guard(GLOBAL)
  20. macro(CHECK_FORTRAN_FUNCTION_EXISTS FUNCTION VARIABLE)
  21. if(NOT DEFINED ${VARIABLE})
  22. message(STATUS "Looking for Fortran ${FUNCTION}")
  23. if(CMAKE_REQUIRED_LIBRARIES)
  24. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
  25. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  26. else()
  27. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
  28. endif()
  29. file(WRITE
  30. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompiler.f
  31. "
  32. program TESTFortran
  33. external ${FUNCTION}
  34. call ${FUNCTION}()
  35. end program TESTFortran
  36. "
  37. )
  38. try_compile(${VARIABLE}
  39. ${CMAKE_BINARY_DIR}
  40. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompiler.f
  41. ${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}
  42. OUTPUT_VARIABLE OUTPUT
  43. )
  44. # message(STATUS "${OUTPUT}")
  45. if(${VARIABLE})
  46. set(${VARIABLE} 1 CACHE INTERNAL "Have Fortran function ${FUNCTION}")
  47. message(STATUS "Looking for Fortran ${FUNCTION} - found")
  48. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  49. "Determining if the Fortran ${FUNCTION} exists passed with the following output:\n"
  50. "${OUTPUT}\n\n")
  51. else()
  52. message(STATUS "Looking for Fortran ${FUNCTION} - not found")
  53. set(${VARIABLE} "" CACHE INTERNAL "Have Fortran function ${FUNCTION}")
  54. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  55. "Determining if the Fortran ${FUNCTION} exists failed with the following output:\n"
  56. "${OUTPUT}\n\n")
  57. endif()
  58. endif()
  59. endmacro()