Repository: spack/spack

Issue:
Installation issue: py-numpy
### Steps to reproduce the issue

`py-numpy@1.22.3%gcc@4.8.5+blas+lapack patches=873745d arch=linux-rhel7-sandybridge` is required by `py-astropy@4.0.1.post1%gcc@4.8.5~extras arch=linux-rhel7-sandybridge` and `py-numpy` is broken.

```console
$ spack spec -I py-numpy@1.22.3%gcc@4.8.5+blas+lapack patches=873745d arch=linux-rhel7-sandybridge
Input spec
--------------------------------
 -   py-numpy@1.22.3%gcc@4.8.5+blas+lapack patches=873745d arch=linux-rhel7-sandybridge

Concretized
--------------------------------
 -   py-numpy@1.22.3%gcc@4.8.5+blas+lapack patches=873745d arch=linux-rhel7-sandybridge
[+]      ^openblas@0.3.20%gcc@4.8.5~bignuma~consistent_fpcsr~ilp64+locking+pic+shared symbol_suffix=none threads=none arch=linux-rhel7-sandybridge
[+]          ^perl@5.34.1%gcc@4.8.5+cpanm+shared+threads arch=linux-rhel7-sandybridge
[+]              ^berkeley-db@18.1.40%gcc@4.8.5+cxx~docs+stl patches=b231fcc arch=linux-rhel7-sandybridge
[+]              ^bzip2@1.0.8%gcc@4.8.5~debug~pic+shared arch=linux-rhel7-sandybridge
[+]                  ^diffutils@3.8%gcc@4.8.5 arch=linux-rhel7-sandybridge
[+]                      ^libiconv@1.16%gcc@4.8.5 libs=shared,static arch=linux-rhel7-sandybridge
[+]              ^gdbm@1.19%gcc@4.8.5 arch=linux-rhel7-sandybridge
[+]                  ^readline@8.1%gcc@4.8.5 arch=linux-rhel7-sandybridge
[+]                      ^ncurses@6.2%gcc@4.8.5~symlinks+termlib abi=none arch=linux-rhel7-sandybridge
[+]                          ^pkgconf@1.8.0%gcc@4.8.5 arch=linux-rhel7-sandybridge
[+]              ^zlib@1.2.12%gcc@4.8.5+optimize+pic+shared patches=0d38234 arch=linux-rhel7-sandybridge
[+]      ^py-cython@0.29.24%gcc@4.8.5 arch=linux-rhel7-sandybridge
[+]          ^py-pip@21.3.1%gcc@4.8.5 arch=linux-rhel7-sandybridge
[+]              ^python@3.9.12%gcc@4.8.5+bz2+ctypes+dbm~debug+ensurepip+libxml2+lzma~nis~optimizations+pic+pyexpat+pythoncmd+readline+shared+sqlite3+ssl~tix~tkinter~ucs4+uuid+zlib patches=0d98e93,4c24573,f2fd060 arch=linux-rhel7-sandybridge
[+]                  ^expat@2.4.8%gcc@4.8.5+libbsd arch=linux-rhel7-sandybridge
[+]                      ^libbsd@0.11.5%gcc@4.8.5 arch=linux-rhel7-sandybridge
[+]                          ^libmd@1.0.4%gcc@4.8.5 arch=linux-rhel7-sandybridge
[+]                  ^gettext@0.21%gcc@4.8.5+bzip2+curses+git~libunistring+libxml2+tar+xz arch=linux-rhel7-sandybridge
[+]                      ^libxml2@2.9.12%gcc@4.8.5~python arch=linux-rhel7-sandybridge
[+]                          ^xz@5.2.5%gcc@4.8.5~pic libs=shared,static arch=linux-rhel7-sandybridge
[+]                      ^tar@1.34%gcc@4.8.5 arch=linux-rhel7-sandybridge
[+]                  ^libffi@3.4.2%gcc@4.8.5 arch=linux-rhel7-sandybridge
[+]                  ^openssl@1.1.1n%gcc@4.8.5~docs~shared certs=system arch=linux-rhel7-sandybridge
[+]                  ^sqlite@3.37.2%gcc@4.8.5+column_metadata+dynamic_extensions+fts~functions+rtree arch=linux-rhel7-sandybridge
[+]                  ^util-linux-uuid@2.36.2%gcc@4.8.5 arch=linux-rhel7-sandybridge
[+]          ^py-setuptools@59.4.0%gcc@4.8.5 arch=linux-rhel7-sandybridge
[+]              ^py-wheel@0.37.0%gcc@4.8.5 arch=linux-rhel7-sandybridge
```

### Error message

<details><summary>Error message</summary>

