Repository: spack/spack

Issue:
qt@5.15.3 build fails: qt/package.py:541, in common_config_args: '_use_spack_dep' is not defined
### Steps to reproduce the issue

`qt@5.15.3` build fails using:
* `spack/develop` (250d5d2c00ad9d6a747499b1160bc4a460266fae from `Wed May 4 13:30:15 2022 -0400`)
* Ubuntu 20.04, X86_64
* GCC 11.1.0

Concrete spec: [qt-vsih2gf.spec.json.txt](https://github.com/spack/spack/files/8626807/qt-vsih2gf.spec.json.txt)

Quickly reproducible using cached dependency binaries and Docker container image:
* `ecpe4s/ubuntu20.04-runner-x86_64:2022-05-01`

```
$> git clone https://github.com/spack/spack
$> (cd spack && git checkout 250d5d2c0)
$> . spack/share/spack/setup-env.sh

$> spack mirror add E4S https://cache.e4s.io
$> spack buildcache keys -it

$> spack install --cache-only --include-build-deps --only dependencies -f ./qt-vsih2gf.spec.json

$> spack install --no-cache -f ./qt-vsih2gf.spec.json
...
# SEE ERROR MESSAGE BELOW
```

### Error message

<details><summary>Error message</summary><pre>
==> Installing qt-5.15.3-vsih2gfjjg63nwtm7bfhkw32ua33kqhv
==> Fetching https://mirror.spack.io/_source-cache/archive/b7/b7412734698a87f4a0ae20751bab32b1b07fdc351476ad8e35328dbe10efdedb.tar.xz
==> Fetching https://mirror.spack.io/_source-cache/archive/93/9378afd071ad5c0ec8f7aef48421e4b9fab02f24c856bee9c0951143941913c5
==> Fetching https://mirror.spack.io/_source-cache/archive/20/2081e9cb85f6712be9b63c70204efa3da954c07d857283eeae16d1b0409704bd
==> Fetching https://mirror.spack.io/_source-cache/archive/84/84b099109d08adf177adf9d3542b6215ec3e42138041d523860dbfdcb59fdaae
==> Applied patch /spack/var/spack/repos/builtin/packages/qt/qt5-pcre.patch
==> Applied patch /spack/var/spack/repos/builtin/packages/qt/qt5-15-gcc-10.patch
==> Applied patch /spack/var/spack/repos/builtin/packages/qt/qt515-quick3d-assimp.patch
==> Applied patch https://src.fedoraproject.org/rpms/qt5-qtbase/raw/6ae41be8260f0f5403367eb01f7cd8319779674a/f/qt5-qtbase-gcc11.patch
==> Applied patch https://src.fedoraproject.org/rpms/qt5-qtdeclarative/raw/593481a2541d3218f285dd7b46bdc5f4c76075ab/f/qt5-qtdeclarative-gcc11.patch
==> Applied patch https://src.fedoraproject.org/rpms/qt5-qtwebsockets/raw/f54f4ce6fa27941e9e6d606103d32056078edc74/f/qt5-qtwebsockets-gcc11.patch
==> Ran patch() for qt
==> qt: Executing phase: 'configure'
==> Error: NameError: name '_use_spack_dep' is not defined

/spack/var/spack/repos/builtin/packages/qt/package.py:541, in common_config_args:
        538            spec, config_args, spack_pkg, qt_name)
        539
        540        if '+gui' in spec:
  >>    541            use_spack_dep('freetype')
        542            if not MACOS_VERSION:
        543                config_args.append('-fontconfig')
        544        else:

See build log for details:
  /tmp/root/spack-stage/spack-stage-qt-5.15.3-vsih2gfjjg63nwtm7bfhkw32ua33kqhv/spack-build-out.txt
</pre></details>


### Information on your system

* **Spack:** 0.18.0.dev0 (250d5d2c00ad9d6a747499b1160bc4a460266fae)
* **Python:** 3.8.10
* **Platform:** linux-ubuntu20.04-cascadelake
* **Concretizer:** clingo

### Additional information

[spack-build-env.txt](https://github.com/spack/spack/files/8626825/spack-build-env.txt)
[spack-build-out.txt](https://github.com/spack/spack/files/8626826/spack-build-out.txt)

@sethrj @wspear

### 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:
@chuckatkins the danger of refactoring. can you please fix asap?

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/qt/package.py b/var/spack/repos/builtin/packages/qt/package.py
--- a/var/spack/repos/builtin/packages/qt/package.py
+++ b/var/spack/repos/builtin/packages/qt/package.py
@@ -134,7 +134,7 @@ class Qt(Package):
     patch('qt514.patch', when='@5.14')
     patch('qt514-isystem.patch', when='@5.14.2')
     # https://bugreports.qt.io/browse/QTBUG-84037
-    patch('qt515-quick3d-assimp.patch', when='@5.15:5')
+    patch('qt515-quick3d-assimp.patch', when='@5.15:5+opengl')
     # https://bugreports.qt.io/browse/QTBUG-90395
     patch('https://src.fedoraproject.org/rpms/qt5-qtbase/raw/6ae41be8260f0f5403367eb01f7cd8319779674a/f/qt5-qtbase-gcc11.patch',
           sha256='9378afd071ad5c0ec8f7aef48421e4b9fab02f24c856bee9c0951143941913c5',
@@ -510,12 +510,16 @@ def patch(self):
             with open(conf_file, 'a') as f:
                 f.write("QMAKE_CXXFLAGS += -std=gnu++98\n")
 
-    def _use_spack_dep(spec, config_args, spack_pkg, qt_name=None):
-        pkg = spec[spack_pkg]
-        config_args.append('-system-' + (qt_name or spack_pkg))
-        if not pkg.external:
-            config_args.extend(pkg.libs.search_flags.split())
-            config_args.extend(pkg.headers.include_flags.split())
+    def _dep_appender_factory(self, config_args):
+        spec = self.spec
+
+        def use_spack_dep(spack_pkg, qt_name=None):
+            pkg = spec[spack_pkg]
+            config_args.append('-system-' + (qt_name or spack_pkg))
+            if not pkg.external:
+                config_args.extend(pkg.libs.search_flags.split())
+                config_args.extend(pkg.headers.include_flags.split())
+        return use_spack_dep
 
     @property
     def common_config_args(self):
@@ -534,8 +538,7 @@ def common_config_args(self):
             '-no-pch',
         ]
 
-        use_spack_dep = lambda spack_pkg, qt_name=None: _use_spack_dep(
-            spec, config_args, spack_pkg, qt_name)
+        use_spack_dep = self._dep_appender_factory(config_args)
 
         if '+gui' in spec:
             use_spack_dep('freetype')
@@ -674,8 +677,7 @@ def configure(self, spec, prefix):
                 '' if version >= Version('5.7') else 'style')
         ])
 
-        use_spack_dep = lambda spack_pkg, qt_name=None: _use_spack_dep(
-            spec, config_args, spack_pkg, qt_name)
+        use_spack_dep = self._dep_appender_factory(config_args)
 
         if MACOS_VERSION:
             if version < Version('5.9'):
@@ -735,6 +737,7 @@ def configure(self, spec, prefix):
             if version >= Version('5.15'):
                 config_args.extend(['-skip', 'qtlocation'])
 
+        else:
             # v5.0: qt3d uses internal-only libassimp
             # v5.5: external-only libassimp
             # v5.6: either internal or external libassimp via autodetection
@@ -743,10 +746,10 @@ def configure(self, spec, prefix):
             # v5.15: qtquick3d switched to the -quick3d-assimp option
             if version >= Version('5.9'):
                 use_spack_dep('assimp')
-            if version >= Version('5.15'):
+            elif version >= Version('5.15'):
                 use_spack_dep('assimp', 'quick3d-assimp')
 
-        elif MACOS_VERSION:
+        if MACOS_VERSION and '+opengl' in spec:
             # These options are only valid if 'multimedia' is enabled, i.e.
             # +opengl is selected. Force them to be off on macOS, but let other
             # platforms decide for themselves.


Test patch: