Repository: spack/spack

Issue:
glib not building on RedHat 7 with dtrace
On my redhat 7 that has dtrace installed, spack fail to build glib, this likely relates to PYTHONHOME being defined when depending on python, this makes system python scripts that sbang to system python exit with error  
ImportError: No module named site

-----

Summary

### Expected Result
spack install glib

### Actual Result

==> Applied patch no-Werror=format-security.patch
==> Building glib [AutotoolsPackage]
==> Executing phase: 'autoreconf'
==> Executing phase: 'configure'
==> Executing phase: 'build'
==> Error: ProcessError: Command exited with status 2:
    'make' '-j48'

4 errors found in build log:
     [ ... ]
     562   Making all in glib
     563   make[2]: Entering directory `/dev/shm/cibo19/spack-stage/spack-stage-giUdzE/glib-2.55.1/glib'
     564     GEN      glibconfig-stamp
     565     GEN      glib_probes.h
     566     GEN      glib_probes.lo
     567   ImportError: No module named site
  >> 568   make[2]: *** [glib_probes.h] Error 1
     569   make[2]: *** Waiting for unfinished jobs....
     570   ImportError: No module named site
  >> 571   make[2]: *** [glib_probes.lo] Error 1

### Steps to reproduce the issue

```console
$ which dtrace
$ head $(which dtrace)
$ dtrace
Usage /usr/bin/dtrace [--help] [-h | -G] [-C [-I<Path>]] -s File.d [-o <File>]   ### OK
$ spack install --keep-stage glib@2.55.1%gcc@4.8.5
$ spack env glib@2.55.1%gcc@4.8.5 dtrace
ImportError: No module named site   ### python error
$ spack env glib@2.55.1%gcc@4.8.5 /bin/bash
$ dtrace
ImportError: No module named site ### python error
$ unset PYTHONHOME
$ dtrace
Usage /usr/bin/dtrace [--help] [-h | -G] [-C [-I<Path>]] -s File.d [-o <File>]   ### OK



Add dtrace variant to glib  (RH7)
fix issue #6965 building glib on a system with dtrace installed (RH7)
The issue seem related to PYTHONHOME env var set on python dependent packages that 
( on RH7 ) seem to prevent usage of system python scripts that, like dtrace are installed with sbang to
system python
It is a kind of workaround, would probably be better to provide a spack installation of dtrace 


Discussion:
Just ran into the same issue. Older glib - 2.53.1 working fine still.
Hi,
Same for me on CentOS 7. The workaround by luigi-calori worked, many thanks for that.

Cheers, Pascal 
@luigi-calori tried this PR and it seems to work as expected, a simple sync with upstream is all you need to trigger Travis to run again (it was broken at the time of this PR).
Can anybody check if this is also solved by #7157 ?

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
@@ -24,16 +24,24 @@
 ##############################################################################
 from spack import *
 
+import os.path
+import shutil
+
 
 class Glib(AutotoolsPackage):
-    """The GLib package contains a low-level libraries useful for
-       providing data structure handling for C, portability wrappers
-       and interfaces for such runtime functionality as an event loop,
-       threads, dynamic loading and an object system."""
+    """GLib provides the core application building blocks for
+    libraries and applications written in C.
+
+    The GLib package contains a low-level libraries useful for
+    providing data structure handling for C, portability wrappers
+    and interfaces for such runtime functionality as an event loop,
+    threads, dynamic loading and an object system.
+    """
 
     homepage = "https://developer.gnome.org/glib/"
     url      = "https://ftp.gnome.org/pub/gnome/sources/glib/2.53/glib-2.53.1.tar.xz"
 
+    version('2.56.0', 'f2b59392f2fb514bbe7791dda0c36da5')
     version('2.55.1', '9cbb6b3c7e75ba75575588497c7707b6')
     version('2.53.1', '3362ef4da713f834ea26904caf3a75f5')
     version('2.49.7', '397ead3fcf325cb921d54e2c9e7dfd7a')
@@ -42,6 +50,13 @@ class Glib(AutotoolsPackage):
     version('2.42.1', '89c4119e50e767d3532158605ee9121a')
 
     variant('libmount', default=False, description='Build with libmount support')
+    variant(
+        'tracing',
+        default='',
+        values=('dtrace', 'systemtap'),
+        multi=True,
+        description='Enable tracing support'
+    )
 
     depends_on('pkgconfig', type='build')
     depends_on('libffi')
@@ -64,16 +79,18 @@ def url_for_version(self, version):
         return url + '/%s/glib-%s.tar.xz' % (version.up_to(2), version)
 
     def configure_args(self):
-        spec = self.spec
         args = []
-
-        if '+libmount' in spec:
-            args.append('--enable-libmount')
-        else:
-            args.append('--disable-libmount')
-
+        args.extend(self.enable_or_disable('libmount'))
+        args.append('--with-python={0}'.format(
+            os.path.basename(self.spec['python'].command.path))
+        )
+        args.extend(self.enable_or_disable('tracing'))
         return args
 
+    @property
+    def dtrace_copy_path(self):
+        return join_path(self.stage.source_path, 'dtrace-copy')
+
     @run_before('configure')
     def fix_python_path(self):
         if not self.spec.satisfies('@2.53.4:'):
@@ -85,6 +102,32 @@ def fix_python_path(self):
                     '#!/usr/bin/env python',
                     *files)
 
+    @run_before('configure')
+    def fix_dtrace_usr_bin_path(self):
+        if 'tracing=dtrace' not in self.spec:
+            return
+
+        # dtrace may cause glib build to fail because it uses
+        # '/usr/bin/python' in the shebang. To work around that
+        # we copy the original script into a temporary folder, and
+        # change the shebang to '/usr/bin/env python'
+        dtrace = which('dtrace').path
+        dtrace_copy = join_path(self.dtrace_copy_path, 'dtrace')
+
+        with working_dir(self.dtrace_copy_path, create=True):
+            shutil.copy(dtrace, dtrace_copy)
+            filter_file(
+                '^#!/usr/bin/python',
+                '#!/usr/bin/env python',
+                dtrace_copy
+            )
+
+        # To have our own copy of dtrace in PATH, we need to
+        # prepend to PATH the temporary folder where it resides
+        env['PATH'] = ':'.join(
+            [self.dtrace_copy_path] + env['PATH'].split(':')
+        )
+
     @run_after('install')
     def filter_sbang(self):
         # Revert sbang, so Spack's sbang hook can fix it up (we have to do


Test patch: