CMakeDetermineFortranCompiler.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. IF(NOT CMAKE_Fortran_COMPILER)
  20. # prefer the environment variable CC
  21. IF($ENV{FC} MATCHES ".+")
  22. GET_FILENAME_COMPONENT(CMAKE_Fortran_COMPILER_INIT $ENV{FC} PROGRAM PROGRAM_ARGS CMAKE_Fortran_FLAGS_ENV_INIT)
  23. IF(CMAKE_Fortran_FLAGS_ENV_INIT)
  24. SET(CMAKE_Fortran_COMPILER_ARG1 "${CMAKE_Fortran_FLAGS_ENV_INIT}" CACHE STRING "First argument to Fortran compiler")
  25. ENDIF(CMAKE_Fortran_FLAGS_ENV_INIT)
  26. IF(EXISTS ${CMAKE_Fortran_COMPILER_INIT})
  27. ELSE(EXISTS ${CMAKE_Fortran_COMPILER_INIT})
  28. MESSAGE(FATAL_ERROR "Could not find compiler set in environment variable FC:\n$ENV{FC}.")
  29. ENDIF(EXISTS ${CMAKE_Fortran_COMPILER_INIT})
  30. ENDIF($ENV{FC} MATCHES ".+")
  31. # next try prefer the compiler specified by the generator
  32. IF(CMAKE_GENERATOR_FC)
  33. IF(NOT CMAKE_Fortran_COMPILER_INIT)
  34. SET(CMAKE_Fortran_COMPILER_INIT ${CMAKE_GENERATOR_FC})
  35. ENDIF(NOT CMAKE_Fortran_COMPILER_INIT)
  36. ENDIF(CMAKE_GENERATOR_FC)
  37. # finally list compilers to try
  38. IF(CMAKE_Fortran_COMPILER_INIT)
  39. SET(CMAKE_Fortran_COMPILER_LIST ${CMAKE_Fortran_COMPILER_INIT})
  40. ELSE(CMAKE_Fortran_COMPILER_INIT)
  41. # Known compilers:
  42. # f77/f90/f95: generic compiler names
  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: Portland Group F77/F90/F95 compilers
  49. # xlf/xlf90/xlf95: IBM (AIX) F77/F90/F95 compilers
  50. # lf95: Lahey-Fujitsu F95 compiler
  51. # fl32: Microsoft Fortran 77 "PowerStation" compiler
  52. # af77: Apogee F77 compiler for Intergraph hardware running CLIX
  53. # epcf90: "Edinburgh Portable Compiler" F90
  54. # fort: Compaq (now HP) Fortran 90/95 compiler for Tru64 and Linux/Alpha
  55. # ifc: Intel Fortran 95 compiler for Linux/x86
  56. # efc: Intel Fortran 95 compiler for IA64
  57. #
  58. # The order is 95 or newer compilers first, then 90,
  59. # then 77 or older compilers, gnu is always last in the group,
  60. # so if you paid for a compiler it is picked by default.
  61. SET(CMAKE_Fortran_COMPILER_LIST
  62. ifort ifc efc f95 pathf2003 pathf95 pgf95 lf95 xlf95 fort
  63. gfortran gfortran-4 g95 f90 pathf90 pgf90 xlf90 epcf90 fort77
  64. frt pgf77 xlf fl32 af77 g77 f77
  65. )
  66. # Vendor-specific compiler names.
  67. SET(_Fortran_COMPILER_NAMES_GNU gfortran gfortran-4 g95 g77)
  68. SET(_Fortran_COMPILER_NAMES_Intel ifort ifc efc)
  69. SET(_Fortran_COMPILER_NAMES_PGI pgf95 pgf90 pgf77)
  70. SET(_Fortran_COMPILER_NAMES_PathScale pathf2003 pathf95 pathf90)
  71. SET(_Fortran_COMPILER_NAMES_XL xlf)
  72. SET(_Fortran_COMPILER_NAMES_VisualAge xlf95 xlf90 xlf)
  73. # Prefer vendors matching the C and C++ compilers.
  74. SET(CMAKE_Fortran_COMPILER_LIST
  75. ${_Fortran_COMPILER_NAMES_${CMAKE_C_COMPILER_ID}}
  76. ${_Fortran_COMPILER_NAMES_${CMAKE_CXX_COMPILER_ID}}
  77. ${CMAKE_Fortran_COMPILER_LIST})
  78. LIST(REMOVE_DUPLICATES CMAKE_Fortran_COMPILER_LIST)
  79. ENDIF(CMAKE_Fortran_COMPILER_INIT)
  80. # Look for directories containing the C and C++ compilers.
  81. SET(_Fortran_COMPILER_HINTS)
  82. FOREACH(lang C CXX)
  83. IF(CMAKE_${lang}_COMPILER AND IS_ABSOLUTE "${CMAKE_${lang}_COMPILER}")
  84. GET_FILENAME_COMPONENT(_hint "${CMAKE_${lang}_COMPILER}" PATH)
  85. IF(IS_DIRECTORY "${_hint}")
  86. LIST(APPEND _Fortran_COMPILER_HINTS "${_hint}")
  87. ENDIF()
  88. SET(_hint)
  89. ENDIF()
  90. ENDFOREACH()
  91. # Find the compiler.
  92. IF(_Fortran_COMPILER_HINTS)
  93. # Prefer directories containing C and C++ compilers.
  94. LIST(REMOVE_DUPLICATES _Fortran_COMPILER_HINTS)
  95. FIND_PROGRAM(CMAKE_Fortran_COMPILER
  96. NAMES ${CMAKE_Fortran_COMPILER_LIST}
  97. PATHS ${_Fortran_COMPILER_HINTS}
  98. NO_DEFAULT_PATH
  99. DOC "Fortran compiler")
  100. ENDIF()
  101. FIND_PROGRAM(CMAKE_Fortran_COMPILER NAMES ${CMAKE_Fortran_COMPILER_LIST} DOC "Fortran compiler")
  102. IF(CMAKE_Fortran_COMPILER_INIT AND NOT CMAKE_Fortran_COMPILER)
  103. SET(CMAKE_Fortran_COMPILER "${CMAKE_Fortran_COMPILER_INIT}" CACHE FILEPATH "Fortran compiler" FORCE)
  104. ENDIF(CMAKE_Fortran_COMPILER_INIT AND NOT CMAKE_Fortran_COMPILER)
  105. ELSE(NOT CMAKE_Fortran_COMPILER)
  106. # we only get here if CMAKE_Fortran_COMPILER was specified using -D or a pre-made CMakeCache.txt
  107. # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
  108. # if CMAKE_Fortran_COMPILER is a list of length 2, use the first item as
  109. # CMAKE_Fortran_COMPILER and the 2nd one as CMAKE_Fortran_COMPILER_ARG1
  110. LIST(LENGTH CMAKE_Fortran_COMPILER _CMAKE_Fortran_COMPILER_LIST_LENGTH)
  111. IF("${_CMAKE_Fortran_COMPILER_LIST_LENGTH}" EQUAL 2)
  112. LIST(GET CMAKE_Fortran_COMPILER 1 CMAKE_Fortran_COMPILER_ARG1)
  113. LIST(GET CMAKE_Fortran_COMPILER 0 CMAKE_Fortran_COMPILER)
  114. ENDIF("${_CMAKE_Fortran_COMPILER_LIST_LENGTH}" EQUAL 2)
  115. # if a compiler was specified by the user but without path,
  116. # now try to find it with the full path
  117. # if it is found, force it into the cache,
  118. # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
  119. # if the C compiler already had a path, reuse it for searching the CXX compiler
  120. GET_FILENAME_COMPONENT(_CMAKE_USER_Fortran_COMPILER_PATH "${CMAKE_Fortran_COMPILER}" PATH)
  121. IF(NOT _CMAKE_USER_Fortran_COMPILER_PATH)
  122. FIND_PROGRAM(CMAKE_Fortran_COMPILER_WITH_PATH NAMES ${CMAKE_Fortran_COMPILER})
  123. MARK_AS_ADVANCED(CMAKE_Fortran_COMPILER_WITH_PATH)
  124. IF(CMAKE_Fortran_COMPILER_WITH_PATH)
  125. SET(CMAKE_Fortran_COMPILER ${CMAKE_Fortran_COMPILER_WITH_PATH}
  126. CACHE STRING "Fortran compiler" FORCE)
  127. ENDIF(CMAKE_Fortran_COMPILER_WITH_PATH)
  128. ENDIF(NOT _CMAKE_USER_Fortran_COMPILER_PATH)
  129. ENDIF(NOT CMAKE_Fortran_COMPILER)
  130. MARK_AS_ADVANCED(CMAKE_Fortran_COMPILER)
  131. # Build a small source file to identify the compiler.
  132. IF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  133. SET(CMAKE_Fortran_COMPILER_ID_RUN 1)
  134. SET(CMAKE_Fortran_PLATFORM_ID "Windows")
  135. # TODO: Set the compiler id. It is probably MSVC but
  136. # the user may be using an integrated Intel compiler.
  137. # SET(CMAKE_Fortran_COMPILER_ID "MSVC")
  138. ENDIF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  139. IF(NOT CMAKE_Fortran_COMPILER_ID_RUN)
  140. SET(CMAKE_Fortran_COMPILER_ID_RUN 1)
  141. # Each entry in this list is a set of extra flags to try
  142. # adding to the compile line to see if it helps produce
  143. # a valid identification executable.
  144. SET(CMAKE_Fortran_COMPILER_ID_TEST_FLAGS
  145. # Try compiling to an object file only.
  146. "-c"
  147. # Intel on windows does not preprocess by default.
  148. "-fpp"
  149. )
  150. # Table of per-vendor compiler id flags with expected output.
  151. LIST(APPEND CMAKE_Fortran_COMPILER_ID_VENDORS Compaq)
  152. SET(CMAKE_Fortran_COMPILER_ID_VENDOR_FLAGS_Compaq "-what")
  153. SET(CMAKE_Fortran_COMPILER_ID_VENDOR_REGEX_Compaq "Compaq Visual Fortran")
  154. LIST(APPEND CMAKE_Fortran_COMPILER_ID_VENDORS NAG) # Numerical Algorithms Group
  155. SET(CMAKE_Fortran_COMPILER_ID_VENDOR_FLAGS_NAG "-V")
  156. SET(CMAKE_Fortran_COMPILER_ID_VENDOR_REGEX_NAG "NAG Fortran Compiler")
  157. # Try to identify the compiler.
  158. SET(CMAKE_Fortran_COMPILER_ID)
  159. INCLUDE(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  160. CMAKE_DETERMINE_COMPILER_ID(Fortran FFLAGS CMakeFortranCompilerId.F)
  161. # Fall back to old is-GNU test.
  162. IF(NOT CMAKE_Fortran_COMPILER_ID)
  163. EXEC_PROGRAM(${CMAKE_Fortran_COMPILER}
  164. ARGS ${CMAKE_Fortran_COMPILER_ID_FLAGS_LIST} -E "\"${CMAKE_ROOT}/Modules/CMakeTestGNU.c\""
  165. OUTPUT_VARIABLE CMAKE_COMPILER_OUTPUT RETURN_VALUE CMAKE_COMPILER_RETURN)
  166. IF(NOT CMAKE_COMPILER_RETURN)
  167. IF("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_GNU.*" )
  168. SET(CMAKE_Fortran_COMPILER_ID "GNU")
  169. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  170. "Determining if the Fortran compiler is GNU succeeded with "
  171. "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
  172. ELSE("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_GNU.*" )
  173. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  174. "Determining if the Fortran compiler is GNU failed with "
  175. "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
  176. ENDIF("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_GNU.*" )
  177. IF(NOT CMAKE_Fortran_PLATFORM_ID)
  178. IF("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_MINGW.*" )
  179. SET(CMAKE_Fortran_PLATFORM_ID "MinGW")
  180. ENDIF("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_MINGW.*" )
  181. IF("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_CYGWIN.*" )
  182. SET(CMAKE_Fortran_PLATFORM_ID "Cygwin")
  183. ENDIF("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_CYGWIN.*" )
  184. ENDIF(NOT CMAKE_Fortran_PLATFORM_ID)
  185. ENDIF(NOT CMAKE_COMPILER_RETURN)
  186. ENDIF(NOT CMAKE_Fortran_COMPILER_ID)
  187. # Set old compiler and platform id variables.
  188. IF("${CMAKE_Fortran_COMPILER_ID}" MATCHES "GNU")
  189. SET(CMAKE_COMPILER_IS_GNUG77 1)
  190. ENDIF("${CMAKE_Fortran_COMPILER_ID}" MATCHES "GNU")
  191. IF("${CMAKE_Fortran_PLATFORM_ID}" MATCHES "MinGW")
  192. SET(CMAKE_COMPILER_IS_MINGW 1)
  193. ELSEIF("${CMAKE_Fortran_PLATFORM_ID}" MATCHES "Cygwin")
  194. SET(CMAKE_COMPILER_IS_CYGWIN 1)
  195. ENDIF("${CMAKE_Fortran_PLATFORM_ID}" MATCHES "MinGW")
  196. ENDIF(NOT CMAKE_Fortran_COMPILER_ID_RUN)
  197. INCLUDE(CMakeFindBinUtils)
  198. # configure variables set in this file for fast reload later on
  199. CONFIGURE_FILE(${CMAKE_ROOT}/Modules/CMakeFortranCompiler.cmake.in
  200. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeFortranCompiler.cmake
  201. @ONLY IMMEDIATE # IMMEDIATE must be here for compatibility mode <= 2.0
  202. )
  203. SET(CMAKE_Fortran_COMPILER_ENV_VAR "FC")