include(GoogleTest)

add_executable(
    fesa_unit_tests
    unit/build_info_test.cpp
    unit/analysis/analysis_model_test.cpp
    unit/analysis/analysis_state_test.cpp
    unit/assembly/parallel_for_test.cpp
    unit/assembly/load_assembler_test.cpp
    unit/assembly/sparse_assembler_test.cpp
    unit/constraints/essential_constraints_test.cpp
    unit/core/diagnostic_test.cpp
    unit/core/source_identity_test.cpp
    unit/core/status_test.cpp
    unit/elements/euler_beam_3d_test.cpp
    unit/fem/dof_manager_test.cpp
    unit/math/matrix_test.cpp
    unit/math/sparse_matrix_test.cpp
    unit/math/vector_test.cpp
    unit/io/abaqus/domain_mapper_test.cpp
    unit/io/abaqus/input_reader_test.cpp
    unit/io/abaqus/input_syntax_test.cpp
    unit/io/hdf5/hdf5_results_writer_test.cpp
    unit/model/domain_test.cpp
    unit/model/model_types_test.cpp
    unit/results/result_records_test.cpp
    unit/results/result_recovery_test.cpp
    unit/results/results_writer_test.cpp
    unit/solvers/linear/linear_solver_test.cpp
    unit/solvers/linear/mkl_pardiso_solver_test.cpp
)

target_link_libraries(
    fesa_unit_tests
    PRIVATE
        fesa_solver
        Fesa::HDF5
        GTest::gtest_main
)

add_executable(
    fesa_integration_tests
    integration/analysis/linear_static_analysis_test.cpp
    integration/app/fesa_application_test.cpp
)

target_link_libraries(
    fesa_integration_tests
    PRIVATE
        fesa_solver
        Fesa::HDF5
        GTest::gtest_main
)

add_executable(
    fesa_reference_tests
    reference/reference_comparison.cpp
    reference/reference_comparison_test.cpp
    reference/b33_reference_comparison_test.cpp
)

target_link_libraries(
    fesa_reference_tests
    PRIVATE
        fesa_solver
        Fesa::HDF5
        GTest::gtest_main
)

target_compile_definitions(
    fesa_reference_tests
    PRIVATE
        FESA_TEST_SOURCE_DIR="${PROJECT_SOURCE_DIR}"
        FESA_TEST_BINARY_DIR="${PROJECT_BINARY_DIR}"
)

if(WIN32)
    if(NOT TARGET MKL::mkl_intel_thread OR NOT TARGET MKL::mkl_core OR
       NOT OMP_DLL_DIR OR NOT OMP_DLLNAME)
        message(FATAL_ERROR "The oneMKL runtime files required by tests were not resolved")
    endif()
    if(NOT EXISTS "${OMP_DLL_DIR}/libmmd.dll")
        message(FATAL_ERROR "The Intel runtime required by the selected HDF5 DLL was not resolved")
    endif()

    file(GLOB _fesa_mkl_dispatch_dlls "${MKL_ROOT}/bin/mkl_def.*.dll")
    if(NOT _fesa_mkl_dispatch_dlls)
        message(FATAL_ERROR "The oneMKL default dispatch runtime was not resolved")
    endif()
    list(SORT _fesa_mkl_dispatch_dlls COMPARE NATURAL ORDER DESCENDING)
    list(GET _fesa_mkl_dispatch_dlls 0 _fesa_mkl_dispatch_dll)

    function(fesa_stage_test_runtime target_name)
        # Stage dynamic backend dependencies before post-build GoogleTest
        # discovery; the exact validation commands do not assume oneAPI in PATH.
        add_custom_command(
            TARGET "${target_name}"
            POST_BUILD
            COMMAND "${CMAKE_COMMAND}" -E copy_if_different
                    "$<TARGET_FILE:TBB::tbb>"
                    "$<TARGET_FILE_DIR:${target_name}>"
            COMMAND "${CMAKE_COMMAND}" -E copy_if_different
                    "$<TARGET_FILE:MKL::mkl_intel_thread>"
                    "$<TARGET_FILE_DIR:${target_name}>"
            COMMAND "${CMAKE_COMMAND}" -E copy_if_different
                    "$<TARGET_FILE:MKL::mkl_core>"
                    "$<TARGET_FILE_DIR:${target_name}>"
            COMMAND "${CMAKE_COMMAND}" -E copy_if_different
                    "${OMP_DLL_DIR}/${OMP_DLLNAME}"
                    "$<TARGET_FILE_DIR:${target_name}>"
            COMMAND "${CMAKE_COMMAND}" -E copy_if_different
                    "${OMP_DLL_DIR}/libmmd.dll"
                    "$<TARGET_FILE_DIR:${target_name}>"
            COMMAND "${CMAKE_COMMAND}" -E copy_if_different
                    "${_fesa_mkl_dispatch_dll}"
                    "$<TARGET_FILE_DIR:${target_name}>"
            # Imported-target runtime closure includes the normalized HDF5 DLL.
            COMMAND "${CMAKE_COMMAND}" -E copy_if_different
                    "$<TARGET_RUNTIME_DLLS:${target_name}>"
                    "$<TARGET_FILE_DIR:${target_name}>"
            COMMAND_EXPAND_LISTS
        )
    endfunction()

    fesa_stage_test_runtime(fesa_unit_tests)
    fesa_stage_test_runtime(fesa_integration_tests)
    fesa_stage_test_runtime(fesa_reference_tests)

    unset(_fesa_mkl_dispatch_dll)
    unset(_fesa_mkl_dispatch_dlls)
endif()

gtest_discover_tests(
    fesa_unit_tests
    PROPERTIES
        LABELS "linear-static-3d-euler-beam;unit"
)

gtest_discover_tests(
    fesa_integration_tests
    PROPERTIES
        LABELS "linear-static-3d-euler-beam;integration"
)

gtest_discover_tests(
    fesa_reference_tests
    PROPERTIES
        LABELS "linear-static-3d-euler-beam;reference"
)

add_custom_target(
    fesa_tests
    DEPENDS
        fesa_unit_tests
        fesa_integration_tests
        fesa_reference_tests
)
