CMakeDetermineFortranCompiler.cmake 12 KB

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