Repository: spack/spack

Issue:
Installation issue: llvm attempts to link against the incorrect python and crashes as a result
### Steps to reproduce the issue

<!-- Fill in the exact spec you are trying to build and the relevant part of the error message -->
```console
#have a newer version of python installed on the system outside of spack
$ spack install llvm
...
```

### Information on your system

<!-- Please include the output of `spack debug report` -->

* **Spack:** 0.15.4-1825-08e825f47
* **Python:** 3.8.5
* **Platform:** linux-gentoo2-skylake

<!-- If you have any relevant configuration detail (custom `packages.yaml` or `modules.yaml`, etc.) you can add that here as well. -->


### Additional information

CMake find_package(Python3) doesn't honor the find_package(Python)'s `PYTHON_EXECUTABLE`, but prefers it to be called
`Python3_EXECUTABLE` instead.  However there is also a bug in compiler-rt upstream in LLVM that doesn't honor this variable either which just delays this problem to later on in the build process.

The following *mostly* fixes this if compiling LLVM against python3, but we also need to check what verison of python spack is building against and possibly set `Python2_EXECUTABLE` instead.

```python
cmake_args.append("-DPython3_EXECUTABLE:PATH={0}".format(spec["python"].command.path),)
```

@naromero77  @trws

### 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

[spack-build-env.txt](https://github.com/spack/spack/files/5539978/spack-build-env.txt)
[spack-build-out.txt](https://github.com/spack/spack/files/5539979/spack-build-out.txt)
[spack-configure-args.txt](https://github.com/spack/spack/files/5539980/spack-configure-args.txt)




Discussion:
Here is the corresponding issue in LLVM https://bugs.llvm.org/show_bug.cgi?id=48180
Here is the corresponding PR to LLVM https://reviews.llvm.org/D91536
Thanks for posting here. Once the issue gets merged into LLVM master, we can figure out how to backport the patch to Spack.
The fix has been merged into LLVM master @naromero77 
@trws I think it would be nice to back port this patch into Spack package. Do you agree?

@robertu94 Would you be willing to do a PR for Spack with a small patch? I have no idea how far back this issue goes.
That seems reasonable, especially since python3 is only going to be more prevalent and I expect impacted versions to stay relevant for quite a while.
@naromero77 what would you want in the patch?  Just code like above to add the Python3_EXECUTABLE, or a larger patch which backports the fix to AddCompilerRT.cmake to older versions of LLVM?  I'm happy to do the former, I'm not sure I want to do all of the testing to ensure I didn't break something else in an older LLVM with a backported patch.
If I understand correctly, two things need to happen:
- We need to include this patch
https://reviews.llvm.org/D91536
- Plus we need to add these lines to the Spack package:
`cmake_args.append("-DPython3_EXECUTABLE:PATH={0}".format(spec["python"].command.path),)`

Correct?

If so, I would be happy if you could do a PR for LLVM 11.0. If it happened to work for LLVM 10.0.x, that would be gravy.
We also need to specify `Python2_EXECUTABLE` if the user chose to depend on Python 2.7 instead of 3.  I'll try to upload a patch soon

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/llvm/package.py b/var/spack/repos/builtin/packages/llvm/package.py
--- a/var/spack/repos/builtin/packages/llvm/package.py
+++ b/var/spack/repos/builtin/packages/llvm/package.py
@@ -225,6 +225,10 @@ class Llvm(CMakePackage, CudaPackage):
     # merged in llvm-11.0.0_rc2
     patch("lldb_external_ncurses-10.patch", when="@10.0.0:10.99+lldb")
 
+    # https://github.com/spack/spack/issues/19908
+    # merged in llvm main prior to 12.0.0
+    patch("llvm_python_path.patch", when="@11.0.0")
+
     # The functions and attributes below implement external package
     # detection for LLVM. See:
     #
@@ -365,16 +369,24 @@ def setup_run_environment(self, env):
 
     def cmake_args(self):
         spec = self.spec
+        python = spec['python']
         cmake_args = [
             "-DLLVM_REQUIRES_RTTI:BOOL=ON",
             "-DLLVM_ENABLE_RTTI:BOOL=ON",
             "-DLLVM_ENABLE_EH:BOOL=ON",
             "-DCLANG_DEFAULT_OPENMP_RUNTIME:STRING=libomp",
-            "-DPYTHON_EXECUTABLE:PATH={0}".format(spec["python"].command.path),
+            "-DPYTHON_EXECUTABLE:PATH={0}".format(python.command.path),
             "-DLIBOMP_USE_HWLOC:BOOL=ON",
             "-DLIBOMP_HWLOC_INSTALL_DIR={0}".format(spec["hwloc"].prefix),
         ]
 
+        if python.version >= Version("3.0.0"):
+            cmake_args.append("-DPython3_EXECUTABLE={0}".format(
+                              python.command.path))
+        else:
+            cmake_args.append("-DPython2_EXECUTABLE={0}".format(
+                              python.command.path))
+
         projects = []
 
         if "+cuda" in spec:


Test patch: