Repository: spack/spack

Issue:
CMake: Could NOT find MPI (missing: MPI_C_FOUND C)
I was trying to build the `legion` spackage, but `MPI` could not be found.


### Steps to reproduce the issue

```console
$  spack install legion
```

### Error Message

```console
-- Could NOT find MPI_C (missing: MPI_C_WORKS)
CMake Error at /home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/cmake-3.15.0-ywkxfyybtze5ubp47jx4pm26hu7icbae/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
  Could NOT find MPI (missing: MPI_C_FOUND C)
Call Stack (most recent call first):
  /home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/cmake-3.15.0-ywkxfyybtze5ubp47jx4pm26hu7icbae/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
  /home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/cmake-3.15.0-ywkxfyybtze5ubp47jx4pm26hu7icbae/share/cmake-3.15/Modules/FindMPI.cmake:1687 (find_package_handle_standard_args)
  cmake/FindGASNet.cmake:160 (find_package)
  cmake/FindGASNet.cmake:262 (_GASNet_create_component_target)
  CMakeLists.txt:218 (find_package)


-- Configuring incomplete, errors occurred!
```


### Information on your system

I test this on multiple plattform and with multiple openmpi versions.

If I dig into the the `CMakeError.log`, I see:
```
Building C object CMakeFiles/cmTC_6d446.dir/test_mpi.c.o
/home/junghans/spack/lib/spack/env/gcc/gcc  -isystem /home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/openmpi-3.1.4-6acrjmf2z73obkgvund42tbimv7j5lwc/include  -fexceptions -pthread -o CMakeFiles/cmTC_6d446.dir/test_mpi.c.o   -c /home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/cmake-3.15.0-ywkxfyybtze5ubp47jx4pm26hu7icbae/share/cmake-3.15/Modules/FindMPI/test_mpi.c
Linking C executable cmTC_6d446
/home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/cmake-3.15.0-ywkxfyybtze5ubp47jx4pm26hu7icbae/bin/cmake -E cmake_link_script CMakeFiles/cmTC_6d446.dir/link.txt --verbose=1
/home/junghans/spack/lib/spack/env/gcc/gcc      -rdynamic CMakeFiles/cmTC_6d446.dir/test_mpi.c.o  -o cmTC_6d446 -Wl,-rpath,/home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/openmpi-3.1.4-6acrjmf2z73obkgvund42tbimv7j5lwc/lib -Wl,-rpath -Wl,/home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/hwloc-1.11.11-k6j3l64m7lqc7ftuimrmn4pymwpacno2/lib -Wl,/home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/openmpi-3.1.4-6acrjmf2z73obkgvund42tbimv7j5lwc/lib -L/home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/hwloc-1.11.11-k6j3l64m7lqc7ftuimrmn4pymwpacno2/lib -pthread /home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/openmpi-3.1.4-6acrjmf2z73obkgvund42tbimv7j5lwc/lib/libmpi.so
/usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/../../../../x86_64-pc-linux-gnu/bin/ld: /home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/openmpi-3.1.4-6acrjmf2z73obkgvund42tbimv7j5lwc/lib: file not recognized: Is a directory
```
So there is this `/home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/openmpi-3.1.4-6acrjmf2z73obkgvund42tbimv7j5lwc/lib` argument in the link line.

if you ran:
```console
$ /home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/openmpi-3.1.4-6acrjmf2z73obkgvund42tbimv7j5lwc/bin/mpicc --showme
/usr/bin/gcc -I/home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/openmpi-3.1.4-6acrjmf2z73obkgvund42tbimv7j5lwc/include -fexceptions -pthread -L/home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/hwloc-1.11.11-k6j3l64m7lqc7ftuimrmn4pymwpacno2/lib -Wl,-rpath -Wl,/home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/hwloc-1.11.11-k6j3l64m7lqc7ftuimrmn4pymwpacno2/lib -Wl,-rpath -Wl,/home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/openmpi-3.1.4-6acrjmf2z73obkgvund42tbimv7j5lwc/lib -L/home/junghans/spack/opt/spack/linux-gentoo2-x86_64/gcc-8.3.0/openmpi-3.1.4-6acrjmf2z73obkgvund42tbimv7j5lwc/lib -lmpi
```
it seems the wrapper inject this directory somehow.


Discussion:
i had this also -- it helped to replace cmake-3.15 with some <3.15
@hightower8083 thanks, that works!
@bradking, I hope you are aware of that issue in cmake-3.15.
Thanks for this information.  I have also been having this problem trying to build ``amrex`` with cmake 3.15.
it seems to concern specifically `openmpi` -- in my cases `3.15` done fine with `mpich`
I tracked down the regression to this MR: https://gitlab.kitware.com/cmake/cmake/merge_requests/3449
The error causes `-Wl,-rpath -Wl,/path1 -Wl,-rpath -Wl,/path2` to be deduplicated now that CMake sees the individual arguments down to `-Wl,-rpath -Wl,/path1 -Wl,/path2` which is invalid. This is probably why it shows up sometimes, but not always.
Recorded in [CMake Issue 19516](https://gitlab.kitware.com/cmake/cmake/issues/19516).



Resolving patch:
diff --git a/var/spack/repos/builtin/packages/cmake/package.py b/var/spack/repos/builtin/packages/cmake/package.py
--- a/var/spack/repos/builtin/packages/cmake/package.py
+++ b/var/spack/repos/builtin/packages/cmake/package.py
@@ -59,6 +59,10 @@ class Cmake(Package):
     version('3.0.2',    'db4c687a31444a929d2fdc36c4dfb95f')
     version('2.8.10.2', '097278785da7182ec0aea8769d06860c')
 
+    # Revert the change that introduced a regression when parsing mpi link
+    # flags, see: https://gitlab.kitware.com/cmake/cmake/issues/19516
+    patch('cmake-revert-findmpi-link-flag-list.patch', when='@3.15.0')
+
     # Fix linker error when using external libs on darwin.
     # See https://gitlab.kitware.com/cmake/cmake/merge_requests/2873
     patch('cmake-macos-add-coreservices.patch', when='@3.11.0:3.13.3')


Test patch: