Repository: spack/spack

Issue:
Installation issue: py-pygobject
### Steps to reproduce the issue

Comment: building for Python 3.7. Also, cairo with X is required

```console
$ spack install py-pygobject ^cairo+X^python@3.7.0
5 errors found in build log:
     198                     ^
     199    gi/pygspawn.c:195:21: warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
     200                 envp[i] = PYGLIB_PyUnicode_AsString(tmp);
     201                         ^
     202    /opt/spack/lib/spack/env/gcc/gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -fPIC -DHAVE_CONFIG_H -DPY_SSIZE_T_CLEAN -I/tmp/root/spack-stage/spack-stage-litit77f/pygobject-3.28.3 -I/tmp/root/spack-stage/spack-stage-litit77f/pygobje
            ct-3.28.3/gi -I/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/pcre-8.42-qjzaysisuwjkct4san2tghocvl37qykr/include -I/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/glib-2.56.3-3t6c3e2qvdgyqub3qhf5yjasd7y5phky/include/glib-2.0 -I/opt/spack/opt/spack/l
            inux-gentoo2-x86_64/gcc-6.4.0/glib-2.56.3-3t6c3e2qvdgyqub3qhf5yjasd7y5phky/lib/glib-2.0/include -I/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/pcre-8.42-qjzaysisuwjkct4san2tghocvl37qykr/include -I/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/gli
            b-2.56.3-3t6c3e2qvdgyqub3qhf5yjasd7y5phky/include/glib-2.0 -I/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/glib-2.56.3-3t6c3e2qvdgyqub3qhf5yjasd7y5phky/lib/glib-2.0/include -I/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/pcre-8.42-qjzaysisuwjkct4
            san2tghocvl37qykr/include -I/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/glib-2.56.3-3t6c3e2qvdgyqub3qhf5yjasd7y5phky/include/glib-2.0 -I/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/glib-2.56.3-3t6c3e2qvdgyqub3qhf5yjasd7y5phky/lib/glib-2.0/incl
            ude -I/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/libffi-3.2.1-yfdfl2vobpvgeutisf4igcpbl3sqyyli/lib/libffi-3.2.1/include -I/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/gobject-introspection-1.49.2-7h4quqdgf24wp5bu5jbpntoqyojr23zh/include/gobje
            ct-introspection-1.0 -I/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/libffi-3.2.1-yfdfl2vobpvgeutisf4igcpbl3sqyyli/lib/libffi-3.2.1/include -I/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/python-3.7.0-7uvmmlxp64xrkopbcuxgtflnduwgzk6z/include/pyth
            on3.7m -c gi/pygi-error.c -o build/temp.linux-x86_64-3.7/gi/pygi-error.o
     203    /usr/x86_64-pc-linux-gnu/gcc-bin/6.4.0/gcc -pthread -shared -L/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/bzip2-1.0.6-7soh6zu722bfpjrxuo3n3b6t5szpbcub/lib -L/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/ncurses-6.1-22r652evcoe7qzh64jdl2zxumezm5
            ekv/lib -L/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/zlib-1.2.11-zx4eejxjybr4waejv6sxezktvlw332ni/lib -L/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/sqlite-3.25.3-5nmaoy4g43a425tcti5r4ovhpbavwgzr/lib -L/opt/spack/opt/spack/linux-gentoo2-x86_6
            4/gcc-6.4.0/openssl-1.1.1-6lvwudl46qq2gzgkchjdscq3gqhvzjka/lib -L/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/libffi-3.2.1-yfdfl2vobpvgeutisf4igcpbl3sqyyli/lib64 -L/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/readline-7.0-a4kn2z32mfha3334jqaimm
            rcpetqfmic/lib -L/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/gdbm-1.18.1-xn57v7wxeoz7i3blkte7wyyknct44ace/lib build/temp.linux-x86_64-3.7/gi/pygi-struct-marshal.o build/temp.linux-x86_64-3.7/gi/pygboxed.o build/temp.linux-x86_64-3.7/gi/pygi-type.o bu
            ild/temp.linux-x86_64-3.7/gi/pygi-resulttuple.o build/temp.linux-x86_64-3.7/gi/pygi-value.o build/temp.linux-x86_64-3.7/gi/pygobject-object.o build/temp.linux-x86_64-3.7/gi/pygoptioncontext.o build/temp.linux-x86_64-3.7/gi/pygi-hashtable.o build/temp.linux-x
            86_64-3.7/gi/pygi-enum-marshal.o build/temp.linux-x86_64-3.7/gi/pygflags.o build/temp.linux-x86_64-3.7/gi/pygtype.o build/temp.linux-x86_64-3.7/gi/gimodule.o build/temp.linux-x86_64-3.7/gi/pygoptiongroup.o build/temp.linux-x86_64-3.7/gi/pygi-util.o build/tem
            p.linux-x86_64-3.7/gi/pygi-ccallback.o build/temp.linux-x86_64-3.7/gi/pygi-boxed.o build/temp.linux-x86_64-3.7/gi/pygi-struct.o build/temp.linux-x86_64-3.7/gi/pygi-argument.o build/temp.linux-x86_64-3.7/gi/gobjectmodule.o build/temp.linux-x86_64-3.7/gi/pygi-
            info.o build/temp.linux-x86_64-3.7/gi/pyglib.o build/temp.linux-x86_64-3.7/gi/pygi-foreign.o build/temp.linux-x86_64-3.7/gi/pygi-cache.o build/temp.linux-x86_64-3.7/gi/pygi-property.o build/temp.linux-x86_64-3.7/gi/pygi-invoke.o build/temp.linux-x86_64-3.7/g
            i/pygi-array.o build/temp.linux-x86_64-3.7/gi/pygparamspec.o build/temp.linux-x86_64-3.7/gi/pygpointer.o build/temp.linux-x86_64-3.7/gi/pygi-basictype.o build/temp.linux-x86_64-3.7/gi/pygi-source.o build/temp.linux-x86_64-3.7/gi/pygi-closure.o build/temp.lin
            ux-x86_64-3.7/gi/pygi-list.o build/temp.linux-x86_64-3.7/gi/pyginterface.o build/temp.linux-x86_64-3.7/gi/pygi-object.o build/temp.linux-x86_64-3.7/gi/pygi-signal-closure.o build/temp.linux-x86_64-3.7/gi/pygi-repository.o build/temp.linux-x86_64-3.7/gi/pygen
            um.o build/temp.linux-x86_64-3.7/gi/pygi-marshal-cleanup.o build/temp.linux-x86_64-3.7/gi/pygspawn.o build/temp.linux-x86_64-3.7/gi/pygi-error.o -L/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/glib-2.56.3-3t6c3e2qvdgyqub3qhf5yjasd7y5phky/lib -L/opt/spa
            ck/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/glib-2.56.3-3t6c3e2qvdgyqub3qhf5yjasd7y5phky/lib -L/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/glib-2.56.3-3t6c3e2qvdgyqub3qhf5yjasd7y5phky/lib -L/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/gobject-
            introspection-1.49.2-7h4quqdgf24wp5bu5jbpntoqyojr23zh/lib -L/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/libffi-3.2.1-yfdfl2vobpvgeutisf4igcpbl3sqyyli/lib/../lib64 -L/opt/spack/opt/spack/linux-gentoo2-x86_64/gcc-6.4.0/python-3.7.0-7uvmmlxp64xrkopbcuxg
            tflnduwgzk6z/lib -lglib-2.0 -lintl -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl -lgirepository-1.0 -lgobject-2.0 -lglib-2.0 -lintl -lffi -lpython3.7m -o build/lib.linux-x86_64-3.7/gi/_gi.cpython-37m-x86_64-linux-gnu.so
  >> 204    /usr/lib/gcc/x86_64-pc-linux-gnu/6.4.0/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lintl
  >> 205    /usr/lib/gcc/x86_64-pc-linux-gnu/6.4.0/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lintl
  >> 206    /usr/lib/gcc/x86_64-pc-linux-gnu/6.4.0/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lintl
  >> 207    collect2: error: ld returned 1 exit status
  >> 208    error: command '/usr/x86_64-pc-linux-gnu/gcc-bin/6.4.0/gcc' failed with exit status 1
```

### Platform and user environment

```commandline
$ uname -a 
Linux beluga 4.15.7-gentoo-r1 #4 SMP Wed Oct 10 18:26:17 CEST 2018 x86_64 Intel(R) Core(TM) i7-6950X CPU @ 3.00GHz GenuineIntel GNU/Linux
x86_64 GNU/Linux
```
[spack-build.out.txt](https://github.com/spack/spack/files/2635733/spack-build.out.txt)

[spack-build.env.txt](https://github.com/spack/spack/files/2635737/spack-build.env.txt)



Discussion:

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/glib/package.py b/var/spack/repos/builtin/packages/glib/package.py
--- a/var/spack/repos/builtin/packages/glib/package.py
+++ b/var/spack/repos/builtin/packages/glib/package.py
@@ -145,3 +145,18 @@ def filter_sbang(self):
 
         files = [join_path(self.prefix.bin, file) for file in files]
         filter_file(pattern, repl, *files, backup=False)
+
+    @run_after('install')
+    def gettext_libdir(self):
+        # Packages that link to glib were also picking up -lintl from glib's
+        # glib-2.0.pc file. However, packages such as py-pygobject were
+        # bypassing spack's compiler wrapper for linking and thus not finding
+        # the gettext library directory. The patch below explitly adds the
+        # appropriate -L path.
+        spec = self.spec
+        if spec.satisfies('@2:2.99'):
+            pattern = 'Libs:'
+            repl = 'Libs: -L{0} -Wl,-rpath={0} '.format(
+                   spec['gettext'].prefix.lib)
+            myfile = join_path(self.prefix.lib.pkgconfig, 'glib-2.0.pc')
+            filter_file(pattern, repl, myfile, backup=False)
diff --git a/var/spack/repos/builtin/packages/py-pygobject/package.py b/var/spack/repos/builtin/packages/py-pygobject/package.py
--- a/var/spack/repos/builtin/packages/py-pygobject/package.py
+++ b/var/spack/repos/builtin/packages/py-pygobject/package.py
@@ -35,6 +35,12 @@ class PyPygobject(PythonPackage):
     # for https://bugzilla.gnome.org/show_bug.cgi?id=668522
     patch('pygobject-2.28.6-gio-types-2.32.patch', when='@2.28.6')
 
+    # pygobject links directly using the compiler, not spack's wrapper.
+    # This causes it to fail to add the appropriate rpaths. This patch modifies
+    # pygobject's setup.py file to add -Wl,-rpath arguments for dependent
+    # libraries found with pkg-config.
+    patch('pygobject-3.28.3-setup-py.patch', when='@3.28.3')
+
     def url_for_version(self, version):
         url = 'http://ftp.gnome.org/pub/GNOME/sources/pygobject'
         return url + '/%s/pygobject-%s.tar.xz' % (version.up_to(2), version)


Test patch: