Repository: spack/spack

Issue:
Installation issue: cp2k +cuda: `gpu_map` is not defined
### Steps to reproduce the issue

```console
$ spack spec -I cp2k@2023.2%gcc@12.3 +cuda cuda_arch=70 ^fftw@3+openmp ^openblas
Input spec
--------------------------------
 -   cp2k@2023.2%gcc@12.3+cuda cuda_arch=70
 -       ^fftw@3+openmp
 -       ^openblas

Concretized
--------------------------------
 -   cp2k@2023.2%gcc@12.3.0~cosma+cuda~cuda_arch_35_k20x~cuda_fft~elpa~enable_regtests+libint~libvori+libxc+mpi~mpi_f08+openmp~pexsi~plumed~pytorch~quip~rocm~sirius~spglib~spla build_system=makefile cuda_arch=70 lmax=5 smm=libxsmm arch=linux-ubuntu22.04-skylake
[+]      ^cuda@12.3.0%gcc@12.3.0~allow-unsupported-compilers~dev build_system=generic arch=linux-ubuntu22.04-skylake
[+]          ^libxml2@2.10.3%gcc@12.3.0+pic~python+shared build_system=autotools arch=linux-ubuntu22.04-skylake
[+]              ^libiconv@1.17%gcc@12.3.0 build_system=autotools libs=shared,static arch=linux-ubuntu22.04-skylake
[+]      ^fftw@3.3.10%gcc@12.3.0+mpi+openmp~pfft_patches build_system=autotools precision=double,float arch=linux-ubuntu22.04-skylake
[+]      ^gcc-runtime@12.3.0%gcc@12.3.0 build_system=generic arch=linux-ubuntu22.04-skylake
[e]      ^gmake@4.3%gcc@12.3.0~guile build_system=generic patches=599f134 arch=linux-ubuntu22.04-skylake
[+]      ^libint@2.6.0%gcc@12.3.0~debug~fma+fortran build_system=autotools tune=cp2k-lmax-5 arch=linux-ubuntu22.04-skylake
[e]          ^autoconf@2.71%gcc@12.3.0 build_system=autotools arch=linux-ubuntu22.04-skylake
[e]          ^automake@1.16.5%gcc@12.3.0 build_system=autotools arch=linux-ubuntu22.04-skylake
[+]          ^boost@1.84.0%gcc@12.3.0+atomic+chrono~clanglibcpp~container~context~contract~coroutine+date_time~debug+exception~fiber+filesystem+graph~graph_parallel~icu+iostreams~json+locale+log+math~mpi+multithreaded~nowide~numpy~pic+program_options~python+random+regex+serialization+shared+signals~singlethreaded~stacktrace+system~taggedlayout+test+thread+timer~type_erasure~versionedlayout+wave build_system=generic cxxstd=11 patches=a440f96 visibility=hidden arch=linux-ubuntu22.04-skylake
[+]              ^zstd@1.5.5%gcc@12.3.0~programs build_system=makefile libs=shared,static arch=linux-ubuntu22.04-skylake
[+]          ^gmp@6.2.1%gcc@12.3.0+cxx build_system=autotools libs=shared,static patches=69ad2e2 arch=linux-ubuntu22.04-skylake
[e]              ^m4@1.4.18%gcc@12.3.0+sigsegv build_system=autotools patches=3877ab5,fc9b616 arch=linux-ubuntu22.04-skylake
[e]          ^libtool@2.4.6%gcc@12.3.0 build_system=autotools arch=linux-ubuntu22.04-skylake
[+]      ^libxc@6.2.2%gcc@12.3.0~cuda~kxc~lxc+shared build_system=autotools arch=linux-ubuntu22.04-skylake
[+]          ^perl@5.38.0%gcc@12.3.0+cpanm+opcode+open+shared+threads build_system=generic patches=714e4d1 arch=linux-ubuntu22.04-skylake
[+]              ^berkeley-db@18.1.40%gcc@12.3.0+cxx~docs+stl build_system=autotools patches=26090f4,b231fcc arch=linux-ubuntu22.04-skylake
[+]      ^libxsmm@1.17%gcc@12.3.0~debug~generator~header-only~large_jit_buffer~shared blas=default build_system=makefile arch=linux-ubuntu22.04-skylake
[e]          ^binutils@2.38%gcc@12.3.0+gas~gold~headers~interwork+ld~libiberty~lto~nls~pgo+plugins build_system=autotools compress_debug_sections=zlib libs=shared,static arch=linux-ubuntu22.04-skylake
[+]      ^netlib-scalapack@2.2.0%gcc@12.3.0~ipo~pic+shared build_system=cmake build_type=Release generator=make patches=072b006,1c9ce5f,244a9aa arch=linux-ubuntu22.04-skylake
[e]          ^cmake@3.22.1%gcc@12.3.0~doc+ncurses+ownlibs build_system=generic build_type=Release arch=linux-ubuntu22.04-skylake
[+]      ^openblas@0.3.25%gcc@12.3.0~bignuma~consistent_fpcsr+fortran~ilp64+locking+pic+shared build_system=makefile symbol_suffix=none threads=openmp arch=linux-ubuntu22.04-skylake
[e]      ^openmpi@4.1.2%gcc@12.3.0~atomics~cuda~cxx~cxx_exceptions~gpfs~internal-hwloc~internal-libevent~internal-pmix~java~legacylaunchers~lustre~memchecker~openshmem~orterunprefix~romio+rsh~singularity~static+vt+wrapper-rpath build_system=autotools fabrics=none schedulers=none arch=linux-ubuntu22.04-skylake
[+]      ^pkgconf@1.9.5%gcc@12.3.0 build_system=autotools arch=linux-ubuntu22.04-skylake
[+]      ^py-fypp@3.1%gcc@12.3.0 build_system=python_pip arch=linux-ubuntu22.04-skylake
[+]          ^py-pip@23.1.2%gcc@12.3.0 build_system=generic arch=linux-ubuntu22.04-skylake
[+]          ^py-setuptools@68.0.0%gcc@12.3.0 build_system=generic arch=linux-ubuntu22.04-skylake
[+]          ^py-wheel@0.41.2%gcc@12.3.0 build_system=generic arch=linux-ubuntu22.04-skylake
[+]      ^py-numpy@1.26.2%gcc@12.3.0 build_system=python_pip patches=873745d arch=linux-ubuntu22.04-skylake
[+]          ^ninja@1.11.1%gcc@12.3.0+re2c build_system=generic arch=linux-ubuntu22.04-skylake
[+]              ^re2c@2.2%gcc@12.3.0 build_system=generic arch=linux-ubuntu22.04-skylake
[+]          ^py-cython@3.0.4%gcc@12.3.0 build_system=python_pip arch=linux-ubuntu22.04-skylake
[+]          ^py-pyproject-metadata@0.7.1%gcc@12.3.0 build_system=python_pip arch=linux-ubuntu22.04-skylake
[+]              ^py-packaging@23.1%gcc@12.3.0 build_system=python_pip arch=linux-ubuntu22.04-skylake
[+]                  ^py-flit-core@3.9.0%gcc@12.3.0 build_system=python_pip arch=linux-ubuntu22.04-skylake
[+]      ^python@3.11.6%gcc@12.3.0+bz2+crypt+ctypes+dbm~debug+libxml2+lzma~nis~optimizations+pic+pyexpat+pythoncmd+readline+shared+sqlite3+ssl~tkinter+uuid+zlib build_system=generic patches=13fa8bf,b0615b2,ebdca64,f2fd060 arch=linux-ubuntu22.04-skylake
[+]          ^bzip2@1.0.8%gcc@12.3.0~debug~pic+shared build_system=generic arch=linux-ubuntu22.04-skylake
[e]              ^diffutils@3.8%gcc@12.3.0 build_system=autotools arch=linux-ubuntu22.04-skylake
[+]          ^expat@2.5.0%gcc@12.3.0+libbsd build_system=autotools arch=linux-ubuntu22.04-skylake
[+]              ^libbsd@0.11.7%gcc@12.3.0 build_system=autotools arch=linux-ubuntu22.04-skylake
[+]                  ^libmd@1.0.4%gcc@12.3.0 build_system=autotools arch=linux-ubuntu22.04-skylake
[+]          ^gdbm@1.23%gcc@12.3.0 build_system=autotools arch=linux-ubuntu22.04-skylake
[+]          ^gettext@0.22.4%gcc@12.3.0+bzip2+curses+git~libunistring+libxml2+pic+shared+tar+xz build_system=autotools arch=linux-ubuntu22.04-skylake
[e]              ^tar@1.34%gcc@12.3.0 build_system=autotools zip=pigz arch=linux-ubuntu22.04-skylake
[+]          ^libffi@3.4.4%gcc@12.3.0 build_system=autotools patches=070b1f3 arch=linux-ubuntu22.04-skylake
[+]          ^libxcrypt@4.4.35%gcc@12.3.0~obsolete_api build_system=autotools patches=4885da3 arch=linux-ubuntu22.04-skylake
[+]          ^ncurses@6.4%gcc@12.3.0~symlinks+termlib abi=none build_system=autotools arch=linux-ubuntu22.04-skylake
[+]          ^openssl@3.1.3%gcc@12.3.0~docs+shared build_system=generic certs=mozilla arch=linux-ubuntu22.04-skylake
[+]              ^ca-certificates-mozilla@2023-05-30%gcc@12.3.0 build_system=generic arch=linux-ubuntu22.04-skylake
[+]          ^readline@8.2%gcc@12.3.0 build_system=autotools patches=bbf97f1 arch=linux-ubuntu22.04-skylake
[+]          ^sqlite@3.43.2%gcc@12.3.0+column_metadata+dynamic_extensions+fts~functions+rtree build_system=autotools arch=linux-ubuntu22.04-skylake
[+]          ^util-linux-uuid@2.38.1%gcc@12.3.0 build_system=autotools arch=linux-ubuntu22.04-skylake
[+]          ^xz@5.4.1%gcc@12.3.0~pic build_system=autotools libs=shared,static arch=linux-ubuntu22.04-skylake
[+]          ^zlib-ng@2.1.5%gcc@12.3.0+compat+opt build_system=autotools arch=linux-ubuntu22.04-skylake
```


