CMakeDetermineFortranCompiler.cmake 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #=============================================================================
  2. # Copyright 2004-2009 Kitware, Inc.
  3. #
  4. # Distributed under the OSI-approved BSD License (the "License");
  5. # see accompanying file Copyright.txt for details.
  6. #
  7. # This software is distributed WITHOUT ANY WARRANTY; without even the
  8. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. # See the License for more information.
  10. #=============================================================================
  11. # (To distribute this file outside of CMake, substitute the full
  12. # License text for the above reference.)
  13. # determine the compiler to use for Fortran programs
  14. # NOTE, a generator may set CMAKE_Fortran_COMPILER before
  15. # loading this file to force a compiler.
  16. # use environment variable FC first if defined by user, next use
  17. # the cmake variable CMAKE_GENERATOR_FC which can be defined by a generator
  18. # as a default compiler
  19. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompiler.cmake)
  20. include(Platform/${CMAKE_SYSTEM_NAME}-Fortran OPTIONAL)
  21. if(NOT CMAKE_Fortran_COMPILER_NAMES)
  22. set(CMAKE_Fortran_COMPILER_NAMES f95)
  23. endif()
  24. if(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  25. set(CMAKE_Fortran_COMPILER_ID_RUN 1)
  26. set(CMAKE_Fortran_PLATFORM_ID "Windows")
  27. set(CMAKE_Fortran_COMPILER_ID "Intel")
  28. set(CMAKE_Fortran_COMPILER "${CMAKE_GENERATOR_FC}")
  29. elseif("${CMAKE_GENERATOR}" MATCHES "Xcode")
  30. set(CMAKE_Fortran_COMPILER_XCODE_TYPE sourcecode.fortran.f90)
  31. else()
  32. if(NOT CMAKE_Fortran_COMPILER)
  33. # prefer the environment variable CC
  34. if($ENV{FC} MATCHES ".+")
  35. get_filename_component(CMAKE_Fortran_COMPILER_INIT $ENV{FC} PROGRAM PROGRAM_ARGS CMAKE_Fortran_FLAGS_ENV_INIT)
  36. if(CMAKE_Fortran_FLAGS_ENV_INIT)
  37. set(CMAKE_Fortran_COMPILER_ARG1 "${CMAKE_Fortran_FLAGS_ENV_INIT}" CACHE STRING "First argument to Fortran compiler")
  38. endif()
  39. if(EXISTS ${CMAKE_Fortran_COMPILER_INIT})
  40. else()
  41. message(FATAL_ERROR "Could not find compiler set in environment variable FC:\n$ENV{FC}.")
  42. endif()
  43. endif()
  44. # next try prefer the compiler specified by the generator
  45. if(CMAKE_GENERATOR_FC)
  46. if(NOT CMAKE_Fortran_COMPILER_INIT)
  47. set(CMAKE_Fortran_COMPILER_INIT ${CMAKE_GENERATOR_FC})
  48. endif()
  49. endif()
  50. # finally list compilers to try
  51. if(NOT CMAKE_Fortran_COMPILER_INIT)
  52. # Known compilers:
  53. # f77/f90/f95: generic compiler names
  54. # g77: GNU Fortran 77 compiler
  55. # gfortran: putative GNU Fortran 95+ compiler (in progress)
  56. # fort77: native F77 compiler under HP-UX (and some older Crays)
  57. # frt: Fujitsu F77 compiler
  58. # pathf90/pathf95/pathf2003: PathScale Fortran compiler
  59. # pgf77/pgf90/pgf95/pgfortran: Portland Group F77/F90/F95 compilers
  60. # xlf/xlf90/xlf95: IBM (AIX) F77/F90/F95 compilers
  61. # lf95: Lahey-Fujitsu F95 compiler
  62. # fl32: Microsoft Fortran 77 "PowerStation" compiler
  63. # af77: Apogee F77 compiler for Intergraph hardware running CLIX
  64. # epcf90: "Edinburgh Portable Compiler" F90
  65. # fort: Compaq (now HP) Fortran 90/95 compiler for Tru64 and Linux/Alpha
  66. # ifc: Intel Fortran 95 compiler for Linux/x86
  67. # efc: Intel Fortran 95 compiler for IA64
  68. #
  69. # The order is 95 or newer compilers first, then 90,
  70. # then 77 or older compilers, gnu is always last in the group,
  71. # so if you paid for a compiler it is picked by default.
  72. set(CMAKE_Fortran_COMPILER_LIST
  73. ifort ifc af95 af90 efc f95 pathf2003 pathf95 pgf95 pgfortran lf95 xlf95
  74. fort gfortran gfortran-4 g95 f90 pathf90 pgf90 xlf90 epcf90 fort77
  75. frt pgf77 xlf fl32 af77 g77 f77
  76. )
  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_PathScale pathf2003 pathf95 pathf90)
  83. set(_Fortran_COMPILER_NAMES_XL xlf)
  84. set(_Fortran_COMPILER_NAMES_VisualAge xlf95 xlf90 xlf)
  85. endif()
  86. _cmake_find_compiler(Fortran)
  87. else()
  88. # we only get here if CMAKE_Fortran_COMPILER was specified using -D or a pre-made CMakeCache.txt
  89. # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
  90. # if CMAKE_Fortran_COMPILER is a list of length 2, use the first item as
  91. # CMAKE_Fortran_COMPILER and the 2nd one as CMAKE_Fortran_COMPILER_ARG1
  92. list(LENGTH CMAKE_Fortran_COMPILER _CMAKE_Fortran_COMPILER_LIST_LENGTH)
  93. if("${_CMAKE_Fortran_COMPILER_LIST_LENGTH}" EQUAL 2)
  94. list(GET CMAKE_Fortran_COMPILER 1 CMAKE_Fortran_COMPILER_ARG1)
  95. list(GET CMAKE_Fortran_COMPILER 0 CMAKE_Fortran_COMPILER)
  96. endif()
  97. # if a compiler was specified by the user but without path,
  98. # now try to find it with the full path
  99. # if it is found, force it into the cache,
  100. # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
  101. # if the C compiler already had a path, reuse it for searching the CXX compiler
  102. get_filename_component(_CMAKE_USER_Fortran_COMPILER_PATH "${CMAKE_Fortran_COMPILER}" PATH)
  103. if(NOT _CMAKE_USER_Fortran_COMPILER_PATH)
  104. find_program(CMAKE_Fortran_COMPILER_WITH_PATH NAMES ${CMAKE_Fortran_COMPILER})
  105. mark_as_advanced(CMAKE_Fortran_COMPILER_WITH_PATH)
  106. if(CMAKE_Fortran_COMPILER_WITH_PATH)
  107. set(CMAKE_Fortran_COMPILER ${CMAKE_Fortran_COMPILER_WITH_PATH}
  108. CACHE STRING "Fortran compiler" FORCE)
  109. endif()
  110. endif()
  111. endif()
  112. mark_as_advanced(CMAKE_Fortran_COMPILER)
  113. # Each entry in this list is a set of extra flags to try
  114. # adding to the compile line to see if it helps produce
  115. # a valid identification executable.
  116. set(CMAKE_Fortran_COMPILER_ID_TEST_FLAGS
  117. # Try compiling to an object file only.
  118. "-c"
  119. # Intel on windows does not preprocess by default.
  120. "-fpp"
  121. )
  122. endif()
  123. # Build a small source file to identify the compiler.
  124. if(NOT CMAKE_Fortran_COMPILER_ID_RUN)
  125. set(CMAKE_Fortran_COMPILER_ID_RUN 1)
  126. # Table of per-vendor compiler id flags with expected output.
  127. list(APPEND CMAKE_Fortran_COMPILER_ID_VENDORS Compaq)
  128. set(CMAKE_Fortran_COMPILER_ID_VENDOR_FLAGS_Compaq "-what")
  129. set(CMAKE_Fortran_COMPILER_ID_VENDOR_REGEX_Compaq "Compaq Visual Fortran")
  130. list(APPEND CMAKE_Fortran_COMPILER_ID_VENDORS NAG) # Numerical Algorithms Group
  131. set(CMAKE_Fortran_COMPILER_ID_VENDOR_FLAGS_NAG "-V")
  132. set(CMAKE_Fortran_COMPILER_ID_VENDOR_REGEX_NAG "NAG Fortran Compiler")
  133. # Try to identify the compiler.
  134. set(CMAKE_Fortran_COMPILER_ID)
  135. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  136. CMAKE_DETERMINE_COMPILER_ID(Fortran FFLAGS CMakeFortranCompilerId.F)
  137. # Fall back to old is-GNU test.
  138. if(NOT CMAKE_Fortran_COMPILER_ID)
  139. exec_program(${CMAKE_Fortran_COMPILER}
  140. ARGS ${CMAKE_Fortran_COMPILER_ID_FLAGS_LIST} -E "\"${CMAKE_ROOT}/Modules/CMakeTestGNU.c\""
  141. OUTPUT_VARIABLE CMAKE_COMPILER_OUTPUT RETURN_VALUE CMAKE_COMPILER_RETURN)
  142. if(NOT CMAKE_COMPILER_RETURN)
  143. if("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_GNU.*" )
  144. set(CMAKE_Fortran_COMPILER_ID "GNU")
  145. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  146. "Determining if the Fortran compiler is GNU succeeded with "
  147. "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
  148. else()
  149. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  150. "Determining if the Fortran compiler is GNU failed with "
  151. "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
  152. endif()
  153. if(NOT CMAKE_Fortran_PLATFORM_ID)
  154. if("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_MINGW.*" )
  155. set(CMAKE_Fortran_PLATFORM_ID "MinGW")
  156. endif()
  157. if("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_CYGWIN.*" )
  158. set(CMAKE_Fortran_PLATFORM_ID "Cygwin")
  159. endif()
  160. endif()
  161. endif()
  162. endif()
  163. # Set old compiler and platform id variables.
  164. if("${CMAKE_Fortran_COMPILER_ID}" MATCHES "GNU")
  165. set(CMAKE_COMPILER_IS_GNUG77 1)
  166. endif()
  167. if("${CMAKE_Fortran_PLATFORM_ID}" MATCHES "MinGW")
  168. set(CMAKE_COMPILER_IS_MINGW 1)
  169. elseif("${CMAKE_Fortran_PLATFORM_ID}" MATCHES "Cygwin")
  170. set(CMAKE_COMPILER_IS_CYGWIN 1)
  171. endif()
  172. endif()
  173. if (NOT _CMAKE_TOOLCHAIN_LOCATION)
  174. get_filename_component(_CMAKE_TOOLCHAIN_LOCATION "${CMAKE_Fortran_COMPILER}" PATH)
  175. endif ()
  176. # if we have a fortran cross compiler, they have usually some prefix, like
  177. # e.g. powerpc-linux-gfortran, arm-elf-gfortran or i586-mingw32msvc-gfortran , optionally
  178. # with a 3-component version number at the end (e.g. arm-eabi-gcc-4.5.2).
  179. # The other tools of the toolchain usually have the same prefix
  180. # NAME_WE cannot be used since then this test will fail for names lile
  181. # "arm-unknown-nto-qnx6.3.0-gcc.exe", where BASENAME would be
  182. # "arm-unknown-nto-qnx6" instead of the correct "arm-unknown-nto-qnx6.3.0-"
  183. if (CMAKE_CROSSCOMPILING
  184. AND "${CMAKE_Fortran_COMPILER_ID}" MATCHES "GNU"
  185. AND NOT _CMAKE_TOOLCHAIN_PREFIX)
  186. get_filename_component(COMPILER_BASENAME "${CMAKE_Fortran_COMPILER}" NAME)
  187. if (COMPILER_BASENAME MATCHES "^(.+-)g?fortran(-[0-9]+\\.[0-9]+\\.[0-9]+)?(\\.exe)?$")
  188. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  189. endif ()
  190. # if "llvm-" is part of the prefix, remove it, since llvm doesn't have its own binutils
  191. # but uses the regular ar, objcopy, etc. (instead of llvm-objcopy etc.)
  192. if ("${_CMAKE_TOOLCHAIN_PREFIX}" MATCHES "(.+-)?llvm-$")
  193. set(_CMAKE_TOOLCHAIN_PREFIX ${CMAKE_MATCH_1})
  194. endif ()
  195. endif ()
  196. include(CMakeFindBinUtils)
  197. if(MSVC_Fortran_ARCHITECTURE_ID)
  198. set(SET_MSVC_Fortran_ARCHITECTURE_ID
  199. "set(MSVC_Fortran_ARCHITECTURE_ID ${MSVC_Fortran_ARCHITECTURE_ID})")
  200. endif()
  201. # configure variables set in this file for fast reload later on
  202. configure_file(${CMAKE_ROOT}/Modules/CMakeFortranCompiler.cmake.in
  203. ${CMAKE_PLATFORM_INFO_DIR}/CMakeFortranCompiler.cmake
  204. @ONLY IMMEDIATE # IMMEDIATE must be here for compatibility mode <= 2.0
  205. )
  206. set(CMAKE_Fortran_COMPILER_ENV_VAR "FC")