1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 Find a Message Passing Interface (MPI) implementation.
10 The Message Passing Interface (MPI) is a library used to write
11 high-performance distributed-memory parallel applications, and is
12 typically deployed on a cluster. MPI is a standard interface (defined
13 by the MPI forum) for which many implementations are available.
15 .. versionadded:: 3.10
16 Major overhaul of the module: many new variables, per-language components,
17 support for a wider variety of runtimes.
19 Variables for using MPI
20 ^^^^^^^^^^^^^^^^^^^^^^^
22 The module exposes the components ``C``, ``CXX``, ``MPICXX`` and ``Fortran``.
23 Each of these controls the various MPI languages to search for.
24 The difference between ``CXX`` and ``MPICXX`` is that ``CXX`` refers to the
25 MPI C API being usable from C++, whereas ``MPICXX`` refers to the MPI-2 C++ API
26 that was removed again in MPI-3.
28 Depending on the enabled components the following variables will be set:
31 Variable indicating that MPI settings for all requested languages have been found.
32 If no components are specified, this is true if MPI settings for all enabled languages
33 were detected. Note that the ``MPICXX`` component does not affect this variable.
35 Minimal version of MPI detected among the requested languages, or all enabled languages
36 if no components were specified.
38 This module will set the following variables per language in your
39 project, where ``<lang>`` is one of C, CXX, or Fortran:
42 Variable indicating the MPI settings for ``<lang>`` were found and that
43 simple MPI test programs compile with the provided settings.
44 ``MPI_<lang>_COMPILER``
45 MPI compiler for ``<lang>`` if such a program exists.
46 ``MPI_<lang>_COMPILE_OPTIONS``
47 Compilation options for MPI programs in ``<lang>``, given as a :ref:`;-list <CMake Language Lists>`.
48 ``MPI_<lang>_COMPILE_DEFINITIONS``
49 Compilation definitions for MPI programs in ``<lang>``, given as a :ref:`;-list <CMake Language Lists>`.
50 ``MPI_<lang>_INCLUDE_DIRS``
51 Include path(s) for MPI header.
52 ``MPI_<lang>_LINK_FLAGS``
53 Linker flags for MPI programs.
54 ``MPI_<lang>_LIBRARIES``
55 All libraries to link MPI programs against.
58 Additionally, the following :prop_tgt:`IMPORTED` targets are defined:
61 Target for using MPI from ``<lang>``.
63 The following variables indicating which bindings are present will be defined:
66 Variable indicating whether the MPI-2 C++ bindings are present (introduced in MPI-2, removed with MPI-3).
67 ``MPI_Fortran_HAVE_F77_HEADER``
68 True if the Fortran 77 header ``mpif.h`` is available.
69 ``MPI_Fortran_HAVE_F90_MODULE``
70 True if the Fortran 90 module ``mpi`` can be used for accessing MPI (MPI-2 and higher only).
71 ``MPI_Fortran_HAVE_F08_MODULE``
72 True if the Fortran 2008 ``mpi_f08`` is available to MPI programs (MPI-3 and higher only).
74 If possible, the MPI version will be determined by this module. The facilities to detect the MPI version
75 were introduced with MPI-1.2, and therefore cannot be found for older MPI versions.
77 ``MPI_<lang>_VERSION_MAJOR``
78 Major version of MPI implemented for ``<lang>`` by the MPI distribution.
79 ``MPI_<lang>_VERSION_MINOR``
80 Minor version of MPI implemented for ``<lang>`` by the MPI distribution.
81 ``MPI_<lang>_VERSION``
82 MPI version implemented for ``<lang>`` by the MPI distribution.
84 Note that there's no variable for the C bindings being accessible through ``mpi.h``, since the MPI standards
85 always have required this binding to work in both C and C++ code.
87 For running MPI programs, the module sets the following variables
89 ``MPIEXEC_EXECUTABLE``
90 Executable for running MPI programs, if such exists.
91 ``MPIEXEC_NUMPROC_FLAG``
92 Flag to pass to ``mpiexec`` before giving it the number of processors to run on.
93 ``MPIEXEC_MAX_NUMPROCS``
94 Number of MPI processors to utilize. Defaults to the number
95 of processors detected on the host system.
97 Flags to pass to ``mpiexec`` directly before the executable to run.
99 Flags to pass to ``mpiexec`` after other flags.
101 Variables for locating MPI
102 ^^^^^^^^^^^^^^^^^^^^^^^^^^
104 This module performs a four step search for an MPI implementation:
106 1. Search for ``MPIEXEC_EXECUTABLE`` and, if found, use its base directory.
107 2. Check if the compiler has MPI support built-in. This is the case if the user passed a
108 compiler wrapper as ``CMAKE_<LANG>_COMPILER`` or if they use Cray system compiler wrappers.
109 3. Attempt to find an MPI compiler wrapper and determine the compiler information from it.
110 4. Try to find an MPI implementation that does not ship such a wrapper by guessing settings.
111 Currently, only Microsoft MPI and MPICH2 on Windows are supported.
113 For controlling the ``MPIEXEC_EXECUTABLE`` step, the following variables may be set:
115 ``MPIEXEC_EXECUTABLE``
116 Manually specify the location of ``mpiexec``.
118 Specify the base directory of the MPI installation.
120 Environment variable to specify the base directory of the MPI installation.
122 Environment variable to specify the base directory of the MPI installation.
124 For controlling the compiler wrapper step, the following variables may be set:
126 ``MPI_<lang>_COMPILER``
127 Search for the specified compiler wrapper and use it.
128 ``MPI_<lang>_COMPILER_FLAGS``
129 Flags to pass to the MPI compiler wrapper during interrogation. Some compiler wrappers
130 support linking debug or tracing libraries if a specific flag is passed and this variable
131 may be used to obtain them.
132 ``MPI_COMPILER_FLAGS``
133 Used to initialize ``MPI_<lang>_COMPILER_FLAGS`` if no language specific flag has been given.
135 ``MPI_EXECUTABLE_SUFFIX``
136 A suffix which is appended to all names that are being looked for. For instance you may set this
137 to ``.mpich`` or ``.openmpi`` to prefer the one or the other on Debian and its derivatives.
139 In order to control the guessing step, the following variable may be set:
141 ``MPI_GUESS_LIBRARY_NAME``
142 Valid values are ``MSMPI`` and ``MPICH2``. If set, only the given library will be searched for.
143 By default, ``MSMPI`` will be preferred over ``MPICH2`` if both are available.
144 This also sets ``MPI_SKIP_COMPILER_WRAPPER`` to ``true``, which may be overridden.
146 Each of the search steps may be skipped with the following control variables:
148 ``MPI_ASSUME_NO_BUILTIN_MPI``
149 If true, the module assumes that the compiler itself does not provide an MPI implementation and
151 ``MPI_SKIP_COMPILER_WRAPPER``
152 If true, no compiler wrapper will be searched for.
153 ``MPI_SKIP_GUESSING``
154 If true, the guessing step will be skipped.
156 Additionally, the following control variable is available to change search behavior:
158 ``MPI_CXX_SKIP_MPICXX``
159 Add some definitions that will disable the MPI-2 C++ bindings.
160 Currently supported are MPICH, Open MPI, Platform MPI and derivatives thereof,
161 for example MVAPICH or Intel MPI.
163 If the find procedure fails for a variable ``MPI_<lang>_WORKS``, then the settings detected by or passed to
164 the module did not work and even a simple MPI test program failed to compile.
166 If all of these parameters were not sufficient to find the right MPI implementation, a user may
167 disable the entire autodetection process by specifying both a list of libraries in ``MPI_<lang>_LIBRARIES``
168 and a list of include directories in ``MPI_<lang>_ADDITIONAL_INCLUDE_DIRS``.
169 Any other variable may be set in addition to these two. The module will then validate the MPI settings and store the
170 settings in the cache.
172 Cache variables for MPI
173 ^^^^^^^^^^^^^^^^^^^^^^^
175 The variable ``MPI_<lang>_INCLUDE_DIRS`` will be assembled from the following variables.
178 ``MPI_<lang>_HEADER_DIR``
179 Location of the ``mpi.h`` header on disk.
183 ``MPI_Fortran_F77_HEADER_DIR``
184 Location of the Fortran 77 header ``mpif.h``, if it exists.
185 ``MPI_Fortran_MODULE_DIR``
186 Location of the ``mpi`` or ``mpi_f08`` modules, if available.
188 For all languages the following variables are additionally considered:
190 ``MPI_<lang>_ADDITIONAL_INCLUDE_DIRS``
191 A :ref:`;-list <CMake Language Lists>` of paths needed in addition to the normal include directories.
192 ``MPI_<include_name>_INCLUDE_DIR``
193 Path variables for include folders referred to by ``<include_name>``.
194 ``MPI_<lang>_ADDITIONAL_INCLUDE_VARS``
195 A :ref:`;-list <CMake Language Lists>` of ``<include_name>`` that will be added to the include locations of ``<lang>``.
197 The variable ``MPI_<lang>_LIBRARIES`` will be assembled from the following variables:
199 ``MPI_<lib_name>_LIBRARY``
200 The location of a library called ``<lib_name>`` for use with MPI.
201 ``MPI_<lang>_LIB_NAMES``
202 A :ref:`;-list <CMake Language Lists>` of ``<lib_name>`` that will be added to the include locations of ``<lang>``.
207 When using ``MPIEXEC_EXECUTABLE`` to execute MPI applications, you should typically
208 use all of the ``MPIEXEC_EXECUTABLE`` flags as follows:
212 ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} ${MPIEXEC_MAX_NUMPROCS}
213 ${MPIEXEC_PREFLAGS} EXECUTABLE ${MPIEXEC_POSTFLAGS} ARGS
215 where ``EXECUTABLE`` is the MPI program, and ``ARGS`` are the arguments to
216 pass to the MPI program.
218 Advanced variables for using MPI
219 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
221 The module can perform some advanced feature detections upon explicit request.
223 **Important notice:** The following checks cannot be performed without *executing* an MPI test program.
224 Consider the special considerations for the behavior of :command:`try_run` during cross compilation.
225 Moreover, running an MPI program can cause additional issues, like a firewall notification on some systems.
226 You should only enable these detections if you absolutely need the information.
228 If the following variables are set to true, the respective search will be performed:
230 ``MPI_DETERMINE_Fortran_CAPABILITIES``
231 Determine for all available Fortran bindings what the values of ``MPI_SUBARRAYS_SUPPORTED`` and
232 ``MPI_ASYNC_PROTECTS_NONBLOCKING`` are and make their values available as ``MPI_Fortran_<binding>_SUBARRAYS``
233 and ``MPI_Fortran_<binding>_ASYNCPROT``, where ``<binding>`` is one of ``F77_HEADER``, ``F90_MODULE`` and
235 ``MPI_DETERMINE_LIBRARY_VERSION``
236 For each language, find the output of ``MPI_Get_library_version`` and make it available as ``MPI_<lang>_LIBRARY_VERSION_STRING``.
237 This information is usually tied to the runtime component of an MPI implementation and might differ depending on ``<lang>``.
238 Note that the return value is entirely implementation defined. This information might be used to identify
239 the MPI vendor and for example pick the correct one of multiple third party binaries that matches the MPI vendor.
241 Backward Compatibility
242 ^^^^^^^^^^^^^^^^^^^^^^
246 For backward compatibility with older versions of FindMPI, these
251 MPI_COMPILER MPI_LIBRARY MPI_EXTRA_LIBRARY
252 MPI_COMPILE_FLAGS MPI_INCLUDE_PATH MPI_LINK_FLAGS
255 In new projects, please use the ``MPI_<lang>_XXX`` equivalents.
256 Additionally, the following variables are deprecated:
258 ``MPI_<lang>_COMPILE_FLAGS``
259 Use ``MPI_<lang>_COMPILE_OPTIONS`` and ``MPI_<lang>_COMPILE_DEFINITIONS`` instead.
260 ``MPI_<lang>_INCLUDE_PATH``
261 For consumption use ``MPI_<lang>_INCLUDE_DIRS`` and for specifying folders use ``MPI_<lang>_ADDITIONAL_INCLUDE_DIRS`` instead.
263 Use ``MPIEXEC_EXECUTABLE`` instead.
264 #]=======================================================================]
267 cmake_policy(SET CMP0057 NEW) # if IN_LIST
268 cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
270 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
271 find_package(PkgConfig QUIET)
273 # Generic compiler names
274 set(_MPI_C_GENERIC_COMPILER_NAMES mpicc mpcc mpicc_r mpcc_r)
275 set(_MPI_CXX_GENERIC_COMPILER_NAMES mpicxx mpiCC mpcxx mpCC mpic++ mpc++
276 mpicxx_r mpiCC_r mpcxx_r mpCC_r mpic++_r mpc++_r)
277 set(_MPI_Fortran_GENERIC_COMPILER_NAMES mpif95 mpif95_r mpf95 mpf95_r
278 mpif90 mpif90_r mpf90 mpf90_r
279 mpif77 mpif77_r mpf77 mpf77_r
282 #Fujitsu cross/own compiler names
283 set(_MPI_Fujitsu_C_COMPILER_NAMES mpifccpx mpifcc)
284 set(_MPI_Fujitsu_CXX_COMPILER_NAMES mpiFCCpx mpiFCC)
285 set(_MPI_Fujitsu_Fortran_COMPILER_NAMES mpifrtpx mpifrt)
288 set(_MPI_GNU_C_COMPILER_NAMES mpigcc mpgcc mpigcc_r mpgcc_r)
289 set(_MPI_GNU_CXX_COMPILER_NAMES mpig++ mpg++ mpig++_r mpg++_r mpigxx)
290 set(_MPI_GNU_Fortran_COMPILER_NAMES mpigfortran mpgfortran mpigfortran_r mpgfortran_r
291 mpig77 mpig77_r mpg77 mpg77_r)
293 # Intel MPI compiler names on Windows
295 list(APPEND _MPI_C_GENERIC_COMPILER_NAMES mpicc.bat)
296 list(APPEND _MPI_CXX_GENERIC_COMPILER_NAMES mpicxx.bat)
297 list(APPEND _MPI_Fortran_GENERIC_COMPILER_NAMES mpifc.bat)
299 # Intel MPI compiler names
300 set(_MPI_Intel_C_COMPILER_NAMES mpiicc.bat)
301 set(_MPI_Intel_CXX_COMPILER_NAMES mpiicpc.bat)
302 set(_MPI_Intel_Fortran_COMPILER_NAMES mpiifort.bat mpif77.bat mpif90.bat)
304 # Intel MPI compiler names
305 set(_MPI_IntelLLVM_C_COMPILER_NAMES mpiicx.bat mpiicc.bat)
306 set(_MPI_IntelLLVM_CXX_COMPILER_NAMES mpiicx.bat mpiicpc.bat) # Not GNU-like mpiicpx.bat
307 set(_MPI_IntelLLVM_Fortran_COMPILER_NAMES mpiifx.bat mpiifort.bat mpif77.bat mpif90.bat)
309 # Intel MPI compiler names for MSMPI
310 set(_MPI_MSVC_C_COMPILER_NAMES mpicl.bat)
311 set(_MPI_MSVC_CXX_COMPILER_NAMES mpicl.bat)
313 # Intel compiler names
314 set(_MPI_Intel_C_COMPILER_NAMES mpiicc)
315 set(_MPI_Intel_CXX_COMPILER_NAMES mpiicpc mpiicxx mpiic++)
316 set(_MPI_Intel_Fortran_COMPILER_NAMES mpiifort mpiif95 mpiif90 mpiif77)
318 # Intel compiler names
319 set(_MPI_IntelLLVM_C_COMPILER_NAMES mpiicx mpiicc)
320 set(_MPI_IntelLLVM_CXX_COMPILER_NAMES mpiicpx mpiicpc mpiicxx mpiic++)
321 set(_MPI_IntelLLVM_Fortran_COMPILER_NAMES mpiifx mpiifort mpiif95 mpiif90 mpiif77)
325 set(_MPI_PGI_C_COMPILER_NAMES mpipgicc mpipgcc mppgcc)
326 set(_MPI_PGI_CXX_COMPILER_NAMES mpipgic++ mpipgCC mppgCC)
327 set(_MPI_PGI_Fortran_COMPILER_NAMES mpipgifort mpipgf95 mpipgf90 mppgf95 mppgf90 mpipgf77 mppgf77)
329 # XLC MPI Compiler names
330 set(_MPI_XL_C_COMPILER_NAMES mpxlc mpxlc_r mpixlc mpixlc_r)
331 set(_MPI_XL_CXX_COMPILER_NAMES mpixlcxx mpixlC mpixlc++ mpxlcxx mpxlc++ mpixlc++ mpxlCC
332 mpixlcxx_r mpixlC_r mpixlc++_r mpxlcxx_r mpxlc++_r mpixlc++_r mpxlCC_r)
333 set(_MPI_XL_Fortran_COMPILER_NAMES mpixlf95 mpixlf95_r mpxlf95 mpxlf95_r
334 mpixlf90 mpixlf90_r mpxlf90 mpxlf90_r
335 mpixlf77 mpixlf77_r mpxlf77 mpxlf77_r
336 mpixlf mpixlf_r mpxlf mpxlf_r)
338 # Cray Compiler names
339 set(_MPI_Cray_C_COMPILER_NAMES cc)
340 set(_MPI_Cray_CXX_COMPILER_NAMES CC)
341 set(_MPI_Cray_Fortran_COMPILER_NAMES ftn)
343 # Prepend vendor-specific compiler wrappers to the list. If we don't know the compiler,
344 # attempt all of them.
345 # By attempting vendor-specific compiler names first, we should avoid situations where the compiler wrapper
346 # stems from a proprietary MPI and won't know which compiler it's being used for. For instance, Intel MPI
347 # controls its settings via the I_MPI_CC environment variables if the generic name is being used.
348 # If we know which compiler we're working with, we can use the most specialized wrapper there is in order to
349 # pick up the right settings for it.
350 foreach (LANG IN ITEMS C CXX Fortran)
351 set(_MPI_${LANG}_COMPILER_NAMES "")
352 foreach (id IN ITEMS Fujitsu FujitsuClang GNU Intel IntelLLVM MSVC PGI XL)
353 if (NOT CMAKE_${LANG}_COMPILER_ID OR CMAKE_${LANG}_COMPILER_ID STREQUAL id)
354 foreach(_COMPILER_NAME IN LISTS _MPI_${id}_${LANG}_COMPILER_NAMES)
355 list(APPEND _MPI_${LANG}_COMPILER_NAMES ${_COMPILER_NAME}${MPI_EXECUTABLE_SUFFIX})
358 unset(_MPI_${id}_${LANG}_COMPILER_NAMES)
360 foreach(_COMPILER_NAME IN LISTS _MPI_${LANG}_GENERIC_COMPILER_NAMES)
361 list(APPEND _MPI_${LANG}_COMPILER_NAMES ${_COMPILER_NAME}${MPI_EXECUTABLE_SUFFIX})
363 unset(_MPI_${LANG}_GENERIC_COMPILER_NAMES)
366 # Names to try for mpiexec
367 # Only mpiexec commands are guaranteed to behave as described in the standard,
368 # mpirun commands are not covered by the standard in any way whatsoever.
369 # lamexec is the executable for LAM/MPI, srun is for SLURM or Open MPI with SLURM support.
370 # srun -n X <executable> is however a valid command, so it behaves 'like' mpiexec.
371 set(_MPIEXEC_NAMES_BASE mpiexec mpiexec.hydra mpiexec.mpd mpirun lamexec srun)
373 unset(_MPIEXEC_NAMES)
374 foreach(_MPIEXEC_NAME IN LISTS _MPIEXEC_NAMES_BASE)
375 list(APPEND _MPIEXEC_NAMES "${_MPIEXEC_NAME}${MPI_EXECUTABLE_SUFFIX}")
377 unset(_MPIEXEC_NAMES_BASE)
379 function (_MPI_check_compiler LANG QUERY_FLAG OUTPUT_VARIABLE RESULT_VARIABLE)
380 if(DEFINED MPI_${LANG}_COMPILER_FLAGS)
381 separate_arguments(_MPI_COMPILER_WRAPPER_OPTIONS NATIVE_COMMAND "${MPI_${LANG}_COMPILER_FLAGS}")
383 separate_arguments(_MPI_COMPILER_WRAPPER_OPTIONS NATIVE_COMMAND "${MPI_COMPILER_FLAGS}")
386 COMMAND ${MPI_${LANG}_COMPILER} ${_MPI_COMPILER_WRAPPER_OPTIONS} ${QUERY_FLAG}
387 OUTPUT_VARIABLE WRAPPER_OUTPUT OUTPUT_STRIP_TRAILING_WHITESPACE
388 ERROR_VARIABLE WRAPPER_OUTPUT ERROR_STRIP_TRAILING_WHITESPACE
389 RESULT_VARIABLE WRAPPER_RETURN)
390 # Some compiler wrappers will yield spurious zero return values, for example
391 # Intel MPI tolerates unknown arguments and if the MPI wrappers loads a shared
392 # library that has invalid or missing version information there would be warning
393 # messages emitted by ld.so in the compiler output. In either case, we'll treat
394 # the output as invalid.
395 if(WRAPPER_OUTPUT MATCHES "undefined reference|unrecognized|need to set|no version information available|command not found")
396 set(WRAPPER_RETURN 255)
398 # Ensure that no error output might be passed upwards.
399 if(NOT WRAPPER_RETURN EQUAL "0")
400 unset(WRAPPER_OUTPUT)
402 # Strip leading whitespace
403 string(REGEX REPLACE "^ +" "" WRAPPER_OUTPUT "${WRAPPER_OUTPUT}")
405 set(${OUTPUT_VARIABLE} "${WRAPPER_OUTPUT}" PARENT_SCOPE)
406 set(${RESULT_VARIABLE} "${WRAPPER_RETURN}" PARENT_SCOPE)
409 macro(_MPI_env_set_ifnot VAR VALUE)
410 if(NOT DEFINED ENV{${VAR}})
411 set(_MPI_${VAR}_WAS_SET FALSE)
412 set(ENV{${VAR}} ${${VALUE}})
414 set(_MPI_${VAR}_WAS_SET TRUE)
418 macro(_MPI_env_unset_ifnot VAR)
419 if(NOT _MPI_${VAR}_WAS_SET)
424 function (_MPI_interrogate_compiler LANG)
425 unset(MPI_COMPILE_CMDLINE)
426 unset(MPI_LINK_CMDLINE)
428 unset(MPI_COMPILE_OPTIONS_WORK)
429 unset(MPI_COMPILE_DEFINITIONS_WORK)
430 unset(MPI_INCLUDE_DIRS_WORK)
431 unset(MPI_LINK_FLAGS_WORK)
432 unset(MPI_LIB_NAMES_WORK)
433 unset(MPI_LIB_FULLPATHS_WORK)
435 # Define the MPICH and Intel MPI compiler variables to the compilers set in CMake.
436 # It's possible to have a per-compiler configuration in these MPI implementations and
437 # a particular MPICH derivate might check compiler interoperability.
438 # Intel MPI in particular does this with I_MPI_CHECK_COMPILER.
439 file(TO_NATIVE_PATH "${CMAKE_${LANG}_COMPILER}" _MPI_UNDERLAYING_COMPILER)
440 # On Windows, the Intel MPI batch scripts can only work with filenames - Full paths will break them.
441 # Due to the lack of other MPICH-based wrappers for Visual C++, we may treat this as default.
443 get_filename_component(_MPI_UNDERLAYING_COMPILER "${_MPI_UNDERLAYING_COMPILER}" NAME)
445 if(LANG STREQUAL "C")
446 _MPI_env_set_ifnot(I_MPI_CC _MPI_UNDERLAYING_COMPILER)
447 _MPI_env_set_ifnot(MPICH_CC _MPI_UNDERLAYING_COMPILER)
448 elseif(LANG STREQUAL "CXX")
449 _MPI_env_set_ifnot(I_MPI_CXX _MPI_UNDERLAYING_COMPILER)
450 _MPI_env_set_ifnot(MPICH_CXX _MPI_UNDERLAYING_COMPILER)
451 elseif(LANG STREQUAL "Fortran")
452 _MPI_env_set_ifnot(I_MPI_FC _MPI_UNDERLAYING_COMPILER)
453 _MPI_env_set_ifnot(MPICH_FC _MPI_UNDERLAYING_COMPILER)
454 _MPI_env_set_ifnot(I_MPI_F77 _MPI_UNDERLAYING_COMPILER)
455 _MPI_env_set_ifnot(MPICH_F77 _MPI_UNDERLAYING_COMPILER)
456 _MPI_env_set_ifnot(I_MPI_F90 _MPI_UNDERLAYING_COMPILER)
457 _MPI_env_set_ifnot(MPICH_F90 _MPI_UNDERLAYING_COMPILER)
460 # Set these two variables for Intel MPI:
461 # - I_MPI_DEBUG_INFO_STRIP: It adds 'objcopy' lines to the compiler output. We support stripping them
462 # (see below), but if we can avoid them in the first place, we should.
463 # - I_MPI_FORT_BIND: By default Intel MPI makes the C/C++ compiler wrappers link Fortran bindings.
464 # This is so that mixed-language code doesn't require additional libraries when linking with mpicc.
465 # For our purposes, this makes little sense, since correct MPI usage from CMake already circumvenes this.
466 set(_MPI_ENV_VALUE "disable")
467 _MPI_env_set_ifnot(I_MPI_DEBUG_INFO_STRIP _MPI_ENV_VALUE)
468 _MPI_env_set_ifnot(I_MPI_FORT_BIND _MPI_ENV_VALUE)
470 # Check whether the -showme:compile option works. This indicates that we have either Open MPI
471 # or a newer version of LAM/MPI, and implies that -showme:link will also work.
472 # Open MPI also supports -show, but separates linker and compiler information
473 _MPI_check_compiler(${LANG} "-showme:compile" MPI_COMPILE_CMDLINE MPI_COMPILER_RETURN)
474 if (MPI_COMPILER_RETURN EQUAL "0")
475 _MPI_check_compiler(${LANG} "-showme:link" MPI_LINK_CMDLINE MPI_COMPILER_RETURN)
477 if (NOT MPI_COMPILER_RETURN EQUAL "0")
478 unset(MPI_COMPILE_CMDLINE)
482 # MPICH and MVAPICH offer -compile-info and -link-info.
483 # For modern versions, both do the same as -show. However, for old versions, they do differ
484 # when called for mpicxx and mpif90 and it's necessary to use them over -show in order to find the
485 # removed MPI C++ bindings.
486 if (NOT MPI_COMPILER_RETURN EQUAL "0")
487 _MPI_check_compiler(${LANG} "-compile-info" MPI_COMPILE_CMDLINE MPI_COMPILER_RETURN)
489 if (MPI_COMPILER_RETURN EQUAL "0")
490 _MPI_check_compiler(${LANG} "-link-info" MPI_LINK_CMDLINE MPI_COMPILER_RETURN)
492 if (NOT MPI_COMPILER_RETURN EQUAL "0")
493 unset(MPI_COMPILE_CMDLINE)
498 # Cray compiler wrappers come usually without a separate mpicc/c++/ftn, but offer
499 # --cray-print-opts=...
500 if (NOT MPI_COMPILER_RETURN EQUAL "0")
501 _MPI_check_compiler(${LANG} "--cray-print-opts=cflags"
502 MPI_COMPILE_CMDLINE MPI_COMPILER_RETURN)
504 if (MPI_COMPILER_RETURN EQUAL "0")
505 # Pass --no-as-needed so the mpi library is always linked. Otherwise, the
506 # Cray compiler wrapper puts an --as-needed flag around the mpi library,
507 # and it is not linked unless code directly refers to it.
508 _MPI_check_compiler(${LANG} "--no-as-needed;--cray-print-opts=libs"
509 MPI_LINK_CMDLINE MPI_COMPILER_RETURN)
511 if (NOT MPI_COMPILER_RETURN EQUAL "0")
512 unset(MPI_COMPILE_CMDLINE)
513 unset(MPI_LINK_CMDLINE)
518 # MPICH, MVAPICH2 and Intel MPI just use "-show". Open MPI also offers this, but the
519 # -showme commands are more specialized.
520 if (NOT MPI_COMPILER_RETURN EQUAL "0")
521 _MPI_check_compiler(${LANG} "-show" MPI_COMPILE_CMDLINE MPI_COMPILER_RETURN)
524 # Older versions of LAM/MPI have "-showme". Open MPI also supports this.
525 # Unknown to MPICH, MVAPICH and Intel MPI.
526 if (NOT MPI_COMPILER_RETURN EQUAL "0")
527 _MPI_check_compiler(${LANG} "-showme" MPI_COMPILE_CMDLINE MPI_COMPILER_RETURN)
530 if (MPI_COMPILER_RETURN EQUAL "0" AND DEFINED MPI_COMPILE_CMDLINE)
531 # Intel MPI can be run with -compchk or I_MPI_CHECK_COMPILER set to 1.
532 # In this case, -show will be prepended with a line to the compiler checker. This is a script that performs
533 # compatibility checks and returns a non-zero exit code together with an error if something fails.
534 # It has to be called as "compchk.sh <arch> <compiler>". Here, <arch> is one out of 32 (i686), 64 (ia64) or 32e (x86_64).
535 # The compiler is identified by filename, and can be either the MPI compiler or the underlying compiler.
536 # NOTE: It is vital to run this script while the environment variables are set up, otherwise it can check the wrong compiler.
537 if(MPI_COMPILE_CMDLINE MATCHES "^([^\" ]+/compchk.sh|\"[^\"]+/compchk.sh\") +([^ ]+)")
538 # Now CMAKE_MATCH_1 contains the path to the compchk.sh file and CMAKE_MATCH_2 the architecture flag.
539 unset(COMPILER_CHECKER_OUTPUT)
541 COMMAND ${CMAKE_MATCH_1} ${CMAKE_MATCH_2} ${MPI_${LANG}_COMPILER}
542 OUTPUT_VARIABLE COMPILER_CHECKER_OUTPUT OUTPUT_STRIP_TRAILING_WHITESPACE
543 ERROR_VARIABLE COMPILER_CHECKER_OUTPUT ERROR_STRIP_TRAILING_WHITESPACE
544 RESULT_VARIABLE MPI_COMPILER_RETURN)
545 # If it returned a non-zero value, the check below will fail and cause the interrogation to be aborted.
546 if(NOT MPI_COMPILER_RETURN EQUAL "0")
547 if(NOT MPI_FIND_QUIETLY)
548 message(STATUS "Intel MPI compiler check failed: ${COMPILER_CHECKER_OUTPUT}")
551 # Since the check passed, we can remove the compchk.sh script.
552 string(REGEX REPLACE "^([^\" ]+|\"[^\"]+\")/compchk.sh.*\n" "" MPI_COMPILE_CMDLINE "${MPI_COMPILE_CMDLINE}")
557 # Revert changes to the environment made previously
558 if(LANG STREQUAL "C")
559 _MPI_env_unset_ifnot(I_MPI_CC)
560 _MPI_env_unset_ifnot(MPICH_CC)
561 elseif(LANG STREQUAL "CXX")
562 _MPI_env_unset_ifnot(I_MPI_CXX)
563 _MPI_env_unset_ifnot(MPICH_CXX)
564 elseif(LANG STREQUAL "Fortran")
565 _MPI_env_unset_ifnot(I_MPI_FC)
566 _MPI_env_unset_ifnot(MPICH_FC)
567 _MPI_env_unset_ifnot(I_MPI_F77)
568 _MPI_env_unset_ifnot(MPICH_F77)
569 _MPI_env_unset_ifnot(I_MPI_F90)
570 _MPI_env_unset_ifnot(MPICH_F90)
573 _MPI_env_unset_ifnot(I_MPI_DEBUG_INFO_STRIP)
574 _MPI_env_unset_ifnot(I_MPI_FORT_BIND)
576 if (NOT (MPI_COMPILER_RETURN EQUAL "0") OR NOT (DEFINED MPI_COMPILE_CMDLINE))
577 # Cannot interrogate this compiler, so exit.
578 set(MPI_${LANG}_WRAPPER_FOUND FALSE PARENT_SCOPE)
581 unset(MPI_COMPILER_RETURN)
583 # We have our command lines, but we might need to copy MPI_COMPILE_CMDLINE
584 # into MPI_LINK_CMDLINE, if we didn't find the link line.
585 if (NOT DEFINED MPI_LINK_CMDLINE)
586 set(MPI_LINK_CMDLINE "${MPI_COMPILE_CMDLINE}")
589 # Visual Studio parsers permit each flag prefixed by either / or -.
590 # We'll normalize this to the - syntax we use for CMake purposes anyways.
592 foreach(_MPI_VARIABLE IN ITEMS COMPILE LINK)
593 # The Intel MPI wrappers on Windows prefix their output with some copyright boilerplate.
594 # To prevent possible problems, we discard this text before proceeding with any further matching.
595 string(REGEX REPLACE "^[^ ]+ for the Intel\\(R\\) MPI Library [^\n]+ for Windows\\*\nCopyright\\(C\\) [^\n]+, Intel Corporation\\. All rights reserved\\.\n\n" ""
596 MPI_${_MPI_VARIABLE}_CMDLINE "${MPI_${_MPI_VARIABLE}_CMDLINE}")
597 string(REGEX REPLACE "(^| )/" "\\1-" MPI_${_MPI_VARIABLE}_CMDLINE "${MPI_${_MPI_VARIABLE}_CMDLINE}")
598 string(REPLACE "-libpath:" "-LIBPATH:" MPI_${_MPI_VARIABLE}_CMDLINE "${MPI_${_MPI_VARIABLE}_CMDLINE}")
602 # For MSVC and cl-compatible compilers, the keyword /link indicates a point after which
603 # everything following is passed to the linker. In this case, we drop all prior information
604 # from the link line and treat any unknown extra flags as linker flags.
605 set(_MPI_FILTERED_LINK_INFORMATION FALSE)
607 if(MPI_LINK_CMDLINE MATCHES " -(link|LINK) ")
608 string(REGEX REPLACE ".+-(link|LINK) +" "" MPI_LINK_CMDLINE "${MPI_LINK_CMDLINE}")
609 set(_MPI_FILTERED_LINK_INFORMATION TRUE)
611 string(REGEX REPLACE " +-(link|LINK) .+" "" MPI_COMPILE_CMDLINE "${MPI_COMPILE_CMDLINE}")
615 # At this point, we obtained some output from a compiler wrapper that works.
616 # We'll now try to parse it into variables with meaning to us.
617 if(LANG STREQUAL "Fortran")
618 # If MPICH (and derivates) didn't recognize the Fortran compiler include flag during configuration,
619 # they'll return a set of three commands, consisting out of a symlink command for mpif.h,
620 # the actual compiler command and deletion of the created symlink.
621 # Especially with M(VA)PICH-1, this appears to happen erroneously, and therefore we should translate
622 # this output into an additional include directory and then drop it from the output.
623 if(MPI_COMPILE_CMDLINE MATCHES "^ln -s ([^\" ]+|\"[^\"]+\") mpif.h")
624 get_filename_component(MPI_INCLUDE_DIRS_WORK "${CMAKE_MATCH_1}" DIRECTORY)
625 string(REGEX REPLACE "^ln -s ([^\" ]+|\"[^\"]+\") mpif.h\n" "" MPI_COMPILE_CMDLINE "${MPI_COMPILE_CMDLINE}")
626 string(REGEX REPLACE "^ln -s ([^\" ]+|\"[^\"]+\") mpif.h\n" "" MPI_LINK_CMDLINE "${MPI_LINK_CMDLINE}")
627 string(REGEX REPLACE "\nrm -f mpif.h$" "" MPI_COMPILE_CMDLINE "${MPI_COMPILE_CMDLINE}")
628 string(REGEX REPLACE "\nrm -f mpif.h$" "" MPI_LINK_CMDLINE "${MPI_LINK_CMDLINE}")
632 # If Intel MPI was configured for static linkage with -static_mpi, the wrapper will by default strip
633 # debug information from resulting binaries (see I_MPI_DEBUG_INFO_STRIP).
634 # Since we cannot process this information into CMake logic, we need to discard the resulting objcopy
635 # commands from the output.
636 string(REGEX REPLACE "(^|\n)objcopy[^\n]+(\n|$)" "" MPI_COMPILE_CMDLINE "${MPI_COMPILE_CMDLINE}")
637 string(REGEX REPLACE "(^|\n)objcopy[^\n]+(\n|$)" "" MPI_LINK_CMDLINE "${MPI_LINK_CMDLINE}")
640 # For Visual C++, extracting compiler options in a generic fashion isn't easy. However, no MPI implementation
641 # on Windows seems to require any specific ones, either.
643 # Extract compile options from the compile command line.
644 string(REGEX MATCHALL "(^| )-f([^\" ]+|\"[^\"]+\")" MPI_ALL_COMPILE_OPTIONS "${MPI_COMPILE_CMDLINE}")
646 foreach(_MPI_COMPILE_OPTION IN LISTS MPI_ALL_COMPILE_OPTIONS)
647 string(REGEX REPLACE "^ " "" _MPI_COMPILE_OPTION "${_MPI_COMPILE_OPTION}")
649 # Ignore -fstack-protector directives: These occur on MPICH and MVAPICH when the libraries
650 # themselves were built with this flag. However, this flag is unrelated to using MPI, and
651 # we won't match the accompanying --param-ssp-size and -Wp,-D_FORTIFY_SOURCE flags and therefore
652 # produce inconsistent results with the regularly flags.
653 # Similarly, aliasing flags do not belong into our flag array.
654 # Also strip out `-framework` flags.
655 if(NOT _MPI_COMPILE_OPTION MATCHES "^-f((no-|)(stack-protector|strict-aliasing)|PI[CE]|pi[ce]|ramework)")
656 list(APPEND MPI_COMPILE_OPTIONS_WORK "${_MPI_COMPILE_OPTION}")
661 # For GNU-style compilers, it's possible to prefix includes and definitions with certain flags to pass them
662 # only to the preprocessor. For CMake purposes, we need to treat, but ignore such scopings.
663 # Note that we do not support spaces between the arguments, i.e. -Wp,-I -Wp,/opt/mympi will not be parsed
664 # correctly. This form does not seem to occur in any common MPI implementation, however.
666 set(_MPI_PREPROCESSOR_FLAG_REGEX "(-Wp,|-Xpreprocessor )?")
668 set(_MPI_PREPROCESSOR_FLAG_REGEX "")
671 # Same deal as above, for the definitions.
672 string(REGEX MATCHALL "(^| )${_MPI_PREPROCESSOR_FLAG_REGEX}-D *([^\" ]+|\"[^\"]+\")" MPI_ALL_COMPILE_DEFINITIONS "${MPI_COMPILE_CMDLINE}")
674 foreach(_MPI_COMPILE_DEFINITION IN LISTS MPI_ALL_COMPILE_DEFINITIONS)
675 string(REGEX REPLACE "^ ?${_MPI_PREPROCESSOR_FLAG_REGEX}-D *" "" _MPI_COMPILE_DEFINITION "${_MPI_COMPILE_DEFINITION}")
676 string(REPLACE "\"" "" _MPI_COMPILE_DEFINITION "${_MPI_COMPILE_DEFINITION}")
677 if(NOT _MPI_COMPILE_DEFINITION MATCHES "^_FORTIFY_SOURCE.*")
678 list(APPEND MPI_COMPILE_DEFINITIONS_WORK "${_MPI_COMPILE_DEFINITION}")
682 # Extract include paths from compile command line
683 string(REGEX MATCHALL "(^| )${_MPI_PREPROCESSOR_FLAG_REGEX}${CMAKE_INCLUDE_FLAG_${LANG}} *([^\" ]+|\"[^\"]+\")"
684 MPI_ALL_INCLUDE_PATHS "${MPI_COMPILE_CMDLINE}")
686 # If extracting failed to work, we'll try using -showme:incdirs.
687 # Unlike before, we do this without the environment variables set up, but since only MPICH derivates are affected by any of them, and
688 # -showme:... is only supported by Open MPI and LAM/MPI, this isn't a concern.
689 if (NOT MPI_ALL_INCLUDE_PATHS)
690 _MPI_check_compiler(${LANG} "-showme:incdirs" MPI_INCDIRS_CMDLINE MPI_INCDIRS_COMPILER_RETURN)
691 if(MPI_INCDIRS_COMPILER_RETURN)
692 separate_arguments(MPI_ALL_INCLUDE_PATHS NATIVE_COMMAND "${MPI_INCDIRS_CMDLINE}")
696 foreach(_MPI_INCLUDE_PATH IN LISTS MPI_ALL_INCLUDE_PATHS)
697 string(REGEX REPLACE "^ ?${_MPI_PREPROCESSOR_FLAG_REGEX}${CMAKE_INCLUDE_FLAG_${LANG}} *" "" _MPI_INCLUDE_PATH "${_MPI_INCLUDE_PATH}")
698 string(REPLACE "\"" "" _MPI_INCLUDE_PATH "${_MPI_INCLUDE_PATH}")
699 string(REPLACE "'" "" _MPI_INCLUDE_PATH "${_MPI_INCLUDE_PATH}")
700 get_filename_component(_MPI_INCLUDE_PATH "${_MPI_INCLUDE_PATH}" REALPATH)
701 list(APPEND MPI_INCLUDE_DIRS_WORK "${_MPI_INCLUDE_PATH}")
704 # The next step are linker flags and library directories. Here, we first take the flags given in raw -L or -LIBPATH: syntax.
705 string(REGEX MATCHALL "(^| )${CMAKE_LIBRARY_PATH_FLAG} *([^\" ]+|\"[^\"]+\")" MPI_DIRECT_LINK_PATHS "${MPI_LINK_CMDLINE}")
706 foreach(_MPI_LPATH IN LISTS MPI_DIRECT_LINK_PATHS)
707 string(REGEX REPLACE "(^| )${CMAKE_LIBRARY_PATH_FLAG} *" "" _MPI_LPATH "${_MPI_LPATH}")
708 list(APPEND MPI_ALL_LINK_PATHS "${_MPI_LPATH}")
711 # If the link commandline hasn't been filtered (e.g. when using MSVC and /link), we need to extract the relevant parts first.
712 if(NOT _MPI_FILTERED_LINK_INFORMATION)
713 string(REGEX MATCHALL "(^| )(-Wl,|-Xlinker +)([^\" ]+|\"[^\"]+\")" MPI_LINK_FLAGS "${MPI_LINK_CMDLINE}")
715 # In this case, we could also find some indirectly given linker paths, e.g. prefixed by -Xlinker or -Wl,
716 # Since syntaxes like -Wl,-L -Wl,/my/path/to/lib are also valid, we parse these paths by first removing -Wl, and -Xlinker
717 # from the list of filtered flags and then parse the remainder of the output.
718 string(REGEX REPLACE "(-Wl,|-Xlinker +)" "" MPI_LINK_FLAGS_RAW "${MPI_LINK_FLAGS}")
720 # Now we can parse the leftover output. Note that spaces can now be handled since the above example would reduce to
721 # -L /my/path/to/lib and can be extracted correctly.
722 string(REGEX MATCHALL "^(${CMAKE_LIBRARY_PATH_FLAG},? *|--library-path=)([^\" ]+|\"[^\"]+\")"
723 MPI_INDIRECT_LINK_PATHS "${MPI_LINK_FLAGS_RAW}")
725 foreach(_MPI_LPATH IN LISTS MPI_INDIRECT_LINK_PATHS)
726 string(REGEX REPLACE "^(${CMAKE_LIBRARY_PATH_FLAG},? *|--library-path=)" "" _MPI_LPATH "${_MPI_LPATH}")
727 list(APPEND MPI_ALL_LINK_PATHS "${_MPI_LPATH}")
730 # We need to remove the flags we extracted from the linker flag list now.
731 string(REGEX REPLACE "(^| )(-Wl,|-Xlinker +)(${CMAKE_LIBRARY_PATH_FLAG},? *(-Wl,|-Xlinker +)?|--library-path=)([^\" ]+|\"[^\"]+\")" ""
732 MPI_LINK_CMDLINE_FILTERED "${MPI_LINK_CMDLINE}")
734 # Some MPI implementations pass on options they themselves were built with. Since -z,noexecstack is a common
735 # hardening, we should strip it. In general, the -z options should be undesirable.
736 string(REGEX REPLACE "(^| )-Wl,-z(,[^ ]+| +-Wl,[^ ]+)" "" MPI_LINK_CMDLINE_FILTERED "${MPI_LINK_CMDLINE_FILTERED}")
737 string(REGEX REPLACE "(^| )-Xlinker +-z +-Xlinker +[^ ]+" "" MPI_LINK_CMDLINE_FILTERED "${MPI_LINK_CMDLINE_FILTERED}")
739 # We only consider options of the form -Wl or -Xlinker:
740 string(REGEX MATCHALL "(^| )(-Wl,|-Xlinker +)([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_FLAGS "${MPI_LINK_CMDLINE_FILTERED}")
742 # As a next step, we assemble the linker flags extracted in a preliminary flags string
743 foreach(_MPI_LINK_FLAG IN LISTS MPI_ALL_LINK_FLAGS)
744 string(STRIP "${_MPI_LINK_FLAG}" _MPI_LINK_FLAG)
745 if (MPI_LINK_FLAGS_WORK)
746 string(APPEND MPI_LINK_FLAGS_WORK " ${_MPI_LINK_FLAG}")
748 set(MPI_LINK_FLAGS_WORK "${_MPI_LINK_FLAG}")
752 # In the filtered case, we obtain the link time flags by just stripping the library paths.
753 string(REGEX REPLACE "(^| )${CMAKE_LIBRARY_PATH_FLAG} *([^\" ]+|\"[^\"]+\")" "" MPI_LINK_CMDLINE_FILTERED "${MPI_LINK_CMDLINE}")
756 # If we failed to extract any linker paths, we'll try using the -showme:libdirs option with the MPI compiler.
757 # This will return a list of folders, not a set of flags!
758 if (NOT MPI_ALL_LINK_PATHS)
759 _MPI_check_compiler(${LANG} "-showme:libdirs" MPI_LIBDIRS_CMDLINE MPI_LIBDIRS_COMPILER_RETURN)
760 if(MPI_LIBDIRS_COMPILER_RETURN)
761 separate_arguments(MPI_ALL_LINK_PATHS NATIVE_COMMAND "${MPI_LIBDIRS_CMDLINE}")
765 # We need to remove potential quotes and convert the paths to CMake syntax while resolving them, too.
766 foreach(_MPI_LPATH IN LISTS MPI_ALL_LINK_PATHS)
767 string(REPLACE "\"" "" _MPI_LPATH "${_MPI_LPATH}")
768 get_filename_component(_MPI_LPATH "${_MPI_LPATH}" REALPATH)
769 list(APPEND MPI_LINK_DIRECTORIES_WORK "${_MPI_LPATH}")
772 # Extract the set of libraries to link against from the link command line
773 # This only makes sense if CMAKE_LINK_LIBRARY_FLAG is defined, i.e. a -lxxxx syntax is supported by the compiler.
774 if(CMAKE_LINK_LIBRARY_FLAG)
775 string(REGEX MATCHALL "(^| )${CMAKE_LINK_LIBRARY_FLAG}([^\" ]+|\"[^\"]+\")"
776 MPI_LIBNAMES "${MPI_LINK_CMDLINE}")
778 foreach(_MPI_LIB_NAME IN LISTS MPI_LIBNAMES)
779 # also match flags starting with "-l:" here
780 string(REGEX REPLACE "^ ?${CMAKE_LINK_LIBRARY_FLAG}(:lib|:)?" "" _MPI_LIB_NAME "${_MPI_LIB_NAME}")
781 string(REPLACE "\"" "" _MPI_LIB_NAME "${_MPI_LIB_NAME}")
782 list(APPEND MPI_LIB_NAMES_WORK "${_MPI_LIB_NAME}")
786 # Treat linker objects given by full path, for example static libraries, import libraries
787 # or shared libraries if there aren't any import libraries in use on the system.
788 # Note that we do not consider CMAKE_<TYPE>_LIBRARY_PREFIX intentionally here: The linker will for a given file
789 # decide how to link it based on file type, not based on a prefix like 'lib'.
790 set(_MPI_LIB_SUFFIX_REGEX "${CMAKE_STATIC_LIBRARY_SUFFIX}")
791 if(DEFINED CMAKE_IMPORT_LIBRARY_SUFFIX)
792 if(NOT (CMAKE_IMPORT_LIBRARY_SUFFIX STREQUAL CMAKE_STATIC_LIBRARY_SUFFIX))
793 string(APPEND _MPI_LIB_SUFFIX_REGEX "|${CMAKE_IMPORT_LIBRARY_SUFFIX}")
796 string(APPEND _MPI_LIB_SUFFIX_REGEX "|${CMAKE_SHARED_LIBRARY_SUFFIX}")
798 set(_MPI_LIB_NAME_REGEX "(([^\" ]+(${_MPI_LIB_SUFFIX_REGEX}))|(\"[^\"]+(${_MPI_LIB_SUFFIX_REGEX})\"))( +|$)")
799 string(REPLACE "." "\\." _MPI_LIB_NAME_REGEX "${_MPI_LIB_NAME_REGEX}")
801 string(REGEX MATCHALL "${_MPI_LIB_NAME_REGEX}" MPI_LIBNAMES "${MPI_LINK_CMDLINE}")
802 foreach(_MPI_LIB_NAME IN LISTS MPI_LIBNAMES)
803 # Do not match "-l:" flags
804 string(REGEX MATCH "^ ?${CMAKE_LINK_LIBRARY_FLAG}:" _MPI_LIB_NAME_TEST "${_MPI_LIB_NAME}")
805 if(_MPI_LIB_NAME_TEST STREQUAL "")
806 string(REGEX REPLACE "^ +\"?|\"? +$" "" _MPI_LIB_NAME "${_MPI_LIB_NAME}")
807 get_filename_component(_MPI_LIB_PATH "${_MPI_LIB_NAME}" DIRECTORY)
808 if(NOT _MPI_LIB_PATH STREQUAL "")
809 list(APPEND MPI_LIB_FULLPATHS_WORK "${_MPI_LIB_NAME}")
811 list(APPEND MPI_LIB_NAMES_WORK "${_MPI_LIB_NAME}")
816 # Save the explicitly given link directories
817 set(MPI_LINK_DIRECTORIES_LEFTOVER "${MPI_LINK_DIRECTORIES_WORK}")
819 # An MPI compiler wrapper could have its MPI libraries in the implicitly
820 # linked directories of the compiler itself.
821 if(DEFINED CMAKE_${LANG}_IMPLICIT_LINK_DIRECTORIES)
822 list(APPEND MPI_LINK_DIRECTORIES_WORK "${CMAKE_${LANG}_IMPLICIT_LINK_DIRECTORIES}")
825 # Determine full path names for all of the libraries that one needs
826 # to link against in an MPI program
827 unset(MPI_PLAIN_LIB_NAMES_WORK)
828 foreach(_MPI_LIB_NAME IN LISTS MPI_LIB_NAMES_WORK)
829 get_filename_component(_MPI_PLAIN_LIB_NAME "${_MPI_LIB_NAME}" NAME_WE)
830 list(APPEND MPI_PLAIN_LIB_NAMES_WORK "${_MPI_PLAIN_LIB_NAME}")
831 find_library(MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY
832 NAMES "${_MPI_LIB_NAME}" "lib${_MPI_LIB_NAME}"
833 HINTS ${MPI_LINK_DIRECTORIES_WORK}
834 DOC "Location of the ${_MPI_PLAIN_LIB_NAME} library for MPI"
836 mark_as_advanced(MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY)
837 # Remove the directory from the remainder list.
838 if(MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY)
839 get_filename_component(_MPI_TAKEN_DIRECTORY "${MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY}" DIRECTORY)
840 list(REMOVE_ITEM MPI_LINK_DIRECTORIES_LEFTOVER "${_MPI_TAKEN_DIRECTORY}")
844 # Add the link directories given explicitly that we haven't used back as linker directories.
846 foreach(_MPI_LINK_DIRECTORY IN LISTS MPI_LINK_DIRECTORIES_LEFTOVER)
847 file(TO_NATIVE_PATH "${_MPI_LINK_DIRECTORY}" _MPI_LINK_DIRECTORY_ACTUAL)
848 string(FIND "${_MPI_LINK_DIRECTORY_ACTUAL}" " " _MPI_LINK_DIRECTORY_CONTAINS_SPACE)
849 if(NOT _MPI_LINK_DIRECTORY_CONTAINS_SPACE EQUAL "-1")
850 set(_MPI_LINK_DIRECTORY_ACTUAL "\"${_MPI_LINK_DIRECTORY_ACTUAL}\"")
852 if(MPI_LINK_FLAGS_WORK)
853 string(APPEND MPI_LINK_FLAGS_WORK " ${CMAKE_LIBRARY_PATH_FLAG}${_MPI_LINK_DIRECTORY_ACTUAL}")
855 set(MPI_LINK_FLAGS_WORK "${CMAKE_LIBRARY_PATH_FLAG}${_MPI_LINK_DIRECTORY_ACTUAL}")
860 # Deal with the libraries given with full path next
861 unset(MPI_DIRECT_LIB_NAMES_WORK)
862 foreach(_MPI_LIB_FULLPATH IN LISTS MPI_LIB_FULLPATHS_WORK)
863 get_filename_component(_MPI_PLAIN_LIB_NAME "${_MPI_LIB_FULLPATH}" NAME_WE)
864 list(APPEND MPI_DIRECT_LIB_NAMES_WORK "${_MPI_PLAIN_LIB_NAME}")
865 set(MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY "${_MPI_LIB_FULLPATH}" CACHE FILEPATH "Location of the ${_MPI_PLAIN_LIB_NAME} library for MPI")
866 mark_as_advanced(MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY)
868 # Directly linked objects should be linked first in case some generic linker flags are needed for them.
869 if(MPI_DIRECT_LIB_NAMES_WORK)
870 set(MPI_PLAIN_LIB_NAMES_WORK "${MPI_DIRECT_LIB_NAMES_WORK};${MPI_PLAIN_LIB_NAMES_WORK}")
873 # MPI might require pthread to work. The above mechanism wouldn't detect it, but we need to
874 # link it in that case. -lpthread is covered by the normal library treatment on the other hand.
875 if(MPI_COMPILE_CMDLINE MATCHES "-pthread")
876 list(APPEND MPI_COMPILE_OPTIONS_WORK "-pthread")
877 if(MPI_LINK_FLAGS_WORK)
878 string(APPEND MPI_LINK_FLAGS_WORK " -pthread")
880 set(MPI_LINK_FLAGS_WORK "-pthread")
884 if(MPI_${LANG}_EXTRA_COMPILE_DEFINITIONS)
885 list(APPEND MPI_COMPILE_DEFINITIONS_WORK "${MPI_${LANG}_EXTRA_COMPILE_DEFINITIONS}")
887 if(MPI_${LANG}_EXTRA_COMPILE_OPTIONS)
888 list(APPEND MPI_COMPILE_OPTIONS_WORK "${MPI_${LANG}_EXTRA_COMPILE_OPTIONS}")
890 if(MPI_${LANG}_EXTRA_LIB_NAMES)
891 list(APPEND MPI_PLAIN_LIB_NAMES_WORK "${MPI_${LANG}_EXTRA_LIB_NAMES}")
894 # If we found MPI, set up all of the appropriate cache entries
895 if(NOT MPI_${LANG}_COMPILE_OPTIONS)
896 set(MPI_${LANG}_COMPILE_OPTIONS ${MPI_COMPILE_OPTIONS_WORK} CACHE STRING "MPI ${LANG} compilation options" FORCE)
898 if(NOT MPI_${LANG}_COMPILE_DEFINITIONS)
899 set(MPI_${LANG}_COMPILE_DEFINITIONS ${MPI_COMPILE_DEFINITIONS_WORK} CACHE STRING "MPI ${LANG} compilation definitions" FORCE)
901 if(NOT MPI_${LANG}_COMPILER_INCLUDE_DIRS)
902 set(MPI_${LANG}_COMPILER_INCLUDE_DIRS ${MPI_INCLUDE_DIRS_WORK} CACHE STRING "MPI ${LANG} compiler wrapper include directories" FORCE)
904 if(NOT MPI_${LANG}_LINK_FLAGS)
905 set(MPI_${LANG}_LINK_FLAGS ${MPI_LINK_FLAGS_WORK} CACHE STRING "MPI ${LANG} linker flags" FORCE)
907 if(NOT MPI_${LANG}_LIB_NAMES)
908 set(MPI_${LANG}_LIB_NAMES ${MPI_PLAIN_LIB_NAMES_WORK} CACHE STRING "MPI ${LANG} libraries to link against" FORCE)
910 set(MPI_${LANG}_WRAPPER_FOUND TRUE PARENT_SCOPE)
913 function(_MPI_guess_settings LANG)
914 set(MPI_GUESS_FOUND FALSE)
915 # Currently only MSMPI and MPICH2 on Windows are supported, so we can skip this search if we're not targeting that.
919 # The environment variables MSMPI_INC and MSMPILIB32/64 are the only ways of locating the MSMPI_SDK,
920 # which is installed separately from the runtime. Thus it's possible to have mpiexec but not MPI headers
921 # or import libraries and vice versa.
922 if(NOT MPI_GUESS_LIBRARY_NAME OR MPI_GUESS_LIBRARY_NAME STREQUAL "MSMPI")
923 # We first attempt to locate the msmpi.lib. Should be find it, we'll assume that the MPI present is indeed
925 if(CMAKE_SIZEOF_VOID_P EQUAL "8")
926 file(TO_CMAKE_PATH "$ENV{MSMPI_LIB64}" MPI_MSMPI_LIB_PATH)
927 file(TO_CMAKE_PATH "$ENV{MSMPI_INC}/x64" MPI_MSMPI_INC_PATH_EXTRA)
929 file(TO_CMAKE_PATH "$ENV{MSMPI_LIB32}" MPI_MSMPI_LIB_PATH)
930 file(TO_CMAKE_PATH "$ENV{MSMPI_INC}/x86" MPI_MSMPI_INC_PATH_EXTRA)
933 find_library(MPI_msmpi_LIBRARY
935 HINTS ${MPI_MSMPI_LIB_PATH}
936 DOC "Location of the msmpi library for Microsoft MPI")
937 mark_as_advanced(MPI_msmpi_LIBRARY)
939 if(MPI_msmpi_LIBRARY)
940 # Next, we attempt to locate the MPI header. Note that for Fortran we know that mpif.h is a way
941 # MSMPI can be used and therefore that header has to be present.
942 if(NOT MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS)
943 get_filename_component(MPI_MSMPI_INC_DIR "$ENV{MSMPI_INC}" REALPATH)
944 set(MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS "${MPI_MSMPI_INC_DIR}" CACHE STRING "MPI ${LANG} additional include directories" FORCE)
945 unset(MPI_MSMPI_INC_DIR)
948 # For MSMPI, one can compile the MPI module by building the mpi.f90 shipped with the MSMPI SDK,
949 # thus it might be present or provided by the user. Figuring out which is supported is done later on.
950 # The PGI Fortran compiler for instance ships a prebuilt set of modules in its own include folder.
951 # Should a user be employing PGI or have built its own set and provided it via cache variables, the
952 # splitting routine would have located the module files.
954 # For C and C++, we're done here (MSMPI does not ship the MPI-2 C++ bindings) - however, for Fortran
955 # we need some extra library to glue Fortran support together:
956 # MSMPI ships 2-4 Fortran libraries, each for different Fortran compiler behaviors. The library names
957 # ending with a c are using the cdecl calling convention, whereas those ending with an s are for Fortran
958 # implementations using stdcall. Therefore, the 64-bit MSMPI only ships those ending in 'c', whereas the 32-bit
959 # has both variants available.
960 # The second difference is the last but one letter, if it's an e(nd), the length of a string argument is
961 # passed by the Fortran compiler after all other arguments on the parameter list, if it's an m(ixed),
962 # it's passed immediately after the string address.
965 # - msmpifec: CHARACTER length passed after the parameter list and using cdecl calling convention
966 # - msmpifmc: CHARACTER length passed directly after string address and using cdecl calling convention
967 # - msmpifes: CHARACTER length passed after the parameter list and using stdcall calling convention
968 # - msmpifms: CHARACTER length passed directly after string address and using stdcall calling convention
969 # 32-bit MSMPI ships all four libraries, 64-bit MSMPI ships only the first two.
971 # As is, Intel Fortran and PGI Fortran both use the 'ec' variant of the calling convention, whereas
972 # the old Compaq Visual Fortran compiler defaulted to the 'ms' version. It's possible to make Intel Fortran
973 # use the CVF calling convention using /iface:cvf, but we assume - and this is also assumed in FortranCInterface -
974 # this isn't the case. It's also possible to make CVF use the 'ec' variant, using /iface=(cref,nomixed_str_len_arg).
976 # Our strategy is now to locate all libraries, but enter msmpifec into the LIB_NAMES array.
977 # Should this not be adequate it's a straightforward way for a user to change the LIB_NAMES array and
978 # have his library found. Still, this should not be necessary outside of exceptional cases, as reasoned.
979 if (LANG STREQUAL "Fortran")
980 set(MPI_MSMPI_CALLINGCONVS c)
981 if(CMAKE_SIZEOF_VOID_P EQUAL "4")
982 list(APPEND MPI_MSMPI_CALLINGCONVS s)
984 foreach(mpistrlenpos IN ITEMS e m)
985 foreach(mpicallingconv IN LISTS MPI_MSMPI_CALLINGCONVS)
986 find_library(MPI_msmpif${mpistrlenpos}${mpicallingconv}_LIBRARY
987 NAMES msmpif${mpistrlenpos}${mpicallingconv}
988 HINTS "${MPI_MSMPI_LIB_PATH}"
989 DOC "Location of the msmpi${mpistrlenpos}${mpicallingconv} library for Microsoft MPI")
990 mark_as_advanced(MPI_msmpif${mpistrlenpos}${mpicallingconv}_LIBRARY)
993 if(NOT MPI_${LANG}_LIB_NAMES)
994 set(MPI_${LANG}_LIB_NAMES "msmpi;msmpifec" CACHE STRING "MPI ${LANG} libraries to link against" FORCE)
997 # At this point we're *not* done. MSMPI requires an additional include file for Fortran giving the value
998 # of MPI_AINT. This file is called mpifptr.h located in the x64 and x86 subfolders, respectively.
999 find_path(MPI_mpifptr_INCLUDE_DIR
1001 HINTS "${MPI_MSMPI_INC_PATH_EXTRA}"
1002 DOC "Location of the mpifptr.h extra header for Microsoft MPI")
1003 if(NOT MPI_${LANG}_ADDITIONAL_INCLUDE_VARS)
1004 set(MPI_${LANG}_ADDITIONAL_INCLUDE_VARS "mpifptr" CACHE STRING "MPI ${LANG} additional include directory variables, given in the form MPI_<name>_INCLUDE_DIR." FORCE)
1006 mark_as_advanced(MPI_${LANG}_ADDITIONAL_INCLUDE_VARS MPI_mpifptr_INCLUDE_DIR)
1008 if(NOT MPI_${LANG}_LIB_NAMES)
1009 set(MPI_${LANG}_LIB_NAMES "msmpi" CACHE STRING "MPI ${LANG} libraries to link against" FORCE)
1012 mark_as_advanced(MPI_${LANG}_LIB_NAMES)
1013 set(MPI_GUESS_FOUND TRUE)
1015 if(_MPIEXEC_NOT_GIVEN)
1016 unset(MPIEXEC_EXECUTABLE CACHE)
1019 find_program(MPIEXEC_EXECUTABLE
1021 HINTS $ENV{MSMPI_BIN} "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\MPI;InstallRoot]/Bin"
1022 DOC "Executable for running MPI programs.")
1026 # At this point there's not many MPIs that we could still consider.
1027 # OpenMPI 1.6.x and below supported Windows, but these ship compiler wrappers that still work.
1028 # The only other relevant MPI implementation without a wrapper is MPICH2, which had Windows support in 1.4.1p1 and older.
1029 if(NOT MPI_GUESS_FOUND AND (NOT MPI_GUESS_LIBRARY_NAME OR MPI_GUESS_LIBRARY_NAME STREQUAL "MPICH2"))
1030 set(MPI_MPICH_PREFIX_PATHS
1031 "$ENV{ProgramW6432}/MPICH2/lib"
1032 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MPICH\\SMPD;binary]/../lib"
1033 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MPICH2;Path]/lib"
1036 # All of C, C++ and Fortran will need mpi.lib, so we'll look for this first
1037 find_library(MPI_mpi_LIBRARY
1039 HINTS ${MPI_MPICH_PREFIX_PATHS})
1040 mark_as_advanced(MPI_mpi_LIBRARY)
1041 # If we found mpi.lib, we detect the rest of MPICH2
1043 set(MPI_MPICH_LIB_NAMES "mpi")
1044 # If MPI-2 C++ bindings are requested, we need to locate cxx.lib as well.
1045 # Otherwise, MPICH_SKIP_MPICXX will be defined and these bindings aren't needed.
1046 if(LANG STREQUAL "CXX" AND NOT MPI_CXX_SKIP_MPICXX)
1047 find_library(MPI_cxx_LIBRARY
1049 HINTS ${MPI_MPICH_PREFIX_PATHS})
1050 mark_as_advanced(MPI_cxx_LIBRARY)
1051 list(APPEND MPI_MPICH_LIB_NAMES "cxx")
1052 # For Fortran, MPICH2 provides three different libraries:
1053 # fmpich2.lib which uses uppercase symbols and cdecl,
1054 # fmpich2s.lib which uses uppercase symbols and stdcall (32-bit only),
1055 # fmpich2g.lib which uses lowercase symbols with double underscores and cdecl.
1056 # fmpich2s.lib would be useful for Compaq Visual Fortran, fmpich2g.lib has to be used with GNU g77 and is also
1057 # provided in the form of an .a archive for MinGW and Cygwin. From our perspective, fmpich2.lib is the only one
1058 # we need to try, and if it doesn't work with the given Fortran compiler we'd find out later on during validation
1059 elseif(LANG STREQUAL "Fortran")
1060 find_library(MPI_fmpich2_LIBRARY
1062 HINTS ${MPI_MPICH_PREFIX_PATHS})
1063 find_library(MPI_fmpich2s_LIBRARY
1065 HINTS ${MPI_MPICH_PREFIX_PATHS})
1066 find_library(MPI_fmpich2g_LIBRARY
1068 HINTS ${MPI_MPICH_PREFIX_PATHS})
1069 mark_as_advanced(MPI_fmpich2_LIBRARY MPI_fmpich2s_LIBRARY MPI_fmpich2g_LIBRARY)
1070 list(APPEND MPI_MPICH_LIB_NAMES "fmpich2")
1073 if(NOT MPI_${LANG}_LIB_NAMES)
1074 set(MPI_${LANG}_LIB_NAMES "${MPI_MPICH_LIB_NAMES}" CACHE STRING "MPI ${LANG} libraries to link against" FORCE)
1076 unset(MPI_MPICH_LIB_NAMES)
1078 if(NOT MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS)
1079 # For MPICH2, the include folder would be in ../include relative to the library folder.
1080 get_filename_component(MPI_MPICH_ROOT_DIR "${MPI_mpi_LIBRARY}" DIRECTORY)
1081 get_filename_component(MPI_MPICH_ROOT_DIR "${MPI_MPICH_ROOT_DIR}" DIRECTORY)
1082 if(IS_DIRECTORY "${MPI_MPICH_ROOT_DIR}/include")
1083 set(MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS "${MPI_MPICH_ROOT_DIR}/include" CACHE STRING "MPI ${LANG} additional include directory variables, given in the form MPI_<name>_INCLUDE_DIR." FORCE)
1085 unset(MPI_MPICH_ROOT_DIR)
1087 set(MPI_GUESS_FOUND TRUE)
1089 if(_MPIEXEC_NOT_GIVEN)
1090 unset(MPIEXEC_EXECUTABLE CACHE)
1093 find_program(MPIEXEC_EXECUTABLE
1094 NAMES ${_MPIEXEC_NAMES}
1095 HINTS "$ENV{ProgramW6432}/MPICH2/bin"
1096 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MPICH\\SMPD;binary]"
1097 "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MPICH2;Path]/bin"
1098 DOC "Executable for running MPI programs.")
1100 unset(MPI_MPICH_PREFIX_PATHS)
1103 set(MPI_${LANG}_GUESS_FOUND "${MPI_GUESS_FOUND}" PARENT_SCOPE)
1106 function(_MPI_adjust_compile_definitions LANG)
1107 if(LANG STREQUAL "CXX")
1108 # To disable the C++ bindings, we need to pass some definitions since the mpi.h header has to deal with both C and C++
1109 # bindings in MPI-2.
1110 if(MPI_CXX_SKIP_MPICXX AND NOT MPI_${LANG}_COMPILE_DEFINITIONS MATCHES "SKIP_MPICXX")
1111 # MPICH_SKIP_MPICXX is being used in MPICH and derivatives like MVAPICH or Intel MPI
1112 # OMPI_SKIP_MPICXX is being used in Open MPI
1113 # _MPICC_H is being used for IBM Platform MPI
1114 list(APPEND MPI_${LANG}_COMPILE_DEFINITIONS "MPICH_SKIP_MPICXX" "OMPI_SKIP_MPICXX" "_MPICC_H")
1115 set(MPI_${LANG}_COMPILE_DEFINITIONS "${MPI_${LANG}_COMPILE_DEFINITIONS}" CACHE STRING "MPI ${LANG} compilation definitions" FORCE)
1120 macro(_MPI_assemble_libraries LANG)
1121 set(MPI_${LANG}_LIBRARIES "")
1122 # Only for libraries do we need to check whether the compiler's linking stage is separate.
1123 if(NOT MPI_${LANG}_COMPILER STREQUAL CMAKE_${LANG}_COMPILER OR NOT MPI_${LANG}_WORKS_IMPLICIT)
1124 foreach(mpilib IN LISTS MPI_${LANG}_LIB_NAMES)
1125 list(APPEND MPI_${LANG}_LIBRARIES ${MPI_${mpilib}_LIBRARY})
1130 macro(_MPI_assemble_include_dirs LANG)
1131 set(MPI_${LANG}_INCLUDE_DIRS
1132 ${MPI_${LANG}_COMPILER_INCLUDE_DIRS}
1133 ${MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS}
1135 if(LANG MATCHES "^(C|CXX)$")
1136 if(MPI_${LANG}_HEADER_DIR)
1137 list(APPEND MPI_${LANG}_INCLUDE_DIRS "${MPI_${LANG}_HEADER_DIR}")
1140 if(MPI_${LANG}_F77_HEADER_DIR)
1141 list(APPEND MPI_${LANG}_INCLUDE_DIRS "${MPI_${LANG}_F77_HEADER_DIR}")
1143 if(MPI_${LANG}_MODULE_DIR)
1144 list(APPEND MPI_${LANG}_INCLUDE_DIRS "${MPI_${LANG}_MODULE_DIR}")
1147 if(MPI_${LANG}_INCLUDE_DIRS)
1148 list(REMOVE_DUPLICATES MPI_${LANG}_INCLUDE_DIRS)
1152 macro(_MPI_split_include_dirs LANG)
1153 # Backwards compatibility: Search INCLUDE_PATH if given.
1154 if(MPI_${LANG}_INCLUDE_PATH)
1155 list(APPEND MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS "${MPI_${LANG}_INCLUDE_PATH}")
1158 # We try to find the headers/modules among those paths (and system paths)
1159 # For C/C++, we just need to have a look for mpi.h.
1160 if(LANG MATCHES "^(C|CXX)$")
1161 find_path(MPI_${LANG}_HEADER_DIR "mpi.h"
1163 ${MPI_${LANG}_COMPILER_INCLUDE_DIRS}
1164 ${MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS}
1166 mark_as_advanced(MPI_${LANG}_HEADER_DIR)
1167 if(MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS)
1168 list(REMOVE_ITEM MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS "${MPI_${LANG}_HEADER_DIR}")
1171 # Fortran is more complicated here: An implementation could provide
1172 # any of the Fortran 77/90/2008 APIs for MPI. For example, MSMPI
1173 # only provides Fortran 77 and - if mpi.f90 is built - potentially
1174 # a Fortran 90 module.
1175 elseif(LANG STREQUAL "Fortran")
1176 find_path(MPI_${LANG}_F77_HEADER_DIR "mpif.h"
1178 ${MPI_${LANG}_COMPILER_INCLUDE_DIRS}
1179 ${MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS}
1181 find_path(MPI_${LANG}_MODULE_DIR
1182 NAMES "mpi.mod" "mpi_f08.mod"
1184 ${MPI_${LANG}_COMPILER_INCLUDE_DIRS}
1185 ${MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS}
1187 if(MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS)
1188 list(REMOVE_ITEM MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS
1189 "${MPI_${LANG}_F77_HEADER_DIR}"
1190 "${MPI_${LANG}_MODULE_DIR}"
1193 mark_as_advanced(MPI_${LANG}_F77_HEADER_DIR MPI_${LANG}_MODULE_DIR)
1196 # Remove duplicates and default system directories from the list.
1197 if(MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS)
1198 list(REMOVE_DUPLICATES MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS)
1199 foreach(MPI_IMPLICIT_INC_DIR IN LISTS CMAKE_${LANG}_IMPLICIT_LINK_DIRECTORIES)
1200 list(REMOVE_ITEM MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS ${MPI_IMPLICIT_INC_DIR})
1204 set(MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS ${MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS} CACHE STRING "MPI ${LANG} additional include directories" FORCE)
1207 macro(_MPI_create_imported_target LANG)
1208 if(NOT TARGET MPI::MPI_${LANG})
1209 add_library(MPI::MPI_${LANG} INTERFACE IMPORTED)
1212 # When this is consumed for compiling CUDA, use '-Xcompiler' to wrap '-pthread' and '-fexceptions'.
1213 string(REPLACE "-pthread" "$<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:SHELL:-Xcompiler >-pthread"
1214 _MPI_${LANG}_COMPILE_OPTIONS "${MPI_${LANG}_COMPILE_OPTIONS}")
1215 string(REPLACE "-fexceptions" "$<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:SHELL:-Xcompiler >-fexceptions"
1216 _MPI_${LANG}_COMPILE_OPTIONS "${_MPI_${LANG}_COMPILE_OPTIONS}")
1217 set_property(TARGET MPI::MPI_${LANG} PROPERTY INTERFACE_COMPILE_OPTIONS "${_MPI_${LANG}_COMPILE_OPTIONS}")
1218 unset(_MPI_${LANG}_COMPILE_OPTIONS)
1220 set_property(TARGET MPI::MPI_${LANG} PROPERTY INTERFACE_COMPILE_DEFINITIONS "${MPI_${LANG}_COMPILE_DEFINITIONS}")
1222 if(MPI_${LANG}_LINK_FLAGS)
1223 string(REPLACE "," "$<COMMA>" _MPI_${LANG}_LINK_FLAGS "${MPI_${LANG}_LINK_FLAGS}")
1224 string(PREPEND _MPI_${LANG}_LINK_FLAGS "$<HOST_LINK:SHELL:")
1225 string(APPEND _MPI_${LANG}_LINK_FLAGS ">")
1226 set_property(TARGET MPI::MPI_${LANG} PROPERTY INTERFACE_LINK_OPTIONS "${_MPI_${LANG}_LINK_FLAGS}")
1228 # If the compiler links MPI implicitly, no libraries will be found as they're contained within
1229 # CMAKE_<LANG>_IMPLICIT_LINK_LIBRARIES already.
1230 if(MPI_${LANG}_LIBRARIES)
1231 set_property(TARGET MPI::MPI_${LANG} PROPERTY INTERFACE_LINK_LIBRARIES "${MPI_${LANG}_LIBRARIES}")
1233 # Given the new design of FindMPI, INCLUDE_DIRS will always be located, even under implicit linking.
1234 set_property(TARGET MPI::MPI_${LANG} PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${MPI_${LANG}_INCLUDE_DIRS}")
1237 function(_MPI_try_staged_settings LANG MPI_TEST_FILE_NAME MODE RUN_BINARY SUPPRESS_ERRORS)
1238 set(WORK_DIR "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/FindMPI")
1239 set(SRC_DIR "${CMAKE_ROOT}/Modules/FindMPI")
1240 set(BIN_FILE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/FindMPI/${MPI_TEST_FILE_NAME}_${LANG}.bin")
1241 unset(MPI_TEST_COMPILE_DEFINITIONS)
1242 if(LANG STREQUAL "Fortran")
1243 if(MODE STREQUAL "F90_MODULE")
1244 set(MPI_Fortran_INCLUDE_LINE "use mpi\n implicit none")
1245 elseif(MODE STREQUAL "F08_MODULE")
1246 set(MPI_Fortran_INCLUDE_LINE "use mpi_f08\n implicit none")
1248 set(MPI_Fortran_INCLUDE_LINE "implicit none\n include 'mpif.h'")
1250 file(READ "${SRC_DIR}/${MPI_TEST_FILE_NAME}.f90.in" MPI_TEST_SOURCE_CONTENT)
1251 string(CONFIGURE "${MPI_TEST_SOURCE_CONTENT}" MPI_TEST_SOURCE_CONTENT)
1252 set(MPI_TEST_SOURCE_FILE "${MPI_TEST_FILE_NAME}.f90")
1253 elseif(LANG STREQUAL "CXX")
1254 file(READ "${SRC_DIR}/${MPI_TEST_FILE_NAME}.c" MPI_TEST_SOURCE_CONTENT)
1255 set(MPI_TEST_SOURCE_FILE "${MPI_TEST_FILE_NAME}.cpp")
1256 if(MODE STREQUAL "TEST_MPICXX")
1257 set(MPI_TEST_COMPILE_DEFINITIONS TEST_MPI_MPICXX)
1260 file(READ "${SRC_DIR}/${MPI_TEST_FILE_NAME}.c" MPI_TEST_SOURCE_CONTENT)
1261 set(MPI_TEST_SOURCE_FILE "${MPI_TEST_FILE_NAME}.c")
1264 set(maybe_no_log NO_LOG)
1266 set(maybe_no_log "")
1269 try_run(MPI_RUN_RESULT_${LANG}_${MPI_TEST_FILE_NAME}_${MODE} MPI_RESULT_${LANG}_${MPI_TEST_FILE_NAME}_${MODE}
1270 SOURCE_FROM_VAR "${MPI_TEST_SOURCE_FILE}" MPI_TEST_SOURCE_CONTENT
1272 LOG_DESCRIPTION "The MPI test ${MPI_TEST_FILE_NAME} for ${LANG} in mode ${MODE}"
1273 COMPILE_DEFINITIONS ${MPI_TEST_COMPILE_DEFINITIONS}
1274 LINK_LIBRARIES MPI::MPI_${LANG}
1275 RUN_OUTPUT_VARIABLE MPI_RUN_OUTPUT_${LANG}_${MPI_TEST_FILE_NAME}_${MODE}
1276 COMPILE_OUTPUT_VARIABLE _MPI_TRY_${MPI_TEST_FILE_NAME}_${MODE}_OUTPUT)
1277 set(MPI_RUN_OUTPUT_${LANG}_${MPI_TEST_FILE_NAME}_${MODE} "${MPI_RUN_OUTPUT_${LANG}_${MPI_TEST_FILE_NAME}_${MODE}}" PARENT_SCOPE)
1279 try_compile(MPI_RESULT_${LANG}_${MPI_TEST_FILE_NAME}_${MODE}
1280 SOURCE_FROM_VAR "${MPI_TEST_SOURCE_FILE}" MPI_TEST_SOURCE_CONTENT
1282 LOG_DESCRIPTION "The MPI test ${MPI_TEST_FILE_NAME} for ${LANG} in mode ${MODE}"
1283 COMPILE_DEFINITIONS ${MPI_TEST_COMPILE_DEFINITIONS}
1284 LINK_LIBRARIES MPI::MPI_${LANG}
1285 COPY_FILE "${BIN_FILE}"
1286 OUTPUT_VARIABLE _MPI_TRY_${MPI_TEST_FILE_NAME}_${MODE}_OUTPUT)
1290 macro(_MPI_check_lang_works LANG SUPPRESS_ERRORS)
1291 # For Fortran we may have by the MPI-3 standard an implementation that provides:
1292 # - the mpi_f08 module
1293 # - *both*, the mpi module and 'mpif.h'
1294 # Since older MPI standards (MPI-1) did not define anything but 'mpif.h', we need to check all three individually.
1295 if( NOT MPI_${LANG}_WORKS )
1296 if(LANG STREQUAL "Fortran")
1297 set(MPI_Fortran_INTEGER_LINE "(kind=MPI_INTEGER_KIND)")
1298 _MPI_try_staged_settings(${LANG} test_mpi F77_HEADER FALSE ${SUPPRESS_ERRORS})
1299 _MPI_try_staged_settings(${LANG} test_mpi F90_MODULE FALSE ${SUPPRESS_ERRORS})
1300 _MPI_try_staged_settings(${LANG} test_mpi F08_MODULE FALSE ${SUPPRESS_ERRORS})
1302 set(MPI_${LANG}_WORKS FALSE)
1304 foreach(mpimethod IN ITEMS F77_HEADER F08_MODULE F90_MODULE)
1305 if(MPI_RESULT_${LANG}_test_mpi_${mpimethod})
1306 set(MPI_${LANG}_WORKS TRUE)
1307 set(MPI_${LANG}_HAVE_${mpimethod} TRUE)
1309 set(MPI_${LANG}_HAVE_${mpimethod} FALSE)
1312 # MPI-1 versions had no MPI_INTGER_KIND defined, so we need to try without it.
1313 # However, MPI-1 also did not define the Fortran 90 and 08 modules, so we only try the F77 header.
1314 unset(MPI_Fortran_INTEGER_LINE)
1315 if(NOT MPI_${LANG}_WORKS)
1316 _MPI_try_staged_settings(${LANG} test_mpi F77_HEADER_NOKIND FALSE ${SUPPRESS_ERRORS})
1317 if(MPI_RESULT_${LANG}_test_mpi_F77_HEADER_NOKIND)
1318 set(MPI_${LANG}_WORKS TRUE)
1319 set(MPI_${LANG}_HAVE_F77_HEADER TRUE)
1323 _MPI_try_staged_settings(${LANG} test_mpi normal FALSE ${SUPPRESS_ERRORS})
1324 # If 'test_mpi' built correctly, we've found valid MPI settings. There might not be MPI-2 C++ support, but there can't
1325 # be MPI-2 C++ support without the C bindings being present, so checking for them is sufficient.
1326 set(MPI_${LANG}_WORKS "${MPI_RESULT_${LANG}_test_mpi_normal}")
1331 # Some systems install various MPI implementations in separate folders in some MPI prefix
1332 # This macro enumerates all such subfolders and adds them to the list of hints that will be searched.
1333 macro(MPI_search_mpi_prefix_folder PREFIX_FOLDER)
1334 if(EXISTS "${PREFIX_FOLDER}")
1335 file(GLOB _MPI_folder_children RELATIVE "${PREFIX_FOLDER}" "${PREFIX_FOLDER}/*")
1336 foreach(_MPI_folder_child IN LISTS _MPI_folder_children)
1337 if(IS_DIRECTORY "${PREFIX_FOLDER}/${_MPI_folder_child}")
1338 list(APPEND MPI_HINT_DIRS "${PREFIX_FOLDER}/${_MPI_folder_child}")
1344 set(MPI_HINT_DIRS ${MPI_HOME} $ENV{MPI_HOME} $ENV{I_MPI_ROOT})
1345 if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
1346 # SUSE Linux Enterprise Server stores its MPI implementations under /usr/lib64/mpi/gcc/<name>
1347 # We enumerate the subfolders and append each as a prefix
1348 MPI_search_mpi_prefix_folder("/usr/lib64/mpi/gcc")
1349 elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "FreeBSD")
1350 # FreeBSD ships mpich under the normal system paths - but available openmpi implementations
1351 # will be found in /usr/local/mpi/<name>
1352 MPI_search_mpi_prefix_folder("/usr/local/mpi")
1355 # Most MPI distributions have some form of mpiexec or mpirun which gives us something we can look for.
1356 # The MPI standard does not mandate the existence of either, but instead only makes requirements if a distribution
1357 # ships an mpiexec program (mpirun executables are not regulated by the standard).
1359 # We defer searching for mpiexec binaries belonging to guesses until later. By doing so, mismatches between mpiexec
1360 # and the MPI we found should be reduced.
1361 if(NOT MPIEXEC_EXECUTABLE)
1362 set(_MPIEXEC_NOT_GIVEN TRUE)
1364 set(_MPIEXEC_NOT_GIVEN FALSE)
1367 find_program(MPIEXEC_EXECUTABLE
1368 NAMES ${_MPIEXEC_NAMES}
1369 PATH_SUFFIXES bin sbin
1370 HINTS ${MPI_HINT_DIRS}
1371 DOC "Executable for running MPI programs.")
1373 # call get_filename_component twice to remove mpiexec and the directory it exists in (typically bin).
1374 # This gives us a fairly reliable base directory to search for /bin /lib and /include from.
1375 get_filename_component(_MPI_BASE_DIR "${MPIEXEC_EXECUTABLE}" PATH)
1376 get_filename_component(_MPI_BASE_DIR "${_MPI_BASE_DIR}" PATH)
1378 # According to the MPI standard, section 8.8 -n is a guaranteed, and the only guaranteed way to
1379 # launch an MPI process using mpiexec if such a program exists.
1380 set(MPIEXEC_NUMPROC_FLAG "-n" CACHE STRING "Flag used by MPI to specify the number of processes for mpiexec; the next option will be the number of processes.")
1381 set(MPIEXEC_PREFLAGS "" CACHE STRING "These flags will be directly before the executable that is being run by mpiexec.")
1382 set(MPIEXEC_POSTFLAGS "" CACHE STRING "These flags will be placed after all flags passed to mpiexec.")
1384 # Set the number of processes to the physical processor count
1385 cmake_host_system_information(RESULT _MPIEXEC_NUMPROCS QUERY NUMBER_OF_PHYSICAL_CORES)
1386 set(MPIEXEC_MAX_NUMPROCS "${_MPIEXEC_NUMPROCS}" CACHE STRING "Maximum number of processors available to run MPI applications.")
1387 unset(_MPIEXEC_NUMPROCS)
1388 mark_as_advanced(MPIEXEC_EXECUTABLE MPIEXEC_NUMPROC_FLAG MPIEXEC_PREFLAGS MPIEXEC_POSTFLAGS MPIEXEC_MAX_NUMPROCS)
1390 #=============================================================================
1391 # Backward compatibility input hacks. Propagate the FindMPI hints to C and
1392 # CXX if the respective new versions are not defined. Translate the old
1393 # MPI_LIBRARY and MPI_EXTRA_LIBRARY to respective MPI_${LANG}_LIBRARIES.
1395 # Once we find the new variables, we translate them back into their old
1396 # equivalents below.
1397 if(NOT MPI_IGNORE_LEGACY_VARIABLES)
1398 foreach (LANG IN ITEMS C CXX)
1399 # Old input variables.
1400 set(_MPI_OLD_INPUT_VARS COMPILER COMPILE_FLAGS INCLUDE_PATH LINK_FLAGS)
1402 # Set new vars based on their old equivalents, if the new versions are not already set.
1403 foreach (var ${_MPI_OLD_INPUT_VARS})
1404 if (NOT MPI_${LANG}_${var} AND MPI_${var})
1405 set(MPI_${LANG}_${var} "${MPI_${var}}")
1409 # Chop the old compile flags into options and definitions
1411 unset(MPI_${LANG}_EXTRA_COMPILE_DEFINITIONS)
1412 unset(MPI_${LANG}_EXTRA_COMPILE_OPTIONS)
1413 if(MPI_${LANG}_COMPILE_FLAGS)
1414 separate_arguments(MPI_SEPARATE_FLAGS NATIVE_COMMAND "${MPI_${LANG}_COMPILE_FLAGS}")
1415 foreach(_MPI_FLAG IN LISTS MPI_SEPARATE_FLAGS)
1416 if(_MPI_FLAG MATCHES "^ *-D([^ ]+)")
1417 list(APPEND MPI_${LANG}_EXTRA_COMPILE_DEFINITIONS "${CMAKE_MATCH_1}")
1419 list(APPEND MPI_${LANG}_EXTRA_COMPILE_OPTIONS "${_MPI_FLAG}")
1422 unset(MPI_SEPARATE_FLAGS)
1425 # If a list of libraries was given, we'll split it into new-style cache variables
1426 unset(MPI_${LANG}_EXTRA_LIB_NAMES)
1427 if(NOT MPI_${LANG}_LIB_NAMES)
1428 foreach(_MPI_LIB IN LISTS MPI_${LANG}_LIBRARIES MPI_LIBRARY MPI_EXTRA_LIBRARY)
1430 get_filename_component(_MPI_PLAIN_LIB_NAME "${_MPI_LIB}" NAME_WE)
1431 get_filename_component(_MPI_LIB_NAME "${_MPI_LIB}" NAME)
1432 get_filename_component(_MPI_LIB_DIR "${_MPI_LIB}" DIRECTORY)
1433 list(APPEND MPI_${LANG}_EXTRA_LIB_NAMES "${_MPI_PLAIN_LIB_NAME}")
1434 find_library(MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY
1435 NAMES "${_MPI_LIB_NAME}" "lib${_MPI_LIB_NAME}"
1436 HINTS ${_MPI_LIB_DIR} $ENV{MPI_LIB}
1437 DOC "Location of the ${_MPI_PLAIN_LIB_NAME} library for MPI"
1439 mark_as_advanced(MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY)
1445 #=============================================================================
1448 unset(MPI_VERSION_MAJOR)
1449 unset(MPI_VERSION_MINOR)
1451 unset(_MPI_MIN_VERSION)
1453 # If the user specified a library name we assume they prefer that library over a wrapper. If not, they can disable skipping manually.
1454 if(NOT DEFINED MPI_SKIP_COMPILER_WRAPPER AND MPI_GUESS_LIBRARY_NAME)
1455 set(MPI_SKIP_COMPILER_WRAPPER TRUE)
1458 # This loop finds the compilers and sends them off for interrogation.
1459 foreach(LANG IN ITEMS C CXX Fortran)
1460 if(CMAKE_${LANG}_COMPILER_LOADED)
1461 if(NOT MPI_FIND_COMPONENTS)
1462 set(_MPI_FIND_${LANG} TRUE)
1463 elseif( LANG IN_LIST MPI_FIND_COMPONENTS)
1464 set(_MPI_FIND_${LANG} TRUE)
1465 elseif( LANG STREQUAL "CXX" AND NOT MPI_CXX_SKIP_MPICXX AND MPICXX IN_LIST MPI_FIND_COMPONENTS )
1466 set(_MPI_FIND_${LANG} TRUE)
1468 set(_MPI_FIND_${LANG} FALSE)
1471 set(_MPI_FIND_${LANG} FALSE)
1472 if(LANG IN_LIST MPI_FIND_COMPONENTS)
1473 string(APPEND _MPI_FAIL_REASON "MPI component '${LANG}' was requested, but language ${LANG} is not enabled. ")
1476 if(_MPI_FIND_${LANG})
1477 if( LANG STREQUAL "CXX" AND NOT MPICXX IN_LIST MPI_FIND_COMPONENTS )
1478 option(MPI_CXX_SKIP_MPICXX "If true, the MPI-2 C++ bindings are disabled using definitions." FALSE)
1479 mark_as_advanced(MPI_CXX_SKIP_MPICXX)
1481 _MPI_adjust_compile_definitions(${LANG})
1482 if(NOT (MPI_${LANG}_LIB_NAMES AND (MPI_${LANG}_INCLUDE_PATH OR MPI_${LANG}_INCLUDE_DIRS OR MPI_${LANG}_COMPILER_INCLUDE_DIRS)))
1483 set(MPI_${LANG}_TRIED_IMPLICIT FALSE)
1484 set(MPI_${LANG}_WORKS_IMPLICIT FALSE)
1485 if(NOT MPI_${LANG}_COMPILER AND NOT MPI_ASSUME_NO_BUILTIN_MPI)
1486 # Should the imported targets be empty, we effectively try whether the compiler supports MPI on its own, which is the case on e.g.
1488 _MPI_create_imported_target(${LANG})
1489 _MPI_check_lang_works(${LANG} TRUE)
1491 # If the compiler can build MPI code on its own, it functions as an MPI compiler and we'll set the variable to point to it.
1492 if(MPI_${LANG}_WORKS)
1493 set(MPI_${LANG}_COMPILER "${CMAKE_${LANG}_COMPILER}" CACHE FILEPATH "MPI compiler for ${LANG}" FORCE)
1494 set(MPI_${LANG}_WORKS_IMPLICIT TRUE)
1496 set(MPI_${LANG}_TRIED_IMPLICIT TRUE)
1499 if(NOT MPI_${LANG}_COMPILER STREQUAL CMAKE_${LANG}_COMPILER OR NOT MPI_${LANG}_WORKS)
1500 set(MPI_${LANG}_WRAPPER_FOUND FALSE)
1501 set(MPI_PINNED_COMPILER FALSE)
1503 if(NOT MPI_SKIP_COMPILER_WRAPPER)
1504 if(MPI_${LANG}_COMPILER)
1505 # If the user supplies a compiler *name* instead of an absolute path, assume that we need to find THAT compiler.
1506 if (NOT IS_ABSOLUTE "${MPI_${LANG}_COMPILER}")
1507 # Get rid of our default list of names and just search for the name the user wants.
1508 set(_MPI_${LANG}_COMPILER_NAMES "${MPI_${LANG}_COMPILER}")
1509 unset(MPI_${LANG}_COMPILER CACHE)
1511 # If the user specifies a compiler, we don't want to try to search libraries either.
1512 set(MPI_PINNED_COMPILER TRUE)
1515 # If we have an MPI base directory, we'll try all compiler names in that one first.
1516 # This should prevent mixing different MPI environments
1518 find_program(MPI_${LANG}_COMPILER
1519 NAMES ${_MPI_${LANG}_COMPILER_NAMES}
1520 PATH_SUFFIXES bin sbin
1521 HINTS ${_MPI_BASE_DIR}
1523 DOC "MPI compiler for ${LANG}"
1527 # If the base directory did not help (for example because the mpiexec isn't in the same directory as the compilers),
1528 # we shall try searching in the default paths.
1529 find_program(MPI_${LANG}_COMPILER
1530 NAMES ${_MPI_${LANG}_COMPILER_NAMES}
1531 PATH_SUFFIXES bin sbin
1532 DOC "MPI compiler for ${LANG}"
1535 if(MPI_${LANG}_COMPILER STREQUAL CMAKE_${LANG}_COMPILER)
1536 set(MPI_PINNED_COMPILER TRUE)
1538 # If we haven't made the implicit compiler test yet, perform it now.
1539 if(NOT MPI_${LANG}_TRIED_IMPLICIT)
1540 _MPI_create_imported_target(${LANG})
1541 _MPI_check_lang_works(${LANG} TRUE)
1544 # Should the MPI compiler not work implicitly for MPI, still interrogate it.
1545 # Otherwise, MPI compilers for which CMake has separate linking stages, e.g. Intel MPI on Windows where link.exe is being used
1546 # directly during linkage instead of CMAKE_<LANG>_COMPILER will not work.
1547 if(NOT MPI_${LANG}_WORKS)
1548 set(MPI_${LANG}_WORKS_IMPLICIT FALSE)
1549 _MPI_interrogate_compiler(${LANG})
1551 set(MPI_${LANG}_WORKS_IMPLICIT TRUE)
1553 elseif(MPI_${LANG}_COMPILER)
1554 _MPI_interrogate_compiler(${LANG})
1558 # We are on a Cray, environment identifier: PE_ENV is set (CRAY), and
1559 # have NOT found an mpic++-like compiler wrapper (previous block),
1560 # and we do NOT use the Cray cc/CC compiler wrappers as CC/CXX CMake
1562 # So as a last resort, we now interrogate cc/CC/ftn for MPI flags.
1563 if(DEFINED ENV{PE_ENV} AND NOT "${MPI_${LANG}_COMPILER}")
1564 set(MPI_PINNED_COMPILER TRUE)
1565 find_program(MPI_${LANG}_COMPILER
1566 NAMES ${_MPI_Cray_${LANG}_COMPILER_NAMES}
1567 PATH_SUFFIXES bin sbin
1568 DOC "MPI compiler for ${LANG}"
1571 # If we haven't made the implicit compiler test yet, perform it now.
1572 if(NOT MPI_${LANG}_TRIED_IMPLICIT)
1573 _MPI_create_imported_target(${LANG})
1574 _MPI_check_lang_works(${LANG} TRUE)
1577 set(MPI_${LANG}_WORKS_IMPLICIT TRUE)
1578 _MPI_interrogate_compiler(${LANG})
1581 if(NOT MPI_PINNED_COMPILER AND NOT MPI_${LANG}_WRAPPER_FOUND)
1582 # If MPI_PINNED_COMPILER wasn't given, and the MPI compiler we potentially found didn't work, we withdraw it.
1583 set(MPI_${LANG}_COMPILER "MPI_${LANG}_COMPILER-NOTFOUND" CACHE FILEPATH "MPI compiler for ${LANG}" FORCE)
1585 if(LANG STREQUAL "C")
1586 set(_MPI_PKG "mpi-c")
1587 elseif(LANG STREQUAL "CXX")
1588 set(_MPI_PKG "mpi-cxx")
1589 elseif(LANG STREQUAL "Fortran")
1590 set(_MPI_PKG "mpi-fort")
1594 if(_MPI_PKG AND PKG_CONFIG_FOUND)
1595 pkg_check_modules("MPI_${LANG}_PKG" "${_MPI_PKG}")
1596 if(MPI_${LANG}_PKG_FOUND)
1597 set(MPI_${LANG}_COMPILE_OPTIONS ${MPI_${LANG}_PKG_CFLAGS} CACHE STRING "MPI ${LANG} compilation options" FORCE)
1598 set(MPI_${LANG}_INCLUDE_PATH ${MPI_${LANG}_PKG_INCLUDE_DIRS} CACHE STRING "MPI ${LANG} include directories" FORCE)
1599 set(MPI_${LANG}_LINK_FLAGS ${MPI_${LANG}_PKG_LDFLAGS} CACHE STRING "MPI ${LANG} linker flags" FORCE)
1600 set(MPI_${LANG}_LIB_NAMES ${MPI_${LANG}_PKG_LIBRARIES} CACHE STRING "MPI ${LANG} libraries to link against" FORCE)
1601 foreach(_MPI_LIB IN LISTS MPI_${LANG}_LIB_NAMES)
1603 get_filename_component(_MPI_PLAIN_LIB_NAME "${_MPI_LIB}" NAME_WE)
1604 get_filename_component(_MPI_LIB_NAME "${_MPI_LIB}" NAME)
1605 get_filename_component(_MPI_LIB_DIR "${_MPI_LIB}" DIRECTORY)
1606 find_library(MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY
1607 NAMES "${_MPI_LIB_NAME}" "lib${_MPI_LIB_NAME}"
1608 HINTS ${_MPI_LIB_DIR}
1609 DOC "Location of the ${_MPI_PLAIN_LIB_NAME} library for MPI"
1611 mark_as_advanced(MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY)
1617 if(NOT MPI_SKIP_GUESSING AND NOT MPI_${LANG}_PKG_FOUND)
1618 # For C++, we may use the settings for C. Should a given compiler wrapper for C++ not exist, but one for C does, we copy over the
1619 # settings for C. An MPI distribution that is in this situation would be IBM Platform MPI.
1620 if(LANG STREQUAL "CXX" AND MPI_C_WRAPPER_FOUND)
1621 set(MPI_${LANG}_COMPILE_OPTIONS ${MPI_C_COMPILE_OPTIONS} CACHE STRING "MPI ${LANG} compilation options" )
1622 set(MPI_${LANG}_COMPILE_DEFINITIONS ${MPI_C_COMPILE_DEFINITIONS} CACHE STRING "MPI ${LANG} compilation definitions" )
1623 set(MPI_${LANG}_COMPILER_INCLUDE_DIRS ${MPI_C_INCLUDE_DIRS} CACHE STRING "MPI ${LANG} compiler wrapper include directories")
1624 set(MPI_${LANG}_LINK_FLAGS ${MPI_C_LINK_FLAGS} CACHE STRING "MPI ${LANG} linker flags" )
1625 set(MPI_${LANG}_LIB_NAMES ${MPI_C_LIB_NAMES} CACHE STRING "MPI ${LANG} libraries to link against" )
1627 _MPI_guess_settings(${LANG})
1634 if(NOT MPI_${LANG}_COMPILER STREQUAL CMAKE_${LANG}_COMPILER)
1635 _MPI_split_include_dirs(${LANG})
1636 _MPI_assemble_include_dirs(${LANG})
1638 set(MPI_${LANG}_INCLUDE_DIRS "")
1640 _MPI_assemble_libraries(${LANG})
1642 # We always create imported targets even if they're empty
1643 _MPI_create_imported_target(${LANG})
1645 if(NOT MPI_${LANG}_WORKS)
1646 _MPI_check_lang_works(${LANG} FALSE)
1649 # Next, we'll initialize the MPI variables that have not been previously set.
1650 set(MPI_${LANG}_COMPILE_OPTIONS "" CACHE STRING "MPI ${LANG} compilation flags" )
1651 set(MPI_${LANG}_COMPILE_DEFINITIONS "" CACHE STRING "MPI ${LANG} compilation definitions" )
1652 set(MPI_${LANG}_COMPILER_INCLUDE_DIRS "" CACHE STRING "MPI ${LANG} compiler wrapper include directories")
1653 set(MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS "" CACHE STRING "MPI ${LANG} additional include directories")
1654 set(MPI_${LANG}_LINK_FLAGS "" CACHE STRING "MPI ${LANG} linker flags" )
1655 if(NOT MPI_${LANG}_COMPILER STREQUAL CMAKE_${LANG}_COMPILER)
1656 set(MPI_${LANG}_LIB_NAMES "" CACHE STRING "MPI ${LANG} libraries to link against" )
1659 MPI_${LANG}_COMPILE_OPTIONS
1660 MPI_${LANG}_COMPILE_DEFINITIONS
1661 MPI_${LANG}_LINK_FLAGS
1662 MPI_${LANG}_LIB_NAMES
1663 MPI_${LANG}_COMPILER_INCLUDE_DIRS
1664 MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS
1665 MPI_${LANG}_COMPILER
1668 # If we've found MPI, then we'll perform additional analysis: Determine the MPI version, MPI library version, supported
1669 # MPI APIs (i.e. MPI-2 C++ bindings). For Fortran we also need to find specific parameters if we're under MPI-3.
1670 if(MPI_${LANG}_WORKS)
1671 if(LANG STREQUAL "CXX" AND NOT DEFINED MPI_MPICXX_FOUND)
1672 if(NOT MPI_CXX_SKIP_MPICXX AND NOT MPI_CXX_VALIDATE_SKIP_MPICXX)
1673 _MPI_try_staged_settings(${LANG} test_mpi MPICXX FALSE FALSE)
1674 if(MPI_RESULT_${LANG}_test_mpi_MPICXX)
1675 set(MPI_MPICXX_FOUND TRUE)
1677 set(MPI_MPICXX_FOUND FALSE)
1680 set(MPI_MPICXX_FOUND FALSE)
1684 # At this point, we know the bindings present but not the MPI version or anything else.
1685 if(NOT DEFINED MPI_${LANG}_VERSION)
1686 unset(MPI_${LANG}_VERSION_MAJOR)
1687 unset(MPI_${LANG}_VERSION_MINOR)
1689 set(MPI_BIN_FOLDER ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/FindMPI)
1691 # For Fortran, we'll want to use the most modern MPI binding to test capabilities other than the
1692 # Fortran parameters, since those depend on the method of consumption.
1693 # For C++, we can always use the C bindings, and should do so, since the C++ bindings do not exist in MPI-3
1694 # whereas the C bindings do, and the C++ bindings never offered any feature advantage over their C counterparts.
1695 if(LANG STREQUAL "Fortran")
1696 if(MPI_${LANG}_HAVE_F08_MODULE)
1697 set(MPI_${LANG}_HIGHEST_METHOD F08_MODULE)
1698 elseif(MPI_${LANG}_HAVE_F90_MODULE)
1699 set(MPI_${LANG}_HIGHEST_METHOD F90_MODULE)
1701 set(MPI_${LANG}_HIGHEST_METHOD F77_HEADER)
1704 # Another difference between C and Fortran is that we can't use the preprocessor to determine whether MPI_VERSION
1705 # and MPI_SUBVERSION are provided. These defines did not exist in MPI 1.0 and 1.1 and therefore might not
1706 # exist. For C/C++, test_mpi.c will handle the MPI_VERSION extraction, but for Fortran, we need mpiver.f90.
1707 if(NOT DEFINED MPI_${LANG}_VERSION)
1708 _MPI_try_staged_settings(${LANG} mpiver ${MPI_${LANG}_HIGHEST_METHOD} FALSE FALSE)
1709 if(MPI_RESULT_${LANG}_mpiver_${MPI_${LANG}_HIGHEST_METHOD})
1710 file(STRINGS ${MPI_BIN_FOLDER}/mpiver_${LANG}.bin _MPI_VERSION_STRING LIMIT_COUNT 1 REGEX "INFO:MPI-VER")
1711 if(_MPI_VERSION_STRING MATCHES ".*INFO:MPI-VER\\[([0-9]+)\\.([0-9]+)\\].*")
1712 set(MPI_${LANG}_VERSION_MAJOR "${CMAKE_MATCH_1}")
1713 set(MPI_${LANG}_VERSION_MINOR "${CMAKE_MATCH_2}")
1714 set(MPI_${LANG}_VERSION "${MPI_${LANG}_VERSION_MAJOR}.${MPI_${LANG}_VERSION_MINOR}")
1719 # Finally, we want to find out which capabilities a given interface supports, compare the MPI-3 standard.
1720 # This is determined by interface specific parameters MPI_SUBARRAYS_SUPPORTED and MPI_ASYNC_PROTECTS_NONBLOCKING
1721 # and might vary between the different methods of consumption.
1722 if(MPI_DETERMINE_Fortran_CAPABILITIES AND NOT MPI_Fortran_CAPABILITIES_DETERMINED)
1723 foreach(mpimethod IN ITEMS F08_MODULE F90_MODULE F77_HEADER)
1724 if(MPI_${LANG}_HAVE_${mpimethod})
1725 set(MPI_${LANG}_${mpimethod}_SUBARRAYS FALSE)
1726 set(MPI_${LANG}_${mpimethod}_ASYNCPROT FALSE)
1727 _MPI_try_staged_settings(${LANG} fortranparam_mpi ${mpimethod} TRUE FALSE)
1728 if(MPI_RESULT_${LANG}_fortranparam_mpi_${mpimethod} AND
1729 NOT "${MPI_RUN_RESULT_${LANG}_fortranparam_mpi_${mpimethod}}" STREQUAL "FAILED_TO_RUN")
1730 if(MPI_RUN_OUTPUT_${LANG}_fortranparam_mpi_${mpimethod} MATCHES
1731 ".*INFO:SUBARRAYS\\[ *([TF]) *\\]-ASYNCPROT\\[ *([TF]) *\\].*")
1732 if(CMAKE_MATCH_1 STREQUAL "T")
1733 set(MPI_${LANG}_${mpimethod}_SUBARRAYS TRUE)
1735 if(CMAKE_MATCH_2 STREQUAL "T")
1736 set(MPI_${LANG}_${mpimethod}_ASYNCPROT TRUE)
1742 set(MPI_Fortran_CAPABILITIES_DETERMINED TRUE)
1745 set(MPI_${LANG}_HIGHEST_METHOD normal)
1747 # By the MPI-2 standard, MPI_VERSION and MPI_SUBVERSION are valid for both C and C++ bindings.
1748 if(NOT DEFINED MPI_${LANG}_VERSION)
1749 file(STRINGS ${MPI_BIN_FOLDER}/test_mpi_${LANG}.bin _MPI_VERSION_STRING LIMIT_COUNT 1 REGEX "INFO:MPI-VER")
1750 if(_MPI_VERSION_STRING MATCHES ".*INFO:MPI-VER\\[([0-9]+)\\.([0-9]+)\\].*")
1751 set(MPI_${LANG}_VERSION_MAJOR "${CMAKE_MATCH_1}")
1752 set(MPI_${LANG}_VERSION_MINOR "${CMAKE_MATCH_2}")
1753 set(MPI_${LANG}_VERSION "${MPI_${LANG}_VERSION_MAJOR}.${MPI_${LANG}_VERSION_MINOR}")
1758 unset(MPI_BIN_FOLDER)
1760 # At this point, we have dealt with determining the MPI version and parameters for each Fortran method available.
1761 # The one remaining issue is to determine which MPI library is installed.
1762 # Determining the version and vendor of the MPI library is only possible via MPI_Get_library_version() at runtime,
1763 # and therefore we cannot do this while cross-compiling (a user may still define MPI_<lang>_LIBRARY_VERSION_STRING
1764 # themselves and we'll attempt splitting it, which is equivalent to provide the try_run output).
1765 # It's also worth noting that the installed version string can depend on the language, or on the system the binary
1766 # runs on if MPI is not statically linked.
1767 if(MPI_DETERMINE_LIBRARY_VERSION AND NOT MPI_${LANG}_LIBRARY_VERSION_STRING)
1768 _MPI_try_staged_settings(${LANG} libver_mpi ${MPI_${LANG}_HIGHEST_METHOD} TRUE FALSE)
1769 if(MPI_RESULT_${LANG}_libver_mpi_${MPI_${LANG}_HIGHEST_METHOD} AND
1770 MPI_RUN_RESULT_${LANG}_libver_mpi_${MPI_${LANG}_HIGHEST_METHOD} EQUAL "0")
1771 string(STRIP "${MPI_RUN_OUTPUT_${LANG}_libver_mpi_${MPI_${LANG}_HIGHEST_METHOD}}"
1772 MPI_${LANG}_LIBRARY_VERSION_STRING)
1774 set(MPI_${LANG}_LIBRARY_VERSION_STRING "NOTFOUND")
1779 set(MPI_${LANG}_FIND_QUIETLY ${MPI_FIND_QUIETLY})
1780 set(MPI_${LANG}_FIND_VERSION ${MPI_FIND_VERSION})
1781 set(MPI_${LANG}_FIND_VERSION_EXACT ${MPI_FIND_VERSION_EXACT})
1783 unset(MPI_${LANG}_REQUIRED_VARS)
1784 if (NOT MPI_${LANG}_COMPILER STREQUAL CMAKE_${LANG}_COMPILER)
1785 foreach(mpilibname IN LISTS MPI_${LANG}_LIB_NAMES)
1786 list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${mpilibname}_LIBRARY")
1788 list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${LANG}_LIB_NAMES")
1789 if(LANG STREQUAL "Fortran")
1790 # For Fortran we only need one of the module or header directories to have *some* support for MPI.
1791 if(NOT MPI_${LANG}_MODULE_DIR)
1792 list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${LANG}_F77_HEADER_DIR")
1794 if(NOT MPI_${LANG}_F77_HEADER_DIR)
1795 list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${LANG}_MODULE_DIR")
1798 list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${LANG}_HEADER_DIR")
1800 if(MPI_${LANG}_ADDITIONAL_INCLUDE_VARS)
1801 foreach(mpiincvar IN LISTS MPI_${LANG}_ADDITIONAL_INCLUDE_VARS)
1802 list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${mpiincvar}_INCLUDE_DIR")
1805 # Append the works variable now. If the settings did not work, this will show up properly.
1806 list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${LANG}_WORKS")
1808 # If the compiler worked implicitly, use its path as output.
1809 # Should the compiler variable be set, we also require it to work.
1810 list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${LANG}_COMPILER")
1811 if(MPI_${LANG}_COMPILER)
1812 list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${LANG}_WORKS")
1815 find_package_handle_standard_args(MPI_${LANG} NAME_MISMATCHED
1816 REQUIRED_VARS ${MPI_${LANG}_REQUIRED_VARS}
1817 VERSION_VAR MPI_${LANG}_VERSION)
1819 if(DEFINED MPI_${LANG}_VERSION)
1820 if(NOT _MPI_MIN_VERSION OR _MPI_MIN_VERSION VERSION_GREATER MPI_${LANG}_VERSION)
1821 set(_MPI_MIN_VERSION MPI_${LANG}_VERSION)
1827 unset(_MPI_REQ_VARS)
1828 foreach(LANG IN ITEMS C CXX Fortran)
1829 if((NOT MPI_FIND_COMPONENTS AND CMAKE_${LANG}_COMPILER_LOADED) OR LANG IN_LIST MPI_FIND_COMPONENTS)
1830 list(APPEND _MPI_REQ_VARS "MPI_${LANG}_FOUND")
1834 if(MPICXX IN_LIST MPI_FIND_COMPONENTS)
1835 list(APPEND _MPI_REQ_VARS "MPI_MPICXX_FOUND")
1838 find_package_handle_standard_args(MPI
1839 REQUIRED_VARS ${_MPI_REQ_VARS}
1840 VERSION_VAR ${_MPI_MIN_VERSION}
1841 REASON_FAILURE_MESSAGE "${_MPI_FAIL_REASON}"
1844 #=============================================================================
1845 # More backward compatibility stuff
1847 # For compatibility reasons, we also define MPIEXEC
1848 set(MPIEXEC "${MPIEXEC_EXECUTABLE}")
1850 # Copy over MPI_<LANG>_INCLUDE_PATH from the assembled INCLUDE_DIRS.
1851 foreach(LANG IN ITEMS C CXX Fortran)
1852 if(MPI_${LANG}_FOUND)
1853 set(MPI_${LANG}_INCLUDE_PATH "${MPI_${LANG}_INCLUDE_DIRS}")
1854 unset(MPI_${LANG}_COMPILE_FLAGS)
1855 if(MPI_${LANG}_COMPILE_OPTIONS)
1856 list(JOIN MPI_${LANG}_COMPILE_OPTIONS " " MPI_${LANG}_COMPILE_FLAGS)
1858 if(MPI_${LANG}_COMPILE_DEFINITIONS)
1859 foreach(_MPI_DEF IN LISTS MPI_${LANG}_COMPILE_DEFINITIONS)
1860 string(APPEND MPI_${LANG}_COMPILE_FLAGS " -D${_MPI_DEF}")
1866 # Bare MPI sans ${LANG} vars are set to CXX then C, depending on what was found.
1867 # This mimics the behavior of the old language-oblivious FindMPI.
1868 set(_MPI_OLD_VARS COMPILER INCLUDE_PATH COMPILE_FLAGS LINK_FLAGS LIBRARIES)
1870 foreach (var ${_MPI_OLD_VARS})
1871 set(MPI_${var} ${MPI_CXX_${var}})
1873 elseif (MPI_C_FOUND)
1874 foreach (var ${_MPI_OLD_VARS})
1875 set(MPI_${var} ${MPI_C_${var}})
1879 # Chop MPI_LIBRARIES into the old-style MPI_LIBRARY and MPI_EXTRA_LIBRARY, and set them in cache.
1881 list(GET MPI_LIBRARIES 0 MPI_LIBRARY_WORK)
1882 set(MPI_LIBRARY "${MPI_LIBRARY_WORK}")
1883 unset(MPI_LIBRARY_WORK)
1885 set(MPI_LIBRARY "MPI_LIBRARY-NOTFOUND")
1888 list(LENGTH MPI_LIBRARIES MPI_NUMLIBS)
1889 if (MPI_NUMLIBS GREATER "1")
1890 set(MPI_EXTRA_LIBRARY_WORK "${MPI_LIBRARIES}")
1891 list(REMOVE_AT MPI_EXTRA_LIBRARY_WORK 0)
1892 set(MPI_EXTRA_LIBRARY "${MPI_EXTRA_LIBRARY_WORK}")
1893 unset(MPI_EXTRA_LIBRARY_WORK)
1895 set(MPI_EXTRA_LIBRARY "MPI_EXTRA_LIBRARY-NOTFOUND")
1897 set(MPI_IGNORE_LEGACY_VARIABLES TRUE)
1898 #=============================================================================
1900 # unset these vars to cleanup namespace
1901 unset(_MPI_OLD_VARS)
1902 unset(_MPI_PREFIX_PATH)
1903 unset(_MPI_BASE_DIR)
1904 foreach (lang C CXX Fortran)
1905 unset(_MPI_${LANG}_COMPILER_NAMES)