CheckFortranFunctionExists.cmake 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # - Check if the Fortran function exists.
  2. # CHECK_FORTRAN_FUNCTION_EXISTS(FUNCTION VARIABLE)
  3. # - macro which checks if the Fortran function exists
  4. # FUNCTION - the name of the Fortran function
  5. # VARIABLE - variable to store the result
  6. #
  7. # The following variables may be set before calling this macro to
  8. # modify the way the check is run:
  9. #
  10. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  11. #=============================================================================
  12. # Copyright 2007-2009 Kitware, Inc.
  13. #
  14. # Distributed under the OSI-approved BSD License (the "License");
  15. # see accompanying file Copyright.txt for details.
  16. #
  17. # This software is distributed WITHOUT ANY WARRANTY; without even the
  18. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  19. # See the License for more information.
  20. #=============================================================================
  21. # (To distribute this file outside of CMake, substitute the full
  22. # License text for the above reference.)
  23. include("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
  24. macro(CHECK_FORTRAN_FUNCTION_EXISTS FUNCTION VARIABLE)
  25. if(NOT DEFINED ${VARIABLE})
  26. message(STATUS "Looking for Fortran ${FUNCTION}")
  27. if(CMAKE_REQUIRED_LIBRARIES)
  28. # this one translates potentially used imported library targets to their files on disk
  29. cmake_expand_imported_targets(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
  30. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
  31. "-DLINK_LIBRARIES:STRING=${_ADJUSTED_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. CMAKE_FLAGS "${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()