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# 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
11
12include(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
13include(Platform/${CMAKE_SYSTEM_NAME}-Determine-Fortran OPTIONAL)
14include(Platform/${CMAKE_SYSTEM_NAME}-Fortran OPTIONAL)
15if(NOT CMAKE_Fortran_COMPILER_NAMES)
16  set(CMAKE_Fortran_COMPILER_NAMES f95)
17endif()
18
19if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
20elseif("${CMAKE_GENERATOR}" MATCHES "Xcode")
21  set(CMAKE_Fortran_COMPILER_XCODE_TYPE sourcecode.fortran.f90)
22  _cmake_find_compiler_path(Fortran)
23else()
24  if(NOT CMAKE_Fortran_COMPILER)
25    # prefer the environment variable CC
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()
36
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()
43
44    # finally list compilers to try
45    if(NOT CMAKE_Fortran_COMPILER_INIT)
46      # Known compilers:
47      #  f77/f90/f95: generic compiler names
48      #  ftn: Cray fortran compiler wrapper
49      #  g77: GNU Fortran 77 compiler
50      #  gfortran: putative GNU Fortran 95+ compiler (in progress)
51      #  fort77: native F77 compiler under HP-UX (and some older Crays)
52      #  frt: Fujitsu F77 compiler
53      #  pathf90/pathf95/pathf2003: PathScale Fortran compiler
54      #  pgf77/pgf90/pgf95/pgfortran: Portland Group F77/F90/F95 compilers
55      #  flang: Flang Fortran compiler
56      #  xlf/xlf90/xlf95: IBM (AIX) F77/F90/F95 compilers
57      #  lf95: Lahey-Fujitsu F95 compiler
58      #  fl32: Microsoft Fortran 77 "PowerStation" compiler
59      #  af77: Apogee F77 compiler for Intergraph hardware running CLIX
60      #  epcf90: "Edinburgh Portable Compiler" F90
61      #  fort: Compaq (now HP) Fortran 90/95 compiler for Tru64 and Linux/Alpha
62      #  ifx: Intel Fortran LLVM-based compiler
63      #  ifort: Intel Classic Fortran compiler
64      #  ifc: Intel Fortran 95 compiler for Linux/x86
65      #  efc: Intel Fortran 95 compiler for IA64
66      #  nagfor: NAG Fortran compiler
67      #
68      #  The order is 95 or newer compilers first, then 90,
69      #  then 77 or older compilers, gnu is always last in the group,
70      #  so if you paid for a compiler it is picked by default.
71      if(CMAKE_HOST_WIN32)
72        set(CMAKE_Fortran_COMPILER_LIST
73          ifort ifx pgf95 pgfortran lf95 fort
74          flang gfortran gfortran-4 g95 f90 pgf90
75          pgf77 g77 f77 nag
76          )
77      else()
78        set(CMAKE_Fortran_COMPILER_LIST
79          ftn
80          ifort ifc ifx efc pgf95 pgfortran lf95 xlf95 fort
81          flang gfortran gfortran-4 g95 f90 pgf90
82          frt pgf77 xlf g77 f77 nag
83          )
84      endif()
85
86      # Vendor-specific compiler names.
87      set(_Fortran_COMPILER_NAMES_GNU       gfortran gfortran-4 g95 g77)
88      set(_Fortran_COMPILER_NAMES_Intel     ifort ifc efc ifx)
89      set(_Fortran_COMPILER_NAMES_Absoft    af95 af90 af77)
90      set(_Fortran_COMPILER_NAMES_PGI       pgf95 pgfortran pgf90 pgf77)
91      set(_Fortran_COMPILER_NAMES_Flang     flang)
92      set(_Fortran_COMPILER_NAMES_PathScale pathf2003 pathf95 pathf90)
93      set(_Fortran_COMPILER_NAMES_XL        xlf)
94      set(_Fortran_COMPILER_NAMES_VisualAge xlf95 xlf90 xlf)
95      set(_Fortran_COMPILER_NAMES_NAG       nagfor)
96    endif()
97
98    _cmake_find_compiler(Fortran)
99
100  else()
101    _cmake_find_compiler_path(Fortran)
102  endif()
103  mark_as_advanced(CMAKE_Fortran_COMPILER)
104
105  # Each entry in this list is a set of extra flags to try
106  # adding to the compile line to see if it helps produce
107  # a valid identification executable.
108  set(CMAKE_Fortran_COMPILER_ID_TEST_FLAGS_FIRST
109    # Get verbose output to help distinguish compilers.
110    "-v"
111    )
112  set(CMAKE_Fortran_COMPILER_ID_TEST_FLAGS
113    # Try compiling to an object file only.
114    "-c"
115
116    # Intel on windows does not preprocess by default.
117    "-fpp"
118    )
119endif()
120
121# Build a small source file to identify the compiler.
122if(NOT CMAKE_Fortran_COMPILER_ID_RUN)
123  set(CMAKE_Fortran_COMPILER_ID_RUN 1)
124
125  # Table of per-vendor compiler output regular expressions.
126  list(APPEND CMAKE_Fortran_COMPILER_ID_MATCH_VENDORS CCur)
127  set(CMAKE_Fortran_COMPILER_ID_MATCH_VENDOR_REGEX_CCur "Concurrent Fortran [0-9]+ Compiler")
128
129  # Table of per-vendor compiler id flags with expected output.
130  list(APPEND CMAKE_Fortran_COMPILER_ID_VENDORS Compaq)
131  set(CMAKE_Fortran_COMPILER_ID_VENDOR_FLAGS_Compaq "-what")
132  set(CMAKE_Fortran_COMPILER_ID_VENDOR_REGEX_Compaq "Compaq Visual Fortran")
133  list(APPEND CMAKE_Fortran_COMPILER_ID_VENDORS NAG) # Numerical Algorithms Group
134  set(CMAKE_Fortran_COMPILER_ID_VENDOR_FLAGS_NAG "-V")
135  set(CMAKE_Fortran_COMPILER_ID_VENDOR_REGEX_NAG "NAG Fortran Compiler")
136
137  # Match the link line from xcodebuild output of the form
138  #  Ld ...
139  #      ...
140  #      /path/to/cc ...CompilerIdFortran/...
141  # to extract the compiler front-end for the language.
142  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\\\"]")
143  set(CMAKE_Fortran_COMPILER_ID_TOOL_MATCH_INDEX 2)
144
145  set(_version_info "")
146  foreach(m MAJOR MINOR PATCH TWEAK)
147    set(_COMP "_${m}")
148    string(APPEND _version_info "
149#if defined(COMPILER_VERSION${_COMP})")
150    foreach(d 1 2 3 4 5 6 7 8)
151      string(APPEND _version_info "
152# undef DEC
153# undef HEX
154# define DEC(n) DEC_${d}(n)
155# define HEX(n) HEX_${d}(n)
156# if COMPILER_VERSION${_COMP} == 0
157        PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[0]'
158# elif COMPILER_VERSION${_COMP} == 1
159        PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[1]'
160# elif COMPILER_VERSION${_COMP} == 2
161        PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[2]'
162# elif COMPILER_VERSION${_COMP} == 3
163        PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[3]'
164# elif COMPILER_VERSION${_COMP} == 4
165        PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[4]'
166# elif COMPILER_VERSION${_COMP} == 5
167        PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[5]'
168# elif COMPILER_VERSION${_COMP} == 6
169        PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[6]'
170# elif COMPILER_VERSION${_COMP} == 7
171        PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[7]'
172# elif COMPILER_VERSION${_COMP} == 8
173        PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[8]'
174# elif COMPILER_VERSION${_COMP} == 9
175        PRINT *, 'INFO:compiler_version${_COMP}_digit_${d}[9]'
176# endif
177")
178    endforeach()
179    string(APPEND _version_info "
180#endif")
181  endforeach()
182  set(CMAKE_Fortran_COMPILER_ID_VERSION_INFO "${_version_info}")
183  unset(_version_info)
184  unset(_COMP)
185
186  # Try to identify the compiler.
187  set(CMAKE_Fortran_COMPILER_ID)
188  include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
189  CMAKE_DETERMINE_COMPILER_ID(Fortran FFLAGS CMakeFortranCompilerId.F)
190
191  _cmake_find_compiler_sysroot(Fortran)
192
193  # Fall back to old is-GNU test.
194  if(NOT CMAKE_Fortran_COMPILER_ID)
195    execute_process(COMMAND ${CMAKE_Fortran_COMPILER} ${CMAKE_Fortran_COMPILER_ID_FLAGS_LIST} -E "${CMAKE_ROOT}/Modules/CMakeTestGNU.c"
196      OUTPUT_VARIABLE CMAKE_COMPILER_OUTPUT RESULT_VARIABLE CMAKE_COMPILER_RETURN)
197    if(NOT CMAKE_COMPILER_RETURN)
198      if(CMAKE_COMPILER_OUTPUT MATCHES "THIS_IS_GNU")
199        set(CMAKE_Fortran_COMPILER_ID "GNU")
200        file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
201          "Determining if the Fortran compiler is GNU succeeded with "
202          "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
203      else()
204        file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
205          "Determining if the Fortran compiler is GNU failed with "
206          "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
207      endif()
208      if(NOT CMAKE_Fortran_PLATFORM_ID)
209        if(CMAKE_COMPILER_OUTPUT MATCHES "THIS_IS_MINGW")
210          set(CMAKE_Fortran_PLATFORM_ID "MinGW")
211        endif()
212        if(CMAKE_COMPILER_OUTPUT MATCHES "THIS_IS_CYGWIN")
213          set(CMAKE_Fortran_PLATFORM_ID "Cygwin")
214        endif()
215      endif()
216    endif()
217  endif()
218
219  # Fall back for GNU MINGW, which is not always detected correctly
220  # (__MINGW32__ is defined for the C language, but perhaps not for Fortran!)
221  if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU" AND NOT CMAKE_Fortran_PLATFORM_ID)
222    execute_process(COMMAND ${CMAKE_Fortran_COMPILER} ${CMAKE_Fortran_COMPILER_ID_FLAGS_LIST} -E "${CMAKE_ROOT}/Modules/CMakeTestGNU.c"
223      OUTPUT_VARIABLE CMAKE_COMPILER_OUTPUT RESULT_VARIABLE CMAKE_COMPILER_RETURN)
224    if(NOT CMAKE_COMPILER_RETURN)
225      if(CMAKE_COMPILER_OUTPUT MATCHES "THIS_IS_MINGW")
226        set(CMAKE_Fortran_PLATFORM_ID "MinGW")
227      endif()
228      if(CMAKE_COMPILER_OUTPUT MATCHES "THIS_IS_CYGWIN")
229        set(CMAKE_Fortran_PLATFORM_ID "Cygwin")
230      endif()
231    endif()
232  endif()
233
234  # Set old compiler and platform id variables.
235  if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
236    set(CMAKE_COMPILER_IS_GNUG77 1)
237  endif()
238endif()
239
240if (NOT _CMAKE_TOOLCHAIN_LOCATION)
241  get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_Fortran_COMPILER}" PATH)
242endif ()
243
244# if we have a fortran cross compiler, they have usually some prefix, like
245# e.g. powerpc-linux-gfortran, arm-elf-gfortran or i586-mingw32msvc-gfortran , optionally
246# with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
247# The other tools of the toolchain usually have the same prefix
248# NAME_WE cannot be used since then this test will fail for names like
249# "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
250# "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
251if (NOT _CMAKE_TOOLCHAIN_PREFIX)
252
253  if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
254    get_filename_component(COMPILER_BASENAME "${CMAKE_Fortran_COMPILER}" NAME)
255    if (COMPILER_BASENAME MATCHES "^(.+-)g?fortran(-[0-9]+\\.[0-9]+\\.[0-9]+)?(\\.exe)?$")
256      set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
257    endif ()
258
259    # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
260    # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
261    if ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
262      set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
263    endif ()
264  endif()
265
266endif ()
267
268set(_CMAKE_PROCESSING_LANGUAGE "Fortran")
269include(CMakeFindBinUtils)
270include(Compiler/${CMAKE_Fortran_COMPILER_ID}-FindBinUtils OPTIONAL)
271unset(_CMAKE_PROCESSING_LANGUAGE)
272
273if(CMAKE_Fortran_XL_CPP)
274  set(_SET_CMAKE_Fortran_XL_CPP
275    "set(CMAKE_Fortran_XL_CPP \"${CMAKE_Fortran_XL_CPP}\")")
276endif()
277
278if(CMAKE_Fortran_COMPILER_SYSROOT)
279  string(CONCAT _SET_CMAKE_Fortran_COMPILER_SYSROOT
280    "set(CMAKE_Fortran_COMPILER_SYSROOT \"${CMAKE_Fortran_COMPILER_SYSROOT}\")\n"
281    "set(CMAKE_COMPILER_SYSROOT \"${CMAKE_Fortran_COMPILER_SYSROOT}\")")
282else()
283  set(_SET_CMAKE_Fortran_COMPILER_SYSROOT "")
284endif()
285
286if(CMAKE_Fortran_COMPILER_ARCHITECTURE_ID)
287  set(_SET_CMAKE_Fortran_COMPILER_ARCHITECTURE_ID
288    "set(CMAKE_Fortran_COMPILER_ARCHITECTURE_ID ${CMAKE_Fortran_COMPILER_ARCHITECTURE_ID})")
289else()
290  set(_SET_CMAKE_Fortran_COMPILER_ARCHITECTURE_ID "")
291endif()
292
293if(MSVC_Fortran_ARCHITECTURE_ID)
294  set(SET_MSVC_Fortran_ARCHITECTURE_ID
295    "set(MSVC_Fortran_ARCHITECTURE_ID ${MSVC_Fortran_ARCHITECTURE_ID})")
296endif()
297# configure variables set in this file for fast reload later on
298configure_file(${CMAKE_ROOT}/Modules/CMakeFortranCompiler.cmake.in
299  ${CMAKE_PLATFORM_INFO_DIR}/CMakeFortranCompiler.cmake
300  @ONLY
301  )
302set(CMAKE_Fortran_COMPILER_ENV_VAR "FC")
303