Repository: spack/spack

Issue:
Installation issue: gperf@3.0.4 %gcc@9.2.0 
### Steps to reproduce the issue

* git hash: 5582de837ef126ad736d44565a8d4231225b29da

```console
rm -rf ~/.spack
cd /scratch
rm -rf spack
git clone git@github.com:spack/spack && cd spack
export SPACK_ROOT=`pwd`
export PATH=`pwd`/bin:$PATH
spack compiler add
spack compiler list

==> Available compilers
-- gcc rhel7-x86_64 ---------------------------------------------
gcc@4.8.5

spack install gcc@9.2.0
source /scratch/spack/share/spack/setup-env.sh
spack load gcc@9.2.0
spack compiler add
spack compiler list

==> Available compilers
-- gcc rhel7-x86_64 ---------------------------------------------
gcc@9.2.0  gcc@4.8.5

spack install gperf % gcc@9.2.0

==> Installing gperf
==> Searching for binary cache of gperf
==> Warning: No Spack mirrors are currently configured
==> No binary for gperf found: installing from source
==> Fetching https://ftpmirror.gnu.org/gperf/gperf-3.0.4.tar.gz
######################################################################## 100.0%
==> Staging archive: /tmp/kellyt/spack-stage/spack-stage-gperf-3.0.4-buvuipk2igiid63wnhg2ckrdoayrpz7c/gperf-3.0.4.tar.gz
==> Created stage in /tmp/kellyt/spack-stage/spack-stage-gperf-3.0.4-buvuipk2igiid63wnhg2ckrdoayrpz7c
==> No patches needed for gperf
==> Building gperf [AutotoolsPackage]
==> Executing phase: 'autoreconf'
==> Executing phase: 'configure'
==> Executing phase: 'build'
==> Error: ProcessError: Command exited with status 2:
    'make' '-j16'

6 errors found in build log:
     95     /scratch/kellyt/spack.20191025/lib/spack/env/gcc/g++ -g -O2 -I. -I.
            /../lib -c ./bool-array.cc
     96     /scratch/kellyt/spack.20191025/lib/spack/env/gcc/g++ -g -O2 -I. -I.
            /../lib -c ./hash-table.cc
     97     /scratch/kellyt/spack.20191025/lib/spack/env/gcc/g++ -g -O2 -I. -I.
            /../lib -c ./search.cc
     98     /scratch/kellyt/spack.20191025/lib/spack/env/gcc/g++ -g -O2 -I. -I.
            /../lib -c ./output.cc
     99     /scratch/kellyt/spack.20191025/lib/spack/env/gcc/g++ -g -O2 -I. -I.
            /../lib -c ./main.cc
     100    /scratch/kellyt/spack.20191025/lib/spack/env/gcc/g++ -g -O2  -o gpe
            rf version.o positions.o options.o keyword.o keyword-list.o input.o
             bool-array.o hash-table.o search.o output.o main.o ../lib/libgp.a 
            -lm
  >> 101    /scratch/kellyt/spack.20191025/opt/spack/linux-rhel7-haswell/gcc-4.
            8.5/gcc-9.2.0-7qmgg3c3xt2bpg4typfxg7imt5zgpi3i/lib/gcc/x86_64-pc-li
            nux-gnu/9.2.0/../../../../lib64/libstdc++.so: undefined reference t
            o `libiconv'
  >> 102    /scratch/kellyt/spack.20191025/opt/spack/linux-rhel7-haswell/gcc-4.
            8.5/gcc-9.2.0-7qmgg3c3xt2bpg4typfxg7imt5zgpi3i/lib/gcc/x86_64-pc-li
            nux-gnu/9.2.0/../../../../lib64/libstdc++.so: undefined reference t
            o `libiconv_close'
  >> 103    /scratch/kellyt/spack.20191025/opt/spack/linux-rhel7-haswell/gcc-4.
            8.5/gcc-9.2.0-7qmgg3c3xt2bpg4typfxg7imt5zgpi3i/lib/gcc/x86_64-pc-li
            nux-gnu/9.2.0/../../../../lib64/libstdc++.so: undefined reference t
            o `libiconv_open'
  >> 104    collect2: error: ld returned 1 exit status
```

### Platform and user environment

Please report your OS here:
```commandline
$ uname -a 
Linux x.x.x 3.10.0-1062.1.2.el7.x86_64 #1 SMP Mon Sep 16 14:19:51 EDT 2019 x86_64 x86_64 x86_64 GNU/Linux

$ lsb_release -d
Description:	Red Hat Enterprise Linux Workstation release 7.7 (Maipo)

% spack arch
linux-rhel7-broadwell
``` 

config files
- [compilers.yaml.txt](https://github.com/spack/spack/files/3774427/compilers.yaml.txt)
- I don't have a packages.yaml file.

### Additional information

Sometimes the issue benefits from additional details. In these cases there are
a few things we can suggest doing. First of all, you can post the full output of:

```console
$ spack spec --install-status  gperf
Input spec
--------------------------------
 -   gperf

Concretized
--------------------------------
 -   gperf@3.0.4%gcc@9.2.0 arch=linux-rhel7-broadwell
```

to show people whether Spack installed a faulty software or if it was not able to
build it at all. 

```console
$ spack location -s gperf
/tmp/$USER/spack-stage/spack-stage-gperf-3.0.4-buvuipk2igiid63wnhg2ckrdoayrpz7c
```
Rebuilding the package with the following options:
```console
$ spack -d install -j 1 gperf
# produced the same error as above.
```


gcc builds fail for C++ projects
It looks like #13202 introduced a libiconv dependency for gcc. This makes C++ builds fail for me with error messages such as these (in this case, cmake bootstrapping):
```
../opt/spack/linux-ubuntu18.04-x86_64/gcc-7.4.0/gcc-9.2.0-7kzpeggfu32layxcv6rzunmef5ah7uwv/lib/gcc/x86_64-pc-linux-gnu/9.2.0/../../../../lib64/libstdc++.so: undefined reference to `libiconv'
```
It looks like the binaries are correctly linked against libiconv but libstdc++ is not.

cc @adamjstewart


Discussion:
Seeing a very similar issue when installing `pango` on spack v0.13.0 with gcc@9.2.0

```
 >> 734    /opt/spack/opt/spack/linux-centos7-x86_64/gcc-4.8.5/gcc-9.2.0-6snvhqjum3bfpup574azrjrt7kfwyg5s/lib/gcc/x86_64-pc-linux-gnu/
            9.2.0/../../../../lib64/libstdc++.so: undefined reference to `libiconv'
  >> 735    /opt/spack/opt/spack/linux-centos7-x86_64/gcc-4.8.5/gcc-9.2.0-6snvhqjum3bfpup574azrjrt7kfwyg5s/lib/gcc/x86_64-pc-linux-gnu/
            9.2.0/../../../../lib64/libstdc++.so: undefined reference to `libiconv_close'
  >> 736    /opt/spack/opt/spack/linux-centos7-x86_64/gcc-4.8.5/gcc-9.2.0-6snvhqjum3bfpup574azrjrt7kfwyg5s/lib/gcc/x86_64-pc-linux-gnu/
            9.2.0/../../../../lib64/libstdc++.so: undefined reference to `libiconv_open'
  >> 737    collect2: error: ld returned 1 exit status
  >> 738    make[2]: *** [cxx-test] Error 1

```
If I back up to hash 6cd997e74379a2dd8719748095bd13a26dfbf74f (2019 Oct 16) this sequence of commands works.  If I diff this this version of `$SPACK_ROOT/var/spack/repos/builtin/packages/gcc/package.py` against 6cd997e74379a2dd8719748095bd13a26dfbf74f (current `develop` branch), I see a lot of changes -- including a suspicious addition of `depends_on('libiconv')`.

```diff
diff --git a/var/spack/repos/builtin/packages/gcc/package.py b/var/spack/repos/builtin/packages/gcc/package.py
index 836c018..5551ba0 100644
--- a/var/spack/repos/builtin/packages/gcc/package.py
+++ b/var/spack/repos/builtin/packages/gcc/package.py
@@ -100,7 +100,6 @@ class Gcc(AutotoolsPackage):
     depends_on('isl@0.15:0.18', when='@6:8.9')
     depends_on('isl@0.15:0.20', when='@9:')
     depends_on('zlib', when='@6:')
-    depends_on('libiconv')
     depends_on('gnat', when='languages=ada')
     depends_on('binutils~libiberty', when='+binutils')
     depends_on('zip', type='build', when='languages=java')
@@ -196,14 +195,6 @@ class Gcc(AutotoolsPackage):
             # https://trac.macports.org/ticket/56502#no1
             # see also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83531
             patch('darwin/headers-10.13-fix.patch', when='@5.5.0')
-        if macos_version() >= Version('10.15'):
-            # Fix system headers for Catalina SDK
-            # (otherwise __OSX_AVAILABLE_STARTING ends up undefined)
-            patch('https://raw.githubusercontent.com/Homebrew/formula-patches/b8b8e65e/gcc/9.2.0-catalina.patch',
-                  sha256='0b8d14a7f3c6a2f0d2498526e86e088926671b5da50a554ffa6b7f73ac4f132b', when='@9.2.0:')
-        # Use -headerpad_max_install_names in the build,
-        # otherwise updated load commands won't fit in the Mach-O header.
-        # This is needed because `gcc` avoids the superenv shim.
         patch('darwin/gcc-7.1.0-headerpad.patch', when='@5:')
         patch('darwin/gcc-6.1.0-jit.patch', when='@5:7')
         patch('darwin/gcc-4.9.patch1', when='@4.9.0:4.9.3')
