Repository: spack/spack

Issue:
The llvm+clang c++ compiler cannot find `-lstdc++`
I installed llvm+clang, but the c++ compiler does not work:
```
$ clang++ a.c 
clang-5.0: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated [-Wdeprecated]
/usr/bin/ld: cannot find -lstdc++
clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)
```
This error in turn causes #5941. Here is how it was configured:
```
$ spack find -dlv llvm@5.0.0
==> 1 installed packages.
-- linux-ubuntu16.04-x86_64 / gcc@6.4.0 -------------------------
vvs4bks    llvm@5.0.0+all_targets build_type=Release +clang+compiler-rt+gold+internal_unwind+libcxx~link_dylib+lldb+polly~shared_libs
atc354d        ^binutils@2.28+gold~libiberty patches=0a0ca84889b6727dc0cc88650f55fd79452dddba762674a83a153374792fdaac ~plugins
z4rue2x            ^gettext@0.19.8.1+bzip2+curses+git~libunistring+libxml2 patches=9acdb4e73f67c241b5ef32505c9ddf7cf6884ca8ea661692f21dca28483b04b8 +tar+xz
bmyxoqb                ^bzip2@1.0.6+shared
zqliw5y                ^libxml2@2.9.4~python
2tho6an                    ^xz@5.2.3
yvlwvyz                    ^zlib@1.2.11+pic+shared
jtmz6tz                ^ncurses@6.0 patches=4110a40613b800da2b2888c352b64c75a82809d48341061e4de5861e8b28423f,f84b2708a42777aadcc7f502a261afe10ca5646a51c1ef8b5e60d2070d926b57 ~symlinks
f3sg6dt                ^tar@1.29 patches=08921fcbd732050c74ddf1de7d8ad95ffdbc09f8b4342456fa2f6a0dd02a957c,5c314db58d005043bf407abaf25eb9823b9032a22fd12a0b142d4bf548130fa4,d428578be7fb99b831eb61e53b8d88a859afe08b479a21238180899707d79ce4,e34d2470461037933737c0a60fbcc5d5ea905fefb6b414b4a2e88a440fcae23a
hrqaevt        ^libedit@3.1-20170329
nreklkn        ^py-lit@0.5.0
n6pbabv            ^python@2.7.14 patches=123082ab3483ded78e86d7c809e98a804b3465b4683c96bd79a2fd799f572244 +pic+shared~tk~ucs4
ztgs5rp                ^openssl@1.0.2k
323dute                ^readline@7.0
25axhq6                ^sqlite@3.20.0
ljvueky        ^swig@3.0.12
nx2htpr            ^pcre@8.40~jit+utf
```


Discussion:
To make my example compile, one has to tell `clang++` to use the `libc++` stdlib as follows:
```
clang++ -stdlib=libc++ a.c
```
Now we have to figure out how to make it use it by default. Here is how: https://stackoverflow.com/questions/19901128/enable-libc-libcxx-by-default-when-using-clang#35471418

I am now testing:
```diff
diff --git a/var/spack/repos/builtin/packages/llvm/package.py b/var/spack/repos/builtin/packages/llvm/package.py
index 450b2a0..5f154b2 100644
--- a/var/spack/repos/builtin/packages/llvm/package.py
+++ b/var/spack/repos/builtin/packages/llvm/package.py
@@ -393,6 +393,8 @@ class Llvm(CMakePackage):
         if '+libcxx' not in spec:
             cmake_args.append('-DLLVM_EXTERNAL_LIBCXX_BUILD:Bool=OFF')
             cmake_args.append('-DLLVM_EXTERNAL_LIBCXXABI_BUILD:Bool=OFF')
+        else:
+            cmake_args.append('-DCLANG_DEFAULT_CXX_STDLIB=libc++')
         if '+compiler-rt' not in spec:
             cmake_args.append('-DLLVM_EXTERNAL_COMPILER_RT_BUILD:Bool=OFF')
 ```
and this fixes it! Now clang++ works:
```
$ clang++ a.c 
clang-5.0: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated [-Wdeprecated]
$
```

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/llvm/package.py b/var/spack/repos/builtin/packages/llvm/package.py
--- a/var/spack/repos/builtin/packages/llvm/package.py
+++ b/var/spack/repos/builtin/packages/llvm/package.py
@@ -390,7 +390,10 @@ def cmake_args(self):
                                '-DLLVM_TOOL_LLDB_BUILD:Bool=OFF'])
         if '+internal_unwind' not in spec:
             cmake_args.append('-DLLVM_EXTERNAL_LIBUNWIND_BUILD:Bool=OFF')
-        if '+libcxx' not in spec:
+        if '+libcxx' in spec:
+            if spec.satisfies('@3.9.0:'):
+                cmake_args.append('-DCLANG_DEFAULT_CXX_STDLIB=libc++')
+        else:
             cmake_args.append('-DLLVM_EXTERNAL_LIBCXX_BUILD:Bool=OFF')
             cmake_args.append('-DLLVM_EXTERNAL_LIBCXXABI_BUILD:Bool=OFF')
         if '+compiler-rt' not in spec:


Test patch: