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