Repository: spack/spack

Issue:
Bug in Compiler Wrappers
I believe I've discovered a bug in the Spack compiler wrappers that is preventing me from using the NAG Fortran compilers. Compilation succeeds outside of Spack, but fails when using Spack with the error message:

```
checking for linker for Fortran main program... configure: error: Could not determine a way to link a Fortran test program!
```

The config.log files show major discrepancies:

```
# Without Spack
configure:19565: checking if nagfor static flag -Bstatic works
NAG Fortran Compiler Release 6.0(Hibiya) Build 1037
Obsolescent: conftest.f, line 1: Fixed source form
[NAG Fortran Compiler normal termination, 1 warning]
configure:19593: result: yes
# With Spack
configure:19565: checking if /blues/gpfs/home/software/spack/lib/spack/env/nag/nagfor static flag -Bstatic works
configure:19593: result: no
```

```
# Without Spack
configure:30125: checking whether Fortran 77 compiler accepts option -O2
configure:30170: nagfor -o conftest -mismatch   conftest.f  > pac_test1.log 2>&1
configure:30170: $? = 0
configure:30201: nagfor -o conftest -O2 -mismatch   conftest.f  > pac_test2.log 2>&1
configure:30201: $? = 0
configure:30209: diff -b pac_test1.log pac_test2.log > pac_test.log
configure:30212: $? = 0
configure:30312: result: yes
# With Spack
configure:30125: checking whether Fortran 77 compiler accepts option -O2
configure:30170: /blues/gpfs/home/software/spack/lib/spack/env/nag/nagfor -o conftest
configure:30170: $? = 1
configure: failed program was:
|       program main
|
|       end
configure:30312: result: no
```

These results came from trying to compile MPICH 3.2 with GCC 5.3.0 C/C++ compilers and NAG 6.0 Fortran compilers. Note that I had to add `FFLAGS=-mismatch` to get the compilation working outside of Spack. I'll submit a PR for this once we get the compiler wrappers working.

@alalazo, @eschnett: Would you be willing to take a look? You're more familiar with the wrapper than I am. I'll do what I can on my end. Config.log files are attached below. 