@@ -264,24 +255,18 @@ class Gcc(AutotoolsPackage):
                         '-I{0}'.format(spec['zlib'].prefix.include),
                         'gcc/Makefile.in')
 
-    # https://gcc.gnu.org/install/configure.html
     def configure_args(self):
         spec = self.spec
 
         # Generic options to compile GCC
         options = [
-            # Distributor options
-            '--with-pkgversion=Spack GCC',
-            '--with-bugurl=https://github.com/spack/spack/issues',
-            # Xcode 10 dropped 32-bit support
             '--disable-multilib',
             '--enable-languages={0}'.format(
                 ','.join(spec.variants['languages'].value)),
-            # Drop gettext dependency
-            '--disable-nls',
-            '--with-libiconv-prefix={0}'.format(spec['libiconv'].prefix),
             '--with-mpfr={0}'.format(spec['mpfr'].prefix),
             '--with-gmp={0}'.format(spec['gmp'].prefix),
+            '--enable-lto',
+            '--with-quad'
         ]
 
         # Use installed libz
@@ -295,7 +280,7 @@ class Gcc(AutotoolsPackage):
         # Binutils
         if spec.satisfies('+binutils'):
             static_bootstrap_flags = '-static-libstdc++ -static-libgcc'
-            options.extend([
+            binutils_options = [
                 '--with-sysroot=/',
                 '--with-stage1-ldflags={0} {1}'.format(
                     self.rpath_args, static_bootstrap_flags),
@@ -305,7 +290,8 @@ class Gcc(AutotoolsPackage):
                 '--with-ld={0}/ld'.format(spec['binutils'].prefix.bin),
                 '--with-gnu-as',
                 '--with-as={0}/as'.format(spec['binutils'].prefix.bin),
-            ])
+            ]
+            options.extend(binutils_options)
 
         # MPC
         if 'mpc' in spec:
@@ -315,6 +301,10 @@ class Gcc(AutotoolsPackage):
         if 'isl' in spec:
             options.append('--with-isl={0}'.format(spec['isl'].prefix))
 
+        # macOS
+        if sys.platform == 'darwin':
+            options.append('--with-build-config=bootstrap-debug')
+
         # nvptx-none offloading for host compiler
         if spec.satisfies('+nvptx'):
             options.extend(['--enable-offload-targets=nvptx-none',
@@ -393,6 +383,12 @@ class Gcc(AutotoolsPackage):
             make('install')
 
     @property
+    def build_targets(self):
+        if sys.platform == 'darwin':
+            return ['bootstrap']
+        return []
+
+    @property
     def install_targets(self):
         if '+strip' in self.spec:
             return ['install-strip']
```


These changes that break gcc@9.2.0 for me were added in Pull #13202 to fix a macOS build issue.  Removing these two lines from `develop` branch fixes the gcc recipe for me.

```diff
diff --git a/var/spack/repos/builtin/packages/gcc/package.py b/var/spack/repos/builtin/packages/gcc/package.py
index 836c018..7e4e4a7 100644
--- a/var/spack/repos/builtin/packages/gcc/package.py
+++ b/var/spack/repos/builtin/packages/gcc/package.py
@@ -100,7 +100,7 @@ class Gcc(AutotoolsPackage):
     depends_on('isl@0.15:0.18', when='@6:8.9')
     depends_on('isl@0.15:0.20', when='@9:')
     depends_on('zlib', when='@6:')
-    depends_on('libiconv')
+    # depends_on('libiconv')
     depends_on('gnat', when='languages=ada')
     depends_on('binutils~libiberty', when='+binutils')
     depends_on('zip', type='build', when='languages=java')
@@ -279,7 +279,7 @@ class Gcc(AutotoolsPackage):
                 ','.join(spec.variants['languages'].value)),
             # Drop gettext dependency
             '--disable-nls',
-            '--with-libiconv-prefix={0}'.format(spec['libiconv'].prefix),
+#            '--with-libiconv-prefix={0}'.format(spec['libiconv'].prefix),
             '--with-mpfr={0}'.format(spec['mpfr'].prefix),
             '--with-gmp={0}'.format(spec['gmp'].prefix),
         ]
