# WRF-CMake (https://github.com/WRF-CMake/wps).
# Copyright 2018 M. Riechert and D. Meyer. Licensed under the MIT License.

# Identical to wrf_enable_mpi in the WRF repository (WRFHelpers.cmake)
function(wps_enable_mpi target language)
    if (NOT ENABLE_MPI)
        return()
    endif()
    get_target_property(old_link_flags ${target} LINK_FLAGS)
    if (old_link_flags)
        set(link_flags "${old_link_flags}")
    else()
        set(link_flags "")
    endif()
    if (language STREQUAL "Fortran")
        target_link_libraries(${target} ${MPI_Fortran_LIBRARIES})
        set(link_flags "${link_flags} ${MPI_Fortran_LINK_FLAGS}")
        target_include_directories(${target} PRIVATE ${MPI_Fortran_INCLUDE_PATH})
    elseif (language STREQUAL "C")
        target_link_libraries(${target} ${MPI_C_LIBRARIES})
        set(link_flags "${link_flags} ${MPI_C_LINK_FLAGS}")
        target_include_directories(${target} PRIVATE ${MPI_C_INCLUDE_PATH})
    else()
        message(FATAL_ERROR "Unsupported MPI language: ${language}")
    endif()
    set_target_properties(${target} PROPERTIES LINK_FLAGS "${link_flags}")
endfunction()