[config-without-spack.txt](https://github.com/LLNL/spack/files/182507/config-without-spack.txt)
[config-with-spack.txt](https://github.com/LLNL/spack/files/182509/config-with-spack.txt)



Discussion:
This bug also affects compilation of MVAPICH2 and OpenMPI with NAG. In the OpenMPI config.log, I'm seeing the following:

```
configure:32556: checking if Fortran compiler works
configure:32585: /blues/gpfs/home/software/spack/lib/spack/env/nag/nagfor -o conftest    conftest.f  >&5
NAG Fortran Compiler Release 6.0(Hibiya) Build 1037
Obsolescent: conftest.f, line 1: Fixed source form
[NAG Fortran Compiler normal termination, 1 warning]
gcc: /blues/gpfs/home/software/spack/opt/spack/linux-x86_64/nag-6.0/openmpi-1.10.2-ylunyxy5mq7nd2qxyln3ljz4icojwczs/lib: No such file or directory
gcc: /blues/gpfs/home/software/spack/opt/spack/linux-x86_64/nag-6.0/openmpi-1.10.2-ylunyxy5mq7nd2qxyln3ljz4icojwczs/lib64: No such file or directory
gcc: unrecognized option '-rpath'
gcc: unrecognized option '-rpath'
gcc: unrecognized option '-rpath'
gcc: unrecognized option '-rpath'
gcc: unrecognized option '-rpath'
configure:32585: $? = 1
configure: program exited with status 1
configure: failed program was:
|       program main
|
|       end
configure:32601: result: no
configure:32615: error: Could not run a simple Fortran program.  Aborting.
```

Maybe the problem is as simple as not using `-rpath` with NAG? More log files attached below:

MVAPICH2
[config-without-spack.txt](https://github.com/LLNL/spack/files/182659/config-without-spack.txt)
[config-with-spack.txt](https://github.com/LLNL/spack/files/182660/config-with-spack.txt)

OpenMPI
[config-without-spack.txt](https://github.com/LLNL/spack/files/182661/config-without-spack.txt)
[config-with-spack.txt](https://github.com/LLNL/spack/files/182663/config-with-spack.txt)

@adamjstewart I'll try to have a look at the two logs tomorrow

Apparently someone else ran into the `-rpath` problem with OpenMPI. Very useful suggestions for getting OpenMPI to build with NAG at http://baradi09.blogspot.com/2014/08/compiling-openmpi-with-nag-fortran.html

@adamjstewart: From the config logs, it looks like this is a compiler naming issue.  When built without Spack, the compilers are called `nagfor` and `gcc`.  When built in Spack, they're called `f90` and `cc`.  libtool is really bad at understanding the latter.  Can you try making the NAG compiler have some better names in `lib/spack/env`?  Annoyingly, you're not going to be able to get one that's perfect since NAG mixes with different C compilers (I'll think about how to fix that), but you could do this:

```
lib/spack/env/
    cc
    nag/
        gcc -> ../cc
        g++ -> ../cc
        nagfor -> ../cc
```

And make sure the names are set correctly in `build_environment`.  Then this should behave like a normal `nag`/`gcc` build.

The other issues look like typical libtool problems.  I think the NAG guys should contribute to libtool so their compiler is understood properly -- I suspect no one is adding options for it, and `libtool` really sucks if it isn't familiar with your compiler... see #255... `-Wl,-Wl,`?  really? :sob: 

@tgamblin: I tried your suggestion but it didn't work. In https://groups.google.com/forum/#!topic/spack/PWI2ntNUxm0 you mentioned that:

> If there are special rpath args you can override that too, but I think NAG uses -Wl,-rpath like most things so you should be good there.

How would I do this? I want to try using `-Wl,-Wl,,` instead of `-Wl,`

@tgamblin: Also, is there any way to set default flags for a specific compiler, like in `lib/spack/spack/compilers/nag.py`? So far I've found myself having to add `FFLAGS=-mismatch` to almost every package that contains Fortran code. The NAG compilers are a little too strict.

This could also be used to address the issues brought up with Clang in #589 and #596.

> This could also be used to address the issues brought up with Clang in #589 and #596.

:+1: 

Does anyone have any more advice on this? #748 solves the rpath issue for me, but I still see the same discrepancies in the config.log and cannot get anything to build with the NAG compiler. Keep in mind that NAG works just fine outside of Spack, so there must be another problem with the compiler wrapper. Does no one else have access to a NAG compiler to test this?

Ping. I still cannot compile a single program with NAG (OpenMPI, MPICH, MVAPICH2, HDF, HDF5). I've tried just about everything I can think with. I'm out of ideas.

I think I'm going to take another stab at getting NAG working in Spack. @davydden I know you had to make a lot of changes to get Clang+GFortran working. Can you point me to those and any other notes/suggestions you have?

Here are the notes https://github.com/dealii/dealii/wiki/deal.II-in-Spack (section on mixing compilers for macOS).

I think they are also somewhere in the Spack docu...

> 14 окт. 2016 г., в 18:55, "Adam J. Stewart" notifications@github.com написал(а):
> 
> I think I'm going to take another stab at getting NAG working in Spack. @davydden I know you had to make a lot of changes to get Clang+GFortran working. Can you point me to those and any other notes/suggestions you have?
> 
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub, or mute the thread.

Unfortunately I still can't get it working, even with these modifications. Exact same errors as in the previously attached `config.log` and `spack-build.out`. I contacted NAG support. Hopefully they can tell me what is causing these discrepancies...

Update from the NAG support team:

They cloned Spack and were able to reproduce the bug. The problem seems to be with the compiler wrappers. If they delete the symlink from nagfor to ../cc, and instead replace it with a much simpler compiler wrapper:

``` bash
#!/bin/sh
echo  /usr/local/bin/nagfor $SPACK_FFLAGS $* >> /tmp/spack_wrapper_nagfor.log
exec /usr/local/bin/nagfor $SPACK_FFLAGS $*
```

they can successfully build mpich. They are going to continue poking around the original compiler wrappers and looking for something that could be causing problems.

Hello, this is NAG Compiler Support, could you try the following edit in nag.py please? It's just a question of adding an extra comma after "-rpath,".

```
themos@kalamata>git diff lib/spack/spack/compilers/nag.py
diff --git a/lib/spack/spack/compilers/nag.py b/lib/spack/spack/compilers/nag.py
index 7beb0ef..5ba235a 100644
--- a/lib/spack/spack/compilers/nag.py
+++ b/lib/spack/spack/compilers/nag.py
@@ -61,11 +61,11 @@ class Nag(Compiler):
     # options with '-Wl,-Wl,,'
     @property
     def f77_rpath_arg(self):
-        return '-Wl,-Wl,,-rpath,'
+        return '-Wl,-Wl,,-rpath,,'

     @property
     def fc_rpath_arg(self):
-        return '-Wl,-Wl,,-rpath,'
+        return '-Wl,-Wl,,-rpath,,'

     @classmethod
     def default_version(self, comp):

```

@ThemosTsikas It worked! Thank you so much for your support, now I can finally use NAG again! 

I'll submit a PR tomorrow and see if I run into any other problems with compiling the NetCDF stack.


Resolving patch:
diff --git a/lib/spack/spack/compilers/nag.py b/lib/spack/spack/compilers/nag.py
--- a/lib/spack/spack/compilers/nag.py
+++ b/lib/spack/spack/compilers/nag.py
@@ -61,11 +61,11 @@ def cxx11_flag(self):
     # options with '-Wl,-Wl,,'
     @property
     def f77_rpath_arg(self):
-        return '-Wl,-Wl,,-rpath,'
+        return '-Wl,-Wl,,-rpath,,'
 
     @property
     def fc_rpath_arg(self):
-        return '-Wl,-Wl,,-rpath,'
+        return '-Wl,-Wl,,-rpath,,'
 
     @classmethod
     def default_version(self, comp):


Test patch: