Repository: spack/spack

Issue:
Installation issue: z3
A Z3 Cmake build using a system Python specified via `packages.yaml` tries to write into the system Python's directory and fails due to privileges.  If a non-system Python3 is used, z3 is installed to its `site-packages`.

Possibly related to #24337 ?

### Steps to reproduce the issue

<!-- Fill in the exact spec you are trying to build and the relevant part of the error message -->
```console
$ spack install z3
 ...
  >> 3267    CMake Error at src/api/python/cmake_install.cmake:49 (file):
     3268      file cannot create directory: /usr/lib/python3/dist-packages/z3.  Maybe
     3269      need administrative privileges.
     3270    Call Stack (most recent call first):
     3271      src/cmake_install.cmake:181 (include)
     3272      cmake_install.cmake:73 (include)
 ...
```

### Information on your system

<!-- Please include the output of `spack debug report` -->
```
* **Spack:** 0.16.2-3213-7b6ca59038
* **Python:** 3.8.5
* **Platform:** linux-ubuntu20.04-broadwell
* **Concretizer:** original
```
<!-- If you have any relevant configuration detail (custom `packages.yaml` or `modules.yaml`, etc.) you can add that here as well. -->

In `packages.yaml`:
```
  python:
    externals:
    - spec: python@3.8.5+bz2+ctypes+dbm+lzma+nis+pyexpat+readline+sqlite3+ssl~tix~tkinter+uuid+zlib
      prefix: /usr
```

### 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]()
* [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. -->

### 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
- [ ] 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'm also hitting this when using an external python.

```console
  >> 3251    CMake Error at src/api/python/cmake_install.cmake:54 (file):
     3252      file cannot create directory:
     3253      /projects/opt/centos7/anaconda3/lib/python3.5/site-packages/z3.  Maybe need
     3254      administrative privileges.
     3255    Call Stack (most recent call first):
     3256      src/cmake_install.cmake:186 (include)
     3257      cmake_install.cmake:78 (include)
     3258    
     3259    
  >> 3260    make: *** [install] Error 1
```

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/z3/package.py b/var/spack/repos/builtin/packages/z3/package.py
--- a/var/spack/repos/builtin/packages/z3/package.py
+++ b/var/spack/repos/builtin/packages/z3/package.py
@@ -35,9 +35,20 @@ class Z3(CMakePackage):
     build_directory = 'build'
 
     def cmake_args(self):
+        spec = self.spec
+
         args = [
             self.define_from_variant('Z3_USE_LIB_GMP', 'gmp'),
             self.define_from_variant('Z3_BUILD_PYTHON_BINDINGS', 'python'),
             self.define_from_variant('Z3_INSTALL_PYTHON_BINDINGS', 'python')
         ]
+
+        if spec.satisfies('+python'):
+            args.append(
+                self.define('CMAKE_INSTALL_PYTHON_PKG_DIR', join_path(
+                    prefix.lib,
+                    'python%s' % spec['python'].version.up_to(2),
+                    'site-packages'))
+            )
+
         return args


Test patch: