CMakeDetermineFortranCompiler.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 af95 af90 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_Absoft af95 af90 af77)
  70. SET(_Fortran_COMPILER_NAMES_PGI pgf95 pgf90 pgf77)
  71. SET(_Fortran_COMPILER_NAMES_PathScale pathf2003 pathf95 pathf90)
  72. SET(_Fortran_COMPILER_NAMES_XL xlf)
  73. SET(_Fortran_COMPILER_NAMES_VisualAge xlf95 xlf90 xlf)
  74. # Prefer vendors matching the C and C++ compilers.
  75. SET(CMAKE_Fortran_COMPILER_LIST
  76. ${_Fortran_COMPILER_NAMES_${CMAKE_C_COMPILER_ID}}
  77. ${_Fortran_COMPILER_NAMES_${CMAKE_CXX_COMPILER_ID}}
  78. ${CMAKE_Fortran_COMPILER_LIST})
  79. LIST(REMOVE_DUPLICATES CMAKE_Fortran_COMPILER_LIST)
  80. ENDIF(CMAKE_Fortran_COMPILER_INIT)
  81. # Look for directories containing the C and C++ compilers.
  82. SET(_Fortran_COMPILER_HINTS)
  83. FOREACH(lang C CXX)
  84. IF(CMAKE_${lang}_COMPILER AND IS_ABSOLUTE "${CMAKE_${lang}_COMPILER}")
  85. GET_FILENAME_COMPONENT(_hint "${CMAKE_${lang}_COMPILER}" PATH)
  86. IF(IS_DIRECTORY "${_hint}")
  87. LIST(APPEND _Fortran_COMPILER_HINTS "${_hint}")
  88. ENDIF()
  89. SET(_hint)
  90. ENDIF()
  91. ENDFOREACH()
  92. # Find the compiler.
  93. IF(_Fortran_COMPILER_HINTS)
  94. # Prefer directories containing C and C++ compilers.
  95. LIST(REMOVE_DUPLICATES _Fortran_COMPILER_HINTS)
  96. FIND_PROGRAM(CMAKE_Fortran_COMPILER
  97. NAMES ${CMAKE_Fortran_COMPILER_LIST}
  98. PATHS ${_Fortran_COMPILER_HINTS}
  99. NO_DEFAULT_PATH
  100. DOC "Fortran compiler")
  101. ENDIF()
  102. FIND_PROGRAM(CMAKE_Fortran_COMPILER NAMES ${CMAKE_Fortran_COMPILER_LIST} DOC "Fortran compiler")
  103. IF(CMAKE_Fortran_COMPILER_INIT AND NOT CMAKE_Fortran_COMPILER)
  104. SET(CMAKE_Fortran_COMPILER "${CMAKE_Fortran_COMPILER_INIT}" CACHE FILEPATH "Fortran compiler" FORCE)
  105. ENDIF(CMAKE_Fortran_COMPILER_INIT AND NOT CMAKE_Fortran_COMPILER)
  106. ELSE(NOT CMAKE_Fortran_COMPILER)
  107. # we only get here if CMAKE_Fortran_COMPILER was specified using -D or a pre-made CMakeCache.txt
  108. # (e.g. via ctest) or set in CMAKE_TOOLCHAIN_FILE
  109. # if CMAKE_Fortran_COMPILER is a list of length 2, use the first item as
  110. # CMAKE_Fortran_COMPILER and the 2nd one as CMAKE_Fortran_COMPILER_ARG1
  111. LIST(LENGTH CMAKE_Fortran_COMPILER _CMAKE_Fortran_COMPILER_LIST_LENGTH)
  112. IF("${_CMAKE_Fortran_COMPILER_LIST_LENGTH}" EQUAL 2)
  113. LIST(GET CMAKE_Fortran_COMPILER 1 CMAKE_Fortran_COMPILER_ARG1)
  114. LIST(GET CMAKE_Fortran_COMPILER 0 CMAKE_Fortran_COMPILER)
  115. ENDIF("${_CMAKE_Fortran_COMPILER_LIST_LENGTH}" EQUAL 2)
  116. # if a compiler was specified by the user but without path,
  117. # now try to find it with the full path
  118. # if it is found, force it into the cache,
  119. # if not, don't overwrite the setting (which was given by the user) with "NOTFOUND"
  120. # if the C compiler already had a path, reuse it for searching the CXX compiler
  121. GET_FILENAME_COMPONENT(_CMAKE_USER_Fortran_COMPILER_PATH "${CMAKE_Fortran_COMPILER}" PATH)
  122. IF(NOT _CMAKE_USER_Fortran_COMPILER_PATH)
  123. FIND_PROGRAM(CMAKE_Fortran_COMPILER_WITH_PATH NAMES ${CMAKE_Fortran_COMPILER})
  124. MARK_AS_ADVANCED(CMAKE_Fortran_COMPILER_WITH_PATH)
  125. IF(CMAKE_Fortran_COMPILER_WITH_PATH)
  126. SET(CMAKE_Fortran_COMPILER ${CMAKE_Fortran_COMPILER_WITH_PATH}
  127. CACHE STRING "Fortran compiler" FORCE)
  128. ENDIF(CMAKE_Fortran_COMPILER_WITH_PATH)
  129. ENDIF(NOT _CMAKE_USER_Fortran_COMPILER_PATH)
  130. ENDIF(NOT CMAKE_Fortran_COMPILER)
  131. MARK_AS_ADVANCED(CMAKE_Fortran_COMPILER)
  132. # Build a small source file to identify the compiler.
  133. IF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  134. SET(CMAKE_Fortran_COMPILER_ID_RUN 1)
  135. SET(CMAKE_Fortran_PLATFORM_ID "Windows")
  136. # TODO: Set the compiler id. It is probably MSVC but
  137. # the user may be using an integrated Intel compiler.
  138. # SET(CMAKE_Fortran_COMPILER_ID "MSVC")
  139. ENDIF(${CMAKE_GENERATOR} MATCHES "Visual Studio")
  140. IF(NOT CMAKE_Fortran_COMPILER_ID_RUN)
  141. SET(CMAKE_Fortran_COMPILER_ID_RUN 1)
  142. # Each entry in this list is a set of extra flags to try
  143. # adding to the compile line to see if it helps produce
  144. # a valid identification executable.
  145. SET(CMAKE_Fortran_COMPILER_ID_TEST_FLAGS
  146. # Try compiling to an object file only.
  147. "-c"
  148. # Intel on windows does not preprocess by default.
  149. "-fpp"
  150. )
  151. # Table of per-vendor compiler id flags with expected output.
  152. LIST(APPEND CMAKE_Fortran_COMPILER_ID_VENDORS Compaq)
  153. SET(CMAKE_Fortran_COMPILER_ID_VENDOR_FLAGS_Compaq "-what")
  154. SET(CMAKE_Fortran_COMPILER_ID_VENDOR_REGEX_Compaq "Compaq Visual Fortran")
  155. LIST(APPEND CMAKE_Fortran_COMPILER_ID_VENDORS NAG) # Numerical Algorithms Group
  156. SET(CMAKE_Fortran_COMPILER_ID_VENDOR_FLAGS_NAG "-V")
  157. SET(CMAKE_Fortran_COMPILER_ID_VENDOR_REGEX_NAG "NAG Fortran Compiler")
  158. # Try to identify the compiler.
  159. SET(CMAKE_Fortran_COMPILER_ID)
  160. INCLUDE(${CMAKE_ROOT}/Modules/CMakeDetermineCompilerId.cmake)
  161. CMAKE_DETERMINE_COMPILER_ID(Fortran FFLAGS CMakeFortranCompilerId.F)
  162. # Fall back to old is-GNU test.
  163. IF(NOT CMAKE_Fortran_COMPILER_ID)
  164. EXEC_PROGRAM(${CMAKE_Fortran_COMPILER}
  165. ARGS ${CMAKE_Fortran_COMPILER_ID_FLAGS_LIST} -E "\"${CMAKE_ROOT}/Modules/CMakeTestGNU.c\""
  166. OUTPUT_VARIABLE CMAKE_COMPILER_OUTPUT RETURN_VALUE CMAKE_COMPILER_RETURN)
  167. IF(NOT CMAKE_COMPILER_RETURN)
  168. IF("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_GNU.*" )
  169. SET(CMAKE_Fortran_COMPILER_ID "GNU")
  170. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  171. "Determining if the Fortran compiler is GNU succeeded with "
  172. "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
  173. ELSE("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_GNU.*" )
  174. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  175. "Determining if the Fortran compiler is GNU failed with "
  176. "the following output:\n${CMAKE_COMPILER_OUTPUT}\n\n")
  177. ENDIF("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_GNU.*" )
  178. IF(NOT CMAKE_Fortran_PLATFORM_ID)
  179. IF("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_MINGW.*" )
  180. SET(CMAKE_Fortran_PLATFORM_ID "MinGW")
  181. ENDIF("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_MINGW.*" )
  182. IF("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_CYGWIN.*" )
  183. SET(CMAKE_Fortran_PLATFORM_ID "Cygwin")
  184. ENDIF("${CMAKE_COMPILER_OUTPUT}" MATCHES ".*THIS_IS_CYGWIN.*" )
  185. ENDIF(NOT CMAKE_Fortran_PLATFORM_ID)
  186. ENDIF(NOT CMAKE_COMPILER_RETURN)
  187. ENDIF(NOT CMAKE_Fortran_COMPILER_ID)
  188. # Set old compiler and platform id variables.
  189. IF("${CMAKE_Fortran_COMPILER_ID}" MATCHES "GNU")
  190. SET(CMAKE_COMPILER_IS_GNUG77 1)
  191. ENDIF("${CMAKE_Fortran_COMPILER_ID}" MATCHES "GNU")
  192. IF("${CMAKE_Fortran_PLATFORM_ID}" MATCHES "MinGW")
  193. SET(CMAKE_COMPILER_IS_MINGW 1)
  194. ELSEIF("${CMAKE_Fortran_PLATFORM_ID}" MATCHES "Cygwin")
  195. SET(CMAKE_COMPILER_IS_CYGWIN 1)
  196. ENDIF("${CMAKE_Fortran_PLATFORM_ID}" MATCHES "MinGW")
  197. ENDIF(NOT CMAKE_Fortran_COMPILER_ID_RUN)
  198. INCLUDE(CMakeFindBinUtils)
  199. IF(MSVC_Fortran_ARCHITECTURE_ID)
  200. SET(SET_MSVC_Fortran_ARCHITECTURE_ID
  201. "SET(MSVC_Fortran_ARCHITECTURE_ID ${MSVC_Fortran_ARCHITECTURE_ID})")
  202. ENDIF()
  203. # configure variables set in this file for fast reload later on
  204. CONFIGURE_FILE(${CMAKE_ROOT}/Modules/CMakeFortranCompiler.cmake.in
  205. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeFortranCompiler.cmake
  206. @ONLY IMMEDIATE # IMMEDIATE must be here for compatibility mode <= 2.0
  207. )
  208. SET(CMAKE_Fortran_COMPILER_ENV_VAR "FC")