CMakeDetermineFortranCompiler.cmake 9.7 KB

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