Repository: spack/spack

Issue:
Unable to build ghostscript 9.21
I'm seeing this error:

```
./obj/aux/echogs -a ./obj/ldt.tr -s -  -lm -ldl  -L/scratch/vendors/spack.20170428/opt/spack/linux-rhel7-x86_64/gcc-4.8.5/libtiff-4.0.6-ymvv4frmgmpru6ewdkalbvibmuvoj3ur/lib -ltiff   -rdynamic -lfontconfig -lfreetype      -lpthread -lm
if [ x != x ]; then LD_RUN_PATH=; export LD_RUN_PATH; fi; \
XCFLAGS= XINCLUDE= XLDFLAGS= XLIBDIRS= XLIBS= \
PSI_FEATURE_DEVS= FEATURE_DEVS= DEVICE_DEVS= DEVICE_DEVS1= DEVICE_DEVS2= DEVICE_DEVS3= \
DEVICE_DEVS4= DEVICE_DEVS5= DEVICE_DEVS6= DEVICE_DEVS7= DEVICE_DEVS8= \
DEVICE_DEVS9= DEVICE_DEVS10= DEVICE_DEVS11= DEVICE_DEVS12= \
DEVICE_DEVS13= DEVICE_DEVS14= DEVICE_DEVS15= DEVICE_DEVS16= \
DEVICE_DEVS17= DEVICE_DEVS18= DEVICE_DEVS19= DEVICE_DEVS20= \
DEVICE_DEVS_EXTRA= \
/bin/sh <./obj/ldt.tr
/bin/ld: ./obj/pngrutil.o: undefined reference to symbol 'inflateValidate@@ZLIB_1.2.9'
/scratch/vendors/spack.20170428/opt/spack/linux-rhel7-x86_64/gcc-4.8.5/zlib-1.2.11-hpdicdxfq5jn2ycttiuiu42hemg444nf/lib/libz.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make: *** [bin/gs] Error 1
```

I'm not familiar with building this package manually, so I 'm looking for help from the community.  Does this error look familiar to anyone?  Ghostscript appears to have its own copy of zlib so this may be an issue with their build system and not with spack.  Comments?

[spack-build.out.txt](https://github.com/LLNL/spack/files/967077/spack-build.out.txt)
[spack-build.env.txt](https://github.com/LLNL/spack/files/967078/spack-build.env.txt)



Discussion:
@KineticTheory: it looks like you're getting a versioned symbol for the `inflateValidate` call in `zlib` that does not match the `zlib` that `ghostscript` wants to link with.  If you look at the compile line for `pngrutil.o`, it looks like this:

```
/scratch/vendors/spack.20170428/lib/spack/env/gcc/gcc -O2 -Wall -Wstrict-prototypes -Wundef -Wmissing-declarations -Wmissing-prototypes -Wwrite-strings -Wno-strict-aliasing -Werror=declaration-after-statement -fno-builtin -fno-common -Werror=return-type -DHAVE_STDINT_H=1 -DHAVE_DIRENT_H=1 -DHAVE_SYS_DIR_H=1 -DHAVE_SYS_TIME_H=1 -DHAVE_SYS_TIMES_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_LIBDL=1 -DGX_COLOR_INDEX_TYPE="unsigned long long" -D__USE_UNIX98=1   -I/scratch/vendors/spack.20170428/opt/spack/linux-rhel7-x86_64/gcc-4.8.5/libtiff-4.0.6-ymvv4frmgmpru6ewdkalbvibmuvoj3ur/include      -I./libpng -I./zlib -I./obj  -DPNG_NO_ASSEMBLER_CODE -o ./obj/pngrutil.o -c ./libpng/pngrutil.c
```

It looks like ghostscript includes vendored versions of a number of dependencies, like `libpng`, `zlib`, etc.  We force an external `libtiff` in the `ghostscript` package using `--with-system-libtiff`, which pulls in transitive link dependencies to Spack's `zlib`.  I think the issue is that we're sticking the includes for the Spack `zlib` on the compile line before the `-I./zlib` there, and they're not compatible with the vendored `zlib`.  Normally we'd try to force ghostscript to use Spack's `zlib`, but it doesn't look like it has options for that.

What happens if you get rid of the `libtiff` dependency and remove `--with-system-libtiff` from the config line? 

I'm not sure what the right thing to do here is.  The Spack compilers *used* to put their `-I` flags after any package-specific includes, but we got rid of that a while ago, and if we do that, then packages can potentially put a `-I/usr/include` on the compile line and override Spack dependency headers with system ones.  We could filter system paths out of the compile arguments, but allow includes within the package prefix or those of dependencies.  In this case I'm not sure there is a workable solution other than using *all* the vendored libraries for `ghostscript`, since `libtiff` depends on `zlib` anyway.
I'm able to reproduce this bug on Fedora 25 as well. The previous version of Ghostscript in Spack builds fine. I'll try taking a look.
Hahahaha

> **Note**: The Ghostscript build system is not user-friendly. In order to use system copies of various graphics libraries, you must do it using unconventional methods. 

http://www.linuxfromscratch.org/blfs/view/svn/pst/gs.html
I have a fix for this. PR incoming soon. Just want to run a couple more tests.

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/ghostscript/package.py b/var/spack/repos/builtin/packages/ghostscript/package.py
--- a/var/spack/repos/builtin/packages/ghostscript/package.py
+++ b/var/spack/repos/builtin/packages/ghostscript/package.py
@@ -23,28 +23,59 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 ##############################################################################
 from spack import *
+import shutil
 
 
-class Ghostscript(Package):
+class Ghostscript(AutotoolsPackage):
     """An interpreter for the PostScript language and for PDF."""
 
     homepage = "http://ghostscript.com/"
     url = "https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs921/ghostscript-9.21.tar.gz"
 
-    version('9.21', '6f60d7fcb5eef6a8bec5abedf21c6a7008a8c0c7')
+    version('9.21', '5f213281761d2750fcf27476c404d17f')
     version('9.18', '33a47567d7a591c00a253caddd12a88a')
 
-    parallel = False
+    depends_on('pkg-config', type='build')
 
+    depends_on('freetype@2.4.2:')
+    depends_on('jpeg')
+    depends_on('lcms')
+    depends_on('libpng')
     depends_on('libtiff')
+    depends_on('zlib')
 
     def url_for_version(self, version):
         baseurl = "https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs{0}/ghostscript-{1}.tar.gz"
         return baseurl.format(version.joined, version.dotted)
 
-    def install(self, spec, prefix):
-        configure('--prefix={0}'.format(prefix),
-                  '--with-system-libtiff')
+    def patch(self):
+        """Ghostscript comes with all of its dependencies vendored.
+        In order to build with Spack versions of these dependencies,
+        we have to remove these vendored dependencies.
+
+        Note that this approach is also recommended by Linux from Scratch:
+        http://www.linuxfromscratch.org/blfs/view/svn/pst/gs.html
+        """
+        directories = ['freetype', 'jpeg', 'lcms2', 'libpng', 'zlib']
+        for directory in directories:
+            shutil.rmtree(directory)
+
+        filter_file('ZLIBDIR=src',
+                    'ZLIBDIR={0}'.format(self.spec['zlib'].prefix.include),
+                    'configure.ac', 'configure',
+                    string=True)
 
+    def configure_args(self):
+        return [
+            '--disable-compile-inits',
+            '--enable-dynamic',
+            '--with-system-libtiff',
+        ]
+
+    def build(self, spec, prefix):
         make()
+        make('so')
+
+    def install(self, spec, prefix):
         make('install')
+        make('soinstall')


Test patch: