include_guard(GLOBAL)

if(NOT WIN32 OR NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
    message(FATAL_ERROR "FESA dependencies require a Windows x64 build.")
endif()

set(MKL_ARCH intel64)
set(MKL_INTERFACE lp64)
set(MKL_LINK dynamic)
set(MKL_THREADING tbb_thread)

find_package(MKL CONFIG REQUIRED)
find_package(TBB CONFIG REQUIRED COMPONENTS tbb)
find_package(HDF5 CONFIG REQUIRED COMPONENTS C shared)

if(BUILD_TESTING)
    find_package(GTest CONFIG REQUIRED)
endif()

function(fesa_require_imported_target target_name)
    if(NOT TARGET "${target_name}")
        message(
            FATAL_ERROR
            "Required imported target '${target_name}' is unavailable. "
            "Verify the installed x64 package and its runtime DLLs."
        )
    endif()

    get_target_property(target_is_imported "${target_name}" IMPORTED)
    if(NOT target_is_imported)
        message(
            FATAL_ERROR
            "Dependency target '${target_name}' must be provided by an "
            "installed package."
        )
    endif()
endfunction()

fesa_require_imported_target(MKL::MKL)
fesa_require_imported_target(TBB::tbb)
fesa_require_imported_target(hdf5::hdf5-shared)

if(BUILD_TESTING)
    fesa_require_imported_target(GTest::gtest_main)
    fesa_require_imported_target(GTest::gtest)
    fesa_require_imported_target(GTest::gmock)
endif()

add_library(HDF5::HDF5 INTERFACE IMPORTED GLOBAL)
set_property(
    TARGET HDF5::HDF5
    PROPERTY INTERFACE_LINK_LIBRARIES hdf5::hdf5-shared
)

set(
    FESA_DEPENDENCY_RUNTIME_MODIFICATIONS
    "PATH=path_list_prepend:${MKL_DLL_DIR}"
    "PATH=path_list_prepend:$<TARGET_FILE_DIR:TBB::tbb>"
    "PATH=path_list_prepend:$<TARGET_FILE_DIR:hdf5::hdf5-shared>"
)
