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

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.

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.

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

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")

If you’re going to do that, you should just use get_filename_component.

1 Like

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

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

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

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

cmake_path(GET CMAKE_CXX_COMPILER FILENAME CXX_COMPILER_NAME)