Merge topic 'cpack-innosetup-linux'
[kiteware-cmake.git] / Modules / CMakeDetermineFortranCompiler.cmake
blob613b0c4efcdaf701ea84cbe247829eba7b54305c
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
5 # determine the compiler to use for Fortran programs
6 # NOTE, a generator may set CMAKE_Fortran_COMPILER before
7 # loading this file to force a compiler.
8 # use environment variable FC first if defined by user, next use
9 # the cmake variable CMAKE_GENERATOR_FC which can be defined by a generator
10 # as a default compiler
12 include(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
13 include(Platform/${CMAKE_SYSTEM_NAME}-Determine-Fortran OPTIONAL)
14 include(Platform/${CMAKE_SYSTEM_NAME}-Fortran OPTIONAL)
15 if(NOT CMAKE_Fortran_COMPILER_NAMES)
16   set(CMAKE_Fortran_COMPILER_NAMES f95)
17 endif()
19 if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
20 elseif("${CMAKE_GENERATOR}" MATCHES "Xcode")
21   set(CMAKE_Fortran_COMPILER_XCODE_TYPE sourcecode.fortran.f90)
22   _cmake_find_compiler_path(Fortran)
23 else()
24   if(NOT CMAKE_Fortran_COMPILER)
25     # prefer the environment variable FC
26     if(NOT $ENV{FC} STREQUAL "")
27       get_filename_component(CMAKE_Fortran_COMPILER_INIT $ENV{FC} PROGRAM PROGRAM_ARGS CMAKE_Fortran_FLAGS_ENV_INIT)
28       if(CMAKE_Fortran_FLAGS_ENV_INIT)
29         set(CMAKE_Fortran_COMPILER_ARG1 "${CMAKE_Fortran_FLAGS_ENV_INIT}" CACHE STRING "Arguments to Fortran compiler")
30       endif()
31       if(EXISTS ${CMAKE_Fortran_COMPILER_INIT})
32       else()
33         message(FATAL_ERROR "Could not find compiler set in environment variable FC:\n$ENV{FC}.")
34       endif()
35     endif()
37     # next try prefer the compiler specified by the generator
38     if(CMAKE_GENERATOR_FC)
39       if(NOT CMAKE_Fortran_COMPILER_INIT)
40         set(CMAKE_Fortran_COMPILER_INIT ${CMAKE_GENERATOR_FC})
41       endif()
42     endif()
44     # finally list compilers to try
45     if(NOT CMAKE_Fortran_COMPILER_INIT)
46       # Known compilers:
47       #  ftn: Cray fortran compiler wrapper
48       #  gfortran: putative GNU Fortran 95+ compiler (in progress)
49       #  frt: Fujitsu Fortran compiler
50       #  pathf90/pathf95/pathf2003: PathScale Fortran compiler
51       #  pgfortran: Portland Group Fortran compilers
52       #  nvfortran: NVHPC Fotran compiler
53       #  flang: Flang Fortran compiler
54       #  xlf: IBM (AIX) Fortran compiler
55       #  lf95: Lahey-Fujitsu F95 compiler
56       #  fl32: Microsoft Fortran 77 "PowerStation" compiler
57       #  af77: Apogee F77 compiler for Intergraph hardware running CLIX
58       #  epcf90: "Edinburgh Portable Compiler" F90
59       #  fort: Compaq (now HP) Fortran 90/95 compiler for Tru64 and Linux/Alpha
60       #  ifx: Intel Fortran LLVM-based compiler
61       #  ifort: Intel Classic Fortran compiler
62       #  nagfor: NAG Fortran compiler
63       #
64       #  GNU is last to be searched,
65       #  so if you paid for a compiler it is picked by default.
66       set(CMAKE_Fortran_COMPILER_LIST
67         ftn
68         ifx ifort nvfortran pgfortran lf95 xlf fort
69         flang lfortran frt nagfor
70         gfortran
71         )
73       # Vendor-specific compiler names.
74       set(_Fortran_COMPILER_NAMES_LCC       lfortran gfortran)
75       set(_Fortran_COMPILER_NAMES_GNU       gfortran)
76       set(_Fortran_COMPILER_NAMES_Intel     ifort ifc efc ifx)
77       set(_Fortran_COMPILER_NAMES_Absoft    af95 af90 af77)
78       set(_Fortran_COMPILER_NAMES_PGI       pgf95 pgfortran pgf90 pgf77)
79       set(_Fortran_COMPILER_NAMES_Flang     flang)
80       set(_Fortran_COMPILER_NAMES_LLVMFlang flang)
81       set(_Fortran_COMPILER_NAMES_PathScale pathf2003 pathf95 pathf90)
82       set(_Fortran_COMPILER_NAMES_XL        xlf)
83       set(_Fortran_COMPILER_NAMES_VisualAge xlf95 xlf90 xlf)
84       set(_Fortran_COMPILER_NAMES_NAG       nagfor)
85       set(_Fortran_COMPILER_NAMES_NVHPC     nvfortran)
86     endif()
88     _cmake_find_compiler(Fortran)
90   else()
91     _cmake_find_compiler_path(Fortran)
92   endif()
93   mark_as_advanced(CMAKE_Fortran_COMPILER)
95   # Each entry in this list is a set of extra flags to try
96   # adding to the compile line to see if it helps produce
97   # a valid identification executable.
98   set(CMAKE_Fortran_COMPILER_ID_TEST_FLAGS_FIRST
99     # Get verbose output to help distinguish compilers.
100     "-v"
102     # Try compiling to an object file only, with verbose output.
103     "-v -c"
104     )
105   set(CMAKE_Fortran_COMPILER_ID_TEST_FLAGS
106     # Try compiling to an object file only.
107     "-c"
109     # Intel on windows does not preprocess by default.
110     "-fpp"
111     )
112 endif()
114 if(CMAKE_Fortran_COMPILER_TARGET)
115   set(CMAKE_Fortran_COMPILER_ID_TEST_FLAGS_FIRST "-v -c --target=${CMAKE_Fortran_COMPILER_TARGET}")
116 endif()
118 # Build a small source file to identify the compiler.
119 if(NOT CMAKE_Fortran_COMPILER_ID_RUN)
120   set(CMAKE_Fortran_COMPILER_ID_RUN 1)
122   # Table of per-vendor compiler output regular expressions.
123   list(APPEND CMAKE_Fortran_COMPILER_ID_MATCH_VENDORS CCur)
124   set(CMAKE_Fortran_COMPILER_ID_MATCH_VENDOR_REGEX_CCur "Concurrent Fortran [0-9]+ Compiler")
126   # Table of per-vendor compiler id flags with expected output.
127   list(APPEND CMAKE_Fortran_COMPILER_ID_VENDORS Compaq)
128   set(CMAKE_Fortran_COMPILER_ID_VENDOR_FLAGS_Compaq "-what")
129   set(CMAKE_Fortran_COMPILER_ID_VENDOR_REGEX_Compaq "Compaq Visual Fortran")
130   list(APPEND CMAKE_Fortran_COMPILER_ID_VENDORS NAG) # Numerical Algorithms Group
131   set(CMAKE_Fortran_COMPILER_ID_VENDOR_FLAGS_NAG "-V")
132   set(CMAKE_Fortran_COMPILER_ID_VENDOR_REGEX_NAG "NAG Fortran Compiler")
134   # Match the link line from xcodebuild output of the form
135   #  Ld ...
136   #      ...
137   #      /path/to/cc ...CompilerIdFortran/...
138   # to extract the compiler front-end for the language.
139   set(CMAKE_Fortran_COMPILER_ID_TOOL_MATCH_REGEX "\nLd[^\n]*(\n[ \t]+[^\n]*)*\n[ \t]+([^ \t\r\n]+)[^\r\n]*-o[^\r\n]*CompilerIdFortran/(\\./)?(CompilerIdFortran.xctest/)?CompilerIdFortran[ \t\n\\\"]")
140   set(CMAKE_Fortran_COMPILER_ID_TOOL_MATCH_INDEX 2)
142   set(_version_info "")
143   foreach(m IN ITEMS MAJOR MINOR PATCH TWEAK)
144     set(_COMP "_${m}")
145     string(APPEND _version_info "
146 #if defined(COMPILER_VERSION${_COMP})")
147     foreach(d RANGE 1 8)
148       string(APPEND _version_info "
149 # undef DEC
150 # undef HEX
151 # define DEC(n) DEC_${d}(n)
152 # define HEX(n) HEX_${d}(n)
153 # if COMPILER_VERSION${_COMP} == 0
154         PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[0]'
155 # elif COMPILER_VERSION${_COMP} == 1
156         PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[1]'
157 # elif COMPILER_VERSION${_COMP} == 2
158         PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[2]'
159 # elif COMPILER_VERSION${_COMP} == 3
160         PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[3]'
161 # elif COMPILER_VERSION${_COMP} == 4
162         PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[4]'
163 # elif COMPILER_VERSION${_COMP} == 5
164         PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[5]'
165 # elif COMPILER_VERSION${_COMP} == 6
166         PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[6]'
167 # elif COMPILER_VERSION${_COMP} == 7
168         PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[7]'
169 # elif COMPILER_VERSION${_COMP} == 8
170         PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[8]'
171 # elif COMPILER_VERSION${_COMP} == 9
172         PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[9]'
173 # endif
175     endforeach()
176     string(APPEND _version_info "
177 #endif")
178   endforeach()
179   set(CMAKE_Fortran_COMPILER_ID_VERSION_INFO "${_version_info}")
180   unset(_version_info)
181   unset(_COMP)
183   # Try to identify the compiler.
184   set(CMAKE_Fortran_COMPILER_ID)
185   include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
186   CMAKE_DETERMINE_COMPILER_ID(Fortran FFLAGS CMakeFortranCompilerId.F)
188   _cmake_find_compiler_sysroot(Fortran)
190   # Fall back to old is-GNU test.
191   if(NOT CMAKE_Fortran_COMPILER_ID)
192     execute_process(COMMAND ${CMAKE_Fortran_COMPILER} ${CMAKE_Fortran_COMPILER_ID_FLAGS_LIST} -E "${CMAKE_ROOT}/Modules/CMakeTestGNU.c"
193       OUTPUT_VARIABLE CMAKE_COMPILER_OUTPUT RESULT_VARIABLE CMAKE_COMPILER_RETURN)
194     if(NOT CMAKE_COMPILER_RETURN)
195       if(CMAKE_COMPILER_OUTPUT MATCHES "THIS_IS_GNU")
196         set(CMAKE_Fortran_COMPILER_ID "GNU")
197         message(CONFIGURE_LOG
198           "Determining if the Fortran compiler is GNU succeeded with "
199           "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
200       else()
201         message(CONFIGURE_LOG
202           "Determining if the Fortran compiler is GNU failed with "
203           "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
204       endif()
205       if(NOT CMAKE_Fortran_PLATFORM_ID)
206         if(CMAKE_COMPILER_OUTPUT MATCHES "THIS_IS_MINGW")
207           set(CMAKE_Fortran_PLATFORM_ID "MinGW")
208         endif()
209         if(CMAKE_COMPILER_OUTPUT MATCHES "THIS_IS_CYGWIN")
210           set(CMAKE_Fortran_PLATFORM_ID "Cygwin")
211         endif()
212       endif()
213     endif()
214   endif()
216   # Fall back for GNU MINGW, which is not always detected correctly
217   # (__MINGW32__ is defined for the C language, but perhaps not for Fortran!)
218   if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU" AND NOT CMAKE_Fortran_PLATFORM_ID)
219     execute_process(COMMAND ${CMAKE_Fortran_COMPILER} ${CMAKE_Fortran_COMPILER_ID_FLAGS_LIST} -E "${CMAKE_ROOT}/Modules/CMakeTestGNU.c"
220       OUTPUT_VARIABLE CMAKE_COMPILER_OUTPUT RESULT_VARIABLE CMAKE_COMPILER_RETURN)
221     if(NOT CMAKE_COMPILER_RETURN)
222       if(CMAKE_COMPILER_OUTPUT MATCHES "THIS_IS_MINGW")
223         set(CMAKE_Fortran_PLATFORM_ID "MinGW")
224       endif()
225       if(CMAKE_COMPILER_OUTPUT MATCHES "THIS_IS_CYGWIN")
226         set(CMAKE_Fortran_PLATFORM_ID "Cygwin")
227       endif()
228     endif()
229   endif()
231   # Set old compiler and platform id variables.
232   if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
233     set(CMAKE_COMPILER_IS_GNUG77 1)
234   endif()
235 endif()
237 if("${CMAKE_Fortran_COMPILER_ID};${CMAKE_Fortran_SIMULATE_ID}" STREQUAL "LLVMFlang;MSVC")
238   # With LLVMFlang targeting the MSVC ABI we link using lld-link.
239   # Detect the implicit link information from the compiler driver
240   # so we can explicitly pass it to the linker.
241   include(${CMAKE_ROOT}/Modules/CMakeParseImplicitLinkInfo.cmake)
242   set(_LLVMFlang_COMMAND "${CMAKE_Fortran_COMPILER}" "-###" ${CMAKE_CURRENT_LIST_DIR}/CMakeFortranCompilerABI.F)
243   if(CMAKE_Fortran_COMPILER_TARGET)
244     list(APPEND _LLVMFlang_COMMAND --target=${CMAKE_Fortran_COMPILER_TARGET})
245   endif()
246   execute_process(COMMAND ${_LLVMFlang_COMMAND}
247     OUTPUT_VARIABLE _LLVMFlang_OUTPUT
248     ERROR_VARIABLE _LLVMFlang_OUTPUT
249     RESULT_VARIABLE _LLVMFlang_RESULT)
250   string(JOIN "\" \"" _LLVMFlang_COMMAND ${_LLVMFlang_COMMAND})
251   message(CONFIGURE_LOG
252     "Running the Fortran compiler: \"${_LLVMFlang_COMMAND}\"\n"
253     "${_LLVMFlang_OUTPUT}"
254     )
255   if(_LLVMFlang_RESULT EQUAL 0)
256     cmake_parse_implicit_link_info("${_LLVMFlang_OUTPUT}"
257                                    CMAKE_Fortran_IMPLICIT_LINK_LIBRARIES
258                                    CMAKE_Fortran_IMPLICIT_LINK_DIRECTORIES
259                                    CMAKE_Fortran_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES
260                                    log
261                                    "${CMAKE_Fortran_IMPLICIT_OBJECT_REGEX}"
262                                    LANGUAGE Fortran)
263     message(CONFIGURE_LOG
264       "Parsed Fortran implicit link information:\n"
265       "${log}\n"
266       )
267     set(_CMAKE_Fortran_IMPLICIT_LINK_INFORMATION_DETERMINED_EARLY 1)
268     if("x${CMAKE_Fortran_COMPILER_ARCHITECTURE_ID}" STREQUAL "xARM64" AND CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 18.0)
269       # LLVMFlang < 18.0 does not add `-defaultlib:` fields to object
270       # files to specify link dependencies on its runtime libraries.
271       # For now, we add them ourselves.
272       list(APPEND CMAKE_Fortran_IMPLICIT_LINK_LIBRARIES "clang_rt.builtins-aarch64.lib")
273     endif()
274   endif()
275   unset(_LLVMFlang_COMMAND)
276   unset(_LLVMFlang_OUTPUT)
277   unset(_LLVMFlang_RESULT)
278 endif()
280 if (NOT _CMAKE_TOOLCHAIN_LOCATION)
281   get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_Fortran_COMPILER}" PATH)
282 endif ()
284 # if we have a fortran cross compiler, they have usually some prefix, like
285 # e.g. powerpc-linux-gfortran, arm-elf-gfortran or i586-mingw32msvc-gfortran , optionally
286 # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
287 # The other tools of the toolchain usually have the same prefix
288 # NAME_WE cannot be used since then this test will fail for names like
289 # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
290 # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
291 if (NOT _CMAKE_TOOLCHAIN_PREFIX)
293   if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
294     get_filename_component(COMPILER_BASENAME "${CMAKE_Fortran_COMPILER}" NAME)
295     if (COMPILER_BASENAME MATCHES "^(.+-)g?fortran(-[0-9]+\\.[0-9]+\\.[0-9]+)?(\\.exe)?$")
296       set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
297     endif ()
299     # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
300     # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
301     if ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
302       set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
303     endif ()
304   endif()
306 endif ()
308 set(_CMAKE_PROCESSING_LANGUAGE "Fortran")
309 include(CMakeFindBinUtils)
310 include(Compiler/${CMAKE_Fortran_COMPILER_ID}-FindBinUtils OPTIONAL)
311 unset(_CMAKE_PROCESSING_LANGUAGE)
313 if(CMAKE_Fortran_XL_CPP)
314   set(_SET_CMAKE_Fortran_XL_CPP
315     "set(CMAKE_Fortran_XL_CPP \"${CMAKE_Fortran_XL_CPP}\")")
316 endif()
318 if(CMAKE_Fortran_COMPILER_SYSROOT)
319   string(CONCAT _SET_CMAKE_Fortran_COMPILER_SYSROOT
320     "set(CMAKE_Fortran_COMPILER_SYSROOT \"${CMAKE_Fortran_COMPILER_SYSROOT}\")\n"
321     "set(CMAKE_COMPILER_SYSROOT \"${CMAKE_Fortran_COMPILER_SYSROOT}\")")
322 else()
323   set(_SET_CMAKE_Fortran_COMPILER_SYSROOT "")
324 endif()
326 if(CMAKE_Fortran_COMPILER_ARCHITECTURE_ID)
327   set(_SET_CMAKE_Fortran_COMPILER_ARCHITECTURE_ID
328     "set(CMAKE_Fortran_COMPILER_ARCHITECTURE_ID ${CMAKE_Fortran_COMPILER_ARCHITECTURE_ID})")
329 else()
330   set(_SET_CMAKE_Fortran_COMPILER_ARCHITECTURE_ID "")
331 endif()
333 if(MSVC_Fortran_ARCHITECTURE_ID)
334   set(SET_MSVC_Fortran_ARCHITECTURE_ID
335     "set(MSVC_Fortran_ARCHITECTURE_ID ${MSVC_Fortran_ARCHITECTURE_ID})")
336 endif()
337 if(CMAKE_Fortran_COMPILER_ID STREQUAL "NVHPC")
338   set(CMAKE_Fortran_VENDOR_SOURCE_FILE_EXTENSIONS ";cuf;CUF")
339 endif()
340 # configure variables set in this file for fast reload later on
341 configure_file(${CMAKE_ROOT}/Modules/CMakeFortranCompiler.cmake.in
342   ${CMAKE_PLATFORM_INFO_DIR}/CMakeFortranCompiler.cmake
343   @ONLY
344   )
345 set(CMAKE_Fortran_COMPILER_ENV_VAR "FC")