Repository: spack/spack

Issue:
flang fails to build with gcc 6.4.0
```
$ spack install -j16 --restage flang%gcc@6.4.0
...
==> Already staged flang-develop-j6mkvco2dtnaaev2fxpxqnnx33fg4rx5 in /home/certik/repos/spack/var/spack/stage/flang-develop-j6mkvco2dtnaaev2fxpxqnnx33fg4rx5
==> No patches needed for flang
==> Building flang [CMakePackage]
==> Executing phase: 'cmake'
==> Error: ProcessError: Command exited with status 1:
    'cmake' '/home/certik/repos/spack/var/spack/stage/flang-develop-j6mkvco2dtna
aev2fxpxqnnx33fg4rx5/flang' '-G' 'Unix Makefiles' '-DCMAKE_INSTALL_PREFIX:PATH=/
home/certik/repos/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.4.0/flang-devel
op-j6mkvco2dtnaaev2fxpxqnnx33fg4rx5' '-DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo' 
'-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON' '-DCMAKE_INSTALL_RPATH_USE_LINK_PATH:BOOL=FAL
SE' '-DCMAKE_INSTALL_RPATH:STRING=/home/certik/repos/spack/opt/spack/linux-ubunt
u16.04-x86_64/gcc-6.4.0/flang-develop-j6mkvco2dtnaaev2fxpxqnnx33fg4rx5/lib:/home
/certik/repos/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.4.0/flang-develop-j
6mkvco2dtnaaev2fxpxqnnx33fg4rx5/lib64:/home/certik/repos/spack/opt/spack/linux-u
buntu16.04-x86_64/gcc-6.4.0/llvm-5.0.0-vvs4bksw5ihlpljb57bhhkczdgpccsaa/lib:/hom
e/certik/repos/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.4.0/binutils-2.28-
atc354dhdpdup3pgboge4w4rizlvpguf/lib:/home/certik/repos/spack/opt/spack/linux-ub
untu16.04-x86_64/gcc-6.4.0/gettext-0.19.8.1-z4rue2xloo4gjfujlq72wc53xqcrwqfa/lib
:/home/certik/repos/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.4.0/bzip2-1.0
.6-bmyxoqbr52auiqqupsmixp65chxmnwuu/lib:/home/certik/repos/spack/opt/spack/linux
-ubuntu16.04-x86_64/gcc-6.4.0/libxml2-2.9.4-zqliw5yhxo4ijx4vhjeb6hdgjv2hu6ep/lib
:/home/certik/repos/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.4.0/xz-5.2.3-
2tho6an2mngb5ym2qvv6jfjppr2aw2bj/lib:/home/certik/repos/spack/opt/spack/linux-ub
untu16.04-x86_64/gcc-6.4.0/zlib-1.2.11-yvlwvyz4fbdmewglmq46o6ivkrczvigf/lib:/hom
e/certik/repos/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.4.0/ncurses-6.0-jt
mz6tz4l5uwnx4ngi2sbqkdbqrpvmbm/lib:/home/certik/repos/spack/opt/spack/linux-ubun
tu16.04-x86_64/gcc-6.4.0/libedit-3.1-20170329-hrqaevtd5acrbcmflznjpffig6txuo56/l
ib:/home/certik/repos/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.4.0/python-
2.7.14-n6pbabvjb3a3wfoo75k5s7ossot2kzyi/lib:/home/certik/repos/spack/opt/spack/l
inux-ubuntu16.04-x86_64/gcc-6.4.0/openssl-1.0.2k-ztgs5rpiobj7tpyccjzesbvdyk6p3f3
i/lib:/home/certik/repos/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.4.0/read
line-7.0-323dutestkw2xm64b2flv7p334ux2hbx/lib:/home/certik/repos/spack/opt/spack
/linux-ubuntu16.04-x86_64/gcc-6.4.0/sqlite-3.20.0-25axhq64ozc5mhqogrgmad2gzh4g4q
4k/lib:/home/certik/repos/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.4.0/pcr
e-8.40-nx2htpr5kmlrvcfzln4n33qx6ff3lg4h/lib' '-DCMAKE_PREFIX_PATH:STRING=/home/c
ertik/repos/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.4.0/llvm-5.0.0-vvs4bk
sw5ihlpljb57bhhkczdgpccsaa;/home/certik/repos/spack/opt/spack/linux-ubuntu16.04-
x86_64/gcc-6.4.0/cmake-3.9.4-t2ydlbo7cmuwscboipwjj34sulgog5gq' '-DWITH_WERROR=OF
F' '-DCMAKE_C_COMPILER=/home/certik/repos/spack/opt/spack/linux-ubuntu16.04-x86_
64/gcc-6.4.0/llvm-5.0.0-vvs4bksw5ihlpljb57bhhkczdgpccsaa/bin/clang' '-DCMAKE_CXX
_COMPILER=/home/certik/repos/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.4.0/
llvm-5.0.0-vvs4bksw5ihlpljb57bhhkczdgpccsaa/bin/clang++' '-DCMAKE_Fortran_COMPIL
ER=/home/certik/repos/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.4.0/llvm-5.
0.0-vvs4bksw5ihlpljb57bhhkczdgpccsaa/bin/flang' '-DFLANG_LIBOMP=/home/certik/rep
os/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.4.0/llvm-5.0.0-vvs4bksw5ihlplj
b57bhhkczdgpccsaa/lib/libomp.so'

6 errors found in build log:
     [ ... ]
     7     -- Detecting C compiler ABI info
     8     -- Detecting C compiler ABI info - done
     9     -- Detecting C compile features
     10    -- Detecting C compile features - done
     11    -- Check for working CXX compiler: /home/certik/repos/spack/opt/spack
/linux-ubuntu16.04-x86_64/gcc-6.4.0/llvm-5.0.0-vvs4bksw5ihlpljb57bhhkczdgpccsaa/
bin/clang++
     12    -- Check for working CXX compiler: /home/certik/repos/spack/opt/spack
/linux-ubuntu16.04-x86_64/gcc-6.4.0/llvm-5.0.0-vvs4bksw5ihlpljb57bhhkczdgpccsaa/
bin/clang++ -- broken
  >> 13    CMake Error at /home/certik/repos/spack/opt/spack/linux-ubuntu16.04-x
86_64/gcc-6.4.0/cmake-3.9.4-t2ydlbo7cmuwscboipwjj34sulgog5gq/share/cmake-3.9/Mod
ules/CMakeTestCXXCompiler.cmake:44 (message):
     14      The C++ compiler
     15      "/home/certik/repos/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.
4.0/llvm-5.0.0-vvs4bksw5ihlpljb57bhhkczdgpccsaa/bin/clang++"
     16      is not able to compile a simple test program.
     17    
     18      It fails with the following output:
     19    
     [ ... ]
     45      -E cmake_link_script CMakeFiles/cmTC_005a3.dir/link.txt --verbose=1
     46    
     47    
     48      /home/certik/repos/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.4
.0/llvm-5.0.0-vvs4bksw5ihlpljb57bhhkczdgpccsaa/bin/clang++
     49      -rdynamic CMakeFiles/cmTC_005a3.dir/testCXXCompiler.cxx.o -o cmTC_0
05a3
     50    
  >> 51    ld: cannot find -lstdc++
     52    
  >> 53      clang-5.0: error: linker command failed with exit code 1 (use -v to
 see
     54      invocation)
     55    
  >> 56      CMakeFiles/cmTC_005a3.dir/build.make:97: recipe for target 'cmTC_00
5a3'
     57      failed
     58    
  >> 59      make[1]: *** [cmTC_005a3] Error 1
     60    
     61      make[1]: Leaving directory
     62      '/home/certik/tmp/spack-stage/spack-stage-T8eI00/flang/spack-build/
CMakeFiles/CMakeTmp'
     63    
     64    
     65      Makefile:126: recipe for target 'cmTC_005a3/fast' failed
     66    
  >> 67    make: *** [cmTC_005a3/fast] Error 2
     68    
     69    
     70    
     71    
     72    
     73      CMake will not be able to correctly generate this project.

See build log for details:
  /home/certik/repos/spack/var/spack/stage/flang-develop-j6mkvco2dtnaaev2fxpxqnn
x33fg4rx5/flang/spack-build.out
```
```


Discussion:
@junghans this was introduced by you in #5459. The clang compiler does not work (`ld: cannot find -lstdc++`, see above). I tried the following patch:
```diff
diff --git a/var/spack/repos/builtin/packages/flang/package.py b/var/spack/repos/builtin/packages/flang/package.py
index 6e1419a..33c4f0f 100644
--- a/var/spack/repos/builtin/packages/flang/package.py
+++ b/var/spack/repos/builtin/packages/flang/package.py
@@ -43,12 +43,12 @@ class Flang(CMakePackage):
     def cmake_args(self):
         options = [
             '-DWITH_WERROR=OFF',
-            '-DCMAKE_C_COMPILER=%s' % os.path.join(
-                self.spec['llvm'].prefix.bin, 'clang'),
-            '-DCMAKE_CXX_COMPILER=%s' % os.path.join(
-                self.spec['llvm'].prefix.bin, 'clang++'),
-            '-DCMAKE_Fortran_COMPILER=%s' % os.path.join(
-                self.spec['llvm'].prefix.bin, 'flang'),
+            #'-DCMAKE_C_COMPILER=%s' % os.path.join(
+            #    self.spec['llvm'].prefix.bin, 'clang'),
+            #'-DCMAKE_CXX_COMPILER=%s' % os.path.join(
+            #    self.spec['llvm'].prefix.bin, 'clang++'),
+            #'-DCMAKE_Fortran_COMPILER=%s' % os.path.join(
+            #    self.spec['llvm'].prefix.bin, 'flang'),
             '-DFLANG_LIBOMP=%s' % find_libraries(
                 'libomp', root=self.spec['llvm'].prefix.lib)
         ]
```
and it actually starts building, it gets to about 37%, then it fails with:
```
...
gfortran: error: unrecognized command line option ‘-Mreentrant’
runtime/flang/CMakeFiles/flang_static.dir/build.make:7435: recipe for target 'ru
ntime/flang/CMakeFiles/flang_static.dir/iso_c_bind.F95.o' failed
make[3]: *** [runtime/flang/CMakeFiles/flang_static.dir/iso_c_bind.F95.o] Error 
1
```
The problem is caused by #5942. So there might be no problem in the flang package itself. We'll see after we fix #5942.
Ok, so #5943 fixes `clang++`, now it works. Unfortunately, `flang` still fails to build. Now I am getting:
```
1 error found in build log:
     [ ... ]
     43    -- Performing Test CXX_SUPPORTS_FDATA_SECTIONS - Success
     44    -- Found PythonInterp: /home/certik/repos/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.4.0/python-2.7.14-n6pbabvjb3a3wfoo75k5s7ossot2kzyi/bin/python2.7 (found version "2.7.14")
     45    -- Flang version: 5.0.0
     46    -- The ASM compiler identification is Clang
     47    -- Found assembler: /home/certik/repos/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.4.0/llvm-5.0.0-vvs4bksw5ihlpljb57bhhkczdgpccsaa/bin/clang
     48    -- The Fortran compiler identification is unknown
  >> 49    CMake Error at runtime/flang/CMakeLists.txt:17 (enable_language):
     50      The CMAKE_Fortran_COMPILER:
     51    
     52        /home/certik/repos/spack/opt/spack/linux-ubuntu16.04-x86_64/gcc-6.4.0/llvm-5.0.0-vvs4bksw5ihlpljb57bhhkczdgpccsaa/bin/flang
     53    
     54      is not a full path to an existing compiler tool.
     55    

