CMakeDetermineFortranCompiler.cmake 9.2 KB

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