CMakeDetermineFortranCompiler.cmake 8.4 KB

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