CMakeTestFortranCompiler.cmake 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.F90)
  16. if(CMAKE_Fortran_ABI_COMPILED)
  17. # The compiler worked so skip dedicated test below.
  18. set(CMAKE_Fortran_COMPILER_WORKS TRUE)
  19. set(CMAKE_Fortran_COMPILER_SUPPORTS_F90 1)
  20. message(STATUS "Check for working Fortran compiler: ${CMAKE_Fortran_COMPILER} - skipped")
  21. else()
  22. cmake_determine_compiler_abi(Fortran ${CMAKE_ROOT}/Modules/CMakeFortranCompilerABI.F)
  23. if(CMAKE_Fortran_ABI_COMPILED)
  24. set(CMAKE_Fortran_COMPILER_WORKS TRUE)
  25. message(STATUS "Check for working Fortran 77 compiler: ${CMAKE_Fortran_COMPILER} - skipped")
  26. endif()
  27. endif()
  28. # This file is used by EnableLanguage in cmGlobalGenerator to
  29. # determine that the selected Fortran compiler can actually compile
  30. # and link the most basic of programs. If not, a fatal error
  31. # is set and cmake stops processing commands and will not generate
  32. # any makefiles or projects.
  33. if(NOT CMAKE_Fortran_COMPILER_WORKS)
  34. PrintTestCompilerStatus("Fortran")
  35. set(__TestCompiler_testFortranCompilerSource "
  36. PROGRAM TESTFortran
  37. PRINT *, 'Hello'
  38. END
  39. ")
  40. # Clear result from normal variable.
  41. unset(CMAKE_Fortran_COMPILER_WORKS)
  42. # Puts test result in cache variable.
  43. try_compile(CMAKE_Fortran_COMPILER_WORKS
  44. SOURCE_FROM_VAR testFortranCompiler.f __TestCompiler_testFortranCompilerSource
  45. OUTPUT_VARIABLE OUTPUT)
  46. unset(__TestCompiler_testFortranCompilerSource)
  47. # Move result from cache to normal variable.
  48. set(CMAKE_Fortran_COMPILER_WORKS ${CMAKE_Fortran_COMPILER_WORKS})
  49. unset(CMAKE_Fortran_COMPILER_WORKS CACHE)
  50. if(NOT CMAKE_Fortran_COMPILER_WORKS)
  51. PrintTestCompilerResult(CHECK_FAIL "broken")
  52. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  53. "Determining if the Fortran compiler works failed with "
  54. "the following output:\n${OUTPUT}\n\n")
  55. string(REPLACE "\n" "\n " _output "${OUTPUT}")
  56. message(FATAL_ERROR "The Fortran compiler\n \"${CMAKE_Fortran_COMPILER}\"\n"
  57. "is not able to compile a simple test program.\nIt fails "
  58. "with the following output:\n ${_output}\n\n"
  59. "CMake will not be able to correctly generate this project.")
  60. endif()
  61. PrintTestCompilerResult(CHECK_PASS "works")
  62. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  63. "Determining if the Fortran compiler works passed with "
  64. "the following output:\n${OUTPUT}\n\n")
  65. endif()
  66. # Test for Fortran 90 support by using an f90-specific construct.
  67. if(NOT DEFINED CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  68. message(CHECK_START "Checking whether ${CMAKE_Fortran_COMPILER} supports Fortran 90")
  69. set(__TestCompiler_testFortranCompilerSource "
  70. PROGRAM TESTFortran90
  71. integer stop ; stop = 1 ; do while ( stop .eq. 0 ) ; end do
  72. END PROGRAM TESTFortran90
  73. ")
  74. try_compile(CMAKE_Fortran_COMPILER_SUPPORTS_F90
  75. SOURCE_FROM_VAR testFortranCompilerF90.f90 __TestCompiler_testFortranCompilerF90Source
  76. OUTPUT_VARIABLE OUTPUT)
  77. unset(__TestCompiler_testFortranCompilerF90Source)
  78. if(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  79. message(CHECK_PASS "yes")
  80. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  81. "Determining if the Fortran compiler supports Fortran 90 passed with "
  82. "the following output:\n${OUTPUT}\n\n")
  83. set(CMAKE_Fortran_COMPILER_SUPPORTS_F90 1)
  84. else()
  85. message(CHECK_FAIL "no")
  86. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  87. "Determining if the Fortran compiler supports Fortran 90 failed with "
  88. "the following output:\n${OUTPUT}\n\n")
  89. set(CMAKE_Fortran_COMPILER_SUPPORTS_F90 0)
  90. endif()
  91. unset(CMAKE_Fortran_COMPILER_SUPPORTS_F90 CACHE)
  92. endif()
  93. # Re-configure to save learned information.
  94. configure_file(
  95. ${CMAKE_ROOT}/Modules/CMakeFortranCompiler.cmake.in
  96. ${CMAKE_PLATFORM_INFO_DIR}/CMakeFortranCompiler.cmake
  97. @ONLY
  98. )
  99. include(${CMAKE_PLATFORM_INFO_DIR}/CMakeFortranCompiler.cmake)
  100. if(CMAKE_Fortran_SIZEOF_DATA_PTR)
  101. foreach(f ${CMAKE_Fortran_ABI_FILES})
  102. include(${f})
  103. endforeach()
  104. unset(CMAKE_Fortran_ABI_FILES)
  105. endif()