CMakeDetermineFortranCompiler.cmake 11 KB

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