CMakeDetermineFortranCompiler.cmake 12 KB

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