Repository: spack/spack

Issue:
Installation issue: m4@1.4.19 %nvhpc
<!-- Thanks for taking the time to report this build failure. To proceed with the report please:


<!-- Fill in the exact spec you are trying to build and the relevant part of the error message -->
```console
$ spack install m4@1.4.19 %nvhpc@21.5
```
with the patch:
```
[+] /home/cessenat/Softs/spack/opt/spack/linux-ubuntu18.04-zen2/nvhpc-21.5/libsigsegv-2.13-i2ssovg5zlhwkneznolqjaoyjedsful4
==> Installing m4-1.4.19-xahq6wg7mhlbqhmkknbbusrx5ofamvb3
==> No binary for m4-1.4.19-xahq6wg7mhlbqhmkknbbusrx5ofamvb3 found: installing from source
==> Using cached archive: /home/cessenat/Softs/spack/var/spack/cache/_source-cache/archive/3b/3be4a26d825ffdfda52a56fc43246456989a3630093cced3fbddf4771ee58a70.tar.gz
1 out of 1 hunk FAILED -- saving rejects to file lib/xalloc-oversized.h.rej
1 out of 1 hunk FAILED -- saving rejects to file lib/intprops.h.rej
==> Patch /home/cessenat/Softs/spack/var/spack/repos/builtin/packages/m4/nvhpc.patch failed.
==> Error: ProcessError: Command exited with status 1:
    '/usr/bin/patch' '-s' '-p' '1' '-i' '/home/cessenat/Softs/spack/var/spack/repos/builtin/packages/m4/nvhpc.patch' '-d' '.'
```
without the patch:
```
[+] /home/cessenat/Softs/spack/opt/spack/linux-ubuntu18.04-zen2/nvhpc-21.5/libsigsegv-2.13-i2ssovg5zlhwkneznolqjaoyjedsful4
==> Installing m4-1.4.19-ypyqafl2adprngtqut4zu2ugkminyfn5
==> No binary for m4-1.4.19-ypyqafl2adprngtqut4zu2ugkminyfn5 found: installing from source
==> Using cached archive: /home/cessenat/Softs/spack/var/spack/cache/_source-cache/archive/3b/3be4a26d825ffdfda52a56fc43246456989a3630093cced3fbddf4771ee58a70.tar.gz
==> No patches needed for m4
==> m4: Executing phase: 'autoreconf'
==> m4: Executing phase: 'configure'
==> m4: Executing phase: 'build'
==> Error: ProcessError: Command exited with status 2:
    'make' '-j6' 'V=1'

5 errors found in build log:
     2290    source='/tmp/cessenat/spack-stage/spack-stage-m4-1.4.19-ypyqafl2adprngtqut4zu2ugkminyfn5/spack-src/lib/malloca.c' object='malloca.o' libtool=no \
     2291    DEPDIR=.deps depmode=pgcc /bin/bash /tmp/cessenat/spack-stage/spack-stage-m4-1.4.19-ypyqafl2adprngtqut4zu2ugkminyfn5/spack-src/build-aux/depcomp \
     2292    /home/cessenat/Softs/spack/lib/spack/env/nvhpc/nvc  -I. -I/tmp/cessenat/spack-stage/spack-stage-m4-1.4.19-ypyqafl2adprngtqut4zu2ugkminyfn5/spack-src/lib     -g
              -O2 -c -o malloca.o /tmp/cessenat/spack-stage/spack-stage-m4-1.4.19-ypyqafl2adprngtqut4zu2ugkminyfn5/spack-src/lib/malloca.c
     2293    source='/tmp/cessenat/spack-stage/spack-stage-m4-1.4.19-ypyqafl2adprngtqut4zu2ugkminyfn5/spack-src/lib/math.c' object='math.o' libtool=no \
     2294    DEPDIR=.deps depmode=pgcc /bin/bash /tmp/cessenat/spack-stage/spack-stage-m4-1.4.19-ypyqafl2adprngtqut4zu2ugkminyfn5/spack-src/build-aux/depcomp \
     2295    /home/cessenat/Softs/spack/lib/spack/env/nvhpc/nvc  -I. -I/tmp/cessenat/spack-stage/spack-stage-m4-1.4.19-ypyqafl2adprngtqut4zu2ugkminyfn5/spack-src/lib     -g
              -O2 -c -o math.o /tmp/cessenat/spack-stage/spack-stage-m4-1.4.19-ypyqafl2adprngtqut4zu2ugkminyfn5/spack-src/lib/math.c
  >> 2296    "/tmp/cessenat/spack-stage/spack-stage-m4-1.4.19-ypyqafl2adprngtqut4zu2ugkminyfn5/spack-src/lib/malloca.c", line 52: error: 64 bits operations with signed and 
             unsigned operands are currently unsupported by __builtin_add_overflow
     2297        if (!INT_ADD_WRAPV (n, plus, &nplus) && !xalloc_oversized (nplus, 1))
     2298             ^
     2299    
     2300    1 error detected in the compilation of "/tmp/cessenat/spack-stage/spack-stage-m4-1.4.19-ypyqafl2adprngtqut4zu2ugkminyfn5/spack-src/lib/malloca.c".
     2301    Makefile:2869: recipe for target 'malloca.o' failed
  >> 2302    make[3]: *** [malloca.o] Error 2
     2303    make[3]: *** Waiting for unfinished jobs....
     2304    make[3]: Leaving directory '/tmp/cessenat/spack-stage/spack-stage-m4-1.4.19-ypyqafl2adprngtqut4zu2ugkminyfn5/spack-src/spack-build/lib'
     2305    Makefile:2481: recipe for target 'all' failed
  >> 2306    make[2]: *** [all] Error 2
     2307    make[2]: Leaving directory '/tmp/cessenat/spack-stage/spack-stage-m4-1.4.19-ypyqafl2adprngtqut4zu2ugkminyfn5/spack-src/spack-build/lib'
     2308    Makefile:2018: recipe for target 'all-recursive' failed
  >> 2309    make[1]: *** [all-recursive] Error 1
     2310    make[1]: Leaving directory '/tmp/cessenat/spack-stage/spack-stage-m4-1.4.19-ypyqafl2adprngtqut4zu2ugkminyfn5/spack-src/spack-build'
     2311    Makefile:1974: recipe for target 'all' failed
  >> 2312    make: *** [all] Error 2

See build log for details:
  /tmp/cessenat/spack-stage/spack-stage-m4-1.4.19-ypyqafl2adprngtqut4zu2ugkminyfn5/spack-build-out.txt
```