```
This change did the trick for me as well. Thanks!
I've also confirmed that the following patch works (and this one should keep the macOS UseCase working).

```diff
diff --git a/var/spack/repos/builtin/packages/gcc/package.py b/var/spack/repos/builtin/packages/gcc/package.py
index 836c018..4a8b75a 100644
--- a/var/spack/repos/builtin/packages/gcc/package.py
+++ b/var/spack/repos/builtin/packages/gcc/package.py
@@ -100,7 +100,7 @@ class Gcc(AutotoolsPackage):
     depends_on('isl@0.15:0.18', when='@6:8.9')
     depends_on('isl@0.15:0.20', when='@9:')
     depends_on('zlib', when='@6:')
-    depends_on('libiconv')
+    depends_on('libiconv', type='build')
     depends_on('gnat', when='languages=ada')
     depends_on('binutils~libiberty', when='+binutils')
     depends_on('zip', type='build', when='languages=java')
@@ -279,7 +279,6 @@ class Gcc(AutotoolsPackage):
                 ','.join(spec.variants['languages'].value)),
             # Drop gettext dependency
             '--disable-nls',
-            '--with-libiconv-prefix={0}'.format(spec['libiconv'].prefix),
             '--with-mpfr={0}'.format(spec['mpfr'].prefix),
             '--with-gmp={0}'.format(spec['gmp'].prefix),
         ]
@@ -328,7 +327,8 @@ class Gcc(AutotoolsPackage):
         if sys.platform == 'darwin':
             options.extend([
                 '--with-native-system-header-dir=/usr/include',
-                '--with-sysroot={0}'.format(macos_sdk_path())
+                '--with-sysroot={0}'.format(macos_sdk_path()),
+                '--with-libiconv-prefix={0}'.format(spec['libiconv'].prefix),
             ])
 
         return options
```

Should I open a PR?  Can someone test this?
Hmm, that’s odd. I only added the libiconv dependency because I noticed the build was picking up my system libiconv. If it’s problematic, we can remove it.
Without the libiconv dependency, gcc seems to pick the symbols from glibc for me (that's on Linux, though). I found a few references to this bug but no fixes (always seems to happen if libiconv is not available in a system prefix). Have you checked whether libstdc++ is actually linked against/using Spack's libiconv in your case?
It looks like none of my libraries link to `libiconv`, only the compiler binaries.
> It looks like none of my libraries link to `libiconv`, only the compiler binaries.

Same for me. Does your libstdc++ still try to use the symbols (`readelf -s .../lib64/libstdc++.so | grep iconv`)?

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/gcc/package.py b/var/spack/repos/builtin/packages/gcc/package.py
--- a/var/spack/repos/builtin/packages/gcc/package.py
+++ b/var/spack/repos/builtin/packages/gcc/package.py
@@ -100,7 +100,7 @@ class Gcc(AutotoolsPackage):
     depends_on('isl@0.15:0.18', when='@6:8.9')
     depends_on('isl@0.15:0.20', when='@9:')
     depends_on('zlib', when='@6:')
-    depends_on('libiconv')
+    depends_on('libiconv', when='platform=darwin')
     depends_on('gnat', when='languages=ada')
     depends_on('binutils~libiberty', when='+binutils')
     depends_on('zip', type='build', when='languages=java')
@@ -279,7 +279,6 @@ def configure_args(self):
                 ','.join(spec.variants['languages'].value)),
             # Drop gettext dependency
             '--disable-nls',
-            '--with-libiconv-prefix={0}'.format(spec['libiconv'].prefix),
             '--with-mpfr={0}'.format(spec['mpfr'].prefix),
             '--with-gmp={0}'.format(spec['gmp'].prefix),
         ]
@@ -328,7 +327,8 @@ def configure_args(self):
         if sys.platform == 'darwin':
             options.extend([
                 '--with-native-system-header-dir=/usr/include',
-                '--with-sysroot={0}'.format(macos_sdk_path())
+                '--with-sysroot={0}'.format(macos_sdk_path()),
+                '--with-libiconv-prefix={0}'.format(spec['libiconv'].prefix)
             ])
 
         return options
@@ -424,7 +424,7 @@ def write_rpath_specs(self):
                               self.prefix.lib, self.prefix.lib64))
         set_install_permissions(specs_file)
 
-    def setup_environment(self, spack_env, run_env):
+    def setup_run_environment(self, env):
         # Search prefix directory for possibly modified compiler names
         from spack.compilers.gcc import Gcc as Compiler
 
@@ -450,6 +450,6 @@ def setup_environment(self, spack_env, run_env):
                     continue
 
                 # Set the proper environment variable
-                run_env.set(lang.upper(), abspath)
+                env.set(lang.upper(), abspath)
                 # Stop searching filename/regex combos for this language
                 break


Test patch: