FindMPI.cmake
Go to the documentation of this file.
1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3 
4 #[=======================================================================[.rst:
5 FindMPI
6 -------
7 
8 Find a Message Passing Interface (MPI) implementation.
9 
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.
14 
15 Variables for using MPI
16 ^^^^^^^^^^^^^^^^^^^^^^^
17 
18 The module exposes the components ``C``, ``CXX``, ``MPICXX`` and ``Fortran``.
19 Each of these controls the various MPI languages to search for.
20 The difference between ``CXX`` and ``MPICXX`` is that ``CXX`` refers to the
21 MPI C API being usable from C++, whereas ``MPICXX`` refers to the MPI-2 C++ API
22 that was removed again in MPI-3.
23 
24 Depending on the enabled components the following variables will be set:
25 
26 ``MPI_FOUND``
27  Variable indicating that MPI settings for all requested languages have been found.
28  If no components are specified, this is true if MPI settings for all enabled languages
29  were detected. Note that the ``MPICXX`` component does not affect this variable.
30 ``MPI_VERSION``
31  Minimal version of MPI detected among the requested languages, or all enabled languages
32  if no components were specified.
33 
34 This module will set the following variables per language in your
35 project, where ``<lang>`` is one of C, CXX, or Fortran:
36 
37 ``MPI_<lang>_FOUND``
38  Variable indicating the MPI settings for ``<lang>`` were found and that
39  simple MPI test programs compile with the provided settings.
40 ``MPI_<lang>_COMPILER``
41  MPI compiler for ``<lang>`` if such a program exists.
42 ``MPI_<lang>_COMPILE_OPTIONS``
43  Compilation options for MPI programs in ``<lang>``, given as a :ref:`;-list <CMake Language Lists>`.
44 ``MPI_<lang>_COMPILE_DEFINITIONS``
45  Compilation definitions for MPI programs in ``<lang>``, given as a :ref:`;-list <CMake Language Lists>`.
46 ``MPI_<lang>_INCLUDE_DIRS``
47  Include path(s) for MPI header.
48 ``MPI_<lang>_LINK_FLAGS``
49  Linker flags for MPI programs.
50 ``MPI_<lang>_LIBRARIES``
51  All libraries to link MPI programs against.
52 
53 Additionally, the following :prop_tgt:`IMPORTED` targets are defined:
54 
55 ``MPI::MPI_<lang>``
56  Target for using MPI from ``<lang>``.
57 
58 The following variables indicating which bindings are present will be defined:
59 
60 ``MPI_MPICXX_FOUND``
61  Variable indicating whether the MPI-2 C++ bindings are present (introduced in MPI-2, removed with MPI-3).
62 ``MPI_Fortran_HAVE_F77_HEADER``
63  True if the Fortran 77 header ``mpif.h`` is available.
64 ``MPI_Fortran_HAVE_F90_MODULE``
65  True if the Fortran 90 module ``mpi`` can be used for accessing MPI (MPI-2 and higher only).
66 ``MPI_Fortran_HAVE_F08_MODULE``
67  True if the Fortran 2008 ``mpi_f08`` is available to MPI programs (MPI-3 and higher only).
68 
69 If possible, the MPI version will be determined by this module. The facilities to detect the MPI version
70 were introduced with MPI-1.2, and therefore cannot be found for older MPI versions.
71 
72 ``MPI_<lang>_VERSION_MAJOR``
73  Major version of MPI implemented for ``<lang>`` by the MPI distribution.
74 ``MPI_<lang>_VERSION_MINOR``
75  Minor version of MPI implemented for ``<lang>`` by the MPI distribution.
76 ``MPI_<lang>_VERSION``
77  MPI version implemented for ``<lang>`` by the MPI distribution.
78 
79 Note that there's no variable for the C bindings being accessible through ``mpi.h``, since the MPI standards
80 always have required this binding to work in both C and C++ code.
81 
82 For running MPI programs, the module sets the following variables
83 
84 ``MPIEXEC_EXECUTABLE``
85  Executable for running MPI programs, if such exists.
86 ``MPIEXEC_NUMPROC_FLAG``
87  Flag to pass to ``mpiexec`` before giving it the number of processors to run on.
88 ``MPIEXEC_MAX_NUMPROCS``
89  Number of MPI processors to utilize. Defaults to the number
90  of processors detected on the host system.
91 ``MPIEXEC_PREFLAGS``
92  Flags to pass to ``mpiexec`` directly before the executable to run.
93 ``MPIEXEC_POSTFLAGS``
94  Flags to pass to ``mpiexec`` after other flags.
95 
96 Variables for locating MPI
97 ^^^^^^^^^^^^^^^^^^^^^^^^^^
98 
99 This module performs a four step search for an MPI implementation:
100 
101 1. Search for ``MPIEXEC_EXECUTABLE`` and, if found, use its base directory.
102 2. Check if the compiler has MPI support built-in. This is the case if the user passed a
103  compiler wrapper as ``CMAKE_<LANG>_COMPILER`` or if they're on a Cray system.
104 3. Attempt to find an MPI compiler wrapper and determine the compiler information from it.
105 4. Try to find an MPI implementation that does not ship such a wrapper by guessing settings.
106  Currently, only Microsoft MPI and MPICH2 on Windows are supported.
107 
108 For controlling the ``MPIEXEC_EXECUTABLE`` step, the following variables may be set:
109 
110 ``MPIEXEC_EXECUTABLE``
111  Manually specify the location of ``mpiexec``.
112 ``MPI_HOME``
113  Specify the base directory of the MPI installation.
114 ``ENV{MPI_HOME}``
115  Environment variable to specify the base directory of the MPI installation.
116 ``ENV{I_MPI_ROOT}``
117  Environment variable to specify the base directory of the MPI installation.
118 
119 For controlling the compiler wrapper step, the following variables may be set:
120 
121 ``MPI_<lang>_COMPILER``
122  Search for the specified compiler wrapper and use it.
123 ``MPI_<lang>_COMPILER_FLAGS``
124  Flags to pass to the MPI compiler wrapper during interrogation. Some compiler wrappers
125  support linking debug or tracing libraries if a specific flag is passed and this variable
126  may be used to obtain them.
127 ``MPI_COMPILER_FLAGS``
128  Used to initialize ``MPI_<lang>_COMPILER_FLAGS`` if no language specific flag has been given.
129  Empty by default.
130 ``MPI_EXECUTABLE_SUFFIX``
131  A suffix which is appended to all names that are being looked for. For instance you may set this
132  to ``.mpich`` or ``.openmpi`` to prefer the one or the other on Debian and its derivatives.
133 
134 In order to control the guessing step, the following variable may be set:
135 
136 ``MPI_GUESS_LIBRARY_NAME``
137  Valid values are ``MSMPI`` and ``MPICH2``. If set, only the given library will be searched for.
138  By default, ``MSMPI`` will be preferred over ``MPICH2`` if both are available.
139  This also sets ``MPI_SKIP_COMPILER_WRAPPER`` to ``true``, which may be overridden.
140 
141 Each of the search steps may be skipped with the following control variables:
142 
143 ``MPI_ASSUME_NO_BUILTIN_MPI``
144  If true, the module assumes that the compiler itself does not provide an MPI implementation and
145  skips to step 2.
146 ``MPI_SKIP_COMPILER_WRAPPER``
147  If true, no compiler wrapper will be searched for.
148 ``MPI_SKIP_GUESSING``
149  If true, the guessing step will be skipped.
150 
151 Additionally, the following control variable is available to change search behavior:
152 
153 ``MPI_CXX_SKIP_MPICXX``
154  Add some definitions that will disable the MPI-2 C++ bindings.
155  Currently supported are MPICH, Open MPI, Platform MPI and derivatives thereof,
156  for example MVAPICH or Intel MPI.
157 
158 If the find procedure fails for a variable ``MPI_<lang>_WORKS``, then the settings detected by or passed to
159 the module did not work and even a simple MPI test program failed to compile.
160 
161 If all of these parameters were not sufficient to find the right MPI implementation, a user may
162 disable the entire autodetection process by specifying both a list of libraries in ``MPI_<lang>_LIBRARIES``
163 and a list of include directories in ``MPI_<lang>_ADDITIONAL_INCLUDE_DIRS``.
164 Any other variable may be set in addition to these two. The module will then validate the MPI settings and store the
165 settings in the cache.
166 
167 Cache variables for MPI
168 ^^^^^^^^^^^^^^^^^^^^^^^
169 
170 The variable ``MPI_<lang>_INCLUDE_DIRS`` will be assembled from the following variables.
171 For C and CXX:
172 
173 ``MPI_<lang>_HEADER_DIR``
174  Location of the ``mpi.h`` header on disk.
175 
176 For Fortran:
177 
178 ``MPI_Fortran_F77_HEADER_DIR``
179  Location of the Fortran 77 header ``mpif.h``, if it exists.
180 ``MPI_Fortran_MODULE_DIR``
181  Location of the ``mpi`` or ``mpi_f08`` modules, if available.
182 
183 For all languages the following variables are additionally considered:
184 
185 ``MPI_<lang>_ADDITIONAL_INCLUDE_DIRS``
186  A :ref:`;-list <CMake Language Lists>` of paths needed in addition to the normal include directories.
187 ``MPI_<include_name>_INCLUDE_DIR``
188  Path variables for include folders referred to by ``<include_name>``.
189 ``MPI_<lang>_ADDITIONAL_INCLUDE_VARS``
190  A :ref:`;-list <CMake Language Lists>` of ``<include_name>`` that will be added to the include locations of ``<lang>``.
191 
192 The variable ``MPI_<lang>_LIBRARIES`` will be assembled from the following variables:
193 
194 ``MPI_<lib_name>_LIBRARY``
195  The location of a library called ``<lib_name>`` for use with MPI.
196 ``MPI_<lang>_LIB_NAMES``
197  A :ref:`;-list <CMake Language Lists>` of ``<lib_name>`` that will be added to the include locations of ``<lang>``.
198 
199 Usage of mpiexec
200 ^^^^^^^^^^^^^^^^
201 
202 When using ``MPIEXEC_EXECUTABLE`` to execute MPI applications, you should typically
203 use all of the ``MPIEXEC_EXECUTABLE`` flags as follows:
204 
205 ::
206 
207  ${MPIEXEC_EXECUTABLE} ${MPIEXEC_NUMPROC_FLAG} ${MPIEXEC_MAX_NUMPROCS}
208  ${MPIEXEC_PREFLAGS} EXECUTABLE ${MPIEXEC_POSTFLAGS} ARGS
209 
210 where ``EXECUTABLE`` is the MPI program, and ``ARGS`` are the arguments to
211 pass to the MPI program.
212 
213 Advanced variables for using MPI
214 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
215 
216 The module can perform some advanced feature detections upon explicit request.
217 
218 **Important notice:** The following checks cannot be performed without *executing* an MPI test program.
219 Consider the special considerations for the behavior of :command:`try_run` during cross compilation.
220 Moreover, running an MPI program can cause additional issues, like a firewall notification on some systems.
221 You should only enable these detections if you absolutely need the information.
222 
223 If the following variables are set to true, the respective search will be performed:
224 
225 ``MPI_DETERMINE_Fortran_CAPABILITIES``
226  Determine for all available Fortran bindings what the values of ``MPI_SUBARRAYS_SUPPORTED`` and
227  ``MPI_ASYNC_PROTECTS_NONBLOCKING`` are and make their values available as ``MPI_Fortran_<binding>_SUBARRAYS``
228  and ``MPI_Fortran_<binding>_ASYNCPROT``, where ``<binding>`` is one of ``F77_HEADER``, ``F90_MODULE`` and
229  ``F08_MODULE``.
230 ``MPI_DETERMINE_LIBRARY_VERSION``
231  For each language, find the output of ``MPI_Get_library_version`` and make it available as ``MPI_<lang>_LIBRARY_VERSION_STRING``.
232  This information is usually tied to the runtime component of an MPI implementation and might differ depending on ``<lang>``.
233  Note that the return value is entirely implementation defined. This information might be used to identify
234  the MPI vendor and for example pick the correct one of multiple third party binaries that matches the MPI vendor.
235 
236 Backward Compatibility
237 ^^^^^^^^^^^^^^^^^^^^^^
238 
239 For backward compatibility with older versions of FindMPI, these
240 variables are set, but deprecated:
241 
242 ::
243 
244  MPI_COMPILER MPI_LIBRARY MPI_EXTRA_LIBRARY
245  MPI_COMPILE_FLAGS MPI_INCLUDE_PATH MPI_LINK_FLAGS
246  MPI_LIBRARIES
247 
248 In new projects, please use the ``MPI_<lang>_XXX`` equivalents.
249 Additionally, the following variables are deprecated:
250 
251 ``MPI_<lang>_COMPILE_FLAGS``
252  Use ``MPI_<lang>_COMPILE_OPTIONS`` and ``MPI_<lang>_COMPILE_DEFINITIONS`` instead.
253 ``MPI_<lang>_INCLUDE_PATH``
254  For consumption use ``MPI_<lang>_INCLUDE_DIRS`` and for specifying folders use ``MPI_<lang>_ADDITIONAL_INCLUDE_DIRS`` instead.
255 ``MPIEXEC``
256  Use ``MPIEXEC_EXECUTABLE`` instead.
257 #]=======================================================================]
258 
259 cmake_policy(PUSH)
260 cmake_policy(SET CMP0057 NEW) # if IN_LIST
261 
262 include(FindPackageHandleStandardArgs)
263 
264 # Generic compiler names
265 set(_MPI_C_GENERIC_COMPILER_NAMES mpicc mpcc mpicc_r mpcc_r)
266 set(_MPI_CXX_GENERIC_COMPILER_NAMES mpicxx mpiCC mpcxx mpCC mpic++ mpc++
267  mpicxx_r mpiCC_r mpcxx_r mpCC_r mpic++_r mpc++_r)
268 set(_MPI_Fortran_GENERIC_COMPILER_NAMES mpif95 mpif95_r mpf95 mpf95_r
269  mpif90 mpif90_r mpf90 mpf90_r
270  mpif77 mpif77_r mpf77 mpf77_r
271  mpifc)
272 
273 # GNU compiler names
274 set(_MPI_GNU_C_COMPILER_NAMES mpigcc mpgcc mpigcc_r mpgcc_r)
275 set(_MPI_GNU_CXX_COMPILER_NAMES mpig++ mpg++ mpig++_r mpg++_r mpigxx)
276 set(_MPI_GNU_Fortran_COMPILER_NAMES mpigfortran mpgfortran mpigfortran_r mpgfortran_r
277  mpig77 mpig77_r mpg77 mpg77_r)
278 
279 # Intel MPI compiler names on Windows
280 if(WIN32)
281  list(APPEND _MPI_C_GENERIC_COMPILER_NAMES mpicc.bat)
282  list(APPEND _MPI_CXX_GENERIC_COMPILER_NAMES mpicxx.bat)
283  list(APPEND _MPI_Fortran_GENERIC_COMPILER_NAMES mpifc.bat)
284 
285  # Intel MPI compiler names
286  set(_MPI_Intel_C_COMPILER_NAMES mpiicc.bat)
287  set(_MPI_Intel_CXX_COMPILER_NAMES mpiicpc.bat)
288  set(_MPI_Intel_Fortran_COMPILER_NAMES mpiifort.bat mpif77.bat mpif90.bat)
289 
290  # Intel MPI compiler names for MSMPI
291  set(_MPI_MSVC_C_COMPILER_NAMES mpicl.bat)
292  set(_MPI_MSVC_CXX_COMPILER_NAMES mpicl.bat)
293 else()
294  # Intel compiler names
295  set(_MPI_Intel_C_COMPILER_NAMES mpiicc)
296  set(_MPI_Intel_CXX_COMPILER_NAMES mpiicpc mpiicxx mpiic++)
297  set(_MPI_Intel_Fortran_COMPILER_NAMES mpiifort mpiif95 mpiif90 mpiif77)
298 endif()
299 
300 # PGI compiler names
301 set(_MPI_PGI_C_COMPILER_NAMES mpipgcc mppgcc)
302 set(_MPI_PGI_CXX_COMPILER_NAMES mpipgCC mppgCC)
303 set(_MPI_PGI_Fortran_COMPILER_NAMES mpipgf95 mpipgf90 mppgf95 mppgf90 mpipgf77 mppgf77)
304 
305 # XLC MPI Compiler names
306 set(_MPI_XL_C_COMPILER_NAMES mpxlc mpxlc_r mpixlc mpixlc_r)
307 set(_MPI_XL_CXX_COMPILER_NAMES mpixlcxx mpixlC mpixlc++ mpxlcxx mpxlc++ mpixlc++ mpxlCC
308  mpixlcxx_r mpixlC_r mpixlc++_r mpxlcxx_r mpxlc++_r mpixlc++_r mpxlCC_r)
309 set(_MPI_XL_Fortran_COMPILER_NAMES mpixlf95 mpixlf95_r mpxlf95 mpxlf95_r
310  mpixlf90 mpixlf90_r mpxlf90 mpxlf90_r
311  mpixlf77 mpixlf77_r mpxlf77 mpxlf77_r
312  mpixlf mpixlf_r mpxlf mpxlf_r)
313 
314 # Prepend vendor-specific compiler wrappers to the list. If we don't know the compiler,
315 # attempt all of them.
316 # By attempting vendor-specific compiler names first, we should avoid situations where the compiler wrapper
317 # stems from a proprietary MPI and won't know which compiler it's being used for. For instance, Intel MPI
318 # controls its settings via the I_MPI_CC environment variables if the generic name is being used.
319 # If we know which compiler we're working with, we can use the most specialized wrapper there is in order to
320 # pick up the right settings for it.
321 foreach (LANG IN ITEMS C CXX Fortran)
322  set(_MPI_${LANG}_COMPILER_NAMES "")
323  foreach (id IN ITEMS GNU Intel MSVC PGI XL)
324  if (NOT CMAKE_${LANG}_COMPILER_ID OR CMAKE_${LANG}_COMPILER_ID STREQUAL id)
325  foreach(_COMPILER_NAME IN LISTS _MPI_${id}_${LANG}_COMPILER_NAMES)
326  list(APPEND _MPI_${LANG}_COMPILER_NAMES ${_COMPILER_NAME}${MPI_EXECUTABLE_SUFFIX})
327  endforeach()
328  endif()
329  unset(_MPI_${id}_${LANG}_COMPILER_NAMES)
330  endforeach()
331  foreach(_COMPILER_NAME IN LISTS _MPI_${LANG}_GENERIC_COMPILER_NAMES)
332  list(APPEND _MPI_${LANG}_COMPILER_NAMES ${_COMPILER_NAME}${MPI_EXECUTABLE_SUFFIX})
333  endforeach()
334  unset(_MPI_${LANG}_GENERIC_COMPILER_NAMES)
335 endforeach()
336 
337 # Names to try for mpiexec
338 # Only mpiexec commands are guaranteed to behave as described in the standard,
339 # mpirun commands are not covered by the standard in any way whatsoever.
340 # lamexec is the executable for LAM/MPI, srun is for SLURM or Open MPI with SLURM support.
341 # srun -n X <executable> is however a valid command, so it behaves 'like' mpiexec.
342 set(_MPIEXEC_NAMES_BASE mpiexec mpiexec.hydra mpiexec.mpd mpirun lamexec srun)
343 
344 unset(_MPIEXEC_NAMES)
345 foreach(_MPIEXEC_NAME IN LISTS _MPIEXEC_NAMES_BASE)
346  list(APPEND _MPIEXEC_NAMES "${_MPIEXEC_NAME}${MPI_EXECUTABLE_SUFFIX}")
347 endforeach()
348 unset(_MPIEXEC_NAMES_BASE)
349 
350 function (_MPI_check_compiler LANG QUERY_FLAG OUTPUT_VARIABLE RESULT_VARIABLE)
351  if(DEFINED MPI_${LANG}_COMPILER_FLAGS)
352  separate_arguments(_MPI_COMPILER_WRAPPER_OPTIONS NATIVE_COMMAND "${MPI_${LANG}_COMPILER_FLAGS}")
353  else()
354  separate_arguments(_MPI_COMPILER_WRAPPER_OPTIONS NATIVE_COMMAND "${MPI_COMPILER_FLAGS}")
355  endif()
356  execute_process(
357  COMMAND ${MPI_${LANG}_COMPILER} ${_MPI_COMPILER_WRAPPER_OPTIONS} ${QUERY_FLAG}
358  OUTPUT_VARIABLE WRAPPER_OUTPUT OUTPUT_STRIP_TRAILING_WHITESPACE
359  ERROR_VARIABLE WRAPPER_OUTPUT ERROR_STRIP_TRAILING_WHITESPACE
360  RESULT_VARIABLE WRAPPER_RETURN)
361  # Some compiler wrappers will yield spurious zero return values, for example
362  # Intel MPI tolerates unknown arguments and if the MPI wrappers loads a shared
363  # library that has invalid or missing version information there would be warning
364  # messages emitted by ld.so in the compiler output. In either case, we'll treat
365  # the output as invalid.
366  if("${WRAPPER_OUTPUT}" MATCHES "undefined reference|unrecognized|need to set|no version information available|command not found")
367  set(WRAPPER_RETURN 255)
368  endif()
369  # Ensure that no error output might be passed upwards.
370  if(NOT WRAPPER_RETURN EQUAL 0)
371  unset(WRAPPER_OUTPUT)
372  else()
373  # Strip leading whitespace
374  string(REGEX REPLACE "^ +" "" WRAPPER_OUTPUT "${WRAPPER_OUTPUT}")
375  endif()
376  set(${OUTPUT_VARIABLE} "${WRAPPER_OUTPUT}" PARENT_SCOPE)
377  set(${RESULT_VARIABLE} "${WRAPPER_RETURN}" PARENT_SCOPE)
378 endfunction()
379 
380 macro(_MPI_env_set_ifnot VAR VALUE)
381  if(NOT DEFINED ENV{${VAR}})
382  set(_MPI_${VAR}_WAS_SET FALSE)
383  set(ENV{${VAR}} ${${VALUE}})
384  else()
385  set(_MPI_${VAR}_WAS_SET TRUE)
386  endif()
387 endmacro()
388 
389 macro(_MPI_env_unset_ifnot VAR)
390  if(NOT _MPI_${VAR}_WAS_SET)
391  unset(ENV{${VAR}})
392  endif()
393 endmacro()
394 
395 function (_MPI_interrogate_compiler LANG)
396  unset(MPI_COMPILE_CMDLINE)
397  unset(MPI_LINK_CMDLINE)
398 
399  unset(MPI_COMPILE_OPTIONS_WORK)
400  unset(MPI_COMPILE_DEFINITIONS_WORK)
401  unset(MPI_INCLUDE_DIRS_WORK)
402  unset(MPI_LINK_FLAGS_WORK)
403  unset(MPI_LIB_NAMES_WORK)
404  unset(MPI_LIB_FULLPATHS_WORK)
405 
406  # Define the MPICH and Intel MPI compiler variables to the compilers set in CMake.
407  # It's possible to have a per-compiler configuration in these MPI implementations and
408  # a particular MPICH derivate might check compiler interoperability.
409  # Intel MPI in particular does this with I_MPI_CHECK_COMPILER.
410  file(TO_NATIVE_PATH "${CMAKE_${LANG}_COMPILER}" _MPI_UNDERLAYING_COMPILER)
411  # On Windows, the Intel MPI batch scripts can only work with filnames - Full paths will break them.
412  # Due to the lack of other MPICH-based wrappers for Visual C++, we may treat this as default.
413  if(MSVC)
414  get_filename_component(_MPI_UNDERLAYING_COMPILER "${_MPI_UNDERLAYING_COMPILER}" NAME)
415  endif()
416  if("${LANG}" STREQUAL "C")
417  _MPI_env_set_ifnot(I_MPI_CC _MPI_UNDERLAYING_COMPILER)
418  _MPI_env_set_ifnot(MPICH_CC _MPI_UNDERLAYING_COMPILER)
419  elseif("${LANG}" STREQUAL "CXX")
420  _MPI_env_set_ifnot(I_MPI_CXX _MPI_UNDERLAYING_COMPILER)
421  _MPI_env_set_ifnot(MPICH_CXX _MPI_UNDERLAYING_COMPILER)
422  elseif("${LANG}" STREQUAL "Fortran")
423  _MPI_env_set_ifnot(I_MPI_FC _MPI_UNDERLAYING_COMPILER)
424  _MPI_env_set_ifnot(MPICH_FC _MPI_UNDERLAYING_COMPILER)
425  _MPI_env_set_ifnot(I_MPI_F77 _MPI_UNDERLAYING_COMPILER)
426  _MPI_env_set_ifnot(MPICH_F77 _MPI_UNDERLAYING_COMPILER)
427  _MPI_env_set_ifnot(I_MPI_F90 _MPI_UNDERLAYING_COMPILER)
428  _MPI_env_set_ifnot(MPICH_F90 _MPI_UNDERLAYING_COMPILER)
429  endif()
430 
431  # Set these two variables for Intel MPI:
432  # - I_MPI_DEBUG_INFO_STRIP: It adds 'objcopy' lines to the compiler output. We support stripping them
433  # (see below), but if we can avoid them in the first place, we should.
434  # - I_MPI_FORT_BIND: By default Intel MPI makes the C/C++ compiler wrappers link Fortran bindings.
435  # This is so that mixed-language code doesn't require additional libraries when linking with mpicc.
436  # For our purposes, this makes little sense, since correct MPI usage from CMake already circumvenes this.
437  set(_MPI_ENV_VALUE "disable")
438  _MPI_env_set_ifnot(I_MPI_DEBUG_INFO_STRIP _MPI_ENV_VALUE)
439  _MPI_env_set_ifnot(I_MPI_FORT_BIND _MPI_ENV_VALUE)
440 
441  # Check whether the -showme:compile option works. This indicates that we have either Open MPI
442  # or a newer version of LAM/MPI, and implies that -showme:link will also work.
443  # Open MPI also supports -show, but separates linker and compiler information
444  _MPI_check_compiler(${LANG} "-showme:compile" MPI_COMPILE_CMDLINE MPI_COMPILER_RETURN)
445  if (MPI_COMPILER_RETURN EQUAL 0)
446  _MPI_check_compiler(${LANG} "-showme:link" MPI_LINK_CMDLINE MPI_COMPILER_RETURN)
447 
448  if (NOT MPI_COMPILER_RETURN EQUAL 0)
449  unset(MPI_COMPILE_CMDLINE)
450  endif()
451  endif()
452 
453  # MPICH and MVAPICH offer -compile-info and -link-info.
454  # For modern versions, both do the same as -show. However, for old versions, they do differ
455  # when called for mpicxx and mpif90 and it's necessary to use them over -show in order to find the
456  # removed MPI C++ bindings.
457  if (NOT MPI_COMPILER_RETURN EQUAL 0)
458  _MPI_check_compiler(${LANG} "-compile-info" MPI_COMPILE_CMDLINE MPI_COMPILER_RETURN)
459 
460  if (MPI_COMPILER_RETURN EQUAL 0)
461  _MPI_check_compiler(${LANG} "-link-info" MPI_LINK_CMDLINE MPI_COMPILER_RETURN)
462 
463  if (NOT MPI_COMPILER_RETURN EQUAL 0)
464  unset(MPI_COMPILE_CMDLINE)
465  endif()
466  endif()
467  endif()
468 
469  # MPICH, MVAPICH2 and Intel MPI just use "-show". Open MPI also offers this, but the
470  # -showme commands are more specialized.
471  if (NOT MPI_COMPILER_RETURN EQUAL 0)
472  _MPI_check_compiler(${LANG} "-show" MPI_COMPILE_CMDLINE MPI_COMPILER_RETURN)
473  endif()
474 
475  # Older versions of LAM/MPI have "-showme". Open MPI also supports this.
476  # Unknown to MPICH, MVAPICH and Intel MPI.
477  if (NOT MPI_COMPILER_RETURN EQUAL 0)
478  _MPI_check_compiler(${LANG} "-showme" MPI_COMPILE_CMDLINE MPI_COMPILER_RETURN)
479  endif()
480 
481  if (MPI_COMPILER_RETURN EQUAL 0 AND DEFINED MPI_COMPILE_CMDLINE)
482  # Intel MPI can be run with -compchk or I_MPI_CHECK_COMPILER set to 1.
483  # In this case, -show will be prepended with a line to the compiler checker. This is a script that performs
484  # compatibility checks and returns a non-zero exit code together with an error if something fails.
485  # It has to be called as "compchk.sh <arch> <compiler>". Here, <arch> is one out of 32 (i686), 64 (ia64) or 32e (x86_64).
486  # The compiler is identified by filename, and can be either the MPI compiler or the underlying compiler.
487  # NOTE: It is vital to run this script while the environment variables are set up, otherwise it can check the wrong compiler.
488  if("${MPI_COMPILE_CMDLINE}" MATCHES "^([^\" ]+/compchk.sh|\"[^\"]+/compchk.sh\") +([^ ]+)")
489  # Now CMAKE_MATCH_1 contains the path to the compchk.sh file and CMAKE_MATCH_2 the architecture flag.
490  unset(COMPILER_CHECKER_OUTPUT)
491  execute_process(
492  COMMAND ${CMAKE_MATCH_1} ${CMAKE_MATCH_2} ${MPI_${LANG}_COMPILER}
493  OUTPUT_VARIABLE COMPILER_CHECKER_OUTPUT OUTPUT_STRIP_TRAILING_WHITESPACE
494  ERROR_VARIABLE COMPILER_CHECKER_OUTPUT ERROR_STRIP_TRAILING_WHITESPACE
495  RESULT_VARIABLE MPI_COMPILER_RETURN)
496  # If it returned a non-zero value, the check below will fail and cause the interrogation to be aborted.
497  if(NOT MPI_COMPILER_RETURN EQUAL 0)
498  if(NOT MPI_FIND_QUIETLY)
499  message(STATUS "Intel MPI compiler check failed: ${COMPILER_CHECKER_OUTPUT}")
500  endif()
501  else()
502  # Since the check passed, we can remove the compchk.sh script.
503  string(REGEX REPLACE "^([^\" ]+|\"[^\"]+\")/compchk.sh.*\n" "" MPI_COMPILE_CMDLINE "${MPI_COMPILE_CMDLINE}")
504  endif()
505  endif()
506  endif()
507 
508  # Revert changes to the environment made previously
509  if("${LANG}" STREQUAL "C")
510  _MPI_env_unset_ifnot(I_MPI_CC)
511  _MPI_env_unset_ifnot(MPICH_CC)
512  elseif("${LANG}" STREQUAL "CXX")
513  _MPI_env_unset_ifnot(I_MPI_CXX)
514  _MPI_env_unset_ifnot(MPICH_CXX)
515  elseif("${LANG}" STREQUAL "Fortran")
516  _MPI_env_unset_ifnot(I_MPI_FC)
517  _MPI_env_unset_ifnot(MPICH_FC)
518  _MPI_env_unset_ifnot(I_MPI_F77)
519  _MPI_env_unset_ifnot(MPICH_F77)
520  _MPI_env_unset_ifnot(I_MPI_F90)
521  _MPI_env_unset_ifnot(MPICH_F90)
522  endif()
523 
524  _MPI_env_unset_ifnot(I_MPI_DEBUG_INFO_STRIP)
525  _MPI_env_unset_ifnot(I_MPI_FORT_BIND)
526 
527  if (NOT (MPI_COMPILER_RETURN EQUAL 0) OR NOT (DEFINED MPI_COMPILE_CMDLINE))
528  # Cannot interrogate this compiler, so exit.
529  set(MPI_${LANG}_WRAPPER_FOUND FALSE PARENT_SCOPE)
530  return()
531  endif()
532  unset(MPI_COMPILER_RETURN)
533 
534  # We have our command lines, but we might need to copy MPI_COMPILE_CMDLINE
535  # into MPI_LINK_CMDLINE, if we didn't find the link line.
536  if (NOT DEFINED MPI_LINK_CMDLINE)
537  set(MPI_LINK_CMDLINE "${MPI_COMPILE_CMDLINE}")
538  endif()
539 
540  # Visual Studio parsers permit each flag prefixed by either / or -.
541  # We'll normalize this to the - syntax we use for CMake purposes anyways.
542  if(MSVC)
543  foreach(_MPI_VARIABLE IN ITEMS COMPILE LINK)
544  # The Intel MPI wrappers on Windows prefix their output with some copyright boilerplate.
545  # To prevent possible problems, we discard this text before proceeding with any further matching.
546  string(REGEX REPLACE "^[^ ]+ for the Intel\\(R\\) MPI Library [^\n]+ for Windows\\*\nCopyright\\(C\\) [^\n]+, Intel Corporation\\. All rights reserved\\.\n\n" ""
547  MPI_${_MPI_VARIABLE}_CMDLINE "${MPI_${_MPI_VARIABLE}_CMDLINE}")
548  string(REGEX REPLACE "(^| )/" "\\1-" MPI_${_MPI_VARIABLE}_CMDLINE "${MPI_${_MPI_VARIABLE}_CMDLINE}")
549  string(REPLACE "-libpath:" "-LIBPATH:" MPI_${_MPI_VARIABLE}_CMDLINE "${MPI_${_MPI_VARIABLE}_CMDLINE}")
550  endforeach()
551  endif()
552 
553  # For MSVC and cl-compatible compilers, the keyword /link indicates a point after which
554  # everything following is passed to the linker. In this case, we drop all prior information
555  # from the link line and treat any unknown extra flags as linker flags.
556  set(_MPI_FILTERED_LINK_INFORMATION FALSE)
557  if(MSVC)
558  if(MPI_LINK_CMDLINE MATCHES " -(link|LINK) ")
559  string(REGEX REPLACE ".+-(link|LINK) +" "" MPI_LINK_CMDLINE "${MPI_LINK_CMDLINE}")
560  set(_MPI_FILTERED_LINK_INFORMATION TRUE)
561  endif()
562  string(REGEX REPLACE " +-(link|LINK) .+" "" MPI_COMPILE_CMDLINE "${MPI_COMPILE_CMDLINE}")
563  endif()
564 
565  if(UNIX)
566  # At this point, we obtained some output from a compiler wrapper that works.
567  # We'll now try to parse it into variables with meaning to us.
568  if("${LANG}" STREQUAL "Fortran")
569  # If MPICH (and derivates) didn't recognize the Fortran compiler include flag during configuration,
570  # they'll return a set of three commands, consisting out of a symlink command for mpif.h,
571  # the actual compiler command and deletion of the created symlink.
572  # Especially with M(VA)PICH-1, this appears to happen erroneously, and therefore we should translate
573  # this output into an additional include directory and then drop it from the output.
574  if("${MPI_COMPILE_CMDLINE}" MATCHES "^ln -s ([^\" ]+|\"[^\"]+\") mpif.h")
575  get_filename_component(MPI_INCLUDE_DIRS_WORK "${CMAKE_MATCH_1}" DIRECTORY)
576  string(REGEX REPLACE "^ln -s ([^\" ]+|\"[^\"]+\") mpif.h\n" "" MPI_COMPILE_CMDLINE "${MPI_COMPILE_CMDLINE}")
577  string(REGEX REPLACE "^ln -s ([^\" ]+|\"[^\"]+\") mpif.h\n" "" MPI_LINK_CMDLINE "${MPI_LINK_CMDLINE}")
578  string(REGEX REPLACE "\nrm -f mpif.h$" "" MPI_COMPILE_CMDLINE "${MPI_COMPILE_CMDLINE}")
579  string(REGEX REPLACE "\nrm -f mpif.h$" "" MPI_LINK_CMDLINE "${MPI_LINK_CMDLINE}")
580  endif()
581  endif()
582 
583  # If Intel MPI was configured for static linkage with -static_mpi, the wrapper will by default strip
584  # debug information from resulting binaries (see I_MPI_DEBUG_INFO_STRIP).
585  # Since we cannot process this information into CMake logic, we need to discard the resulting objcopy
586  # commands from the output.
587  string(REGEX REPLACE "(^|\n)objcopy[^\n]+(\n|$)" "" MPI_COMPILE_CMDLINE "${MPI_COMPILE_CMDLINE}")
588  string(REGEX REPLACE "(^|\n)objcopy[^\n]+(\n|$)" "" MPI_LINK_CMDLINE "${MPI_LINK_CMDLINE}")
589  endif()
590 
591  # For Visual C++, extracting compiler options in a generic fashion isn't easy. However, no MPI implementation
592  # on Windows seems to require any specific ones, either.
593  if(NOT MSVC)
594  # Extract compile options from the compile command line.
595  string(REGEX MATCHALL "(^| )-f([^\" ]+|\"[^\"]+\")" MPI_ALL_COMPILE_OPTIONS "${MPI_COMPILE_CMDLINE}")
596 
597  foreach(_MPI_COMPILE_OPTION IN LISTS MPI_ALL_COMPILE_OPTIONS)
598  string(REGEX REPLACE "^ " "" _MPI_COMPILE_OPTION "${_MPI_COMPILE_OPTION}")
599 
600  # Ignore -fstack-protector directives: These occur on MPICH and MVAPICH when the libraries
601  # themselves were built with this flag. However, this flag is unrelated to using MPI, and
602  # we won't match the accompanying --param-ssp-size and -Wp,-D_FORTIFY_SOURCE flags and therefore
603  # produce inconsistent results with the regularly flags.
604  # Similarly, aliasing flags do not belong into our flag array.
605  if(NOT "${_MPI_COMPILE_OPTION}" MATCHES "^-f((no-|)(stack-protector|strict-aliasing)|PI[CE]|pi[ce])")
606  list(APPEND MPI_COMPILE_OPTIONS_WORK "${_MPI_COMPILE_OPTION}")
607  endif()
608  endforeach()
609  endif()
610 
611  # For GNU-style compilers, it's possible to prefix includes and definitions with certain flags to pass them
612  # only to the preprocessor. For CMake purposes, we need to treat, but ignore such scopings.
613  # Note that we do not support spaces between the arguments, i.e. -Wp,-I -Wp,/opt/mympi will not be parsed
614  # correctly. This form does not seem to occur in any common MPI implementation, however.
615  if(NOT MSVC)
616  set(_MPI_PREPROCESSOR_FLAG_REGEX "(-Wp,|-Xpreprocessor )?")
617  else()
618  set(_MPI_PREPROCESSOR_FLAG_REGEX "")
619  endif()
620 
621  # Same deal as above, for the definitions.
622  string(REGEX MATCHALL "(^| )${_MPI_PREPROCESSOR_FLAG_REGEX}-D *([^\" ]+|\"[^\"]+\")" MPI_ALL_COMPILE_DEFINITIONS "${MPI_COMPILE_CMDLINE}")
623 
624  foreach(_MPI_COMPILE_DEFINITION IN LISTS MPI_ALL_COMPILE_DEFINITIONS)
625  string(REGEX REPLACE "^ ?${_MPI_PREPROCESSOR_FLAG_REGEX}-D *" "" _MPI_COMPILE_DEFINITION "${_MPI_COMPILE_DEFINITION}")
626  string(REPLACE "\"" "" _MPI_COMPILE_DEFINITION "${_MPI_COMPILE_DEFINITION}")
627  if(NOT "${_MPI_COMPILE_DEFINITION}" MATCHES "^_FORTIFY_SOURCE.*")
628  list(APPEND MPI_COMPILE_DEFINITIONS_WORK "${_MPI_COMPILE_DEFINITION}")
629  endif()
630  endforeach()
631 
632  # Extract include paths from compile command line
633  string(REGEX MATCHALL "(^| )${_MPI_PREPROCESSOR_FLAG_REGEX}${CMAKE_INCLUDE_FLAG_${LANG}} *([^\" ]+|\"[^\"]+\")"
634  MPI_ALL_INCLUDE_PATHS "${MPI_COMPILE_CMDLINE}")
635 
636  # If extracting failed to work, we'll try using -showme:incdirs.
637  # Unlike before, we do this without the environment variables set up, but since only MPICH derivates are affected by any of them, and
638  # -showme:... is only supported by Open MPI and LAM/MPI, this isn't a concern.
639  if (NOT MPI_ALL_INCLUDE_PATHS)
640  _MPI_check_compiler(${LANG} "-showme:incdirs" MPI_INCDIRS_CMDLINE MPI_INCDIRS_COMPILER_RETURN)
641  if(MPI_INCDIRS_COMPILER_RETURN)
642  separate_arguments(MPI_ALL_INCLUDE_PATHS NATIVE_COMMAND "${MPI_INCDIRS_CMDLINE}")
643  endif()
644  endif()
645 
646  foreach(_MPI_INCLUDE_PATH IN LISTS MPI_ALL_INCLUDE_PATHS)
647  string(REGEX REPLACE "^ ?${_MPI_PREPROCESSOR_FLAG_REGEX}${CMAKE_INCLUDE_FLAG_${LANG}} *" "" _MPI_INCLUDE_PATH "${_MPI_INCLUDE_PATH}")
648  string(REPLACE "\"" "" _MPI_INCLUDE_PATH "${_MPI_INCLUDE_PATH}")
649  get_filename_component(_MPI_INCLUDE_PATH "${_MPI_INCLUDE_PATH}" REALPATH)
650  list(APPEND MPI_INCLUDE_DIRS_WORK "${_MPI_INCLUDE_PATH}")
651  endforeach()
652 
653  # The next step are linker flags and library directories. Here, we first take the flags given in raw -L or -LIBPATH: syntax.
654  string(REGEX MATCHALL "(^| )${CMAKE_LIBRARY_PATH_FLAG} *([^\" ]+|\"[^\"]+\")" MPI_DIRECT_LINK_PATHS "${MPI_LINK_CMDLINE}")
655  foreach(_MPI_LPATH IN LISTS MPI_DIRECT_LINK_PATHS)
656  string(REGEX REPLACE "(^| )${CMAKE_LIBRARY_PATH_FLAG} *" "" _MPI_LPATH "${_MPI_LPATH}")
657  list(APPEND MPI_ALL_LINK_PATHS "${_MPI_LPATH}")
658  endforeach()
659 
660  # If the link commandline hasn't been filtered (e.g. when using MSVC and /link), we need to extract the relevant parts first.
661  if(NOT _MPI_FILTERED_LINK_INFORMATION)
662  string(REGEX MATCHALL "(^| )(-Wl,|-Xlinker +)([^\" ]+|\"[^\"]+\")" MPI_LINK_FLAGS "${MPI_LINK_CMDLINE}")
663 
664  # In this case, we could also find some indirectly given linker paths, e.g. prefixed by -Xlinker or -Wl,
665  # Since syntaxes like -Wl,-L -Wl,/my/path/to/lib are also valid, we parse these paths by first removing -Wl, and -Xlinker
666  # from the list of filtered flags and then parse the remainder of the output.
667  string(REGEX REPLACE "(-Wl,|-Xlinker +)" "" MPI_LINK_FLAGS_RAW "${MPI_LINK_FLAGS}")
668 
669  # Now we can parse the leftover output. Note that spaces can now be handled since the above example would reduce to
670  # -L /my/path/to/lib and can be extracted correctly.
671  string(REGEX MATCHALL "^(${CMAKE_LIBRARY_PATH_FLAG},? *|--library-path=)([^\" ]+|\"[^\"]+\")"
672  MPI_INDIRECT_LINK_PATHS "${MPI_LINK_FLAGS_RAW}")
673 
674  foreach(_MPI_LPATH IN LISTS MPI_INDIRECT_LINK_PATHS)
675  string(REGEX REPLACE "^(${CMAKE_LIBRARY_PATH_FLAG},? *|--library-path=)" "" _MPI_LPATH "${_MPI_LPATH}")
676  list(APPEND MPI_ALL_LINK_PATHS "${_MPI_LPATH}")
677  endforeach()
678 
679  # We need to remove the flags we extracted from the linker flag list now.
680  string(REGEX REPLACE "(^| )(-Wl,|-Xlinker +)(${CMAKE_LIBRARY_PATH_FLAG},? *(-Wl,|-Xlinker +)?|--library-path=)([^\" ]+|\"[^\"]+\")" ""
681  MPI_LINK_CMDLINE_FILTERED "${MPI_LINK_CMDLINE}")
682 
683  # Some MPI implementations pass on options they themselves were built with. Since -z,noexecstack is a common
684  # hardening, we should strip it. In general, the -z options should be undesirable.
685  string(REGEX REPLACE "(^| )-Wl,-z(,[^ ]+| +-Wl,[^ ]+)" "" MPI_LINK_CMDLINE_FILTERED "${MPI_LINK_CMDLINE_FILTERED}")
686  string(REGEX REPLACE "(^| )-Xlinker +-z +-Xlinker +[^ ]+" "" MPI_LINK_CMDLINE_FILTERED "${MPI_LINK_CMDLINE_FILTERED}")
687 
688  # We only consider options of the form -Wl or -Xlinker:
689  string(REGEX MATCHALL "(^| )(-Wl,|-Xlinker +)([^\" ]+|\"[^\"]+\")" MPI_ALL_LINK_FLAGS "${MPI_LINK_CMDLINE_FILTERED}")
690 
691  # As a next step, we assemble the linker flags extracted in a preliminary flags string
692  foreach(_MPI_LINK_FLAG IN LISTS MPI_ALL_LINK_FLAGS)
693  string(STRIP "${_MPI_LINK_FLAG}" _MPI_LINK_FLAG)
694  if (MPI_LINK_FLAGS_WORK)
695  string(APPEND MPI_LINK_FLAGS_WORK " ${_MPI_LINK_FLAG}")
696  else()
697  set(MPI_LINK_FLAGS_WORK "${_MPI_LINK_FLAG}")
698  endif()
699  endforeach()
700  else()
701  # In the filtered case, we obtain the link time flags by just stripping the library paths.
702  string(REGEX REPLACE "(^| )${CMAKE_LIBRARY_PATH_FLAG} *([^\" ]+|\"[^\"]+\")" "" MPI_LINK_CMDLINE_FILTERED "${MPI_LINK_CMDLINE}")
703  endif()
704 
705  # If we failed to extract any linker paths, we'll try using the -showme:libdirs option with the MPI compiler.
706  # This will return a list of folders, not a set of flags!
707  if (NOT MPI_ALL_LINK_PATHS)
708  _MPI_check_compiler(${LANG} "-showme:libdirs" MPI_LIBDIRS_CMDLINE MPI_LIBDIRS_COMPILER_RETURN)
709  if(MPI_LIBDIRS_COMPILER_RETURN)
710  separate_arguments(MPI_ALL_LINK_PATHS NATIVE_COMMAND "${MPI_LIBDIRS_CMDLINE}")
711  endif()
712  endif()
713 
714  # We need to remove potential quotes and convert the paths to CMake syntax while resolving them, too.
715  foreach(_MPI_LPATH IN LISTS MPI_ALL_LINK_PATHS)
716  string(REPLACE "\"" "" _MPI_LPATH "${_MPI_LPATH}")
717  get_filename_component(_MPI_LPATH "${_MPI_LPATH}" REALPATH)
718  list(APPEND MPI_LINK_DIRECTORIES_WORK "${_MPI_LPATH}")
719  endforeach()
720 
721  # Extract the set of libraries to link against from the link command line
722  # This only makes sense if CMAKE_LINK_LIBRARY_FLAG is defined, i.e. a -lxxxx syntax is supported by the compiler.
723  if(CMAKE_LINK_LIBRARY_FLAG)
724  string(REGEX MATCHALL "(^| )${CMAKE_LINK_LIBRARY_FLAG}([^\" ]+|\"[^\"]+\")"
725  MPI_LIBNAMES "${MPI_LINK_CMDLINE}")
726 
727  foreach(_MPI_LIB_NAME IN LISTS MPI_LIBNAMES)
728  string(REGEX REPLACE "^ ?${CMAKE_LINK_LIBRARY_FLAG}" "" _MPI_LIB_NAME "${_MPI_LIB_NAME}")
729  string(REPLACE "\"" "" _MPI_LIB_NAME "${_MPI_LIB_NAME}")
730  list(APPEND MPI_LIB_NAMES_WORK "${_MPI_LIB_NAME}")
731  endforeach()
732  endif()
733 
734  # Treat linker objects given by full path, for example static libraries, import libraries
735  # or shared libraries if there aren't any import libraries in use on the system.
736  # Note that we do not consider CMAKE_<TYPE>_LIBRARY_PREFIX intentionally here: The linker will for a given file
737  # decide how to link it based on file type, not based on a prefix like 'lib'.
738  set(_MPI_LIB_SUFFIX_REGEX "${CMAKE_STATIC_LIBRARY_SUFFIX}")
739  if(DEFINED CMAKE_IMPORT_LIBRARY_SUFFIX)
740  if(NOT ("${CMAKE_IMPORT_LIBRARY_SUFFIX}" STREQUAL "${CMAKE_STATIC_LIBRARY_SUFFIX}"))
741  string(APPEND _MPI_SUFFIX_REGEX "|${CMAKE_IMPORT_LIBRARY_SUFFIX}")
742  endif()
743  else()
744  string(APPEND _MPI_LIB_SUFFIX_REGEX "|${CMAKE_SHARED_LIBRARY_SUFFIX}")
745  endif()
746  set(_MPI_LIB_NAME_REGEX "(([^\" ]+(${_MPI_LIB_SUFFIX_REGEX}))|(\"[^\"]+(${_MPI_LIB_SUFFIX_REGEX})\"))( +|$)")
747  string(REPLACE "." "\\." _MPI_LIB_NAME_REGEX "${_MPI_LIB_NAME_REGEX}")
748 
749  string(REGEX MATCHALL "${_MPI_LIB_NAME_REGEX}" MPI_LIBNAMES "${MPI_LINK_CMDLINE}")
750  foreach(_MPI_LIB_NAME IN LISTS MPI_LIBNAMES)
751  string(REGEX REPLACE "^ +\"?|\"? +$" "" _MPI_LIB_NAME "${_MPI_LIB_NAME}")
752  get_filename_component(_MPI_LIB_PATH "${_MPI_LIB_NAME}" DIRECTORY)
753  if(NOT "${_MPI_LIB_PATH}" STREQUAL "")
754  list(APPEND MPI_LIB_FULLPATHS_WORK "${_MPI_LIB_NAME}")
755  else()
756  list(APPEND MPI_LIB_NAMES_WORK "${_MPI_LIB_NAME}")
757  endif()
758  endforeach()
759 
760  # Save the explicitly given link directories
761  set(MPI_LINK_DIRECTORIES_LEFTOVER "${MPI_LINK_DIRECTORIES_WORK}")
762 
763  # An MPI compiler wrapper could have its MPI libraries in the implictly
764  # linked directories of the compiler itself.
765  if(DEFINED CMAKE_${LANG}_IMPLICIT_LINK_DIRECTORIES)
766  list(APPEND MPI_LINK_DIRECTORIES_WORK "${CMAKE_${LANG}_IMPLICIT_LINK_DIRECTORIES}")
767  endif()
768 
769  # Determine full path names for all of the libraries that one needs
770  # to link against in an MPI program
771  unset(MPI_PLAIN_LIB_NAMES_WORK)
772  foreach(_MPI_LIB_NAME IN LISTS MPI_LIB_NAMES_WORK)
773  get_filename_component(_MPI_PLAIN_LIB_NAME "${_MPI_LIB_NAME}" NAME_WE)
774  list(APPEND MPI_PLAIN_LIB_NAMES_WORK "${_MPI_PLAIN_LIB_NAME}")
775  find_library(MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY
776  NAMES "${_MPI_LIB_NAME}" "lib${_MPI_LIB_NAME}"
777  HINTS ${MPI_LINK_DIRECTORIES_WORK}
778  DOC "Location of the ${_MPI_PLAIN_LIB_NAME} library for MPI"
779  )
780  mark_as_advanced(MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY)
781  # Remove the directory from the remainder list.
782  if(MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY)
783  get_filename_component(_MPI_TAKEN_DIRECTORY "${MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY}" DIRECTORY)
784  list(REMOVE_ITEM MPI_LINK_DIRECTORIES_LEFTOVER "${_MPI_TAKEN_DIRECTORY}")
785  endif()
786  endforeach()
787 
788  # Add the link directories given explicitly that we haven't used back as linker directories.
789  if(NOT WIN32)
790  foreach(_MPI_LINK_DIRECTORY IN LISTS MPI_LINK_DIRECTORIES_LEFTOVER)
791  file(TO_NATIVE_PATH "${_MPI_LINK_DIRECTORY}" _MPI_LINK_DIRECTORY_ACTUAL)
792  string(FIND "${_MPI_LINK_DIRECTORY_ACTUAL}" " " _MPI_LINK_DIRECTORY_CONTAINS_SPACE)
793  if(NOT _MPI_LINK_DIRECTORY_CONTAINS_SPACE EQUAL -1)
794  set(_MPI_LINK_DIRECTORY_ACTUAL "\"${_MPI_LINK_DIRECTORY_ACTUAL}\"")
795  endif()
796  if(MPI_LINK_FLAGS_WORK)
797  string(APPEND MPI_LINK_FLAGS_WORK " ${CMAKE_LIBRARY_PATH_FLAG}${_MPI_LINK_DIRECTORY_ACTUAL}")
798  else()
799  set(MPI_LINK_FLAGS_WORK "${CMAKE_LIBRARY_PATH_FLAG}${_MPI_LINK_DIRECTORY_ACTUAL}")
800  endif()
801  endforeach()
802  endif()
803 
804  # Deal with the libraries given with full path next
805  unset(MPI_DIRECT_LIB_NAMES_WORK)
806  foreach(_MPI_LIB_FULLPATH IN LISTS MPI_LIB_FULLPATHS_WORK)
807  get_filename_component(_MPI_PLAIN_LIB_NAME "${_MPI_LIB_FULLPATH}" NAME_WE)
808  list(APPEND MPI_DIRECT_LIB_NAMES_WORK "${_MPI_PLAIN_LIB_NAME}")
809  set(MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY "${_MPI_LIB_FULLPATH}" CACHE FILEPATH "Location of the ${_MPI_PLAIN_LIB_NAME} library for MPI")
810  mark_as_advanced(MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY)
811  endforeach()
812  # Directly linked objects should be linked first in case some generic linker flags are needed for them.
813  if(MPI_DIRECT_LIB_NAMES_WORK)
814  set(MPI_PLAIN_LIB_NAMES_WORK "${MPI_DIRECT_LIB_NAMES_WORK};${MPI_PLAIN_LIB_NAMES_WORK}")
815  endif()
816 
817  # MPI might require pthread to work. The above mechanism wouldn't detect it, but we need to
818  # link it in that case. -lpthread is covered by the normal library treatment on the other hand.
819  if("${MPI_COMPILE_CMDLINE}" MATCHES "-pthread")
820  list(APPEND MPI_COMPILE_OPTIONS_WORK "-pthread")
821  if(MPI_LINK_FLAGS_WORK)
822  string(APPEND MPI_LINK_FLAGS_WORK " -pthread")
823  else()
824  set(MPI_LINK_FLAGS_WORK "-pthread")
825  endif()
826  endif()
827 
828  if(MPI_${LANG}_EXTRA_COMPILE_DEFINITIONS)
829  list(APPEND MPI_COMPILE_DEFINITIONS_WORK "${MPI_${LANG}_EXTRA_COMPILE_DEFINITIONS}")
830  endif()
831  if(MPI_${LANG}_EXTRA_COMPILE_OPTIONS)
832  list(APPEND MPI_COMPILE_OPTIONS_WORK "${MPI_${LANG}_EXTRA_COMPILE_OPTIONS}")
833  endif()
834  if(MPI_${LANG}_EXTRA_LIB_NAMES)
835  list(APPEND MPI_PLAIN_LIB_NAMES_WORK "${MPI_${LANG}_EXTRA_LIB_NAMES}")
836  endif()
837 
838  # If we found MPI, set up all of the appropriate cache entries
839  if(NOT MPI_${LANG}_COMPILE_OPTIONS)
840  set(MPI_${LANG}_COMPILE_OPTIONS ${MPI_COMPILE_OPTIONS_WORK} CACHE STRING "MPI ${LANG} compilation options" FORCE)
841  endif()
842  if(NOT MPI_${LANG}_COMPILE_DEFINITIONS)
843  set(MPI_${LANG}_COMPILE_DEFINITIONS ${MPI_COMPILE_DEFINITIONS_WORK} CACHE STRING "MPI ${LANG} compilation definitions" FORCE)
844  endif()
845  if(NOT MPI_${LANG}_COMPILER_INCLUDE_DIRS)
846  set(MPI_${LANG}_COMPILER_INCLUDE_DIRS ${MPI_INCLUDE_DIRS_WORK} CACHE STRING "MPI ${LANG} compiler wrapper include directories" FORCE)
847  endif()
848  if(NOT MPI_${LANG}_LINK_FLAGS)
849  set(MPI_${LANG}_LINK_FLAGS ${MPI_LINK_FLAGS_WORK} CACHE STRING "MPI ${LANG} linker flags" FORCE)
850  endif()
851  if(NOT MPI_${LANG}_LIB_NAMES)
852  set(MPI_${LANG}_LIB_NAMES ${MPI_PLAIN_LIB_NAMES_WORK} CACHE STRING "MPI ${LANG} libraries to link against" FORCE)
853  endif()
854  set(MPI_${LANG}_WRAPPER_FOUND TRUE PARENT_SCOPE)
855 endfunction()
856 
857 function(_MPI_guess_settings LANG)
858  set(MPI_GUESS_FOUND FALSE)
859  # Currently only MSMPI and MPICH2 on Windows are supported, so we can skip this search if we're not targeting that.
860  if(WIN32)
861  # MSMPI
862 
863  # The environment variables MSMPI_INC and MSMPILIB32/64 are the only ways of locating the MSMPI_SDK,
864  # which is installed separately from the runtime. Thus it's possible to have mpiexec but not MPI headers
865  # or import libraries and vice versa.
866  if(NOT MPI_GUESS_LIBRARY_NAME OR "${MPI_GUESS_LIBRARY_NAME}" STREQUAL "MSMPI")
867  # We first attempt to locate the msmpi.lib. Should be find it, we'll assume that the MPI present is indeed
868  # Microsoft MPI.
869  if("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
870  set(MPI_MSMPI_LIB_PATH "$ENV{MSMPI_LIB64}")
871  set(MPI_MSMPI_INC_PATH_EXTRA "$ENV{MSMPI_INC}/x64")
872  else()
873  set(MPI_MSMPI_LIB_PATH "$ENV{MSMPI_LIB32}")
874  set(MPI_MSMPI_INC_PATH_EXTRA "$ENV{MSMPI_INC}/x86")
875  endif()
876 
877  find_library(MPI_msmpi_LIBRARY
878  NAMES msmpi
879  HINTS ${MPI_MSMPI_LIB_PATH}
880  DOC "Location of the msmpi library for Microsoft MPI")
881  mark_as_advanced(MPI_msmpi_LIBRARY)
882 
883  if(MPI_msmpi_LIBRARY)
884  # Next, we attempt to locate the MPI header. Note that for Fortran we know that mpif.h is a way
885  # MSMPI can be used and therefore that header has to be present.
886  if(NOT MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS)
887  get_filename_component(MPI_MSMPI_INC_DIR "$ENV{MSMPI_INC}" REALPATH)
888  set(MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS "${MPI_MSMPI_INC_DIR}" CACHE STRING "MPI ${LANG} additional include directories" FORCE)
889  unset(MPI_MSMPI_INC_DIR)
890  endif()
891 
892  # For MSMPI, one can compile the MPI module by building the mpi.f90 shipped with the MSMPI SDK,
893  # thus it might be present or provided by the user. Figuring out which is supported is done later on.
894  # The PGI Fortran compiler for instance ships a prebuilt set of modules in its own include folder.
895  # Should a user be employing PGI or have built its own set and provided it via cache variables, the
896  # splitting routine would have located the module files.
897 
898  # For C and C++, we're done here (MSMPI does not ship the MPI-2 C++ bindings) - however, for Fortran
899  # we need some extra library to glue Fortran support together:
900  # MSMPI ships 2-4 Fortran libraries, each for different Fortran compiler behaviors. The library names
901  # ending with a c are using the cdecl calling convention, whereas those ending with an s are for Fortran
902  # implementations using stdcall. Therefore, the 64-bit MSMPI only ships those ending in 'c', whereas the 32-bit
903  # has both variants available.
904  # The second difference is the last but one letter, if it's an e(nd), the length of a string argument is
905  # passed by the Fortran compiler after all other arguments on the parameter list, if it's an m(ixed),
906  # it's passed immediately after the string address.
907 
908  # To summarize:
909  # - msmpifec: CHARACTER length passed after the parameter list and using cdecl calling convention
910  # - msmpifmc: CHARACTER length passed directly after string address and using cdecl calling convention
911  # - msmpifes: CHARACTER length passed after the parameter list and using stdcall calling convention
912  # - msmpifms: CHARACTER length passed directly after string address and using stdcall calling convention
913  # 32-bit MSMPI ships all four libraries, 64-bit MSMPI ships only the first two.
914 
915  # As is, Intel Fortran and PGI Fortran both use the 'ec' variant of the calling convention, whereas
916  # the old Compaq Visual Fortran compiler defaulted to the 'ms' version. It's possible to make Intel Fortran
917  # use the CVF calling convention using /iface:cvf, but we assume - and this is also assumed in FortranCInterface -
918  # this isn't the case. It's also possible to make CVF use the 'ec' variant, using /iface=(cref,nomixed_str_len_arg).
919 
920  # Our strategy is now to locate all libraries, but enter msmpifec into the LIB_NAMES array.
921  # Should this not be adequate it's a straightforward way for a user to change the LIB_NAMES array and
922  # have his library found. Still, this should not be necessary outside of exceptional cases, as reasoned.
923  if ("${LANG}" STREQUAL "Fortran")
924  set(MPI_MSMPI_CALLINGCONVS c)
925  if("${CMAKE_SIZEOF_VOID_P}" EQUAL 4)
926  list(APPEND MPI_MSMPI_CALLINGCONVS s)
927  endif()
928  foreach(mpistrlenpos IN ITEMS e m)
929  foreach(mpicallingconv IN LISTS MPI_MSMPI_CALLINGCONVS)
930  find_library(MPI_msmpif${mpistrlenpos}${mpicallingconv}_LIBRARY
931  NAMES msmpif${mpistrlenpos}${mpicallingconv}
932  HINTS "${MPI_MSMPI_LIB_PATH}"
933  DOC "Location of the msmpi${mpistrlenpos}${mpicallingconv} library for Microsoft MPI")
934  mark_as_advanced(MPI_msmpif${mpistrlenpos}${mpicallingconv}_LIBRARY)
935  endforeach()
936  endforeach()
937  if(NOT MPI_${LANG}_LIB_NAMES)
938  set(MPI_${LANG}_LIB_NAMES "msmpi;msmpifec" CACHE STRING "MPI ${LANG} libraries to link against" FORCE)
939  endif()
940 
941  # At this point we're *not* done. MSMPI requires an additional include file for Fortran giving the value
942  # of MPI_AINT. This file is called mpifptr.h located in the x64 and x86 subfolders, respectively.
943  find_path(MPI_mpifptr_INCLUDE_DIR
944  NAMES "mpifptr.h"
945  HINTS "${MPI_MSMPI_INC_PATH_EXTRA}"
946  DOC "Location of the mpifptr.h extra header for Microsoft MPI")
947  if(NOT MPI_${LANG}_ADDITIONAL_INCLUDE_VARS)
948  set(MPI_${LANG}_ADDITIONAL_INCLUDE_VARS "mpifptr" CACHE STRING "MPI ${LANG} additional include directory variables, given in the form MPI_<name>_INCLUDE_DIR." FORCE)
949  endif()
950  mark_as_advanced(MPI_${LANG}_ADDITIONAL_INCLUDE_VARS MPI_mpifptr_INCLUDE_DIR)
951  else()
952  if(NOT MPI_${LANG}_LIB_NAMES)
953  set(MPI_${LANG}_LIB_NAMES "msmpi" CACHE STRING "MPI ${LANG} libraries to link against" FORCE)
954  endif()
955  endif()
956  mark_as_advanced(MPI_${LANG}_LIB_NAMES)
957  set(MPI_GUESS_FOUND TRUE)
958 
959  if(_MPIEXEC_NOT_GIVEN)
960  unset(MPIEXEC_EXECUTABLE CACHE)
961  endif()
962 
963  find_program(MPIEXEC_EXECUTABLE
964  NAMES mpiexec
965  HINTS $ENV{MSMPI_BIN} "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\MPI;InstallRoot]/Bin"
966  DOC "Executable for running MPI programs.")
967  endif()
968  endif()
969 
970  # At this point there's not many MPIs that we could still consider.
971  # OpenMPI 1.6.x and below supported Windows, but these ship compiler wrappers that still work.
972  # The only other relevant MPI implementation without a wrapper is MPICH2, which had Windows support in 1.4.1p1 and older.
973  if(NOT MPI_GUESS_FOUND AND (NOT MPI_GUESS_LIBRARY_NAME OR "${MPI_GUESS_LIBRARY_NAME}" STREQUAL "MPICH2"))
974  set(MPI_MPICH_PREFIX_PATHS
975  "$ENV{ProgramW6432}/MPICH2/lib"
976  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MPICH\\SMPD;binary]/../lib"
977  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MPICH2;Path]/lib"
978  )
979 
980  # All of C, C++ and Fortran will need mpi.lib, so we'll look for this first
981  find_library(MPI_mpi_LIBRARY
982  NAMES mpi
983  HINTS ${MPI_MPICH_PREFIX_PATHS})
984  mark_as_advanced(MPI_mpi_LIBRARY)
985  # If we found mpi.lib, we detect the rest of MPICH2
986  if(MPI_mpi_LIBRARY)
987  set(MPI_MPICH_LIB_NAMES "mpi")
988  # If MPI-2 C++ bindings are requested, we need to locate cxx.lib as well.
989  # Otherwise, MPICH_SKIP_MPICXX will be defined and these bindings aren't needed.
990  if("${LANG}" STREQUAL "CXX" AND NOT MPI_CXX_SKIP_MPICXX)
991  find_library(MPI_cxx_LIBRARY
992  NAMES cxx
993  HINTS ${MPI_MPICH_PREFIX_PATHS})
994  mark_as_advanced(MPI_cxx_LIBRARY)
995  list(APPEND MPI_MPICH_LIB_NAMES "cxx")
996  # For Fortran, MPICH2 provides three different libraries:
997  # fmpich2.lib which uses uppercase symbols and cdecl,
998  # fmpich2s.lib which uses uppercase symbols and stdcall (32-bit only),
999  # fmpich2g.lib which uses lowercase symbols with double underscores and cdecl.
1000  # fmpich2s.lib would be useful for Compaq Visual Fortran, fmpich2g.lib has to be used with GNU g77 and is also
1001  # provided in the form of an .a archive for MinGW and Cygwin. From our perspective, fmpich2.lib is the only one
1002  # we need to try, and if it doesn't work with the given Fortran compiler we'd find out later on during validation
1003  elseif("${LANG}" STREQUAL "Fortran")
1004  find_library(MPI_fmpich2_LIBRARY
1005  NAMES fmpich2
1006  HINTS ${MPI_MPICH_PREFIX_PATHS})
1007  find_library(MPI_fmpich2s_LIBRARY
1008  NAMES fmpich2s
1009  HINTS ${MPI_MPICH_PREFIX_PATHS})
1010  find_library(MPI_fmpich2g_LIBRARY
1011  NAMES fmpich2g
1012  HINTS ${MPI_MPICH_PREFIX_PATHS})
1013  mark_as_advanced(MPI_fmpich2_LIBRARY MPI_fmpich2s_LIBRARY MPI_fmpich2g_LIBRARY)
1014  list(APPEND MPI_MPICH_LIB_NAMES "fmpich2")
1015  endif()
1016 
1017  if(NOT MPI_${LANG}_LIB_NAMES)
1018  set(MPI_${LANG}_LIB_NAMES "${MPI_MPICH_LIB_NAMES}" CACHE STRING "MPI ${LANG} libraries to link against" FORCE)
1019  endif()
1020  unset(MPI_MPICH_LIB_NAMES)
1021 
1022  if(NOT MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS)
1023  # For MPICH2, the include folder would be in ../include relative to the library folder.
1024  get_filename_component(MPI_MPICH_ROOT_DIR "${MPI_mpi_LIBRARY}" DIRECTORY)
1025  get_filename_component(MPI_MPICH_ROOT_DIR "${MPI_MPICH_ROOT_DIR}" DIRECTORY)
1026  if(IS_DIRECTORY "${MPI_MPICH_ROOT_DIR}/include")
1027  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)
1028  endif()
1029  unset(MPI_MPICH_ROOT_DIR)
1030  endif()
1031  set(MPI_GUESS_FOUND TRUE)
1032 
1033  if(_MPIEXEC_NOT_GIVEN)
1034  unset(MPIEXEC_EXECUTABLE CACHE)
1035  endif()
1036 
1037  find_program(MPIEXEC_EXECUTABLE
1038  NAMES ${_MPIEXEC_NAMES}
1039  HINTS "$ENV{ProgramW6432}/MPICH2/bin"
1040  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MPICH\\SMPD;binary]"
1041  "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MPICH2;Path]/bin"
1042  DOC "Executable for running MPI programs.")
1043  endif()
1044  unset(MPI_MPICH_PREFIX_PATHS)
1045  endif()
1046  endif()
1047  set(MPI_${LANG}_GUESS_FOUND "${MPI_GUESS_FOUND}" PARENT_SCOPE)
1048 endfunction()
1049 
1051  if("${LANG}" STREQUAL "CXX")
1052  # To disable the C++ bindings, we need to pass some definitions since the mpi.h header has to deal with both C and C++
1053  # bindings in MPI-2.
1054  if(MPI_CXX_SKIP_MPICXX AND NOT MPI_${LANG}_COMPILE_DEFINITIONS MATCHES "SKIP_MPICXX")
1055  # MPICH_SKIP_MPICXX is being used in MPICH and derivatives like MVAPICH or Intel MPI
1056  # OMPI_SKIP_MPICXX is being used in Open MPI
1057  # _MPICC_H is being used for IBM Platform MPI
1058  list(APPEND MPI_${LANG}_COMPILE_DEFINITIONS "MPICH_SKIP_MPICXX" "OMPI_SKIP_MPICXX" "_MPICC_H")
1059  set(MPI_${LANG}_COMPILE_DEFINITIONS "${MPI_${LANG}_COMPILE_DEFINITIONS}" CACHE STRING "MPI ${LANG} compilation definitions" FORCE)
1060  endif()
1061  endif()
1062 endfunction()
1063 
1065  set(MPI_${LANG}_LIBRARIES "")
1066  # Only for libraries do we need to check whether the compiler's linking stage is separate.
1067  if(NOT "${MPI_${LANG}_COMPILER}" STREQUAL "${CMAKE_${LANG}_COMPILER}" OR NOT MPI_${LANG}_WORKS_IMPLICIT)
1068  foreach(mpilib IN LISTS MPI_${LANG}_LIB_NAMES)
1069  list(APPEND MPI_${LANG}_LIBRARIES ${MPI_${mpilib}_LIBRARY})
1070  endforeach()
1071  endif()
1072 endmacro()
1073 
1074 macro(_MPI_assemble_include_dirs LANG)
1075  set(MPI_${LANG}_INCLUDE_DIRS
1076  ${MPI_${LANG}_COMPILER_INCLUDE_DIRS}
1077  ${MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS}
1078  )
1079  if("${LANG}" MATCHES "(C|CXX)")
1080  if(MPI_${LANG}_HEADER_DIR)
1081  list(APPEND MPI_${LANG}_INCLUDE_DIRS "${MPI_${LANG}_HEADER_DIR}")
1082  endif()
1083  else() # Fortran
1084  if(MPI_${LANG}_F77_HEADER_DIR)
1085  list(APPEND MPI_${LANG}_INCLUDE_DIRS "${MPI_${LANG}_F77_HEADER_DIR}")
1086  endif()
1087  if(MPI_${LANG}_MODULE_DIR)
1088  list(APPEND MPI_${LANG}_INCLUDE_DIRS "${MPI_${LANG}_MODULE_DIR}")
1089  endif()
1090  endif()
1091  if(MPI_${LANG}_INCLUDE_DIRS)
1092  list(REMOVE_DUPLICATES MPI_${LANG}_INCLUDE_DIRS)
1093  endif()
1094 endmacro()
1095 
1096 macro(_MPI_split_include_dirs LANG)
1097  # Backwards compatibility: Search INCLUDE_PATH if given.
1098  if(MPI_${LANG}_INCLUDE_PATH)
1099  list(APPEND MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS "${MPI_${LANG}_INCLUDE_PATH}")
1100  endif()
1101 
1102  # We try to find the headers/modules among those paths (and system paths)
1103  # For C/C++, we just need to have a look for mpi.h.
1104  if("${LANG}" MATCHES "(C|CXX)")
1105  find_path(MPI_${LANG}_HEADER_DIR "mpi.h"
1106  HINTS
1107  ${MPI_${LANG}_COMPILER_INCLUDE_DIRS}
1108  ${MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS}
1109  )
1110  mark_as_advanced(MPI_${LANG}_HEADER_DIR)
1111  if(MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS)
1112  list(REMOVE_ITEM MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS "${MPI_${LANG}_HEADER_DIR}")
1113  endif()
1114 
1115  # Fortran is more complicated here: An implementation could provide
1116  # any of the Fortran 77/90/2008 APIs for MPI. For example, MSMPI
1117  # only provides Fortran 77 and - if mpi.f90 is built - potentially
1118  # a Fortran 90 module.
1119  elseif("${LANG}" STREQUAL "Fortran")
1120  find_path(MPI_${LANG}_F77_HEADER_DIR "mpif.h"
1121  HINTS
1122  ${MPI_${LANG}_COMPILER_INCLUDE_DIRS}
1123  ${MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS}
1124  )
1125  find_path(MPI_${LANG}_MODULE_DIR
1126  NAMES "mpi.mod" "mpi_f08.mod"
1127  HINTS
1128  ${MPI_${LANG}_COMPILER_INCLUDE_DIRS}
1129  ${MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS}
1130  )
1131  if(MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS)
1132  list(REMOVE_ITEM MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS
1133  "${MPI_${LANG}_F77_HEADER_DIR}"
1134  "${MPI_${LANG}_MODULE_DIR}"
1135  )
1136  endif()
1137  mark_as_advanced(MPI_${LANG}_F77_HEADER_DIR MPI_${LANG}_MODULE_DIR)
1138  endif()
1139 
1140  # Remove duplicates and default system directories from the list.
1141  if(MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS)
1142  list(REMOVE_DUPLICATES MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS)
1143  foreach(MPI_IMPLICIT_INC_DIR IN LISTS CMAKE_${LANG}_IMPLICIT_LINK_DIRECTORIES)
1144  list(REMOVE_ITEM MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS ${MPI_IMPLICIT_INC_DIR})
1145  endforeach()
1146  endif()
1147 
1148  set(MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS ${MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS} CACHE STRING "MPI ${LANG} additional include directories" FORCE)
1149 endmacro()
1150 
1151 macro(_MPI_create_imported_target LANG)
1152  if(NOT TARGET MPI::MPI_${LANG})
1153  add_library(MPI::MPI_${LANG} INTERFACE IMPORTED)
1154  endif()
1155 
1156  # When this is consumed for compiling CUDA, use '-Xcompiler' to wrap '-pthread'.
1157  if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12 OR CMAKE_CUDA_COMPILER_LOADED)
1158  string(REPLACE "-pthread" "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcompiler >-pthread"
1159  _MPI_${LANG}_COMPILE_OPTIONS "${MPI_${LANG}_COMPILE_OPTIONS}")
1160  else()
1161  set(_MPI_${LANG}_COMPILE_OPTIONS "${MPI_${LANG}_COMPILE_OPTIONS}")
1162  endif()
1163  set_property(TARGET MPI::MPI_${LANG} PROPERTY INTERFACE_COMPILE_OPTIONS "${_MPI_${LANG}_COMPILE_OPTIONS}")
1164  unset(_MPI_${LANG}_COMPILE_OPTIONS)
1165 
1166  set_property(TARGET MPI::MPI_${LANG} PROPERTY INTERFACE_COMPILE_DEFINITIONS "${MPI_${LANG}_COMPILE_DEFINITIONS}")
1167 
1168  if(MPI_${LANG}_LINK_FLAGS)
1169  set_property(TARGET MPI::MPI_${LANG} PROPERTY INTERFACE_LINK_OPTIONS "SHELL:${MPI_${LANG}_LINK_FLAGS}")
1170  endif()
1171  # If the compiler links MPI implicitly, no libraries will be found as they're contained within
1172  # CMAKE_<LANG>_IMPLICIT_LINK_LIBRARIES already.
1173  if(MPI_${LANG}_LIBRARIES)
1174  set_property(TARGET MPI::MPI_${LANG} PROPERTY INTERFACE_LINK_LIBRARIES "${MPI_${LANG}_LIBRARIES}")
1175  endif()
1176  # Given the new design of FindMPI, INCLUDE_DIRS will always be located, even under implicit linking.
1177  set_property(TARGET MPI::MPI_${LANG} PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${MPI_${LANG}_INCLUDE_DIRS}")
1178 endmacro()
1179 
1180 get_filename_component(_FindMPI_MODULE_DIR
1181  "${CMAKE_CURRENT_LIST_FILE}" PATH)
1182 
1183 function(_MPI_try_staged_settings LANG MPI_TEST_FILE_NAME MODE RUN_BINARY SUPPRESS_ERRORS)
1184  set(WORK_DIR "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/FindMPI")
1185  set(SRC_DIR "${_FindMPI_MODULE_DIR}/FindMPI")
1186  set(BIN_FILE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/FindMPI/${MPI_TEST_FILE_NAME}_${LANG}.bin")
1187  unset(MPI_TEST_COMPILE_DEFINITIONS)
1188  if("${LANG}" STREQUAL "Fortran")
1189  if("${MODE}" STREQUAL "F90_MODULE")
1190  set(MPI_Fortran_INCLUDE_LINE "use mpi\n implicit none")
1191  elseif("${MODE}" STREQUAL "F08_MODULE")
1192  set(MPI_Fortran_INCLUDE_LINE "use mpi_f08\n implicit none")
1193  else() # F77 header
1194  set(MPI_Fortran_INCLUDE_LINE "implicit none\n include 'mpif.h'")
1195  endif()
1196  configure_file("${SRC_DIR}/${MPI_TEST_FILE_NAME}.f90.in" "${WORK_DIR}/${MPI_TEST_FILE_NAME}.f90" @ONLY)
1197  set(MPI_TEST_SOURCE_FILE "${WORK_DIR}/${MPI_TEST_FILE_NAME}.f90")
1198  elseif("${LANG}" STREQUAL "CXX")
1199  configure_file("${SRC_DIR}/${MPI_TEST_FILE_NAME}.c" "${WORK_DIR}/${MPI_TEST_FILE_NAME}.cpp" COPYONLY)
1200  set(MPI_TEST_SOURCE_FILE "${WORK_DIR}/${MPI_TEST_FILE_NAME}.cpp")
1201  if("${MODE}" STREQUAL "TEST_MPICXX")
1202  set(MPI_TEST_COMPILE_DEFINITIONS TEST_MPI_MPICXX)
1203  endif()
1204  else() # C
1205  set(MPI_TEST_SOURCE_FILE "${SRC_DIR}/${MPI_TEST_FILE_NAME}.c")
1206  endif()
1207  if(RUN_BINARY)
1208  try_run(MPI_RUN_RESULT_${LANG}_${MPI_TEST_FILE_NAME}_${MODE} MPI_RESULT_${LANG}_${MPI_TEST_FILE_NAME}_${MODE}
1209  "${CMAKE_BINARY_DIR}" SOURCES "${MPI_TEST_SOURCE_FILE}"
1210  COMPILE_DEFINITIONS ${MPI_TEST_COMPILE_DEFINITIONS}
1211  LINK_LIBRARIES MPI::MPI_${LANG}
1212  RUN_OUTPUT_VARIABLE MPI_RUN_OUTPUT_${LANG}_${MPI_TEST_FILE_NAME}_${MODE}
1213  COMPILE_OUTPUT_VARIABLE _MPI_TRY_${MPI_TEST_FILE_NAME}_${MODE}_OUTPUT)
1214  set(MPI_RUN_OUTPUT_${LANG}_${MPI_TEST_FILE_NAME}_${MODE} "${MPI_RUN_OUTPUT_${LANG}_${MPI_TEST_FILE_NAME}_${MODE}}" PARENT_SCOPE)
1215  else()
1216  try_compile(MPI_RESULT_${LANG}_${MPI_TEST_FILE_NAME}_${MODE}
1217  "${CMAKE_BINARY_DIR}" SOURCES "${MPI_TEST_SOURCE_FILE}"
1218  COMPILE_DEFINITIONS ${MPI_TEST_COMPILE_DEFINITIONS}
1219  LINK_LIBRARIES MPI::MPI_${LANG}
1220  COPY_FILE "${BIN_FILE}"
1221  OUTPUT_VARIABLE _MPI_TRY_${MPI_TEST_FILE_NAME}_${MODE}_OUTPUT)
1222  endif()
1223  if(NOT SUPPRESS_ERRORS)
1224  if(NOT MPI_RESULT_${LANG}_${MPI_TEST_FILE_NAME}_${MODE})
1225  file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
1226  "The MPI test ${MPI_TEST_FILE_NAME} for ${LANG} in mode ${MODE} failed to compile with the following output:\n${_MPI_TRY_${MPI_TEST_FILE_NAME}_${MODE}_OUTPUT}\n\n")
1227  elseif(DEFINED MPI_RUN_RESULT_${LANG}_${MPI_TEST_FILE_NAME}_${MODE} AND MPI_RUN_RESULT_${LANG}_${MPI_TEST_FILE_NAME}_${MODE})
1228  file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
1229  "The MPI test ${MPI_TEST_FILE_NAME} for ${LANG} in mode ${MODE} failed to run with the following output:\n${MPI_RUN_OUTPUT_${LANG}_${MPI_TEST_FILE_NAME}_${MODE}}\n\n")
1230  endif()
1231  endif()
1232 endfunction()
1233 
1234 macro(_MPI_check_lang_works LANG SUPPRESS_ERRORS)
1235  # For Fortran we may have by the MPI-3 standard an implementation that provides:
1236  # - the mpi_f08 module
1237  # - *both*, the mpi module and 'mpif.h'
1238  # Since older MPI standards (MPI-1) did not define anything but 'mpif.h', we need to check all three individually.
1239  if( NOT MPI_${LANG}_WORKS )
1240  if("${LANG}" STREQUAL "Fortran")
1241  set(MPI_Fortran_INTEGER_LINE "(kind=MPI_INTEGER_KIND)")
1242  _MPI_try_staged_settings(${LANG} test_mpi F77_HEADER FALSE ${SUPPRESS_ERRORS})
1243  _MPI_try_staged_settings(${LANG} test_mpi F90_MODULE FALSE ${SUPPRESS_ERRORS})
1244  _MPI_try_staged_settings(${LANG} test_mpi F08_MODULE FALSE ${SUPPRESS_ERRORS})
1245 
1246  set(MPI_${LANG}_WORKS FALSE)
1247 
1248  foreach(mpimethod IN ITEMS F77_HEADER F08_MODULE F90_MODULE)
1249  if(MPI_RESULT_${LANG}_test_mpi_${mpimethod})
1250  set(MPI_${LANG}_WORKS TRUE)
1251  set(MPI_${LANG}_HAVE_${mpimethod} TRUE)
1252  else()
1253  set(MPI_${LANG}_HAVE_${mpimethod} FALSE)
1254  endif()
1255  endforeach()
1256  # MPI-1 versions had no MPI_INTGER_KIND defined, so we need to try without it.
1257  # However, MPI-1 also did not define the Fortran 90 and 08 modules, so we only try the F77 header.
1258  unset(MPI_Fortran_INTEGER_LINE)
1259  if(NOT MPI_${LANG}_WORKS)
1260  _MPI_try_staged_settings(${LANG} test_mpi F77_HEADER_NOKIND FALSE ${SUPPRESS_ERRORS})
1261  if(MPI_RESULT_${LANG}_test_mpi_F77_HEADER_NOKIND)
1262  set(MPI_${LANG}_WORKS TRUE)
1263  set(MPI_${LANG}_HAVE_F77_HEADER TRUE)
1264  endif()
1265  endif()
1266  else()
1267  _MPI_try_staged_settings(${LANG} test_mpi normal FALSE ${SUPPRESS_ERRORS})
1268  # If 'test_mpi' built correctly, we've found valid MPI settings. There might not be MPI-2 C++ support, but there can't
1269  # be MPI-2 C++ support without the C bindings being present, so checking for them is sufficient.
1270  set(MPI_${LANG}_WORKS "${MPI_RESULT_${LANG}_test_mpi_normal}")
1271  endif()
1272  endif()
1273 endmacro()
1274 
1275 # Some systems install various MPI implementations in separate folders in some MPI prefix
1276 # This macro enumerates all such subfolders and adds them to the list of hints that will be searched.
1277 macro(MPI_search_mpi_prefix_folder PREFIX_FOLDER)
1278  if(EXISTS "${PREFIX_FOLDER}")
1279  file(GLOB _MPI_folder_children RELATIVE "${PREFIX_FOLDER}" "${PREFIX_FOLDER}/*")
1280  foreach(_MPI_folder_child IN LISTS _MPI_folder_children)
1281  if(IS_DIRECTORY "${PREFIX_FOLDER}/${_MPI_folder_child}")
1282  list(APPEND MPI_HINT_DIRS "${PREFIX_FOLDER}/${_MPI_folder_child}")
1283  endif()
1284  endforeach()
1285  endif()
1286 endmacro()
1287 
1288 set(MPI_HINT_DIRS ${MPI_HOME} $ENV{MPI_HOME} $ENV{I_MPI_ROOT})
1289 if("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux")
1290  # SUSE Linux Enterprise Server stores its MPI implementations under /usr/lib64/mpi/gcc/<name>
1291  # We enumerate the subfolders and append each as a prefix
1292  MPI_search_mpi_prefix_folder("/usr/lib64/mpi/gcc")
1293 elseif("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "FreeBSD")
1294  # FreeBSD ships mpich under the normal system paths - but available openmpi implementations
1295  # will be found in /usr/local/mpi/<name>
1296  MPI_search_mpi_prefix_folder("/usr/local/mpi")
1297 endif()
1298 
1299 # Most MPI distributions have some form of mpiexec or mpirun which gives us something we can look for.
1300 # The MPI standard does not mandate the existence of either, but instead only makes requirements if a distribution
1301 # ships an mpiexec program (mpirun executables are not regulated by the standard).
1302 
1303 # We defer searching for mpiexec binaries belonging to guesses until later. By doing so, mismatches between mpiexec
1304 # and the MPI we found should be reduced.
1305 if(NOT MPIEXEC_EXECUTABLE)
1306  set(_MPIEXEC_NOT_GIVEN TRUE)
1307 else()
1308  set(_MPIEXEC_NOT_GIVEN FALSE)
1309 endif()
1310 
1311 find_program(MPIEXEC_EXECUTABLE
1312  NAMES ${_MPIEXEC_NAMES}
1313  PATH_SUFFIXES bin sbin
1314  HINTS ${MPI_HINT_DIRS}
1315  DOC "Executable for running MPI programs.")
1316 
1317 # call get_filename_component twice to remove mpiexec and the directory it exists in (typically bin).
1318 # This gives us a fairly reliable base directory to search for /bin /lib and /include from.
1319 get_filename_component(_MPI_BASE_DIR "${MPIEXEC_EXECUTABLE}" PATH)
1320 get_filename_component(_MPI_BASE_DIR "${_MPI_BASE_DIR}" PATH)
1321 
1322 # According to the MPI standard, section 8.8 -n is a guaranteed, and the only guaranteed way to
1323 # launch an MPI process using mpiexec if such a program exists.
1324 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.")
1325 set(MPIEXEC_PREFLAGS "" CACHE STRING "These flags will be directly before the executable that is being run by mpiexec.")
1326 set(MPIEXEC_POSTFLAGS "" CACHE STRING "These flags will be placed after all flags passed to mpiexec.")
1327 
1328 # Set the number of processes to the physical processor count
1329 cmake_host_system_information(RESULT _MPIEXEC_NUMPROCS QUERY NUMBER_OF_PHYSICAL_CORES)
1330 set(MPIEXEC_MAX_NUMPROCS "${_MPIEXEC_NUMPROCS}" CACHE STRING "Maximum number of processors available to run MPI applications.")
1331 unset(_MPIEXEC_NUMPROCS)
1332 mark_as_advanced(MPIEXEC_EXECUTABLE MPIEXEC_NUMPROC_FLAG MPIEXEC_PREFLAGS MPIEXEC_POSTFLAGS MPIEXEC_MAX_NUMPROCS)
1333 
1334 #=============================================================================
1335 # Backward compatibility input hacks. Propagate the FindMPI hints to C and
1336 # CXX if the respective new versions are not defined. Translate the old
1337 # MPI_LIBRARY and MPI_EXTRA_LIBRARY to respective MPI_${LANG}_LIBRARIES.
1338 #
1339 # Once we find the new variables, we translate them back into their old
1340 # equivalents below.
1341 if(NOT MPI_IGNORE_LEGACY_VARIABLES)
1342  foreach (LANG IN ITEMS C CXX)
1343  # Old input variables.
1344  set(_MPI_OLD_INPUT_VARS COMPILER COMPILE_FLAGS INCLUDE_PATH LINK_FLAGS)
1345 
1346  # Set new vars based on their old equivalents, if the new versions are not already set.
1347  foreach (var ${_MPI_OLD_INPUT_VARS})
1348  if (NOT MPI_${LANG}_${var} AND MPI_${var})
1349  set(MPI_${LANG}_${var} "${MPI_${var}}")
1350  endif()
1351  endforeach()
1352 
1353  # Chop the old compile flags into options and definitions
1354 
1355  unset(MPI_${LANG}_EXTRA_COMPILE_DEFINITIONS)
1356  unset(MPI_${LANG}_EXTRA_COMPILE_OPTIONS)
1357  if(MPI_${LANG}_COMPILE_FLAGS)
1358  separate_arguments(MPI_SEPARATE_FLAGS NATIVE_COMMAND "${MPI_${LANG}_COMPILE_FLAGS}")
1359  foreach(_MPI_FLAG IN LISTS MPI_SEPARATE_FLAGS)
1360  if("${_MPI_FLAG}" MATCHES "^ *-D([^ ]+)")
1361  list(APPEND MPI_${LANG}_EXTRA_COMPILE_DEFINITIONS "${CMAKE_MATCH_1}")
1362  else()
1363  list(APPEND MPI_${LANG}_EXTRA_COMPILE_OPTIONS "${_MPI_FLAG}")
1364  endif()
1365  endforeach()
1366  unset(MPI_SEPARATE_FLAGS)
1367  endif()
1368 
1369  # If a list of libraries was given, we'll split it into new-style cache variables
1370  unset(MPI_${LANG}_EXTRA_LIB_NAMES)
1371  if(NOT MPI_${LANG}_LIB_NAMES)
1372  foreach(_MPI_LIB IN LISTS MPI_${LANG}_LIBRARIES MPI_LIBRARY MPI_EXTRA_LIBRARY)
1373  if(_MPI_LIB)
1374  get_filename_component(_MPI_PLAIN_LIB_NAME "${_MPI_LIB}" NAME_WE)
1375  get_filename_component(_MPI_LIB_NAME "${_MPI_LIB}" NAME)
1376  get_filename_component(_MPI_LIB_DIR "${_MPI_LIB}" DIRECTORY)
1377  list(APPEND MPI_${LANG}_EXTRA_LIB_NAMES "${_MPI_PLAIN_LIB_NAME}")
1378  find_library(MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY
1379  NAMES "${_MPI_LIB_NAME}" "lib${_MPI_LIB_NAME}"
1380  HINTS ${_MPI_LIB_DIR} $ENV{MPI_LIB}
1381  DOC "Location of the ${_MPI_PLAIN_LIB_NAME} library for MPI"
1382  )
1383  mark_as_advanced(MPI_${_MPI_PLAIN_LIB_NAME}_LIBRARY)
1384  endif()
1385  endforeach()
1386  endif()
1387  endforeach()
1388 endif()
1389 #=============================================================================
1390 
1391 unset(MPI_VERSION)
1392 unset(MPI_VERSION_MAJOR)
1393 unset(MPI_VERSION_MINOR)
1394 
1395 unset(_MPI_MIN_VERSION)
1396 
1397 # If the user specified a library name we assume they prefer that library over a wrapper. If not, they can disable skipping manually.
1398 if(NOT DEFINED MPI_SKIP_COMPILER_WRAPPER AND MPI_GUESS_LIBRARY_NAME)
1399  set(MPI_SKIP_COMPILER_WRAPPER TRUE)
1400 endif()
1401 
1402 # This loop finds the compilers and sends them off for interrogation.
1403 foreach(LANG IN ITEMS C CXX Fortran)
1404  if(CMAKE_${LANG}_COMPILER_LOADED)
1405  if(NOT MPI_FIND_COMPONENTS)
1406  set(_MPI_FIND_${LANG} TRUE)
1407  elseif( ${LANG} IN_LIST MPI_FIND_COMPONENTS)
1408  set(_MPI_FIND_${LANG} TRUE)
1409  elseif( ${LANG} STREQUAL CXX AND NOT MPI_CXX_SKIP_MPICXX AND MPICXX IN_LIST MPI_FIND_COMPONENTS )
1410  set(_MPI_FIND_${LANG} TRUE)
1411  else()
1412  set(_MPI_FIND_${LANG} FALSE)
1413  endif()
1414  else()
1415  set(_MPI_FIND_${LANG} FALSE)
1416  string(APPEND _MPI_FAIL_REASON "MPI component '${LANG}' was requested, but language ${LANG} is not enabled. ")
1417  endif()
1418  if(_MPI_FIND_${LANG})
1419  if( ${LANG} STREQUAL CXX AND NOT MPICXX IN_LIST MPI_FIND_COMPONENTS )
1420  set(MPI_CXX_SKIP_MPICXX FALSE CACHE BOOL "If true, the MPI-2 C++ bindings are disabled using definitions.")
1421  mark_as_advanced(MPI_CXX_SKIP_MPICXX)
1422  endif()
1423  if(NOT (MPI_${LANG}_LIB_NAMES AND (MPI_${LANG}_INCLUDE_PATH OR MPI_${LANG}_INCLUDE_DIRS OR MPI_${LANG}_COMPILER_INCLUDE_DIRS)))
1424  set(MPI_${LANG}_TRIED_IMPLICIT FALSE)
1425  set(MPI_${LANG}_WORKS_IMPLICIT FALSE)
1426  if(NOT MPI_${LANG}_COMPILER AND NOT MPI_ASSUME_NO_BUILTIN_MPI)
1427  # Should the imported targets be empty, we effectively try whether the compiler supports MPI on its own, which is the case on e.g.
1428  # Cray PrgEnv.
1429  _MPI_create_imported_target(${LANG})
1430  _MPI_check_lang_works(${LANG} TRUE)
1431 
1432  # 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.
1433  if(MPI_${LANG}_WORKS)
1434  set(MPI_${LANG}_COMPILER "${CMAKE_${LANG}_COMPILER}" CACHE FILEPATH "MPI compiler for ${LANG}" FORCE)
1435  set(MPI_${LANG}_WORKS_IMPLICIT TRUE)
1436  endif()
1437  set(MPI_${LANG}_TRIED_IMPLICIT TRUE)
1438  endif()
1439 
1440  if(NOT "${MPI_${LANG}_COMPILER}" STREQUAL "${CMAKE_${LANG}_COMPILER}" OR NOT MPI_${LANG}_WORKS)
1441  set(MPI_${LANG}_WRAPPER_FOUND FALSE)
1442  set(MPI_PINNED_COMPILER FALSE)
1443 
1444  if(NOT MPI_SKIP_COMPILER_WRAPPER)
1445  if(MPI_${LANG}_COMPILER)
1446  # If the user supplies a compiler *name* instead of an absolute path, assume that we need to find THAT compiler.
1447  if (NOT IS_ABSOLUTE "${MPI_${LANG}_COMPILER}")
1448  # Get rid of our default list of names and just search for the name the user wants.
1449  set(_MPI_${LANG}_COMPILER_NAMES "${MPI_${LANG}_COMPILER}")
1450  unset(MPI_${LANG}_COMPILER CACHE)
1451  endif()
1452  # If the user specifies a compiler, we don't want to try to search libraries either.
1453  set(MPI_PINNED_COMPILER TRUE)
1454  endif()
1455 
1456  # If we have an MPI base directory, we'll try all compiler names in that one first.
1457  # This should prevent mixing different MPI environments
1458  if(_MPI_BASE_DIR)
1459  find_program(MPI_${LANG}_COMPILER
1460  NAMES ${_MPI_${LANG}_COMPILER_NAMES}
1461  PATH_SUFFIXES bin sbin
1462  HINTS ${_MPI_BASE_DIR}
1463  NO_DEFAULT_PATH
1464  DOC "MPI compiler for ${LANG}"
1465  )
1466  endif()
1467 
1468  # If the base directory did not help (for example because the mpiexec isn't in the same directory as the compilers),
1469  # we shall try searching in the default paths.
1470  find_program(MPI_${LANG}_COMPILER
1471  NAMES ${_MPI_${LANG}_COMPILER_NAMES}
1472  PATH_SUFFIXES bin sbin
1473  DOC "MPI compiler for ${LANG}"
1474  )
1475 
1476  if("${MPI_${LANG}_COMPILER}" STREQUAL "${CMAKE_${LANG}_COMPILER}")
1477  set(MPI_PINNED_COMPILER TRUE)
1478 
1479  # If we haven't made the implicit compiler test yet, perform it now.
1480  if(NOT MPI_${LANG}_TRIED_IMPLICIT)
1481  _MPI_create_imported_target(${LANG})
1482  _MPI_check_lang_works(${LANG} TRUE)
1483  endif()
1484 
1485  # Should the MPI compiler not work implicitly for MPI, still interrogate it.
1486  # Otherwise, MPI compilers for which CMake has separate linking stages, e.g. Intel MPI on Windows where link.exe is being used
1487  # directly during linkage instead of CMAKE_<LANG>_COMPILER will not work.
1488  if(NOT MPI_${LANG}_WORKS)
1489  set(MPI_${LANG}_WORKS_IMPLICIT FALSE)
1490  _MPI_interrogate_compiler(${LANG})
1491  else()
1492  set(MPI_${LANG}_WORKS_IMPLICIT TRUE)
1493  endif()
1494  elseif(MPI_${LANG}_COMPILER)
1495  _MPI_interrogate_compiler(${LANG})
1496  endif()
1497  endif()
1498 
1499  if(NOT MPI_PINNED_COMPILER AND NOT MPI_${LANG}_WRAPPER_FOUND)
1500  # If MPI_PINNED_COMPILER wasn't given, and the MPI compiler we potentially found didn't work, we withdraw it.
1501  set(MPI_${LANG}_COMPILER "MPI_${LANG}_COMPILER-NOTFOUND" CACHE FILEPATH "MPI compiler for ${LANG}" FORCE)
1502  if(NOT MPI_SKIP_GUESSING)
1503  # 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
1504  # settings for C. An MPI distribution that is in this situation would be IBM Platform MPI.
1505  if("${LANG}" STREQUAL "CXX" AND MPI_C_WRAPPER_FOUND)
1506  set(MPI_${LANG}_COMPILE_OPTIONS ${MPI_C_COMPILE_OPTIONS} CACHE STRING "MPI ${LANG} compilation options" )
1507  set(MPI_${LANG}_COMPILE_DEFINITIONS ${MPI_C_COMPILE_DEFINITIONS} CACHE STRING "MPI ${LANG} compilation definitions" )
1508  set(MPI_${LANG}_COMPILER_INCLUDE_DIRS ${MPI_C_INCLUDE_DIRS} CACHE STRING "MPI ${LANG} compiler wrapper include directories")
1509  set(MPI_${LANG}_LINK_FLAGS ${MPI_C_LINK_FLAGS} CACHE STRING "MPI ${LANG} linker flags" )
1510  set(MPI_${LANG}_LIB_NAMES ${MPI_C_LIB_NAMES} CACHE STRING "MPI ${LANG} libraries to link against" )
1511  else()
1512  _MPI_guess_settings(${LANG})
1513  endif()
1514  endif()
1515  endif()
1516  endif()
1517  endif()
1518 
1519  if(NOT "${MPI_${LANG}_COMPILER}" STREQUAL "${CMAKE_${LANG}_COMPILER}")
1520  _MPI_split_include_dirs(${LANG})
1521  _MPI_assemble_include_dirs(${LANG})
1522  else()
1523  set(MPI_${LANG}_INCLUDE_DIRS "")
1524  endif()
1525  _MPI_assemble_libraries(${LANG})
1526 
1527  _MPI_adjust_compile_definitions(${LANG})
1528  # We always create imported targets even if they're empty
1529  _MPI_create_imported_target(${LANG})
1530 
1531  if(NOT MPI_${LANG}_WORKS)
1532  _MPI_check_lang_works(${LANG} FALSE)
1533  endif()
1534 
1535  # Next, we'll initialize the MPI variables that have not been previously set.
1536  set(MPI_${LANG}_COMPILE_OPTIONS "" CACHE STRING "MPI ${LANG} compilation flags" )
1537  set(MPI_${LANG}_COMPILE_DEFINITIONS "" CACHE STRING "MPI ${LANG} compilation definitions" )
1538  set(MPI_${LANG}_COMPILER_INCLUDE_DIRS "" CACHE STRING "MPI ${LANG} compiler wrapper include directories")
1539  set(MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS "" CACHE STRING "MPI ${LANG} additional include directories")
1540  set(MPI_${LANG}_LINK_FLAGS "" CACHE STRING "MPI ${LANG} linker flags" )
1541  if(NOT MPI_${LANG}_COMPILER STREQUAL CMAKE_${LANG}_COMPILER)
1542  set(MPI_${LANG}_LIB_NAMES "" CACHE STRING "MPI ${LANG} libraries to link against" )
1543  endif()
1544  mark_as_advanced(
1545  MPI_${LANG}_COMPILE_OPTIONS
1546  MPI_${LANG}_COMPILE_DEFINITIONS
1547  MPI_${LANG}_LINK_FLAGS
1548  MPI_${LANG}_LIB_NAMES
1549  MPI_${LANG}_COMPILER_INCLUDE_DIRS
1550  MPI_${LANG}_ADDITIONAL_INCLUDE_DIRS
1551  MPI_${LANG}_COMPILER
1552  )
1553 
1554  # If we've found MPI, then we'll perform additional analysis: Determine the MPI version, MPI library version, supported
1555  # MPI APIs (i.e. MPI-2 C++ bindings). For Fortran we also need to find specific parameters if we're under MPI-3.
1556  if(MPI_${LANG}_WORKS)
1557  if("${LANG}" STREQUAL "CXX" AND NOT DEFINED MPI_MPICXX_FOUND)
1558  if(NOT MPI_CXX_SKIP_MPICXX AND NOT MPI_CXX_VALIDATE_SKIP_MPICXX)
1559  _MPI_try_staged_settings(${LANG} test_mpi MPICXX FALSE FALSE)
1560  if(MPI_RESULT_${LANG}_test_mpi_MPICXX)
1561  set(MPI_MPICXX_FOUND TRUE)
1562  else()
1563  set(MPI_MPICXX_FOUND FALSE)
1564  endif()
1565  else()
1566  set(MPI_MPICXX_FOUND FALSE)
1567  endif()
1568  endif()
1569 
1570  # At this point, we know the bindings present but not the MPI version or anything else.
1571  if(NOT DEFINED MPI_${LANG}_VERSION)
1572  unset(MPI_${LANG}_VERSION_MAJOR)
1573  unset(MPI_${LANG}_VERSION_MINOR)
1574  endif()
1575  set(MPI_BIN_FOLDER ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/FindMPI)
1576 
1577  # For Fortran, we'll want to use the most modern MPI binding to test capabilities other than the
1578  # Fortran parameters, since those depend on the method of consumption.
1579  # For C++, we can always use the C bindings, and should do so, since the C++ bindings do not exist in MPI-3
1580  # whereas the C bindings do, and the C++ bindings never offered any feature advantage over their C counterparts.
1581  if("${LANG}" STREQUAL "Fortran")
1582  if(MPI_${LANG}_HAVE_F08_MODULE)
1583  set(MPI_${LANG}_HIGHEST_METHOD F08_MODULE)
1584  elseif(MPI_${LANG}_HAVE_F90_MODULE)
1585  set(MPI_${LANG}_HIGHEST_METHOD F90_MODULE)
1586  else()
1587  set(MPI_${LANG}_HIGHEST_METHOD F77_HEADER)
1588  endif()
1589 
1590  # Another difference between C and Fortran is that we can't use the preprocessor to determine whether MPI_VERSION
1591  # and MPI_SUBVERSION are provided. These defines did not exist in MPI 1.0 and 1.1 and therefore might not
1592  # exist. For C/C++, test_mpi.c will handle the MPI_VERSION extraction, but for Fortran, we need mpiver.f90.
1593  if(NOT DEFINED MPI_${LANG}_VERSION)
1594  _MPI_try_staged_settings(${LANG} mpiver ${MPI_${LANG}_HIGHEST_METHOD} FALSE FALSE)
1595  if(MPI_RESULT_${LANG}_mpiver_${MPI_${LANG}_HIGHEST_METHOD})
1596  file(STRINGS ${MPI_BIN_FOLDER}/mpiver_${LANG}.bin _MPI_VERSION_STRING LIMIT_COUNT 1 REGEX "INFO:MPI-VER")
1597  if("${_MPI_VERSION_STRING}" MATCHES ".*INFO:MPI-VER\\[([0-9]+)\\.([0-9]+)\\].*")
1598  set(MPI_${LANG}_VERSION_MAJOR "${CMAKE_MATCH_1}")
1599  set(MPI_${LANG}_VERSION_MINOR "${CMAKE_MATCH_2}")
1600  set(MPI_${LANG}_VERSION "${MPI_${LANG}_VERSION_MAJOR}.${MPI_${LANG}_VERSION_MINOR}")
1601  endif()
1602  endif()
1603  endif()
1604 
1605  # Finally, we want to find out which capabilities a given interface supports, compare the MPI-3 standard.
1606  # This is determined by interface specific parameters MPI_SUBARRAYS_SUPPORTED and MPI_ASYNC_PROTECTS_NONBLOCKING
1607  # and might vary between the different methods of consumption.
1608  if(MPI_DETERMINE_Fortran_CAPABILITIES AND NOT MPI_Fortran_CAPABILITIES_DETERMINED)
1609  foreach(mpimethod IN ITEMS F08_MODULE F90_MODULE F77_HEADER)
1610  if(MPI_${LANG}_HAVE_${mpimethod})
1611  set(MPI_${LANG}_${mpimethod}_SUBARRAYS FALSE)
1612  set(MPI_${LANG}_${mpimethod}_ASYNCPROT FALSE)
1613  _MPI_try_staged_settings(${LANG} fortranparam_mpi ${mpimethod} TRUE FALSE)
1614  if(MPI_RESULT_${LANG}_fortranparam_mpi_${mpimethod} AND
1615  NOT "${MPI_RUN_RESULT_${LANG}_fortranparam_mpi_${mpimethod}}" STREQUAL "FAILED_TO_RUN")
1616  if("${MPI_RUN_OUTPUT_${LANG}_fortranparam_mpi_${mpimethod}}" MATCHES
1617  ".*INFO:SUBARRAYS\\[ *([TF]) *\\]-ASYNCPROT\\[ *([TF]) *\\].*")
1618  if("${CMAKE_MATCH_1}" STREQUAL "T")
1619  set(MPI_${LANG}_${mpimethod}_SUBARRAYS TRUE)
1620  endif()
1621  if("${CMAKE_MATCH_2}" STREQUAL "T")
1622  set(MPI_${LANG}_${mpimethod}_ASYNCPROT TRUE)
1623  endif()
1624  endif()
1625  endif()
1626  endif()
1627  endforeach()
1628  set(MPI_Fortran_CAPABILITIES_DETERMINED TRUE)
1629  endif()
1630  else()
1631  set(MPI_${LANG}_HIGHEST_METHOD normal)
1632 
1633  # By the MPI-2 standard, MPI_VERSION and MPI_SUBVERSION are valid for both C and C++ bindings.
1634  if(NOT DEFINED MPI_${LANG}_VERSION)
1635  file(STRINGS ${MPI_BIN_FOLDER}/test_mpi_${LANG}.bin _MPI_VERSION_STRING LIMIT_COUNT 1 REGEX "INFO:MPI-VER")
1636  if("${_MPI_VERSION_STRING}" MATCHES ".*INFO:MPI-VER\\[([0-9]+)\\.([0-9]+)\\].*")
1637  set(MPI_${LANG}_VERSION_MAJOR "${CMAKE_MATCH_1}")
1638  set(MPI_${LANG}_VERSION_MINOR "${CMAKE_MATCH_2}")
1639  set(MPI_${LANG}_VERSION "${MPI_${LANG}_VERSION_MAJOR}.${MPI_${LANG}_VERSION_MINOR}")
1640  endif()
1641  endif()
1642  endif()
1643 
1644  unset(MPI_BIN_FOLDER)
1645 
1646  # At this point, we have dealt with determining the MPI version and parameters for each Fortran method available.
1647  # The one remaining issue is to determine which MPI library is installed.
1648  # Determining the version and vendor of the MPI library is only possible via MPI_Get_library_version() at runtime,
1649  # and therefore we cannot do this while cross-compiling (a user may still define MPI_<lang>_LIBRARY_VERSION_STRING
1650  # themselves and we'll attempt splitting it, which is equivalent to provide the try_run output).
1651  # It's also worth noting that the installed version string can depend on the language, or on the system the binary
1652  # runs on if MPI is not statically linked.
1653  if(MPI_DETERMINE_LIBRARY_VERSION AND NOT MPI_${LANG}_LIBRARY_VERSION_STRING)
1654  _MPI_try_staged_settings(${LANG} libver_mpi ${MPI_${LANG}_HIGHEST_METHOD} TRUE FALSE)
1655  if(MPI_RESULT_${LANG}_libver_mpi_${MPI_${LANG}_HIGHEST_METHOD} AND
1656  "${MPI_RUN_RESULT_${LANG}_libver_mpi_${MPI_${LANG}_HIGHEST_METHOD}}" EQUAL "0")
1657  string(STRIP "${MPI_RUN_OUTPUT_${LANG}_libver_mpi_${MPI_${LANG}_HIGHEST_METHOD}}"
1658  MPI_${LANG}_LIBRARY_VERSION_STRING)
1659  else()
1660  set(MPI_${LANG}_LIBRARY_VERSION_STRING "NOTFOUND")
1661  endif()
1662  endif()
1663  endif()
1664 
1665  set(MPI_${LANG}_FIND_QUIETLY ${MPI_FIND_QUIETLY})
1666  set(MPI_${LANG}_FIND_VERSION ${MPI_FIND_VERSION})
1667  set(MPI_${LANG}_FIND_VERSION_EXACT ${MPI_FIND_VERSION_EXACT})
1668 
1669  unset(MPI_${LANG}_REQUIRED_VARS)
1670  if (NOT "${MPI_${LANG}_COMPILER}" STREQUAL "${CMAKE_${LANG}_COMPILER}")
1671  foreach(mpilibname IN LISTS MPI_${LANG}_LIB_NAMES)
1672  list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${mpilibname}_LIBRARY")
1673  endforeach()
1674  list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${LANG}_LIB_NAMES")
1675  if("${LANG}" STREQUAL "Fortran")
1676  # For Fortran we only need one of the module or header directories to have *some* support for MPI.
1677  if(NOT MPI_${LANG}_MODULE_DIR)
1678  list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${LANG}_F77_HEADER_DIR")
1679  endif()
1680  if(NOT MPI_${LANG}_F77_HEADER_DIR)
1681  list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${LANG}_MODULE_DIR")
1682  endif()
1683  else()
1684  list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${LANG}_HEADER_DIR")
1685  endif()
1686  if(MPI_${LANG}_ADDITIONAL_INCLUDE_VARS)
1687  foreach(mpiincvar IN LISTS MPI_${LANG}_ADDITIONAL_INCLUDE_VARS)
1688  list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${mpiincvar}_INCLUDE_DIR")
1689  endforeach()
1690  endif()
1691  # Append the works variable now. If the settings did not work, this will show up properly.
1692  list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${LANG}_WORKS")
1693  else()
1694  # If the compiler worked implicitly, use its path as output.
1695  # Should the compiler variable be set, we also require it to work.
1696  list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${LANG}_COMPILER")
1697  if(MPI_${LANG}_COMPILER)
1698  list(APPEND MPI_${LANG}_REQUIRED_VARS "MPI_${LANG}_WORKS")
1699  endif()
1700  endif()
1701  if (CMAKE_VERSION VERSION_LESS "3.17")
1702  set(_MPI_name_mismatched)
1703  else ()
1704  set(_MPI_name_mismatched NAME_MISMATCHED)
1705  endif ()
1706  find_package_handle_standard_args(MPI_${LANG} ${_MPI_name_mismatched}
1707  REQUIRED_VARS ${MPI_${LANG}_REQUIRED_VARS}
1708  VERSION_VAR MPI_${LANG}_VERSION)
1709  unset(_MPI_name_mismatched)
1710 
1711  if(DEFINED MPI_${LANG}_VERSION)
1712  if(NOT _MPI_MIN_VERSION OR _MPI_MIN_VERSION VERSION_GREATER MPI_${LANG}_VERSION)
1713  set(_MPI_MIN_VERSION MPI_${LANG}_VERSION)
1714  endif()
1715  endif()
1716  endif()
1717 endforeach()
1718 
1719 unset(_MPI_REQ_VARS)
1720 foreach(LANG IN ITEMS C CXX Fortran)
1721  if((NOT MPI_FIND_COMPONENTS AND CMAKE_${LANG}_COMPILER_LOADED) OR LANG IN_LIST MPI_FIND_COMPONENTS)
1722  list(APPEND _MPI_REQ_VARS "MPI_${LANG}_FOUND")
1723  endif()
1724 endforeach()
1725 
1726 if(MPICXX IN_LIST MPI_FIND_COMPONENTS)
1727  list(APPEND _MPI_REQ_VARS "MPI_MPICXX_FOUND")
1728 endif()
1729 
1730 find_package_handle_standard_args(MPI
1731  REQUIRED_VARS ${_MPI_REQ_VARS}
1732  VERSION_VAR ${_MPI_MIN_VERSION}
1733  HANDLE_COMPONENTS)
1734 
1735 #=============================================================================
1736 # More backward compatibility stuff
1737 
1738 # For compatibility reasons, we also define MPIEXEC
1739 set(MPIEXEC "${MPIEXEC_EXECUTABLE}")
1740 
1741 # Copy over MPI_<LANG>_INCLUDE_PATH from the assembled INCLUDE_DIRS.
1742 foreach(LANG IN ITEMS C CXX Fortran)
1743  if(MPI_${LANG}_FOUND)
1744  set(MPI_${LANG}_INCLUDE_PATH "${MPI_${LANG}_INCLUDE_DIRS}")
1745  unset(MPI_${LANG}_COMPILE_FLAGS)
1746  if(MPI_${LANG}_COMPILE_OPTIONS)
1747  list(JOIN MPI_${LANG}_COMPILE_OPTIONS " " MPI_${LANG}_COMPILE_FLAGS)
1748  endif()
1749  if(MPI_${LANG}_COMPILE_DEFINITIONS)
1750  foreach(_MPI_DEF IN LISTS MPI_${LANG}_COMPILE_DEFINITIONS)
1751  string(APPEND MPI_${LANG}_COMPILE_FLAGS " -D${_MPI_DEF}")
1752  endforeach()
1753  endif()
1754  endif()
1755 endforeach()
1756 
1757 # Bare MPI sans ${LANG} vars are set to CXX then C, depending on what was found.
1758 # This mimics the behavior of the old language-oblivious FindMPI.
1759 set(_MPI_OLD_VARS COMPILER INCLUDE_PATH COMPILE_FLAGS LINK_FLAGS LIBRARIES)
1760 if (MPI_CXX_FOUND)
1761  foreach (var ${_MPI_OLD_VARS})
1762  set(MPI_${var} ${MPI_CXX_${var}})
1763  endforeach()
1764 elseif (MPI_C_FOUND)
1765  foreach (var ${_MPI_OLD_VARS})
1766  set(MPI_${var} ${MPI_C_${var}})
1767  endforeach()
1768 endif()
1769 
1770 # Chop MPI_LIBRARIES into the old-style MPI_LIBRARY and MPI_EXTRA_LIBRARY, and set them in cache.
1771 if (MPI_LIBRARIES)
1772  list(GET MPI_LIBRARIES 0 MPI_LIBRARY_WORK)
1773  set(MPI_LIBRARY "${MPI_LIBRARY_WORK}")
1774  unset(MPI_LIBRARY_WORK)
1775 else()
1776  set(MPI_LIBRARY "MPI_LIBRARY-NOTFOUND")
1777 endif()
1778 
1779 list(LENGTH MPI_LIBRARIES MPI_NUMLIBS)
1780 if (MPI_NUMLIBS GREATER 1)
1781  set(MPI_EXTRA_LIBRARY_WORK "${MPI_LIBRARIES}")
1782  list(REMOVE_AT MPI_EXTRA_LIBRARY_WORK 0)
1783  set(MPI_EXTRA_LIBRARY "${MPI_EXTRA_LIBRARY_WORK}")
1784  unset(MPI_EXTRA_LIBRARY_WORK)
1785 else()
1786  set(MPI_EXTRA_LIBRARY "MPI_EXTRA_LIBRARY-NOTFOUND")
1787 endif()
1788 set(MPI_IGNORE_LEGACY_VARIABLES TRUE)
1789 #=============================================================================
1790 
1791 # unset these vars to cleanup namespace
1792 unset(_MPI_OLD_VARS)
1793 unset(_MPI_PREFIX_PATH)
1794 unset(_MPI_BASE_DIR)
1795 foreach (lang C CXX Fortran)
1796  unset(_MPI_${LANG}_COMPILER_NAMES)
1797 endforeach()
1798 
1799 cmake_policy(POP)
function _MPI_check_compiler(LANG, QUERY_FLAG, OUTPUT_VARIABLE, RESULT_VARIABLE)
Definition: FindMPI.cmake:350
location
component
order
on
macro _MPI_env_unset_ifnot(VAR)
Definition: FindMPI.cmake:389
function _MPI_guess_settings(LANG)
Definition: FindMPI.cmake:857
version
info
function _MPI_try_staged_settings(LANG, MPI_TEST_FILE_NAME, MODE, RUN_BINARY, SUPPRESS_ERRORS)
Definition: FindMPI.cmake:1183
string
language
lang
mode
function _MPI_adjust_compile_definitions(LANG)
Definition: FindMPI.cmake:1050
enabled
value
macro _MPI_assemble_libraries(LANG)
Definition: FindMPI.cmake:1064
macro step(n, desc)