Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Modules / CMakeDetermineFortranCompiler.cmake
1
2 #=============================================================================
3 # Copyright 2004-2009 Kitware, Inc.
4 #
5 # Distributed under the OSI-approved BSD License (the "License");
6 # see accompanying file Copyright.txt for details.
7 #
8 # This software is distributed WITHOUT ANY WARRANTY; without even the
9 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 # See the License for more information.
11 #=============================================================================
12 # (To distribute this file outside of CMake, substitute the full
13 #  License text for the above reference.)
14
15 # determine the compiler to use for Fortran programs
16 # NOTE, a generator may set CMAKE_Fortran_COMPILER before
17 # loading this file to force a compiler.
18 # use environment variable FC first if defined by user, next use
19 # the cmake variable CMAKE_GENERATOR_FC which can be defined by a generator
20 # as a default compiler
21
22 include(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
23 include(Platform/${CMAKE_SYSTEM_NAME}-Fortran OPTIONAL)
24 if(NOT CMAKE_Fortran_COMPILER_NAMES)
25   set(CMAKE_Fortran_COMPILER_NAMES f95)
26 endif()
27
28 if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
29   set(CMAKE_Fortran_COMPILER_ID_RUN 1)
30   set(CMAKE_Fortran_PLATFORM_ID "Windows")
31   set(CMAKE_Fortran_COMPILER_ID "Intel")
32   set(CMAKE_Fortran_COMPILER "${CMAKE_GENERATOR_FC}")
33 elseif("${CMAKE_GENERATOR}" MATCHES "Xcode")
34   set(CMAKE_Fortran_COMPILER_XCODE_TYPE sourcecode.fortran.f90)
35 else()
36   if(NOT CMAKE_Fortran_COMPILER)
37     # prefer the environment variable CC
38     if($ENV{FC} MATCHES ".+")
39       get_filename_component(CMAKE_Fortran_COMPILER_INIT $ENV{FC} PROGRAM PROGRAM_ARGS CMAKE_Fortran_FLAGS_ENV_INIT)
40       if(CMAKE_Fortran_FLAGS_ENV_INIT)
41         set(CMAKE_Fortran_COMPILER_ARG1 "${CMAKE_Fortran_FLAGS_ENV_INIT}" CACHE STRING "First argument to Fortran compiler")
42       endif()
43       if(EXISTS ${CMAKE_Fortran_COMPILER_INIT})
44       else()
45         message(FATAL_ERROR "Could not find compiler set in environment variable FC:\n$ENV{FC}.")
46       endif()
47     endif()
48
49     # next try prefer the compiler specified by the generator
50     if(CMAKE_GENERATOR_FC)
51       if(NOT CMAKE_Fortran_COMPILER_INIT)
52         set(CMAKE_Fortran_COMPILER_INIT ${CMAKE_GENERATOR_FC})
53       endif()
54     endif()
55
56     # finally list compilers to try
57     if(NOT CMAKE_Fortran_COMPILER_INIT)
58       # Known compilers:
59       #  f77/f90/f95: generic compiler names
60       #  g77: GNU Fortran 77 compiler
61       #  gfortran: putative GNU Fortran 95+ compiler (in progress)
62       #  fort77: native F77 compiler under HP-UX (and some older Crays)
63       #  frt: Fujitsu F77 compiler
64       #  pathf90/pathf95/pathf2003: PathScale Fortran compiler
65       #  pgf77/pgf90/pgf95/pgfortran: Portland Group F77/F90/F95 compilers
66       #  xlf/xlf90/xlf95: IBM (AIX) F77/F90/F95 compilers
67       #  lf95: Lahey-Fujitsu F95 compiler
68       #  fl32: Microsoft Fortran 77 "PowerStation" compiler
69       #  af77: Apogee F77 compiler for Intergraph hardware running CLIX
70       #  epcf90: "Edinburgh Portable Compiler" F90
71       #  fort: Compaq (now HP) Fortran 90/95 compiler for Tru64 and Linux/Alpha
72       #  ifc: Intel Fortran 95 compiler for Linux/x86
73       #  efc: Intel Fortran 95 compiler for IA64
74       #
75       #  The order is 95 or newer compilers first, then 90,
76       #  then 77 or older compilers, gnu is always last in the group,
77       #  so if you paid for a compiler it is picked by default.
78       set(CMAKE_Fortran_COMPILER_LIST
79         ifort ifc af95 af90 efc f95 pathf2003 pathf95 pgf95 pgfortran lf95 xlf95
80         fort gfortran gfortran-4 g95 f90 pathf90 pgf90 xlf90 epcf90 fort77
81         frt pgf77 xlf fl32 af77 g77 f77
82         )
83
84       # Vendor-specific compiler names.
85       set(_Fortran_COMPILER_NAMES_GNU       gfortran gfortran-4 g95 g77)
86       set(_Fortran_COMPILER_NAMES_Intel     ifort ifc efc)
87       set(_Fortran_COMPILER_NAMES_Absoft    af95 af90 af77)
88       set(_Fortran_COMPILER_NAMES_PGI       pgf95 pgfortran pgf90 pgf77)
89       set(_Fortran_COMPILER_NAMES_PathScale pathf2003 pathf95 pathf90)
90       set(_Fortran_COMPILER_NAMES_XL        xlf)
91       set(_Fortran_COMPILER_NAMES_VisualAge xlf95 xlf90 xlf)
92     endif()
93
94     _cmake_find_compiler(Fortran)
95
96   else()
97      # we only get here if CMAKE_Fortran_COMPILER was specified using -D or a pre-made CMakeCache.txt
98     # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
99     # if CMAKE_Fortran_COMPILER is a list of length 2, use the first item as
100     # CMAKE_Fortran_COMPILER and the 2nd one as CMAKE_Fortran_COMPILER_ARG1
101
102     list(LENGTH CMAKE_Fortran_COMPILER _CMAKE_Fortran_COMPILER_LIST_LENGTH)
103     if("${_CMAKE_Fortran_COMPILER_LIST_LENGTH}" EQUAL 2)
104       list(GET CMAKE_Fortran_COMPILER 1 CMAKE_Fortran_COMPILER_ARG1)
105       list(GET CMAKE_Fortran_COMPILER 0 CMAKE_Fortran_COMPILER)
106     endif()
107
108     # if a compiler was specified by the user but without path,
109     # now try to find it with the full path
110     # if it is found, force it into the cache,
111     # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
112     # if the C compiler already had a path, reuse it for searching the CXX compiler
113     get_filename_component(_CMAKE_USER_Fortran_COMPILER_PATH "${CMAKE_Fortran_COMPILER}" PATH)
114     if(NOT _CMAKE_USER_Fortran_COMPILER_PATH)
115       find_program(CMAKE_Fortran_COMPILER_WITH_PATH NAMES ${CMAKE_Fortran_COMPILER})
116       mark_as_advanced(CMAKE_Fortran_COMPILER_WITH_PATH)
117       if(CMAKE_Fortran_COMPILER_WITH_PATH)
118         set(CMAKE_Fortran_COMPILER ${CMAKE_Fortran_COMPILER_WITH_PATH}
119           CACHE STRING "Fortran compiler" FORCE)
120       endif()
121     endif()
122   endif()
123   mark_as_advanced(CMAKE_Fortran_COMPILER)
124
125   # Each entry in this list is a set of extra flags to try
126   # adding to the compile line to see if it helps produce
127   # a valid identification executable.
128   set(CMAKE_Fortran_COMPILER_ID_TEST_FLAGS
129     # Try compiling to an object file only.
130     "-c"
131
132     # Intel on windows does not preprocess by default.
133     "-fpp"
134     )
135 endif()
136
137 # Build a small source file to identify the compiler.
138 if(NOT CMAKE_Fortran_COMPILER_ID_RUN)
139   set(CMAKE_Fortran_COMPILER_ID_RUN 1)
140
141   # Table of per-vendor compiler id flags with expected output.
142   list(APPEND CMAKE_Fortran_COMPILER_ID_VENDORS Compaq)
143   set(CMAKE_Fortran_COMPILER_ID_VENDOR_FLAGS_Compaq "-what")
144   set(CMAKE_Fortran_COMPILER_ID_VENDOR_REGEX_Compaq "Compaq Visual Fortran")
145   list(APPEND CMAKE_Fortran_COMPILER_ID_VENDORS NAG) # Numerical Algorithms Group
146   set(CMAKE_Fortran_COMPILER_ID_VENDOR_FLAGS_NAG "-V")
147   set(CMAKE_Fortran_COMPILER_ID_VENDOR_REGEX_NAG "NAG Fortran Compiler")
148
149   # Try to identify the compiler.
150   set(CMAKE_Fortran_COMPILER_ID)
151   include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
152   CMAKE_DETERMINE_COMPILER_ID(Fortran FFLAGS CMakeFortranCompilerId.F)
153
154   # Fall back to old is-GNU test.
155   if(NOT CMAKE_Fortran_COMPILER_ID)
156     exec_program(${CMAKE_Fortran_COMPILER}
157       ARGS ${CMAKE_Fortran_COMPILER_ID_FLAGS_LIST} -E "\"${CMAKE_ROOT}/Modules/CMakeTestGNU.c\""
158       OUTPUT_VARIABLE CMAKE_COMPILER_OUTPUT RETURN_VALUE CMAKE_COMPILER_RETURN)
159     if(NOT CMAKE_COMPILER_RETURN)
160       if("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_GNU.*" )
161         set(CMAKE_Fortran_COMPILER_ID "GNU")
162         file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
163           "Determining if the Fortran compiler is GNU succeeded with "
164           "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
165       else()
166         file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
167           "Determining if the Fortran compiler is GNU failed with "
168           "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
169       endif()
170       if(NOT CMAKE_Fortran_PLATFORM_ID)
171         if("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_MINGW.*" )
172           set(CMAKE_Fortran_PLATFORM_ID "MinGW")
173         endif()
174         if("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_CYGWIN.*" )
175           set(CMAKE_Fortran_PLATFORM_ID "Cygwin")
176         endif()
177       endif()
178     endif()
179   endif()
180
181   # Set old compiler and platform id variables.
182   if("${CMAKE_Fortran_COMPILER_ID}" MATCHES "GNU")
183     set(CMAKE_COMPILER_IS_GNUG77 1)
184   endif()
185   if("${CMAKE_Fortran_PLATFORM_ID}" MATCHES "MinGW")
186     set(CMAKE_COMPILER_IS_MINGW 1)
187   elseif("${CMAKE_Fortran_PLATFORM_ID}" MATCHES "Cygwin")
188     set(CMAKE_COMPILER_IS_CYGWIN 1)
189   endif()
190 endif()
191
192 if (NOT _CMAKE_TOOLCHAIN_LOCATION)
193   get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_Fortran_COMPILER}" PATH)
194 endif ()
195
196 # if we have a fortran cross compiler, they have usually some prefix, like
197 # e.g. powerpc-linux-gfortran, arm-elf-gfortran or i586-mingw32msvc-gfortran , optionally
198 # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
199 # The other tools of the toolchain usually have the same prefix
200 # NAME_WE cannot be used since then this test will fail for names lile
201 # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
202 # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
203 if (CMAKE_CROSSCOMPILING
204     AND "${CMAKE_Fortran_COMPILER_ID}" MATCHES "GNU"
205     AND NOT _CMAKE_TOOLCHAIN_PREFIX)
206   get_filename_component(COMPILER_BASENAME "${CMAKE_Fortran_COMPILER}" NAME)
207   if (COMPILER_BASENAME MATCHES "^(.+-)g?fortran(-[0-9]+\\.[0-9]+\\.[0-9]+)?(\\.exe)?$")
208     set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
209   endif ()
210
211   # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
212   # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
213   if ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
214     set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
215   endif ()
216 endif ()
217
218 include(CMakeFindBinUtils)
219
220 if(MSVC_Fortran_ARCHITECTURE_ID)
221   set(SET_MSVC_Fortran_ARCHITECTURE_ID
222     "set(MSVC_Fortran_ARCHITECTURE_ID ${MSVC_Fortran_ARCHITECTURE_ID})")
223 endif()
224 # configure variables set in this file for fast reload later on
225 configure_file(${CMAKE_ROOT}/Modules/CMakeFortranCompiler.cmake.in
226   ${CMAKE_PLATFORM_INFO_DIR}/CMakeFortranCompiler.cmake
227   @ONLY IMMEDIATE # IMMEDIATE must be here for compatibility mode <= 2.0
228   )
229 set(CMAKE_Fortran_COMPILER_ENV_VAR "FC")