### Error message

<details><summary>Error message</summary>

<pre>
==> Installing cp2k-2023.2-5u6xiqqz5hrq5pd5xy6obngxrvak3d5o [36/36]
==> No binary for cp2k-2023.2-5u6xiqqz5hrq5pd5xy6obngxrvak3d5o found: installing from source
==> Using cached archive: /home/lercole/src/spack-dev/var/spack/cache/_source-cache/archive/ad/adbcc903c1a78cba98f49fe6905a62b49f12e3dfd7cedea00616d1a5f50550db.tar.bz2
==> No patches needed for cp2k
==> cp2k: Executing phase: 'edit'
==> Error: NameError: name 'gpu_map' is not defined

/home/lercole/src/spack-dev/var/spack/repos/builtin/packages/cp2k/package.py:733, in edit:
        730                    libs += ["-lcufft", "-lcublas"]
        731
        732            cuda_arch = spec.variants["cuda_arch"].value[0]
  >>    733            gpuver = gpu_map[cuda_arch]
        734            if cuda_arch == "35" and spec.satisfies("+cuda_arch_35_k20x"):
        735                gpuver = "K20X"
        736

See build log for details:
  /tmp/lercole/spack-stage/spack-stage-cp2k-2023.2-5u6xiqqz5hrq5pd5xy6obngxrvak3d5o/spack-build-out.txt
