FindMPI.cmake 94 KB

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