Repository: spack/spack

Issue:
KeyError: KeyError: 'F77'
Building hdf5 ~fortran with a compilers.yaml compiler where "f77: null" and "fc: null" were set in the yaml produces a KeyError: 'F77'. Must I specify a fortran compiler even if I don't want to build any fortran?

### Expected Result

Successful hdf5 installation:

```console
$ ./bin/spack install hdf5@1.8.12%gcc@4.9.3 cppflags="-fPIC -g" ~cxx~debug~fortran~hl~mpi+pic~shared~szip~threadsafe arch=linux-rhel7-x86_64
==> Installing zlib
==> Fetching http://zlib.net/fossils/zlib-1.2.11.tar.gz
######################################################################## 100.0%
==> Staging archive: /usr/workspace/wsrzc/pozulp1/sp/reprospack/spack/var/spack/stage/zlib-1.2.11-e3vvrmkyza76kosidmhdlr7yaoyw4iht/zlib-1.2.11.tar.gz
==> Created stage in /usr/workspace/wsrzc/pozulp1/sp/reprospack/spack/var/spack/stage/zlib-1.2.11-e3vvrmkyza76kosidmhdlr7yaoyw4iht
==> No patches needed for zlib
==> Building zlib [Package]
==> Executing phase: 'install'
==> Successfully installed zlib
  Fetch: 0.51s.  Build: 16.30s.  Total: 16.81s.
[+] /usr/workspace/wsrzc/pozulp1/sp/reprospack/spack/opt/spack/linux-rhel7-x86_64/gcc-4.9.3/zlib-1.2.11-e3vvrmkyza76kosidmhdlr7yaoyw4iht
==> Installing hdf5
==> Fetching https://support.hdfgroup.org/ftp/HDF5/releases/hdf5-1.8/hdf5-1.8.12/src/hdf5-1.8.12.tar.gz
######################################################################## 100.0%
==> Staging archive: /usr/workspace/wsrzc/pozulp1/sp/reprospack/spack/var/spack/stage/hdf5-1.8.12-gdeup2jke2rni7v27q2qqbahjajb7ywz/hdf5-1.8.12.tar.gz
==> Created stage in /usr/workspace/wsrzc/pozulp1/sp/reprospack/spack/var/spack/stage/hdf5-1.8.12-gdeup2jke2rni7v27q2qqbahjajb7ywz
==> No patches needed for hdf5
==> Building hdf5 [AutotoolsPackage]
==> Executing phase: 'autoreconf'
==> Executing phase: 'configure'
==> Executing phase: 'build'
==> Executing phase: 'install'
==> Successfully installed hdf5
  Fetch: 0.13s.  Build: 1m 53.93s.  Total: 1m 54.05s.
```

### Actual Result

KeyError: 'F77'

```console
$ ./bin/spack install hdf5@1.8.12%gcc@4.9.3 cppflags="-fPIC -g" ~cxx~debug~fortran~hl~mpi+pic~shared~szip~threadsafe arch=linux-rhel7-x86_64
==> Installing zlib
==> Fetching http://zlib.net/fossils/zlib-1.2.11.tar.gz
######################################################################## 100.0%
==> Staging archive: /usr/workspace/wsrzc/pozulp1/sp/reprospack/spack/var/spack/stage/zlib-1.2.11-e3vvrmkyza76kosidmhdlr7yaoyw4iht/zlib-1.2.11.tar.gz
==> Created stage in /usr/workspace/wsrzc/pozulp1/sp/reprospack/spack/var/spack/stage/zlib-1.2.11-e3vvrmkyza76kosidmhdlr7yaoyw4iht
==> No patches needed for zlib
==> Building zlib [Package]
==> Executing phase: 'install'
==> Successfully installed zlib
  Fetch: 0.51s.  Build: 16.30s.  Total: 16.81s.
[+] /usr/workspace/wsrzc/pozulp1/sp/reprospack/spack/opt/spack/linux-rhel7-x86_64/gcc-4.9.3/zlib-1.2.11-e3vvrmkyza76kosidmhdlr7yaoyw4iht
==> Installing hdf5
==> Fetching https://support.hdfgroup.org/ftp/HDF5/releases/hdf5-1.8/hdf5-1.8.12/src/hdf5-1.8.12.tar.gz
######################################################################## 100.0%
==> Staging archive: /usr/workspace/wsrzc/pozulp1/sp/reprospack/spack/var/spack/stage/hdf5-1.8.12-gdeup2jke2rni7v27q2qqbahjajb7ywz/hdf5-1.8.12.tar.gz
==> Created stage in /usr/workspace/wsrzc/pozulp1/sp/reprospack/spack/var/spack/stage/hdf5-1.8.12-gdeup2jke2rni7v27q2qqbahjajb7ywz
==> No patches needed for hdf5
==> Building hdf5 [AutotoolsPackage]
==> Executing phase: 'autoreconf'
==> Executing phase: 'configure'
==> Executing phase: 'build'
==> Executing phase: 'install'
==> Error: KeyError: 'F77'
KeyError: KeyError: 'F77'

/usr/workspace/wsrzc/pozulp1/sp/reprospack/spack/lib/spack/spack/mixins.py:200, in _filter_compiler_wrappers_impl:
     12    
     13            x.filter(os.environ['CC'], self.compiler.cc, **filter_kwargs)
     14            x.filter(os.environ['CXX'], self.compiler.cxx, **filter_kwargs)
  >> 15            x.filter(os.environ['F77'], self.compiler.f77, **filter_kwargs)
     16            x.filter(os.environ['FC'], self.compiler.fc, **filter_kwargs)
     17    
     18            # Remove this linking flag if present (it turns RPATH into RUNPATH)
```
### Steps to reproduce the issue

