Repository: spack/spack

Issue:
Installation issue: IOR fails to find pnetcdf libraries
### Steps to reproduce the issue

```console
$ spack install ior+ncmpi
...
    293    /home/robl/work/soft/mpich/bin/mpicc  -g -O2   -o ior ior.o utilities.o parse_options.o aiori-POSIX.o aiori-MPIIO.o  aiori-NCMPI.o  -lpnetcdf -lm
  >> 294    /usr/bin/ld: cannot find -lpnetcdf
  >> 295    collect2: error: ld returned 1 exit status
     296    Makefile:404: recipe for target 'ior' failed
  >> 297    make[2]: *** [ior] Error 1
     298    make[2]: Leaving directory '/tmp/robl/spack-stage/spack-stage-9s5EYg/ior-3.0.1/src'
     299    Makefile:308: recipe for target 'all' failed
  >> 300    make[1]: *** [all] Error 2
     301    make[1]: Leaving directory '/tmp/robl/spack-stage/spack-stage-9s5EYg/ior-3.0.1/src'
     302    Makefile:361: recipe for target 'all-recursive' failed
  >> 303    make: *** [all-recursive] Error 1

```

### Platform and user environment

```commandline
$ uname -a
Linux hodor 4.15.0-45-generic #48-Ubuntu SMP Tue Jan 29 16:28:13 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
$ lsb_release -d
Description:    Ubuntu 18.04.2 LTS
``` 
### Additional information

In my packages.yaml file I have made my own build of MPICH a system package:

```  mpich:
        paths:
            mpich@master: /home/robl/work/soft/mpich
```

More information about spack: 
```console
$ spack spec --install-status ior+ncmpi
Input spec
--------------------------------
 -   ior+ncmpi

Concretized
--------------------------------
 -   ior@3.0.1%gcc@7.3.0~hdf5+ncmpi arch=linux-ubuntu18.04-x86_64 
[+]      ^autoconf@2.69%gcc@7.3.0 arch=linux-ubuntu18.04-x86_64 
[+]      ^automake@1.15.1%gcc@7.3.0 arch=linux-ubuntu18.04-x86_64 
[+]      ^libtool@2.4.6%gcc@7.3.0 arch=linux-ubuntu18.04-x86_64 
[+]          ^m4@1.4.18%gcc@7.3.0 patches=3877ab548f88597ab2327a2230ee048d2d07ace1062efe81fc92e91b7f39cd00,c0a408fbffb7255fcc75e26bd8edab116fc81d216bfd18b473668b7739a4158e,fc9b61654a3ba1a8d6cd78ce087e7c96366c290bc8d2c299f09828d793b853c8 +sigsegv arch=linux-ubuntu18.04-x86_64 
[+]              ^libsigsegv@2.11%gcc@7.3.0 arch=linux-ubuntu18.04-x86_64 
[+]      ^mpich@master%gcc@7.3.0 device=ch3 +hydra netmod=tcp +pmi+romio~verbs arch=linux-ubuntu18.04-x86_64 
[+]      ^parallel-netcdf@1.11.0%gcc@7.3.0+cxx+fortran+pic arch=linux-ubuntu18.04-x86_64 
...


As suggested, I rebuilt the package with the following options:
```console
$ spack -d install -j 1 ior+ncmpi
...

and am attaching the generated files.  The generated files show no -L flags for parallel-netcdf.

[spack-cc-ior-7kzsppv.out.log](https://github.com/spack/spack/files/2870769/spack-cc-ior-7kzsppv.out.log)
[spack-cc-ior-7kzsppv.in.log](https://github.com/spack/spack/files/2870770/spack-cc-ior-7kzsppv.in.log)




Discussion:

Resolving patch:
diff --git a/lib/spack/spack/build_environment.py b/lib/spack/spack/build_environment.py
--- a/lib/spack/spack/build_environment.py
+++ b/lib/spack/spack/build_environment.py
@@ -45,6 +45,7 @@
 import llnl.util.tty as tty
 from llnl.util.tty.color import cescape, colorize
 from llnl.util.filesystem import mkdirp, install, install_tree
+from llnl.util.lang import dedupe
 
 import spack.build_systems.cmake
 import spack.build_systems.meson
@@ -275,18 +276,22 @@ def set_build_environment_variables(pkg, env, dirty):
     for dep in link_deps:
         if is_system_path(dep.prefix):
             continue
-        # TODO: packages with alternative implementations of .libs which
-        # are external may place libraries in nonstandard directories, so
-        # there should be a check for that
         query = pkg.spec[dep.name]
+        dep_link_dirs = list()
         try:
-            dep_link_dirs = list(query.libs.directories)
-            link_dirs.extend(dep_link_dirs)
-            if dep in rpath_deps:
-                rpath_dirs.extend(dep_link_dirs)
+            dep_link_dirs.extend(query.libs.directories)
         except spack.spec.NoLibrariesError:
             tty.debug("No libraries found for {0}".format(dep.name))
 
+        for default_lib_dir in ['lib', 'lib64']:
+            default_lib_prefix = os.path.join(dep.prefix, default_lib_dir)
+            if os.path.isdir(default_lib_prefix):
+                dep_link_dirs.append(default_lib_prefix)
+
+        link_dirs.extend(dep_link_dirs)
+        if dep in rpath_deps:
+            rpath_dirs.extend(dep_link_dirs)
+
         # TODO: fix the line below, currently the logic is broken for
         # TODO: packages that uses directories as namespaces e.g.
         # TODO: #include <boost/xxx.hpp>
@@ -295,6 +300,10 @@ def set_build_environment_variables(pkg, env, dirty):
         if os.path.isdir(dep.prefix.include):
             include_dirs.append(dep.prefix.include)
 
+    link_dirs = list(dedupe(filter_system_paths(link_dirs)))
+    include_dirs = list(dedupe(filter_system_paths(include_dirs)))
+    rpath_dirs = list(dedupe(filter_system_paths(rpath_dirs)))
+
     env.set(SPACK_LINK_DIRS, ':'.join(link_dirs))
     env.set(SPACK_INCLUDE_DIRS, ':'.join(include_dirs))
     env.set(SPACK_RPATH_DIRS, ':'.join(rpath_dirs))


Test patch: