Skip to content

Commit 0298b49

Browse files
committed
More robust MPI handling/finding
Untested, but this *should* behave better than the previous implementation and should try really hard to "do what you mean" TM.
1 parent 75a1aa4 commit 0298b49

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

CMakeLists.txt

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ else()
8888
endif()
8989

9090
if(gfortran_compiler)
91+
set(OLD_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
9192
set(CMAKE_REQUIRED_FLAGS "-fcoarray=single -ffree-form")
9293
endif()
9394
include(CheckFortranSourceCompiles)
@@ -99,7 +100,8 @@ CHECK_Fortran_SOURCE_COMPILES("
99100
end program
100101
" Check_Simple_Coarray_Fortran_Source_Compiles)
101102
if(gfortran_compiler)
102-
unset(CMAKE_REQUIRED_FLAGS)
103+
set (CMAKE_REQUIRED_FLAGS ${OLD_REQUIRED_FLAGS})
104+
unset(OLD_REQUIRED_FLAGS)
103105
endif()
104106

105107

@@ -108,17 +110,37 @@ endif()
108110
#----------------------------------------------------------------------------
109111

110112
# If the user passes FC=mpif90 etc. check and prefer that location
111-
get_filename_component( FTN_MPI_DIR "${CMAKE_Fortran_COMPILER}"
113+
get_filename_component( FTN_COMPILER_NAME "${CMAKE_Fortran_COMPILER}"
114+
NAME )
115+
get_filename_component( C_COMPILER_NAME "${CMAKE_C_COMPILER}"
116+
NAME )
117+
get_filename_component( FTN_COMPILER_DIR "${CMAKE_Fortran_COMPILER}"
112118
REALPATH )
113-
get_filename_component( C_MPI_DIR "${CMAKE_C_COMPILER}"
119+
get_filename_component( C_COMPILER_DIR "${CMAKE_C_COMPILER}"
114120
REALPATH )
115-
set ( MPI_HOME "${MPI_HOME}" "${FTN_MPI_DIR}/.." "${C_MPI_DIR}/.." )
116121

117-
# Check the install.sh defaut mpich installation directories
118-
set ( MPI_HOME "${MPI_HOME}" "${CMAKE_SOURCE_DIR}/prerequisites/installations/mpich/3.1.4" "${CMAKE_SOURCE_DIR}/prerequisites/installations/mpich/*" )
122+
if (FTN_COMPILER_NAME MATCHES '^[mM][pP][iI]')
123+
set (MPI_Fortran_COMPILER "${CMAKE_Fortran_COMPILER}")
124+
endif()
125+
if (C_COMPILER_NAME MATCHES '^[mM][pP][iI]')
126+
set (MPI_C_COMPILER "${CMAKE_C_COMPILER}")
127+
endif()
128+
129+
find_package( MPI )
130+
131+
if ( (NOT MPI_C_FOUND) OR (NOT MPI_Fortran_FOUND) )
132+
find_program (MY_MPI_EXEC NAMES mpirun mpiexec lamexec srun
133+
PATHS "${CMAKE_SOURCE_DIR/prerequisites/installations/mpich/3.1.4}" "${CMAKE_SOURCE_DIR}/prerequisites/installations/mpich/*" ENV PATH
134+
HINTS "${FTN_COMPILER_DIR}" "${C_COMPILER_DIR}"
135+
PATH_SUFFIXES bin)
136+
set ( MPI_HOME "${MPI_HOME}" "${MY_MPI_EXEC}" "${MY_MPI_EXEC}/.." )
137+
find_package( MPI REQUIRED )
138+
endif()
119139

120-
find_package( MPI REQUIRED )
121140

141+
#----------------
142+
# Setup MPI flags
143+
#----------------
122144
set(CMAKE_C_COMPILE_FLAGS ${CMAKE_C_COMPILE_FLAGS} ${MPI_C_COMPILE_FLAGS})
123145
set(CMAKE_C_LINK_FLAGS ${CMAKE_C_LINK_FLAGS} ${MPI_C_LINK_FLAGS})
124146
set(CMAKE_Fortran_COMPILE_FLAGS ${CMAKE_Fortran_COMPILE_FLAGS} ${MPI_Fortran_COMPILE_FLAGS})

0 commit comments

Comments
 (0)