```console
$ cat compilers.yaml
compilers:
- compiler:
    environment: {}
    extra_rpaths: []
    flags: {}
    modules: []
    operating_system: rhel7
    paths:
      cc: /usr/tce/packages/gcc/gcc-4.9.3/bin/gcc
      cxx: /usr/tce/packages/gcc/gcc-4.9.3/bin/g++
      f77: null
      fc: null
    spec: gcc@4.9.3
    target: x86_64
$ mkdir -p ~/.spack/linux/ && cp compilers.yaml ~/.spack/linux
$ git clone https://github.com/spack/spack.git
$ ./spack/bin/spack install hdf5@1.8.12%gcc@4.9.3 cppflags="-fPIC -g"
```

### Information on your system
LLNL toss3 system **rzgenie** using the following ~/.spack/linux/compilers.yaml

```console
$ cat ~/.spack/linux/compilers.yaml
compilers:
- compiler:
    environment: {}
    extra_rpaths: []
    flags: {}
    modules: []
    operating_system: rhel7
    paths:
      cc: /usr/tce/packages/gcc/gcc-4.9.3/bin/gcc
      cxx: /usr/tce/packages/gcc/gcc-4.9.3/bin/g++
      f77: null
      fc: null
    spec: gcc@4.9.3
    target: x86_64
```


Discussion:
@scheibelp @becker33 in case it helps, when I wrote this compilers.yaml it worked with this spack:

commit f39fa2fee948590ac4c895c1339f41eac9ebd575
Author: George Hartzell <hartzell@alerce.com>
Date:   Thu Sep 21 06:22:09 2017 -0700

so if my issue is actually a spack bug then it's a regression.
@mmpozulp @scheibelp I can’t test since I’m commenting from my phone, but I think we just need `hasattr` guards around the compiler field accesses. 

Resolving patch:
diff --git a/lib/spack/spack/mixins.py b/lib/spack/spack/mixins.py
--- a/lib/spack/spack/mixins.py
+++ b/lib/spack/spack/mixins.py
@@ -195,10 +195,15 @@ def _filter_compiler_wrappers_impl(self):
 
         x = llnl.util.filesystem.FileFilter(*abs_files)
 
-        x.filter(os.environ['CC'], self.compiler.cc, **filter_kwargs)
-        x.filter(os.environ['CXX'], self.compiler.cxx, **filter_kwargs)
-        x.filter(os.environ['F77'], self.compiler.f77, **filter_kwargs)
-        x.filter(os.environ['FC'], self.compiler.fc, **filter_kwargs)
+        replacements = [
+            ('CC', self.compiler.cc),
+            ('CXX', self.compiler.cxx),
+            ('F77', self.compiler.f77),
+            ('FC', self.compiler.fc)
+        ]
+        for env_var, compiler_path in replacements:
+            if env_var in os.environ:
+                x.filter(os.environ[env_var], compiler_path, **filter_kwargs)
 
         # Remove this linking flag if present (it turns RPATH into RUNPATH)
         x.filter('-Wl,--enable-new-dtags', '', **filter_kwargs)


Test patch: