CMakeDetermineFortranCompiler.cmake 9.7 KB

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