### Information on your system

<!-- Please include the output of `spack debug report` -->
* **Spack:** 0.16.1-3442-f9ecc4966d
* **Python:** 3.6.9
* **Platform:** linux-ubuntu18.04-zen2
* **Concretizer:** original

<!-- If you have any relevant configuration detail (custom `packages.yaml` or `modules.yaml`, etc.) you can add that here as well. -->
from ~/.spack/linux/compilers.yaml
```
- compiler:
    spec: nvhpc@21.5
    # It seems manual load . /etc/profile.d/modules.sh ; module load nvhpc is required
    paths:
      cc: /opt/nvidia/hpc_sdk/Linux_x86_64/21.5/compilers/bin/nvc
      cxx: /opt/nvidia/hpc_sdk/Linux_x86_64/21.5/compilers/bin/nvc++
      f77: /opt/nvidia/hpc_sdk/Linux_x86_64/21.5/compilers/bin/nvfortran
      fc: /opt/nvidia/hpc_sdk/Linux_x86_64/21.5/compilers/bin/nvfortran
    flags: {}
    operating_system: ubuntu18.04
    target: x86_64
    modules: [nvhpc/21.5]
    environment: {}
    extra_rpaths: []

```
### Additional information

<!-- Please upload the following files. They should be present in the stage directory of the failing build. Also upload any config.log or similar file if one exists. -->
* [spack-build-out.txt](https://github.com/spack/spack/files/6770814/spack-build-out.txt)
* [spack-build-env.txt](https://github.com/spack/spack/files/6770817/spack-build-env.txt)


<!-- Some packages have maintainers who have volunteered to debug build failures. Run `spack maintainers <name-of-the-package>` and @mention them here if they exist. -->
There is no maintainer but @lee218llnl may have ideas to make a new patch.
So could @samcmill as nvhpc expert.

### General information

<!-- These boxes can be checked by replacing [ ] with [x] or by clicking them after submitting the issue. -->
- [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:
I created the oneapi.patch, I think @samcmill might have a better idea on how to fix things for nvhpc since it looks like he created the nvhpc.patch file.
I submitted a PR to only apply nvhpc.patch for version 1.4.18. I'm not sure about your build failure, though, as I was able to get m4@1.4.19%nvhpc@21.5 to build OK in my environment.
> I submitted a PR to only apply nvhpc.patch for version 1.4.18. I'm not sure about your build failure, though, as I was able to get m4@1.4.19%nvhpc@21.5 to build OK in my environment.

I succeeded with 1.4.17 but not with 1.4.19 on Ubuntu 18.04 ; nevertheless I also (like you) succeeeded with CentOS 7.7 and 7.8, but failed with Ubuntu 20.4 ; this means all that is not really clean...
@cessenat thanks for the additional tests. I do not have an equivalent system and thus cannot reproduce. Also, I don't support the m4 package. I hate to just punt, but I think this may be on the m4 developers or the nvhpc folks to devise a solution.
I took a quick look and have some idea of what's going on.

Making the patch condition on the m4 version is definitely the right thing to do.

Here's a simple reproducer that compiles with gcc or clang, but errors with nvc:

```
#include <stddef.h>

int main() {
  size_t a = 0;
  int b = 1;
  ptrdiff_t r = 2;

  __builtin_add_overflow(a, b, &r);
}
```

I believe the reason for the difference on CentOS 7 and some other older systems versus newer ones is in `lib/intprops.h`.  If `__builtin_add_overflow` is not available, then it falls back to another code path.  On CentOS 7, the reproducer above errors with `undefined reference to '__builtin_add_overflow'` regardless of the compiler, vs. `error: 64 bits operations with signed and unsigned operands are currently unsupported by __builtin_add_overflow` on CentOS 8.

Older versions of nvc did not support `__builtin_add_overflow`, so the `nvhpc.patch` explicitly disabled that code path.  It's looking like a new version of that patch will be needed for 1.4.19 to workaround this limitation.
https://github.com/spack/spack/pull/24730 has been merged but it looks like this should stay open until someone can implement the changes described in https://github.com/spack/spack/issues/24726#issuecomment-875115965

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/m4/package.py b/var/spack/repos/builtin/packages/m4/package.py
--- a/var/spack/repos/builtin/packages/m4/package.py
+++ b/var/spack/repos/builtin/packages/m4/package.py
@@ -18,7 +18,12 @@ class M4(AutotoolsPackage, GNUMirrorPackage):
 
     patch('gnulib-pgi.patch', when='@1.4.18')
     patch('pgi.patch', when='@1.4.17')
+    # The NVIDIA compilers do not currently support some GNU builtins.
+    # Detect this case and use the fallback path.
     patch('nvhpc.patch', when='@1.4.18 %nvhpc')
+    patch('nvhpc-1.4.19.patch', when='@1.4.19 %nvhpc')
+    # Workaround bug where __LONG_WIDTH__ is not defined
+    patch('nvhpc-long-width.patch', when='@1.4.19 %nvhpc')
     patch('oneapi.patch', when='@1.4.18 %oneapi')
     # from: https://github.com/Homebrew/homebrew-core/blob/master/Formula/m4.rb
     # Patch credit to Jeremy Huddleston Sequoia <jeremyhu@apple.com>


Test patch: