FindMPI.cmake 85 KB

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