From 9455621ec23c3a1e898b94ae54f751533474a10a Mon Sep 17 00:00:00 2001
From: Barry Smith <bsmith@mcs.anl.gov>
Date: Wed, 2 Nov 2016 11:17:13 -0500
Subject: [PATCH] Add support for gfortran to be used with clang (#2192)

1) list gfortran as a fc and f77 compiler that can work with clang
2) allow compatible gfortran to ./spack compiler find with clang by matching version numbers

This is based on the discussions in

https://github.com/LLNL/spack/issues/237
https://github.com/dealii/dealii/wiki/deal.II-in-Spack#mixing-gcc-and-clang-on-osx

This is not a long term solution but something to get us through the next months until the compiler
infrastructure is reworked to allow mixing and matching for C/C++ and Fortran compilers

Funded-by: IDEAS
Project: IDEAS/xSDK
Time: 1.5 hours
---
 lib/spack/env/clang/gfortran       |  1 +
 lib/spack/spack/compilers/clang.py | 31 ++++++++++++++++++++++++++----
 2 files changed, 28 insertions(+), 4 deletions(-)
 create mode 120000 lib/spack/env/clang/gfortran

diff --git a/lib/spack/env/clang/gfortran b/lib/spack/env/clang/gfortran
new file mode 120000
index 0000000000..82c2b8e90a
--- /dev/null
+++ b/lib/spack/env/clang/gfortran
@@ -0,0 +1 @@
+../cc
\ No newline at end of file
diff --git a/lib/spack/spack/compilers/clang.py b/lib/spack/spack/compilers/clang.py
index 34eec4ea7b..da18adcecd 100644
--- a/lib/spack/spack/compilers/clang.py
+++ b/lib/spack/spack/compilers/clang.py
@@ -24,6 +24,7 @@
 ##############################################################################
 import re
 import os
+import sys
 import spack
 import spack.compiler as cpr
 from spack.compiler import *
@@ -41,18 +42,18 @@ class Clang(Compiler):
     cxx_names = ['clang++']
 
     # Subclasses use possible names of Fortran 77 compiler
-    f77_names = []
+    f77_names = ['gfortran']
 
     # Subclasses use possible names of Fortran 90 compiler
-    fc_names = []
+    fc_names = ['gfortran']
 
     # Named wrapper links within spack.build_env_path
     link_paths = {'cc': 'clang/clang',
                   'cxx': 'clang/clang++',
                   # Use default wrappers for fortran, in case provided in
                   # compilers.yaml
-                  'f77': 'f77',
-                  'fc': 'f90'}
+                  'f77': 'clang/gfortran',
+                  'fc': 'clang/gfortran'}
 
     @property
     def is_apple(self):
@@ -121,6 +122,28 @@ def _find_full_path(self, path):
         full_path = xcrun('-f', basename, output=str)
         return full_path.strip()
 
+    @classmethod
+    def fc_version(cls, fc):
+        version = get_compiler_version(
+            fc, '-dumpversion',
+            # older gfortran versions don't have simple dumpversion output.
+            r'(?:GNU Fortran \(GCC\))?(\d+\.\d+(?:\.\d+)?)')
+        # This is horribly ad hoc, we need to map from gcc/gfortran version
+        # to clang version, but there could be multiple clang
+        # versions that work for a single gcc/gfortran version
+        if sys.platform == 'darwin':
+            clangversionfromgcc = {'6.2.0': '8.0.0-apple'}
+        else:
+            clangversionfromgcc = {}
+        if version in clangversionfromgcc:
+            return clangversionfromgcc[version]
+        else:
+            return 'unknown'
+
+    @classmethod
+    def f77_version(cls, f77):
+        return cls.fc_version(f77)
+
     def setup_custom_environment(self, env):
         """Set the DEVELOPER_DIR environment for the Xcode toolchain.
 
-- 
GitLab