CMakeDetermineFortranCompiler.cmake 12 KB

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