Repository: spack/spack

Issue:
Spack does not auto-detect Apple's clang compiler
On OS X (El Capitan), Spack does not auto-detect the default system compiler `/usr/bin/cc`. It should probably generate a configuration entry like this:

``` yaml
    clang@apple-7.0.2:
      cc: /usr/bin/cc
      cxx: /usr/bin/c++
      f77: None
      fc: None
```

Note that Apple uses their own, non-standard version numbers. The compiler describes itself like this:

```
$ /usr/bin/cc --version
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.2.0
Thread model: posix
```

Spack does detect the very outdated gcc version that is also available as `/usr/bin/gcc`; this version is so old (4.2.1) that I cannot recommend using it for anything.



Discussion:
@BarrySmith had the same issue in #237.  I'm going to put some discussion there -- I have some questions about how Fortran should be detected by default.

I like the proposed version notation.  It should be easy to add detection for this to `clang.py`

Out of curiosity, when you run `/usr/bin/cc --version` on the clang compilers, does the output go to STDOUT or STDERR? This may be related to a fix that I just put in place. See #350. If it does indeed print to STDERR, it may be fixed now.

  stdout

> On Jan 21, 2016, at 10:44 AM, Adam J. Stewart notifications@github.com wrote:
> 
> Out of curiosity, when you run /usr/bin/cc --version on the clang compilers, does the output go to STDOUT or STDERR? This may be related to a fix that I just put in place. See #350. If it does indeed print to STDERR, it may be fixed now.
> 
> —
> Reply to this email directly or view it on GitHub.

I believe the Mac OS detection thing is b/c the version output is different on Mac OS X and the regex currently doesn't deal with it.  I just need to add the appropriate regex.

Oh, that's an easy fix. I can make the change in the regex if you want, but I don't have any way of testing it.

I'll submit a PR anyway. I can't imagine how I could break anything.

I'm happy to test it if you do :)


Resolving patch:
diff --git a/lib/spack/spack/compilers/clang.py b/lib/spack/spack/compilers/clang.py
--- a/lib/spack/spack/compilers/clang.py
+++ b/lib/spack/spack/compilers/clang.py
@@ -22,7 +22,10 @@
 # along with this program; if not, write to the Free Software Foundation,
 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 ##############################################################################
+import re
+import spack.compiler as cpr
 from spack.compiler import *
+from spack.util.executable import *
 
 class Clang(Compiler):
     # Subclasses use possible names of C compiler
@@ -47,11 +50,34 @@ class Clang(Compiler):
     @classmethod
     def default_version(self, comp):
         """The '--version' option works for clang compilers.
-           Output looks like this::
+           On most platforms, output looks like this::
 
                clang version 3.1 (trunk 149096)
                Target: x86_64-unknown-linux-gnu
                Thread model: posix
+
+          On Mac OS X, it looks like this:
+
+               Apple LLVM version 7.0.2 (clang-700.1.81)
+               Target: x86_64-apple-darwin15.2.0
+               Thread model: posix
+
         """
-        return get_compiler_version(
-            comp, '--version', r'(?:clang version|based on LLVM) ([^ )]+)')
+        if comp not in cpr._version_cache:
+            compiler = Executable(comp)
+            output = compiler('--version', output=str, error=str)
+
+            ver = 'unknown'
+            match = re.search(r'^Apple LLVM version ([^ )]+)', output)
+            if match:
+                # Apple's LLVM compiler has its own versions, so suffix them.
+                ver = match.group(1) + '-apple'
+            else:
+                # Normal clang compiler versions are left as-is
+                match = re.search(r'^clang version ([^ )]+)', output)
+                if match:
+                    ver = match.group(1)
+
+            cpr._version_cache[comp] = ver
+
+        return cpr._version_cache[comp]


Test patch: