CMakeDetermineFortranCompiler.cmake 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. else()
  30. if(NOT CMAKE_Fortran_COMPILER)
  31. # prefer the environment variable CC
  32. if($ENV{FC} MATCHES ".+")
  33. get_filename_component(CMAKE_Fortran_COMPILER_INIT $ENV{FC} PROGRAM PROGRAM_ARGS CMAKE_Fortran_FLAGS_ENV_INIT)
  34. if(CMAKE_Fortran_FLAGS_ENV_INIT)
  35. set(CMAKE_Fortran_COMPILER_ARG1 "${CMAKE_Fortran_FLAGS_ENV_INIT}" CACHE STRING "First argument to Fortran compiler")
  36. endif()
  37. if(EXISTS ${CMAKE_Fortran_COMPILER_INIT})
  38. else()
  39. message(FATAL_ERROR "Could not find compiler set in environment variable FC:\n$ENV{FC}.")
  40. endif()
  41. endif()
  42. # next try prefer the compiler specified by the generator
  43. if(CMAKE_GENERATOR_FC)
  44. if(NOT CMAKE_Fortran_COMPILER_INIT)
  45. set(CMAKE_Fortran_COMPILER_INIT ${CMAKE_GENERATOR_FC})
  46. endif()
  47. endif()
  48. # finally list compilers to try
  49. if(NOT CMAKE_Fortran_COMPILER_INIT)
  50. # Known compilers:
  51. # f77/f90/f95: generic compiler names
  52. # g77: GNU Fortran 77 compiler
  53. # gfortran: putative GNU Fortran 95+ compiler (in progress)
  54. # fort77: native F77 compiler under HP-UX (and some older Crays)
  55. # frt: Fujitsu F77 compiler
  56. # pathf90/pathf95/pathf2003: PathScale Fortran compiler
  57. # pgf77/pgf90/pgf95/pgfortran: Portland Group F77/F90/F95 compilers
  58. # xlf/xlf90/xlf95: IBM (AIX) F77/F90/F95 compilers
  59. # lf95: Lahey-Fujitsu F95 compiler
  60. # fl32: Microsoft Fortran 77 "PowerStation" compiler
  61. # af77: Apogee F77 compiler for Intergraph hardware running CLIX
  62. # epcf90: "Edinburgh Portable Compiler" F90
  63. # fort: Compaq (now HP) Fortran 90/95 compiler for Tru64 and Linux/Alpha
  64. # ifc: Intel Fortran 95 compiler for Linux/x86
  65. # efc: Intel Fortran 95 compiler for IA64
  66. #
  67. # The order is 95 or newer compilers first, then 90,
  68. # then 77 or older compilers, gnu is always last in the group,
  69. # so if you paid for a compiler it is picked by default.
  70. set(CMAKE_Fortran_COMPILER_LIST
  71. ifort ifc af95 af90 efc f95 pathf2003 pathf95 pgf95 pgfortran lf95 xlf95
  72. fort gfortran gfortran-4 g95 f90 pathf90 pgf90 xlf90 epcf90 fort77
  73. frt pgf77 xlf fl32 af77 g77 f77
  74. )
  75. # Vendor-specific compiler names.
  76. set(_Fortran_COMPILER_NAMES_GNU gfortran gfortran-4 g95 g77)
  77. set(_Fortran_COMPILER_NAMES_Intel ifort ifc efc)
  78. set(_Fortran_COMPILER_NAMES_Absoft af95 af90 af77)
  79. set(_Fortran_COMPILER_NAMES_PGI pgf95 pgfortran pgf90 pgf77)
  80. set(_Fortran_COMPILER_NAMES_PathScale pathf2003 pathf95 pathf90)
  81. set(_Fortran_COMPILER_NAMES_XL xlf)
  82. set(_Fortran_COMPILER_NAMES_VisualAge xlf95 xlf90 xlf)
  83. endif()
  84. _cmake_find_compiler(Fortran)
  85. else()
  86. # we only get here if CMAKE_Fortran_COMPILER was specified using -D or a pre-made CMakeCache.txt
  87. # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
  88. # if CMAKE_Fortran_COMPILER is a list of length 2, use the first item as
  89. # CMAKE_Fortran_COMPILER and the 2nd one as CMAKE_Fortran_COMPILER_ARG1
  90. list(LENGTH CMAKE_Fortran_COMPILER _CMAKE_Fortran_COMPILER_LIST_LENGTH)
  91. if("${_CMAKE_Fortran_COMPILER_LIST_LENGTH}" EQUAL 2)
  92. list(GET CMAKE_Fortran_COMPILER 1 CMAKE_Fortran_COMPILER_ARG1)
  93. list(GET CMAKE_Fortran_COMPILER 0 CMAKE_Fortran_COMPILER)
  94. endif()
  95. # if a compiler was specified by the user but without path,
  96. # now try to find it with the full path
  97. # if it is found, force it into the cache,
  98. # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
  99. # if the C compiler already had a path, reuse it for searching the CXX compiler
  100. get_filename_component(_CMAKE_USER_Fortran_COMPILER_PATH "${CMAKE_Fortran_COMPILER}" PATH)
  101. if(NOT _CMAKE_USER_Fortran_COMPILER_PATH)
  102. find_program(CMAKE_Fortran_COMPILER_WITH_PATH NAMES ${CMAKE_Fortran_COMPILER})
  103. mark_as_advanced(CMAKE_Fortran_COMPILER_WITH_PATH)
  104. if(CMAKE_Fortran_COMPILER_WITH_PATH)
  105. set(CMAKE_Fortran_COMPILER ${CMAKE_Fortran_COMPILER_WITH_PATH}
  106. CACHE STRING "Fortran compiler" FORCE)
  107. endif()
  108. endif()
  109. endif()
  110. mark_as_advanced(CMAKE_Fortran_COMPILER)
  111. # Each entry in this list is a set of extra flags to try
  112. # adding to the compile line to see if it helps produce
  113. # a valid identification executable.
  114. set(CMAKE_Fortran_COMPILER_ID_TEST_FLAGS
  115. # Try compiling to an object file only.
  116. "-c"
  117. # Intel on windows does not preprocess by default.
  118. "-fpp"
  119. )
  120. endif()
  121. # Build a small source file to identify the compiler.
  122. if(NOT CMAKE_Fortran_COMPILER_ID_RUN)
  123. set(CMAKE_Fortran_COMPILER_ID_RUN 1)
  124. # Table of per-vendor compiler id flags with expected output.
  125. list(APPEND CMAKE_Fortran_COMPILER_ID_VENDORS Compaq)
  126. set(CMAKE_Fortran_COMPILER_ID_VENDOR_FLAGS_Compaq "-what")
  127. set(CMAKE_Fortran_COMPILER_ID_VENDOR_REGEX_Compaq "Compaq Visual Fortran")
  128. list(APPEND CMAKE_Fortran_COMPILER_ID_VENDORS NAG) # Numerical Algorithms Group
  129. set(CMAKE_Fortran_COMPILER_ID_VENDOR_FLAGS_NAG "-V")
  130. set(CMAKE_Fortran_COMPILER_ID_VENDOR_REGEX_NAG "NAG Fortran Compiler")
  131. # Try to identify the compiler.
  132. set(CMAKE_Fortran_COMPILER_ID)
  133. include(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  134. CMAKE_DETERMINE_COMPILER_ID(Fortran FFLAGS CMakeFortranCompilerId.F)
  135. # Fall back to old is-GNU test.
  136. if(NOT CMAKE_Fortran_COMPILER_ID)
  137. exec_program(${CMAKE_Fortran_COMPILER}
  138. ARGS ${CMAKE_Fortran_COMPILER_ID_FLAGS_LIST} -E "\"${CMAKE_ROOT}/Modules/CMakeTestGNU.c\""
  139. OUTPUT_VARIABLE CMAKE_COMPILER_OUTPUT RETURN_VALUE CMAKE_COMPILER_RETURN)
  140. if(NOT CMAKE_COMPILER_RETURN)
  141. if("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_GNU.*" )
  142. set(CMAKE_Fortran_COMPILER_ID "GNU")
  143. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  144. "Determining if the Fortran compiler is GNU succeeded with "
  145. "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
  146. else()
  147. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  148. "Determining if the Fortran compiler is GNU failed with "
  149. "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
  150. endif()
  151. if(NOT CMAKE_Fortran_PLATFORM_ID)
  152. if("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_MINGW.*" )
  153. set(CMAKE_Fortran_PLATFORM_ID "MinGW")
  154. endif()
  155. if("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_CYGWIN.*" )
  156. set(CMAKE_Fortran_PLATFORM_ID "Cygwin")
  157. endif()
  158. endif()
  159. endif()
  160. endif()
  161. # Set old compiler and platform id variables.
  162. if("${CMAKE_Fortran_COMPILER_ID}" MATCHES "GNU")
  163. set(CMAKE_COMPILER_IS_GNUG77 1)
  164. endif()
  165. if("${CMAKE_Fortran_PLATFORM_ID}" MATCHES "MinGW")
  166. set(CMAKE_COMPILER_IS_MINGW 1)
  167. elseif("${CMAKE_Fortran_PLATFORM_ID}" MATCHES "Cygwin")
  168. set(CMAKE_COMPILER_IS_CYGWIN 1)
  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_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeFortranCompiler.cmake
  179. @ONLY IMMEDIATE # IMMEDIATE must be here for compatibility mode <= 2.0
  180. )
  181. set(CMAKE_Fortran_COMPILER_ENV_VAR "FC")