CMakeDetermineFortranCompiler.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 FC
  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. # ftn: Cray fortran compiler wrapper
  42. # gfortran: putative GNU Fortran 95+ compiler (in progress)
  43. # frt: Fujitsu Fortran compiler
  44. # pathf90/pathf95/pathf2003: PathScale Fortran compiler
  45. # pgfortran: Portland Group Fortran compilers
  46. # nvfortran: NVHPC Fotran compiler
  47. # flang: Flang Fortran compiler
  48. # xlf: IBM (AIX) Fortran compiler
  49. # lf95: Lahey-Fujitsu F95 compiler
  50. # fl32: Microsoft Fortran 77 "PowerStation" compiler
  51. # af77: Apogee F77 compiler for Intergraph hardware running CLIX
  52. # epcf90: "Edinburgh Portable Compiler" F90
  53. # fort: Compaq (now HP) Fortran 90/95 compiler for Tru64 and Linux/Alpha
  54. # ifx: Intel Fortran LLVM-based compiler
  55. # ifort: Intel Classic Fortran compiler
  56. # nagfor: NAG Fortran compiler
  57. #
  58. # GNU is last to be searched,
  59. # so if you paid for a compiler it is picked by default.
  60. set(CMAKE_Fortran_COMPILER_LIST
  61. ftn
  62. ifx ifort nvfortran pgfortran lf95 xlf fort
  63. flang lfortran frt nagfor
  64. gfortran
  65. )
  66. # Vendor-specific compiler names.
  67. set(_Fortran_COMPILER_NAMES_LCC lfortran gfortran)
  68. set(_Fortran_COMPILER_NAMES_GNU gfortran)
  69. set(_Fortran_COMPILER_NAMES_Intel ifort ifc efc ifx)
  70. set(_Fortran_COMPILER_NAMES_Absoft af95 af90 af77)
  71. set(_Fortran_COMPILER_NAMES_PGI pgf95 pgfortran pgf90 pgf77)
  72. set(_Fortran_COMPILER_NAMES_Flang flang)
  73. set(_Fortran_COMPILER_NAMES_LLVMFlang flang)
  74. set(_Fortran_COMPILER_NAMES_PathScale pathf2003 pathf95 pathf90)
  75. set(_Fortran_COMPILER_NAMES_XL xlf)
  76. set(_Fortran_COMPILER_NAMES_VisualAge xlf95 xlf90 xlf)
  77. set(_Fortran_COMPILER_NAMES_NAG nagfor)
  78. set(_Fortran_COMPILER_NAMES_NVHPC nvfortran)
  79. endif()
  80. _cmake_find_compiler(Fortran)
  81. else()
  82. _cmake_find_compiler_path(Fortran)
  83. endif()
  84. mark_as_advanced(CMAKE_Fortran_COMPILER)
  85. # Each entry in this list is a set of extra flags to try
  86. # adding to the compile line to see if it helps produce
  87. # a valid identification executable.
  88. set(CMAKE_Fortran_COMPILER_ID_TEST_FLAGS_FIRST
  89. # Get verbose output to help distinguish compilers.
  90. "-v"
  91. # Try compiling to an object file only, with verbose output.
  92. "-v -c"
  93. )
  94. set(CMAKE_Fortran_COMPILER_ID_TEST_FLAGS
  95. # Try compiling to an object file only.
  96. "-c"
  97. # Intel on windows does not preprocess by default.
  98. "-fpp"
  99. )
  100. endif()
  101. if(CMAKE_Fortran_COMPILER_TARGET)
  102. set(CMAKE_Fortran_COMPILER_ID_TEST_FLAGS_FIRST "-v -c --target=${CMAKE_Fortran_COMPILER_TARGET}")
  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 IN ITEMS MAJOR MINOR PATCH TWEAK)
  126. set(_COMP "_${m}")
  127. string(APPEND _version_info "
  128. #if defined(COMPILER_VERSION${_COMP})")
  129. foreach(d RANGE 1 8)
  130. string(APPEND _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. string(APPEND _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. _cmake_find_compiler_sysroot(Fortran)
  169. # Fall back to old is-GNU test.
  170. if(NOT CMAKE_Fortran_COMPILER_ID)
  171. execute_process(COMMAND ${CMAKE_Fortran_COMPILER} ${CMAKE_Fortran_COMPILER_ID_FLAGS_LIST} -E "${CMAKE_ROOT}/Modules/CMakeTestGNU.c"
  172. OUTPUT_VARIABLE CMAKE_COMPILER_OUTPUT RESULT_VARIABLE CMAKE_COMPILER_RETURN)
  173. if(NOT CMAKE_COMPILER_RETURN)
  174. if(CMAKE_COMPILER_OUTPUT MATCHES "THIS_IS_GNU")
  175. set(CMAKE_Fortran_COMPILER_ID "GNU")
  176. message(CONFIGURE_LOG
  177. "Determining if the Fortran compiler is GNU succeeded with "
  178. "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
  179. else()
  180. message(CONFIGURE_LOG
  181. "Determining if the Fortran compiler is GNU failed with "
  182. "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
  183. endif()
  184. if(NOT CMAKE_Fortran_PLATFORM_ID)
  185. if(CMAKE_COMPILER_OUTPUT MATCHES "THIS_IS_MINGW")
  186. set(CMAKE_Fortran_PLATFORM_ID "MinGW")
  187. endif()
  188. if(CMAKE_COMPILER_OUTPUT MATCHES "THIS_IS_CYGWIN")
  189. set(CMAKE_Fortran_PLATFORM_ID "Cygwin")
  190. endif()
  191. endif()
  192. endif()
  193. endif()
  194. # Fall back for GNU MINGW, which is not always detected correctly
  195. # (__MINGW32__ is defined for the C language, but perhaps not for Fortran!)
  196. if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU" AND NOT CMAKE_Fortran_PLATFORM_ID)
  197. execute_process(COMMAND ${CMAKE_Fortran_COMPILER} ${CMAKE_Fortran_COMPILER_ID_FLAGS_LIST} -E "${CMAKE_ROOT}/Modules/CMakeTestGNU.c"
  198. OUTPUT_VARIABLE CMAKE_COMPILER_OUTPUT RESULT_VARIABLE CMAKE_COMPILER_RETURN)
  199. if(NOT CMAKE_COMPILER_RETURN)
  200. if(CMAKE_COMPILER_OUTPUT MATCHES "THIS_IS_MINGW")
  201. set(CMAKE_Fortran_PLATFORM_ID "MinGW")
  202. endif()
  203. if(CMAKE_COMPILER_OUTPUT MATCHES "THIS_IS_CYGWIN")
  204. set(CMAKE_Fortran_PLATFORM_ID "Cygwin")
  205. endif()
  206. endif()
  207. endif()
  208. # Set old compiler and platform id variables.
  209. if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
  210. set(CMAKE_COMPILER_IS_GNUG77 1)
  211. endif()
  212. endif()
  213. if("${CMAKE_Fortran_COMPILER_ID};${CMAKE_Fortran_SIMULATE_ID}" STREQUAL "LLVMFlang;MSVC")
  214. # With LLVMFlang targeting the MSVC ABI we link using lld-link.
  215. # Detect the implicit link information from the compiler driver
  216. # so we can explicitly pass it to the linker.
  217. include(${CMAKE_ROOT}/Modules/CMakeParseImplicitLinkInfo.cmake)
  218. set(_LLVMFlang_COMMAND "${CMAKE_Fortran_COMPILER}" "-###" ${CMAKE_CURRENT_LIST_DIR}/CMakeFortranCompilerABI.F)
  219. if(CMAKE_Fortran_COMPILER_TARGET)
  220. list(APPEND _LLVMFlang_COMMAND --target=${CMAKE_Fortran_COMPILER_TARGET})
  221. endif()
  222. execute_process(COMMAND ${_LLVMFlang_COMMAND}
  223. OUTPUT_VARIABLE _LLVMFlang_OUTPUT
  224. ERROR_VARIABLE _LLVMFlang_OUTPUT
  225. RESULT_VARIABLE _LLVMFlang_RESULT)
  226. string(JOIN "\" \"" _LLVMFlang_COMMAND ${_LLVMFlang_COMMAND})
  227. message(CONFIGURE_LOG
  228. "Running the Fortran compiler: \"${_LLVMFlang_COMMAND}\"\n"
  229. "${_LLVMFlang_OUTPUT}"
  230. )
  231. if(_LLVMFlang_RESULT EQUAL 0)
  232. cmake_parse_implicit_link_info("${_LLVMFlang_OUTPUT}"
  233. CMAKE_Fortran_IMPLICIT_LINK_LIBRARIES
  234. CMAKE_Fortran_IMPLICIT_LINK_DIRECTORIES
  235. CMAKE_Fortran_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES
  236. log
  237. "${CMAKE_Fortran_IMPLICIT_OBJECT_REGEX}"
  238. LANGUAGE Fortran)
  239. message(CONFIGURE_LOG
  240. "Parsed Fortran implicit link information:\n"
  241. "${log}\n"
  242. )
  243. set(_CMAKE_Fortran_IMPLICIT_LINK_INFORMATION_DETERMINED_EARLY 1)
  244. if("x${CMAKE_Fortran_COMPILER_ARCHITECTURE_ID}" STREQUAL "xARM64")
  245. # FIXME(LLVMFlang): It does not add `-defaultlib:` fields to object
  246. # files to specify link dependencies on its runtime libraries.
  247. # For now, we add them ourselves.
  248. list(APPEND CMAKE_Fortran_IMPLICIT_LINK_LIBRARIES "clang_rt.builtins-aarch64.lib")
  249. endif()
  250. endif()
  251. unset(_LLVMFlang_COMMAND)
  252. unset(_LLVMFlang_OUTPUT)
  253. unset(_LLVMFlang_RESULT)
  254. endif()
  255. if (NOT _CMAKE_TOOLCHAIN_LOCATION)
  256. get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_Fortran_COMPILER}" PATH)
  257. endif ()
  258. # if we have a fortran cross compiler, they have usually some prefix, like
  259. # e.g. powerpc-linux-gfortran, arm-elf-gfortran or i586-mingw32msvc-gfortran , optionally
  260. # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
  261. # The other tools of the toolchain usually have the same prefix
  262. # NAME_WE cannot be used since then this test will fail for names like
  263. # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
  264. # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
  265. if (NOT _CMAKE_TOOLCHAIN_PREFIX)
  266. if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
  267. get_filename_component(COMPILER_BASENAME "${CMAKE_Fortran_COMPILER}" NAME)
  268. if (COMPILER_BASENAME MATCHES "^(.+-)g?fortran(-[0-9]+\\.[0-9]+\\.[0-9]+)?(\\.exe)?$")
  269. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  270. endif ()
  271. # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
  272. # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
  273. if ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
  274. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  275. endif ()
  276. endif()
  277. endif ()
  278. set(_CMAKE_PROCESSING_LANGUAGE "Fortran")
  279. include(CMakeFindBinUtils)
  280. include(Compiler/${CMAKE_Fortran_COMPILER_ID}-FindBinUtils OPTIONAL)
  281. unset(_CMAKE_PROCESSING_LANGUAGE)
  282. if(CMAKE_Fortran_XL_CPP)
  283. set(_SET_CMAKE_Fortran_XL_CPP
  284. "set(CMAKE_Fortran_XL_CPP \"${CMAKE_Fortran_XL_CPP}\")")
  285. endif()
  286. if(CMAKE_Fortran_COMPILER_SYSROOT)
  287. string(CONCAT _SET_CMAKE_Fortran_COMPILER_SYSROOT
  288. "set(CMAKE_Fortran_COMPILER_SYSROOT \"${CMAKE_Fortran_COMPILER_SYSROOT}\")\n"
  289. "set(CMAKE_COMPILER_SYSROOT \"${CMAKE_Fortran_COMPILER_SYSROOT}\")")
  290. else()
  291. set(_SET_CMAKE_Fortran_COMPILER_SYSROOT "")
  292. endif()
  293. if(CMAKE_Fortran_COMPILER_ARCHITECTURE_ID)
  294. set(_SET_CMAKE_Fortran_COMPILER_ARCHITECTURE_ID
  295. "set(CMAKE_Fortran_COMPILER_ARCHITECTURE_ID ${CMAKE_Fortran_COMPILER_ARCHITECTURE_ID})")
  296. else()
  297. set(_SET_CMAKE_Fortran_COMPILER_ARCHITECTURE_ID "")
  298. endif()
  299. if(MSVC_Fortran_ARCHITECTURE_ID)
  300. set(SET_MSVC_Fortran_ARCHITECTURE_ID
  301. "set(MSVC_Fortran_ARCHITECTURE_ID ${MSVC_Fortran_ARCHITECTURE_ID})")
  302. endif()
  303. if(CMAKE_Fortran_COMPILER_ID STREQUAL "NVHPC")
  304. set(CMAKE_Fortran_VENDOR_SOURCE_FILE_EXTENSIONS ";cuf;CUF")
  305. endif()
  306. # configure variables set in this file for fast reload later on
  307. configure_file(${CMAKE_ROOT}/Modules/CMakeFortranCompiler.cmake.in
  308. ${CMAKE_PLATFORM_INFO_DIR}/CMakeFortranCompiler.cmake
  309. @ONLY
  310. )
  311. set(CMAKE_Fortran_COMPILER_ENV_VAR "FC")