CMakeDetermineFortranCompiler.cmake 12 KB

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