CMakeTestFortranCompiler.cmake 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompiler.f "
  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 ${CMAKE_BINARY_DIR}
  44. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompiler.f
  45. OUTPUT_VARIABLE OUTPUT)
  46. # Move result from cache to normal variable.
  47. set(CMAKE_Fortran_COMPILER_WORKS ${CMAKE_Fortran_COMPILER_WORKS})
  48. unset(CMAKE_Fortran_COMPILER_WORKS CACHE)
  49. if(NOT CMAKE_Fortran_COMPILER_WORKS)
  50. PrintTestCompilerResult(CHECK_FAIL "broken")
  51. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  52. "Determining if the Fortran compiler works failed with "
  53. "the following output:\n${OUTPUT}\n\n")
  54. string(REPLACE "\n" "\n " _output "${OUTPUT}")
  55. message(FATAL_ERROR "The Fortran compiler\n \"${CMAKE_Fortran_COMPILER}\"\n"
  56. "is not able to compile a simple test program.\nIt fails "
  57. "with the following output:\n ${_output}\n\n"
  58. "CMake will not be able to correctly generate this project.")
  59. endif()
  60. PrintTestCompilerResult(CHECK_PASS "works")
  61. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  62. "Determining if the Fortran compiler works passed with "
  63. "the following output:\n${OUTPUT}\n\n")
  64. endif()
  65. # Test for Fortran 90 support by using an f90-specific construct.
  66. if(NOT DEFINED CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  67. message(CHECK_START "Checking whether ${CMAKE_Fortran_COMPILER} supports Fortran 90")
  68. file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompilerF90.f90 "
  69. PROGRAM TESTFortran90
  70. integer stop ; stop = 1 ; do while ( stop .eq. 0 ) ; end do
  71. END PROGRAM TESTFortran90
  72. ")
  73. try_compile(CMAKE_Fortran_COMPILER_SUPPORTS_F90 ${CMAKE_BINARY_DIR}
  74. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testFortranCompilerF90.f90
  75. OUTPUT_VARIABLE OUTPUT)
  76. if(CMAKE_Fortran_COMPILER_SUPPORTS_F90)
  77. message(CHECK_PASS "yes")
  78. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  79. "Determining if the Fortran compiler supports Fortran 90 passed with "
  80. "the following output:\n${OUTPUT}\n\n")
  81. set(CMAKE_Fortran_COMPILER_SUPPORTS_F90 1)
  82. else()
  83. message(CHECK_FAIL "no")
  84. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  85. "Determining if the Fortran compiler supports Fortran 90 failed with "
  86. "the following output:\n${OUTPUT}\n\n")
  87. set(CMAKE_Fortran_COMPILER_SUPPORTS_F90 0)
  88. endif()
  89. unset(CMAKE_Fortran_COMPILER_SUPPORTS_F90 CACHE)
  90. endif()
  91. # Re-configure to save learned information.
  92. configure_file(
  93. ${CMAKE_ROOT}/Modules/CMakeFortranCompiler.cmake.in
  94. ${CMAKE_PLATFORM_INFO_DIR}/CMakeFortranCompiler.cmake
  95. @ONLY
  96. )
  97. include(${CMAKE_PLATFORM_INFO_DIR}/CMakeFortranCompiler.cmake)
  98. if(CMAKE_Fortran_SIZEOF_DATA_PTR)
  99. foreach(f ${CMAKE_Fortran_ABI_FILES})
  100. include(${f})
  101. endforeach()
  102. unset(CMAKE_Fortran_ABI_FILES)
  103. endif()