# How to detect whether the current compiler has mpi support builtin?

**URL:** <https://discourse.cmake.org/t/how-to-detect-whether-the-current-compiler-has-mpi-support-builtin/5416>\
**Category:** Code\
**Created:** [April 9, 2022, 10:25am UTC](https://discourse.cmake.org/t/how-to-detect-whether-the-current-compiler-has-mpi-support-builtin/5416 "2022-04-09T10:25:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hzhangxyz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hzhangxyz/32/2323_2.png) [@hzhangxyz](https://discourse.cmake.org/u/hzhangxyz)\
**Post date:** [April 9, 2022, 10:25am UTC](https://discourse.cmake.org/t/how-to-detect-whether-the-current-compiler-has-mpi-support-builtin/5416/1 "2022-04-09T10:25:56Z")

</div>

I want to check if the given `CMAKE_CXX_COMPILER` has mpi support builtin, such as mpicc, with which there is no need to add include path or link library manually.

I used `if(CMAKE_CXX_COMPILER MATCHES "mpi")` before. However, I just find that intel’s new compiler has the location `/opt/intel/oneapi/compiler/latest/linux/bin/icx`, which matches string “mpi” in “compiler”, but does not have mpi builtin.

So I want to know what is the standard to way to detect it.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [April 9, 2022, 12:29pm UTC](https://discourse.cmake.org/t/how-to-detect-whether-the-current-compiler-has-mpi-support-builtin/5416/2 "2022-04-09T12:29:09Z")

</div>

I think one should just use `FindMPI` to find MPI. If the compiler comes with it, hopefully it doesn’t have to do much work.

---

<div class="post-metadata">

**Author:** ![hzhangxyz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hzhangxyz/32/2323_2.png) [@hzhangxyz](https://discourse.cmake.org/u/hzhangxyz)\
**Post date:** [April 9, 2022, 12:53pm UTC](https://discourse.cmake.org/t/how-to-detect-whether-the-current-compiler-has-mpi-support-builtin/5416/3 "2022-04-09T12:53:44Z")

</div>

If there is no better solution. I will use this way:

```auto
string(REPLACE "/" ";" CMAKE_CXX_COMPILER_PATH ${CMAKE_CXX_COMPILER})
list(GET CMAKE_CXX_COMPILER_PATH -1 CMAKE_CXX_COMPILER_NAME)
if(CMAKE_CXX_COMPILER_NAME MATCHES "mpi")

```

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [April 9, 2022, 12:56pm UTC](https://discourse.cmake.org/t/how-to-detect-whether-the-current-compiler-has-mpi-support-builtin/5416/4 "2022-04-09T12:56:51Z")

</div>

If you’re going to do that, you should just use [`get_filename_component`](https://cmake.org/cmake/help/latest/command/get_filename_component.html).

---

<div class="post-metadata">

**Author:** ![hzhangxyz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hzhangxyz/32/2323_2.png) [@hzhangxyz](https://discourse.cmake.org/u/hzhangxyz)\
**Post date:** [April 9, 2022, 1:05pm UTC](https://discourse.cmake.org/t/how-to-detect-whether-the-current-compiler-has-mpi-support-builtin/5416/5 "2022-04-09T13:05:43Z")

</div>

Ooops, Thank you very much. it looks much pretty.

For people who comes here in the future: At last I use this

```auto
get_filename_component(CXX_COMPILER_NAME ${CMAKE_CXX_COMPILER} NAME)
if(CXX_COMPILER_NAME MATCHES "mpi")

```

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [April 9, 2022, 9:19pm UTC](https://discourse.cmake.org/t/how-to-detect-whether-the-current-compiler-has-mpi-support-builtin/5416/6 "2022-04-09T21:19:08Z")

</div>

FYI, `get_filename_component()` is superseded by `cmake_path()`:

```auto
cmake_path(GET CMAKE_CXX_COMPILER FILENAME CXX_COMPILER_NAME)

```
