CMakeTestFortranCompiler.cmake 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. string(REPLACE "\n" "\n " _output "${OUTPUT}")
  53. message(FATAL_ERROR "The Fortran compiler\n \"${CMAKE_Fortran_COMPILER}\"\n"
  54. "is not able to compile a simple test program.\nIt fails "
  55. "with the following output:\n ${_output}\n\n"
  56. "CMake will not be able to correctly generate this project.")
  57. endif()
  58. PrintTestCompilerResult(CHECK_PASS "works")
  59. endif()
  60. # Test for Fortran 90 support by using an f90-specific construct.
  61. if(NOT DEFINED CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  62. message(CHECK_START "Checking whether ${CMAKE_Fortran_COMPILER} supports Fortran 90")
  63. set(__TestCompiler_testFortranCompilerSource "
  64. PROGRAM TESTFortran90
  65. integer stop ; stop = 1 ; do while ( stop .eq. 0 ) ; end do
  66. END PROGRAM TESTFortran90
  67. ")
  68. try_compile(CMAKE_Fortran_COMPILER_SUPPORTS_F90
  69. SOURCE_FROM_VAR testFortranCompilerF90.f90 __TestCompiler_testFortranCompilerF90Source
  70. OUTPUT_VARIABLE OUTPUT)
  71. unset(__TestCompiler_testFortranCompilerF90Source)
  72. if(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  73. message(CHECK_PASS "yes")
  74. set(CMAKE_Fortran_COMPILER_SUPPORTS_F90 1)
  75. else()
  76. message(CHECK_FAIL "no")
  77. set(CMAKE_Fortran_COMPILER_SUPPORTS_F90 0)
  78. endif()
  79. unset(CMAKE_Fortran_COMPILER_SUPPORTS_F90 CACHE)
  80. endif()
  81. # Re-configure to save learned information.
  82. configure_file(
  83. ${CMAKE_ROOT}/Modules/CMakeFortranCompiler.cmake.in
  84. ${CMAKE_PLATFORM_INFO_DIR}/CMakeFortranCompiler.cmake
  85. @ONLY
  86. )
  87. include(${CMAKE_PLATFORM_INFO_DIR}/CMakeFortranCompiler.cmake)
  88. if(CMAKE_Fortran_SIZEOF_DATA_PTR)
  89. foreach(f ${CMAKE_Fortran_ABI_FILES})
  90. include(${f})
  91. endforeach()
  92. unset(CMAKE_Fortran_ABI_FILES)
  93. endif()