Repository: spack/spack

Issue:
Error building automake
On a Fedora 25 system, `spack install automake` fails with an error during the build phase. The build log ends with:

```
<snip>
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating t/wrap/aclocal-1.15
config.status: creating t/wrap/automake-1.15
config.status: linking <$HOME>/spack/var/spack/stage/automake-1.15-bhojsqx6uunp35vog7p2v7nmgcvypb36/automake-1.15/GNUmakefile to GNUmakefile
==> 'make' '-j8'
  GEN      bin/automake
  GEN      bin/aclocal
  GEN      t/ax/cc-no-c-o
  GEN      doc/aclocal.1
  GEN      runtest
  GEN      doc/automake.1
  GEN      t/ax/shell-no-trail-bslash
  GEN      lib/Automake/Config.pm
  GEN      t/ax/test-defs.sh
  GEN      doc/aclocal-1.15.1
  GEN      doc/automake-1.15.1
help2man: can't get `--help' info from automake-1.15
Try `--no-discard-stderr' if option outputs to stderr
Makefile:3687: recipe for target 'doc/automake-1.15.1' failed
make: *** [doc/automake-1.15.1] Error 2
make: *** Waiting for unfinished jobs....

```


Discussion:
Trying older versions...

- `spack install automake@1.14.1` fails in the same way
- `spack install automake@1.11.6` installs successfully
@fmahebert: can you try adding `depends_on('perl', type=('build', 'run'))` to the `automake` package?  If that works, can you submit a PR for this?
From googling, it looks like this fails when the system `perl` does not have the `Thread::Core` module.  We should probably expose the `perl` run dependency anyway.
I ran into this some months ago on a newly configured Centos 7 machine. I fixed it with 
```
yum install perl-Thread-Queue
``` 
[refer to this](https://github.com/hpcugent/easybuild-easyconfigs/issues/1822) 
I ran into this same problem once. Adding a Perl dependency fixed the package for me on Fedora and broke the package for me on CentOS. If I remember correctly, the package creates and runs a Perl script during installation and the shebang is too long. We may need to use some of @hartzell's magic on this one.
@tgamblin, @adamjstewart: adding the `perl` dependency to the `automake` package fixed the build issue on my Fedora machine. Would you like me to submit the PR, or wait until a CentOS-compatible solution is identified?

@sknigh: Installing `perl-Thread-Queue` also worked.
We definitely don't want to break the package on CentOS. Let me test it out and see if I can reproduce the problem.
On a recent `develop` on CentOS 7, I can build `spack install automake` w/out any problems.

If I add @tgamblin's `depends_on('perl', type=('build', 'run'))` to the automake package then I get this failure:

```
[...]
  GEN      doc/aclocal-1.15.1
help2man: can't get `--help' info from aclocal-1.15
Try `--no-discard-stderr' if option outputs to stderr
make: *** [doc/aclocal-1.15.1] Error 126
make: *** Waiting for unfinished jobs....
```

@adamjstewart -- have I reproduced the problem you saw or discovered something new?
Hmm, I just tried this again and I was able to build `automake` without any modifications on Fedora 25.
Ok, I can see most of the bits where the long sbang lines would cause that failure.

I'm not sure about my "magic", but I think that I can come up with a blunt object and bang it back into shape.
@adamjstewart: I checked again — I still find I can only build (unmodified) `automake` if I've previously installed Fedora's `perl-Thread-Queue` package.
We can add a `perl-thread-queue` package to Spack. But if we add a Perl dependency to Automake, we'll need @hartzell's magic to avoid some nasty shebang length issues.
I have the same issue on a freshly installed Arch Linux. It would be really nice to have this dependency. Is there a hot fix for the moment?
@erikzenker On Arch it appears to be a different issue. For me I got the same error message and managed to fix it by modifying the source code of automake and escape all curly braces in regex expressions. Here is a [patch for the fix](https://gist.github.com/xywei/03b546054f0d2e2f76c5ac530c88268a).
On Fedora 26, `automake` fails to compile even when I manually add a `perl` dependency to the package. I have not investigated further, though.
@michaelkuhn Using the `dnf` package `perl-Thread-Queue` still works (for me) to enable building `automake` on Fedora 26.
Ubuntu-17.10  (so yeah, admittedly I'm running not-yet-released software) gives this error too, even after installing perl-Thread-Queue (or on Ubuntu `libthread-queue-any-perl` and adding a dependency on perl. 

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/autoconf/package.py b/var/spack/repos/builtin/packages/autoconf/package.py
--- a/var/spack/repos/builtin/packages/autoconf/package.py
+++ b/var/spack/repos/builtin/packages/autoconf/package.py
@@ -39,9 +39,30 @@ class Autoconf(AutotoolsPackage):
     # Note: m4 is not a pure build-time dependency of autoconf. m4 is
     # needed when autoconf runs, not only when autoconf is built.
     depends_on('m4@1.4.6:', type=('build', 'run'))
+    depends_on('perl', type=('build', 'run'))
 
     build_directory = 'spack-build'
 
+    def patch(self):
+        # The full perl shebang might be too long; we have to fix this here
+        # because autom4te is called during the build
+        filter_file('^#! @PERL@ -w',
+                    '#! /usr/bin/env perl',
+                    'bin/autom4te.in')
+
+    @run_after('install')
+    def filter_sbang(self):
+        # We have to do this after install because otherwise the install
+        # target will try to rebuild the binaries (filter_file updates the
+        # timestamps)
+        perl = join_path(self.spec['perl'].prefix.bin, 'perl')
+
+        # Revert sbang, so Spack's sbang hook can fix it up
+        filter_file('^#! /usr/bin/env perl',
+                    '#! {0} -w'.format(perl),
+                    '{0}/autom4te'.format(self.prefix.bin),
+                    backup=False)
+
     def _make_executable(self, name):
         return Executable(join_path(self.prefix.bin, name))
 
diff --git a/var/spack/repos/builtin/packages/automake/package.py b/var/spack/repos/builtin/packages/automake/package.py
--- a/var/spack/repos/builtin/packages/automake/package.py
+++ b/var/spack/repos/builtin/packages/automake/package.py
@@ -31,14 +31,23 @@ class Automake(AutotoolsPackage):
     homepage = 'http://www.gnu.org/software/automake/'
     url      = 'http://ftp.gnu.org/gnu/automake/automake-1.15.tar.gz'
 
+    version('1.15.1', '95df3f2d6eb8f81e70b8cb63a93c8853')
     version('1.15',   '716946a105ca228ab545fc37a70df3a3')
     version('1.14.1', 'd052a3e884631b9c7892f2efce542d75')
     version('1.11.6', '0286dc30295b62985ca51919202ecfcc')
 
     depends_on('autoconf', type='build')
+    depends_on('perl', type=('build', 'run'))
 
     build_directory = 'spack-build'
 
+    def patch(self):
+        # The full perl shebang might be too long
+        for file in ('aclocal', 'automake'):
+            filter_file('^#!@PERL@ -w',
+                        '#!/usr/bin/env perl',
+                        't/wrap/{0}.in'.format(file))
+
     def _make_executable(self, name):
         return Executable(join_path(self.prefix.bin, name))
 


Test patch: