# Set up MPI

# Summary of CMake flags and compile definitions:
#
# CMake flags:
#  MPI_INCLUDE_PATH: directory of MPI headers
#  MPI_LIBRARIES: libraries of MPI
#
# Compile definitions:
#  WITH_MPI: whether to enable MPI support

message(STATUS "")
find_package(MPI REQUIRED)

message(STATUS "")
if(MPI_FOUND)
    message(STATUS "  MPI support enabled")
    # MPICC
    message(STATUS "    MPI C Version: " ${MPI_C_VERSION})
    message(STATUS "    MPI C library: ")
    foreach(__lib ${MPI_C_LIBRARIES})
        message(STATUS "      " ${__lib})
    endforeach()

    # MPICXX
    message(STATUS "    MPI CXX Version: " ${MPI_CXX_VERSION})
    message(STATUS "    MPI CXX library: ")
    foreach(__lib ${MPI_CXX_LIBRARIES})
        message(STATUS "      " ${__lib})
    endforeach()

    # MPIRUN
    message(STATUS "    MPI executables: " ${MPIEXEC_EXECUTABLE})
else()
	message(FATAL_ERROR "  MPI support not found")
endif()
message(STATUS "")

# Compile definitions and header directories should be defined PUBLIC,
# otherwise there may be inconsistencies in headers, especially for C++
# which uses templates.
function(tbp_set_mpi arg)
    target_compile_definitions(${arg} PUBLIC WITH_MPI)
    target_include_directories(${arg} PUBLIC ${MPI_INCLUDE_PATH})
    target_link_libraries(${arg} PUBLIC ${MPI_LIBRARIES})
endfunction()
