CMakeTestFortranCompiler.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. if(CMAKE_Fortran_COMPILER_FORCED)
  4. # The compiler configuration was forced by the user.
  5. # Assume the user has configured all compiler information.
  6. set(CMAKE_Fortran_COMPILER_WORKS TRUE)
  7. return()
  8. endif()
  9. include(CMakeTestCompilerCommon)
  10. # Remove any cached result from an older CMake version.
  11. # We now store this in CMakeFortranCompiler.cmake.
  12. unset(CMAKE_Fortran_COMPILER_WORKS CACHE)
  13. # Try to identify the ABI and configure it into CMakeFortranCompiler.cmake
  14. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerABI.cmake)
  15. CMAKE_DETERMINE_COMPILER_ABI(Fortran ${CMAKE_ROOT}/Modules/CMakeFortranCompilerABI.F)
  16. if(CMAKE_Fortran_ABI_COMPILED)
  17. # The compiler worked so skip dedicated test below.
  18. set(CMAKE_Fortran_COMPILER_WORKS TRUE)
  19. message(STATUS "Check for working Fortran compiler: ${CMAKE_Fortran_COMPILER} - skipped")
  20. endif()
  21. # This file is used by EnableLanguage in cmGlobalGenerator to
  22. # determine that the selected Fortran compiler can actually compile
  23. # and link the most basic of programs. If not, a fatal error
  24. # is set and cmake stops processing commands and will not generate
  25. # any makefiles or projects.
  26. if(NOT CMAKE_Fortran_COMPILER_WORKS)
  27. PrintTestCompilerStatus("Fortran")
  28. file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompiler.f "
  29. PROGRAM TESTFortran
  30. PRINT *, 'Hello'
  31. END
  32. ")
  33. # Clear result from normal variable.
  34. unset(CMAKE_Fortran_COMPILER_WORKS)
  35. # Puts test result in cache variable.
  36. try_compile(CMAKE_Fortran_COMPILER_WORKS ${CMAKE_BINARY_DIR}
  37. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompiler.f
  38. OUTPUT_VARIABLE OUTPUT)
  39. # Move result from cache to normal variable.
  40. set(CMAKE_Fortran_COMPILER_WORKS ${CMAKE_Fortran_COMPILER_WORKS})
  41. unset(CMAKE_Fortran_COMPILER_WORKS CACHE)
  42. if(NOT CMAKE_Fortran_COMPILER_WORKS)
  43. PrintTestCompilerResult(CHECK_FAIL "broken")
  44. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  45. "Determining if the Fortran compiler works failed with "
  46. "the following output:\n${OUTPUT}\n\n")
  47. string(REPLACE "\n" "\n " _output "${OUTPUT}")
  48. message(FATAL_ERROR "The Fortran compiler\n \"${CMAKE_Fortran_COMPILER}\"\n"
  49. "is not able to compile a simple test program.\nIt fails "
  50. "with the following output:\n ${_output}\n\n"
  51. "CMake will not be able to correctly generate this project.")
  52. endif()
  53. PrintTestCompilerResult(CHECK_PASS "works")
  54. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  55. "Determining if the Fortran compiler works passed with "
  56. "the following output:\n${OUTPUT}\n\n")
  57. endif()
  58. # Test for Fortran 90 support by using an f90-specific construct.
  59. if(NOT DEFINED CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  60. message(CHECK_START "Checking whether ${CMAKE_Fortran_COMPILER} supports Fortran 90")
  61. file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompilerF90.f90 "
  62. PROGRAM TESTFortran90
  63. integer stop ; stop = 1 ; do while ( stop .eq. 0 ) ; end do
  64. END PROGRAM TESTFortran90
  65. ")
  66. try_compile(CMAKE_Fortran_COMPILER_SUPPORTS_F90 ${CMAKE_BINARY_DIR}
  67. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompilerF90.f90
  68. OUTPUT_VARIABLE OUTPUT)
  69. if(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  70. message(CHECK_PASS "yes")
  71. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  72. "Determining if the Fortran compiler supports Fortran 90 passed with "
  73. "the following output:\n${OUTPUT}\n\n")
  74. set(CMAKE_Fortran_COMPILER_SUPPORTS_F90 1)
  75. else()
  76. message(CHECK_FAIL "no")
  77. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  78. "Determining if the Fortran compiler supports Fortran 90 failed with "
  79. "the following output:\n${OUTPUT}\n\n")
  80. set(CMAKE_Fortran_COMPILER_SUPPORTS_F90 0)
  81. endif()
  82. unset(CMAKE_Fortran_COMPILER_SUPPORTS_F90 CACHE)
  83. endif()
  84. # Re-configure to save learned information.
  85. configure_file(
  86. ${CMAKE_ROOT}/Modules/CMakeFortranCompiler.cmake.in
  87. ${CMAKE_PLATFORM_INFO_DIR}/CMakeFortranCompiler.cmake
  88. @ONLY
  89. )
  90. include(${CMAKE_PLATFORM_INFO_DIR}/CMakeFortranCompiler.cmake)
  91. if(CMAKE_Fortran_SIZEOF_DATA_PTR)
  92. foreach(f ${CMAKE_Fortran_ABI_FILES})
  93. include(${f})
  94. endforeach()
  95. unset(CMAKE_Fortran_ABI_FILES)
  96. endif()