See build log for details:
  /home/certik/repos/spack/var/spack/stage/flang-develop-j6mkvco2dtnaaev2fxpxqnnx33fg4rx5/flang/spack-build.out
```
I wonder if llvm has to be patched to produce the flang compiler? That's what that patch seems to be doing, but it's only applied for LLVM 4.0.1, so I am now trying:
```
spack install -j16 --restage -v flang%gcc@6.4.0 ^llvm@4.0.1
```
That worked!

So I think the flang package must depend on llvm@4.0.1 specifically.
Yes, I thought I wrote 4.0.1 in the `depends_on()`.

However there is a patch for llvm-5.0 as well.

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/flang/package.py b/var/spack/repos/builtin/packages/flang/package.py
--- a/var/spack/repos/builtin/packages/flang/package.py
+++ b/var/spack/repos/builtin/packages/flang/package.py
@@ -35,10 +35,16 @@ class Flang(CMakePackage):
     version('develop', git='https://github.com/flang-compiler/flang', branch='master')
 
     depends_on(
-        "llvm+clang",
-        patches=patch('https://github.com/llvm-mirror/clang/pull/33.diff',
+            "llvm+clang@4.0.1,5.0.0",
+        patches=[
+            patch('https://github.com/llvm-mirror/clang/pull/33.diff',
                       sha256='e46d7ab305e5e95c51f4656d9b52058143cd85d859b312b3c80e93a02d54b4a5',
-                      when='@4.0.1', level=1, working_dir='tools/clang'))
+                      when='@4.0.1', level=1, working_dir='tools/clang'),
+            patch('https://github.com/llvm-mirror/clang/pull/35.diff',
+                      sha256='7f39555783993f78b75c380ca5ef167c1d8b88cc75c6542f6c94e0b6acfb7c5d',
+                      when='@5.0', level=1, working_dir='tools/clang')
+        ]
+    )
 
     def cmake_args(self):
         options = [


Test patch: