Possible regression: FindMPI.cmake doesn't locate MPI in non-standard installation location

Hi all,

On Linux, I typically install several different MPI versions to /opt for testing. After updating CMake to 4.3.1 (the behavior probably changed long before this), it seems CMake can no longer fully find and use these installations. Debugging through the find_package() process with --debug-find, it appears that this is due to the _MPI_BASE_DIR variable derived from MPIEXEC_EXECUTABLE not being used in some cases.

When MPIEXEC_EXECUTABLE is specified, mpiexec and mpicc appear to be found fine:

CMake Debug Log at /usr/share/cmake-4.3/Modules/FindMPI.cmake:1543 (find_program):
   find_program called with the following settings:

     VAR: MPIEXEC_EXECUTABLE
     NAMES:
     Documentation: Executable for running MPI programs.
     Framework
       Only Search Frameworks: 0
       Search Frameworks Last: 0
       Search Frameworks First: 0
     AppBundle
       Only Search AppBundle: 0
       Search AppBundle Last: 0
       Search AppBundle First: 0
     CMAKE_FIND_USE_CMAKE_PATH: 1
     CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: 1
     CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: 1
     CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: 1 
     CMAKE_FIND_USE_INSTALL_PREFIX: 1	 

   find_program considered the following locations:

   The item was found at

     /opt/openmpi-5.0.5/bin/mpiexec	 

CMake Debug Log at /usr/share/cmake-4.3/Modules/FindMPI.cmake:1694 (find_program):
   find_program called with the following settings:

     VAR: MPI_C_COMPILER
     NAMES: "mpigcc"
            "mpgcc"
            "mpigcc_r"
            "mpgcc_r"
            "mpicc"
            "mpcc"
            "mpicc_r"
            "mpcc_r"
     Documentation: MPI compiler for C	 
     Framework
       Only Search Frameworks: 0
       Search Frameworks Last: 0
       Search Frameworks First: 0
     AppBundle
       Only Search AppBundle: 0
       Search AppBundle Last: 0
       Search AppBundle First: 0
     NO_DEFAULT_PATH Enabled

   find_program considered the following locations:

     /opt/openmpi-5.0.5/bin/mpigcc	 
     /opt/openmpi-5.0.5/sbin/mpigcc	 
     /opt/openmpi-5.0.5/mpigcc
     /opt/openmpi-5.0.5/bin/mpgcc
     /opt/openmpi-5.0.5/sbin/mpgcc	 
     /opt/openmpi-5.0.5/mpgcc
     /opt/openmpi-5.0.5/bin/mpigcc_r	 
     /opt/openmpi-5.0.5/sbin/mpigcc_r	 
     /opt/openmpi-5.0.5/mpigcc_r
     /opt/openmpi-5.0.5/bin/mpgcc_r	 
     /opt/openmpi-5.0.5/sbin/mpgcc_r	 
     /opt/openmpi-5.0.5/mpgcc_r

   The item was found at

     /opt/openmpi-5.0.5/bin/mpicc

But mpi.h is not found:

Checking for module 'mpi-c'
   Package 'mpi-c' not found
CMake Debug Log at /usr/share/cmake-4.3/Modules/FindMPI.cmake:1337 (find_path):
   find_path called with the following settings:

     VAR: MPI_C_HEADER_DIR
     NAMES: "mpi.h"
     Documentation: Path to a file.	 
     Framework
       Only Search Frameworks: 0
       Search Frameworks Last: 0
       Search Frameworks First: 0
     AppBundle
       Only Search AppBundle: 0
       Search AppBundle Last: 0
       Search AppBundle First: 0
     CMAKE_FIND_USE_CMAKE_PATH: 1
     CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: 1
     CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: 1
     CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: 1 
     CMAKE_FIND_USE_INSTALL_PREFIX: 1	 

   find_path considered the following locations:

     /usr/lib64/go1.25.7/go/bin/mpi.h	 
     /usr/local/bin/mpi.h
     /usr/bin/mpi.h
     /bin/mpi.h
     /usr/games/mpi.h
     /usr/lib64/libexec/kf5/mpi.h
     /usr/lib64/qt5/bin/mpi.h
     /usr/lib64/qt6/bin/mpi.h
     /usr/local/include/mpi.h
     /usr/local/mpi.h
     /usr/include/mpi.h
     /usr/mpi.h
     /include/mpi.h
     /usr/X11R6/include/mpi.h
     /usr/X11R6/mpi.h
     /usr/pkg/include/mpi.h
     /usr/pkg/mpi.h
     /opt/include/mpi.h
     /opt/mpi.h
     /usr/include/X11/mpi.h

   The item was not found.

I also tried using MPI_HOME, but that didn’t work either.

Patching FindMPI.cmake as in:

diff --git a/FindMPI.cmake b/usr/share/cmake-4.3/Modules/FindMPI.cmake
index ebfd79f..f0ce949 100644
--- a/FindMPI.cmake
+++ b/usr/share/cmake-4.3/Modules/FindMPI.cmake
@@ -1326,6 +1326,12 @@ macro(_MPI_split_include_dirs LANG)
   # We try to find the headers/modules among those paths (and system paths)
   # For C/C++, we just need to have a look for mpi.h.
   if(LANG MATCHES "^(C|CXX)$")
+    if (_MPI_BASE_DIR)
+      find_path(MPI_${LANG}_HEADER_DIR "mpi.h"
+        HINTS
+          ${_MPI_BASE_DIR}/include
+      )
+    endif()
     find_path(MPI_${LANG}_HEADER_DIR "mpi.h"
       HINTS
         ${MPI_${LANG}_COMPILER_INCLUDE_DIRS}

works to find the header, but of course is just a quick solution and also still leaves me with:

Could NOT find MPI_C (missing: MPI_C_LIB_NAMES MPI_C_WORKS)
CMake Error at /usr/share/cmake-4.3/Modules/FindPackageHandleStandardArgs.cmake:290 (message):
   Could NOT find MPI (missing: MPI_C_FOUND)
 Call Stack (most recent call first):
   /usr/share/cmake-4.3/Modules/FindPackageHandleStandardArgs.cmake:654 (_FPHSA_FAILURE_MESSAGE)
   /usr/share/cmake-4.3/Modules/FindMPI.cmake:2012 (find_package_handle_standard_args)

at which point the module is complicated enough that I’m not sure how to get the last two variables set correctly without setting them manually.

Any help on temporarily patching this would be much appreciated!