</pre></details>


### Information on your system

* **Spack:** 0.22.0.dev0 (92e08b160e6519fff6258f17cf7fe64088f26279)
* **Python:** 3.10.10
* **Platform:** linux-ubuntu22.04-skylake
* **Concretizer:** clingo

### Additional information

This bug was introduced by #35718 
@dev-zero @mtaillefumier

### 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:

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/cp2k/package.py b/var/spack/repos/builtin/packages/cp2k/package.py
--- a/var/spack/repos/builtin/packages/cp2k/package.py
+++ b/var/spack/repos/builtin/packages/cp2k/package.py
@@ -330,18 +330,6 @@ class Cp2k(MakefilePackage, CudaPackage, CMakePackage, ROCmPackage):
 
     supported_cuda_arch_list = ("35", "37", "60", "70", "80")
     supported_rocm_arch_list = ("gfx906", "gfx908", "gfx90a", "gfx90a:xnack-", "gfx90a:xnack+")
-    gpu_map = {
-        "35": "K40",
-        "37": "K80",
-        "60": "P100",
-        "70": "V100",
-        "80": "A100",
-        "gfx906": "Mi50",
-        "gfx908": "Mi100",
-        "gfx90a": "Mi250",
-        "gfx90a:xnack-": "Mi250",
-        "gfx90a:xnack+": "Mi250",
-    }
     cuda_msg = "cp2k only supports cuda_arch {0}".format(supported_cuda_arch_list)
     rocm_msg = "cp2k only supports amdgpu_target {0}".format(supported_rocm_arch_list)
 
@@ -687,6 +675,18 @@ def edit(self, spec, prefix):
             fcflags += ["-I{0}".format(sirius.prefix.include.sirius)]
             libs += list(sirius.libs)
 
+        gpu_map = {
+            "35": "K40",
+            "37": "K80",
+            "60": "P100",
+            "70": "V100",
+            "80": "A100",
+            "gfx906": "Mi50",
+            "gfx908": "Mi100",
+            "gfx90a": "Mi250",
+            "gfx90a:xnack-": "Mi250",
+            "gfx90a:xnack+": "Mi250",
+        }
         gpuver = ""
         if spec.satisfies("+cuda"):
             libs += [


Test patch: