CheckFortranFunctionExists.cmake 2.2 KB

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