<pre>
==> Waiting for berkeley-db-18.1.40-fyry4g6hq3nkqcsz6dlh3u5mmyju55wx
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/berkeley-db-18.1.40-fyry4g6hq3nkqcsz6dlh3u5mmyju55wx
==> Waiting for libiconv-1.16-4qqf5pqpbxwnlt3owfm6pj6fb3sn37sq
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/libiconv-1.16-4qqf5pqpbxwnlt3owfm6pj6fb3sn37sq
==> Waiting for pkgconf-1.8.0-7kmkypfjqaorpzuf2p2mivjuebg6hyij
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/pkgconf-1.8.0-7kmkypfjqaorpzuf2p2mivjuebg6hyij
==> Waiting for zlib-1.2.12-msuii4kp4e46g7lh5dr74czppjsvk2re
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/zlib-1.2.12-msuii4kp4e46g7lh5dr74czppjsvk2re
==> Waiting for libmd-1.0.4-35xqwipnqako2qgdsfrkqxjdybmqpzkn
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/libmd-1.0.4-35xqwipnqako2qgdsfrkqxjdybmqpzkn
==> Waiting for xz-5.2.5-go6qry4bqfqaj246a5ck55yf6pkb2ndn
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/xz-5.2.5-go6qry4bqfqaj246a5ck55yf6pkb2ndn
==> Waiting for libffi-3.4.2-irxuvtzy3ddqou67xrjactho3zdtgtwl
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/libffi-3.4.2-irxuvtzy3ddqou67xrjactho3zdtgtwl
==> Waiting for diffutils-3.8-wj3acfud7326ub447f4kjq2upmllofy6
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/diffutils-3.8-wj3acfud7326ub447f4kjq2upmllofy6
==> Waiting for tar-1.34-xpxr5qszuxtggvr6idkxl5ju2wizcdzk
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/tar-1.34-xpxr5qszuxtggvr6idkxl5ju2wizcdzk
==> Waiting for ncurses-6.2-d7foh77mcfcqax3cu4jccznlgwtmhvzc
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/ncurses-6.2-d7foh77mcfcqax3cu4jccznlgwtmhvzc
==> Waiting for util-linux-uuid-2.36.2-chk6oezxwyvc2m36r2nkoi7vfuon2bfc
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/util-linux-uuid-2.36.2-chk6oezxwyvc2m36r2nkoi7vfuon2bfc
==> Waiting for libbsd-0.11.5-d7zeoouaepet4ba2cjz5qbj4su6i4sf3
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/libbsd-0.11.5-d7zeoouaepet4ba2cjz5qbj4su6i4sf3
==> Waiting for libxml2-2.9.12-i4llyifcykcr7gejt2wzp4zyb6dvcog7
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/libxml2-2.9.12-i4llyifcykcr7gejt2wzp4zyb6dvcog7
==> Waiting for bzip2-1.0.8-owdgloujfknipzijl2xtvoqsvjmxt4gd
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/bzip2-1.0.8-owdgloujfknipzijl2xtvoqsvjmxt4gd
==> Waiting for readline-8.1-kyg6pdhggjwcef2rzz57rsjl7lfhqkxq
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/readline-8.1-kyg6pdhggjwcef2rzz57rsjl7lfhqkxq
==> Waiting for expat-2.4.8-6v7cu7nettemntpao5foj4ppciu2gtv2
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/expat-2.4.8-6v7cu7nettemntpao5foj4ppciu2gtv2
==> Waiting for gettext-0.21-r4ou6mew4n37myqfily2vc4v7mpxxipl
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/gettext-0.21-r4ou6mew4n37myqfily2vc4v7mpxxipl
==> Waiting for gdbm-1.19-sxfep2mtglsl2nhcxui374xl57fjnrwg
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/gdbm-1.19-sxfep2mtglsl2nhcxui374xl57fjnrwg
==> Waiting for sqlite-3.37.2-2trymqenzisuidmzfz5b7dvwbxs3weew
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/sqlite-3.37.2-2trymqenzisuidmzfz5b7dvwbxs3weew
==> Waiting for perl-5.34.1-bp4i65ba2r6tblhekbdotpvpr3oxlrgo
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/perl-5.34.1-bp4i65ba2r6tblhekbdotpvpr3oxlrgo
==> Waiting for openblas-0.3.20-ziwwf5puvcrpmsdg7igyg67vz6roklhd
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/openblas-0.3.20-ziwwf5puvcrpmsdg7igyg67vz6roklhd
==> Waiting for openssl-1.1.1n-hwcooagjm5otgokd6wd6pzxxab74dmep
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/openssl-1.1.1n-hwcooagjm5otgokd6wd6pzxxab74dmep
==> Waiting for python-3.9.12-5ro5aitwt7ustj4m5fcnkffmy2l6o4wj
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/python-3.9.12-5ro5aitwt7ustj4m5fcnkffmy2l6o4wj
==> Waiting for py-pip-21.3.1-flr2jbdnsv5t5wqedv33iqoyjblg2aok
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/py-pip-21.3.1-flr2jbdnsv5t5wqedv33iqoyjblg2aok
==> Waiting for py-wheel-0.37.0-53alem6evlqsigg2bqapouyyrrd7zr23
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/py-wheel-0.37.0-53alem6evlqsigg2bqapouyyrrd7zr23
==> Waiting for py-setuptools-59.4.0-dihwzooxmrbevsj4cpyyckrjq6ssgdj4
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/py-setuptools-59.4.0-dihwzooxmrbevsj4cpyyckrjq6ssgdj4
==> Waiting for py-cython-0.29.24-ip2sdqhpkqjuy4mwck47jmjzfmd2bhuv
[+] /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/py-cython-0.29.24-ip2sdqhpkqjuy4mwck47jmjzfmd2bhuv
==> Waiting for py-numpy-1.22.3-r4iztlkbhxe2r6jnlshkb4wk2ysj4ma7
==> Installing py-numpy-1.22.3-r4iztlkbhxe2r6jnlshkb4wk2ysj4ma7
==> No binary for py-numpy-1.22.3-r4iztlkbhxe2r6jnlshkb4wk2ysj4ma7 found: installing from source
==> Using cached archive: /scratch/users/grayson5/spack/var/spack/cache/_source-cache/archive/db/dbc7601a3b7472d559dc7b933b18b4b66f9aa7452c120e87dfb33d02008c8a18.zip
==> Applied patch /scratch/users/grayson5/spack/var/spack/repos/builtin/packages/py-numpy/check_executables.patch
==> py-numpy: Executing phase: 'install'
==> Error: ProcessError: Command exited with status 1:
    '/scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/python-3.9.12-5ro5aitwt7ustj4m5fcnkffmy2l6o4wj/bin/python3.9' '-m' 'pip' '-vvv' '--no-input' '--no-cache-dir' '--disable-pip-version-check' 'install' '--no-deps' '--ignore-installed' '--no-build-isolation' '--no-warn-script-location' '--no-index' '--prefix=/scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/py-numpy-1.22.3-r4iztlkbhxe2r6jnlshkb4wk2ysj4ma7' '.'

3 errors found in build log:
     109      INFO:     library_dirs = ['/scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/openblas-0.3.20-ziwwf5puvcrpmsdg7igyg67vz6roklhd/lib
            ']
     110      INFO:     language = c
     111      INFO:     define_macros = [('HAVE_CBLAS', None)]
     112      INFO:     runtime_library_dirs = ['/scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/openblas-0.3.20-ziwwf5puvcrpmsdg7igyg67vz6ro
            klhd/lib']
     113      INFO:
     114      Warning: attempted relative import with no known parent package
  >> 115    /scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/python-3.9.12-5ro5aitwt7ustj4m5fcnkffmy2l6o4wj/lib/python3.9/distutils/dist.py:27
            4: UserWarning: Unknown distribution option: 'define_macros'
     116        warnings.warn(msg)
     117      running bdist_wheel
     118      running build
     119      running config_cc
     120      INFO: unifing config_cc, config, build_clib, build_ext, build commands --compiler options
     121      running config_fc

     ...

     130      INFO: building library "npymath" sources
     131      WARN: Could not locate executable armflang
     132      _configtest.cxx: In function 'int main()':
     133      _configtest.cxx:1:17: warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]
     134       int main(void){ auto x = 0.0; return static_cast<int>(x); }
     135                       ^
  >> 136    _configtest.cxx:1:22: error: 'x' does not name a type
     137       int main(void){ auto x = 0.0; return static_cast<int>(x); }
     138                            ^
  >> 139      _configtest.cxx:1:55: error: 'x' was not declared in this scope
     140       int main(void){ auto x = 0.0; return static_cast<int>(x); }
     141                                                             ^
     142      Traceback (most recent call last):
     143        File "/scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/py-pip-21.3.1-flr2jbdnsv5t5wqedv33iqoyjblg2aok/lib/python3.9/site-packa
            ges/pip/_vendor/pep517/in_process/_in_process.py", line 363, in <module>
     144          main()
     145        File "/scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/py-pip-21.3.1-flr2jbdnsv5t5wqedv33iqoyjblg2aok/lib/python3.9/site-packa
            ges/pip/_vendor/pep517/in_process/_in_process.py", line 345, in main

See build log for details:
  /tmp/grayson5/spack-stage/spack-stage-py-numpy-1.22.3-r4iztlkbhxe2r6jnlshkb4wk2ysj4ma7/spack-build-out.txt

==> Updating view at /scratch/users/grayson5/spack/var/spack/environments/main3/.spack-env/view
</pre></details>

### Information on your system

* **Spack:** 0.18.0.dev0
* **Python:** 3.6.8
* **Platform:** linux-rhel7-sandybridge
* **Concretizer:** clingo

### Additional information

@adamjstewart  @rgommers

[spack-build-out.txt](https://github.com/spack/spack/files/8583641/spack-build-out.txt)
[spack-build-env.txt](https://github.com/spack/spack/files/8583642/spack-build-env.txt)

### General information

- [X] I have run `spack debug report` and reported the version of Spack/Python/Platform
- [X] I have run `spack maintainers <name-of-the-package>` and **@mentioned** any maintainers
- [X] I have uploaded the build log and environment files
- [X] I have searched the issues of this repo and believe this is not a duplicate


Discussion:
My first guess would be that GCC 4.8 is too old to build modern numpy, but @rgommers can confirm or deny. I know GCC 4.7 hasn't worked in a long time, wouldn't be surprised if 4.8 was no longer supported too. If we know what versions of GCC are supported we can add a `conflict` to the package to prevent this kind of issue. [The docs](https://numpy.org/doc/stable/user/building.html) still claim that GCC 4.X is supported but this isn't really true.
Also note that we inject `-std=c99` into the build when building with GCC 4.8–5.1. Not sure if that's relevant, but worth mentioning. I don't see any similar build issues reported on numpy's GitHub.
Switching to a newer GCC worked for me. This may be related to numpy/numpy#21302, which was fixed in numpy/numpy#21306. How does one backport the relevant Numpy PR?
Oh good catch. I think we have a couple options. If `-std=c++11` is required to build numpy, we could inject that instead by modifying `flag_handler` in that package. If we want to add the patch from that PR, we could do that too. Spack allows for both patch "files" as well as patch "URLs" like `https://github.com/numpy/numpy/pull/21306.patch?full_index=1`. Can you first try modifying the package to inject `-std=c++11` (run `spack edit py-numpy` and it will open in your `$EDITOR`)? If that doesn't work we can try the patch.
With `-std=c++11`, I'm not sure how to apply it only to C++ code,

```
cc1: warning: command line option '-std=c++11' is valid for C++/ObjC++ but not for C [enabled by default]
cc1: warning: command line option '-std=c++11' is valid for C++/ObjC++ but not for C [enabled by default]
```

and only to compilation step,

```
INFO: /scratch/users/grayson5/spack/lib/spack/env/gcc/gcc /tmp/tmp75q6a__y/tmp/tmp75q6a__y/source.o -L/scratch/users/grayson5/spack/opt/spack/linux-rhel7-sandybridge/gcc-4.8.5/openblas-0.3.20-ziwwf5puvcrpmsdg7igyg67vz6roklhd/lib -lopenblas -o /tmp/tmp75q6a__y/a.out
/usr/bin/ld: cannot find -l-std=c++11
collect2: error: ld returned 1 exit status
```

I'll try the patch next.
You may have to try something like:
```python
if self.spec.satisfies('%gcc@:5.1'):
    if name == 'cflags':
        flags.append(self.compiler.c99_flag)
    elif name == 'cxxflags':
        flags.append(self.compiler.cxx11_flag)
```
@rgommers any suggestions for this issue? Should we backport the patch or inject the right compiler flags?
It hasn't been backported in NumPy itself yet, but I think it will - I've just proposed doing so on the PR with the fix. Backporting the patch seems reasonable. It should only apply to 1.22.0 - 1.22.3
@charmoniumQ want to submit a PR to add this patch? If not @rgommers or I can do this, should only be 1 line of code.

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/py-numpy/package.py b/var/spack/repos/builtin/packages/py-numpy/package.py
--- a/var/spack/repos/builtin/packages/py-numpy/package.py
+++ b/var/spack/repos/builtin/packages/py-numpy/package.py
@@ -134,6 +134,11 @@ class PyNumpy(PythonPackage):
     patch('https://github.com/numpy/numpy/pull/20881.patch?full_index=1',
           sha256='802970a9034d40a8a8f49a03f489d5361d5eabf69249621e6757651448910f1a',
           when='@1.20.3:1.22.1')
+    # Patch to update compiler flags.
+    # See https://github.com/spack/spack/issues/30373
+    patch('https://github.com/numpy/numpy/pull/21448.patch?full_index=1',
+          sha256='e9508c3b3a1e1a24669014a0c1b9f3b009a149ea3886cf711eaef2a32b247fdb',
+          when='@1.22.0:1.22.3')
 
     # version 1.21.0 runs into an infinit loop during printing
     # (e.g. print(numpy.ones(1000)) when compiled with gcc 11


Test patch: