# Real IBackend implementations - reached when SCAVENGER_BUILD_BACKENDS
# and/or SCAVENGER_BUILD_IMAGE_GEN is ON (see root CMakeLists.txt and
# core/CMakeLists.txt), since these need the OpenVINO C++ SDK,
# llama.cpp-Vulkan, and/or the OpenVINO GenAI C++ SDK, exactly like
# core/tools/'s Phase 1 smoke tests (see docs/DEVELOPMENT.md for what to
# install first). The two flags are independent: a text-gen-only build
# only needs SCAVENGER_BUILD_BACKENDS; SCAVENGER_BUILD_IMAGE_GEN alone
# builds just openvino_image_backend.cpp.
#
# Sources are added directly onto the existing scavenger_core target
# (rather than a separate library) so callers - the CLI demos and the
# pybind11 module - link against one thing.

if(SCAVENGER_BUILD_BACKENDS)
    find_package(OpenVINO REQUIRED)

    target_sources(scavenger_core PRIVATE
        openvino_backend.cpp
        llama_vulkan_backend.cpp
        llama_chat_session.cpp
    )
    target_link_libraries(scavenger_core PUBLIC openvino::runtime llama)

    # Phase 5, Tier 2: OpenVinoBackend's USM code path (see
    # openvino_backend.cpp's SCAVENGER_ENABLE_TIER2_USM #ifdef and
    # docs/tier2_spike_log.md). Only reachable when SCAVENGER_BUILD_BACKENDS
    # is also ON (needs OpenVinoBackend itself); the OpenCL::Headers/
    # OpenCL::HeadersCpp/OpenCL::OpenCL targets this links against are
    # fetched once at the root CMakeLists.txt level, shared with
    # SCAVENGER_BUILD_TIER2_SPIKE's usm_spike tool.
    if(SCAVENGER_ENABLE_TIER2_USM)
        target_sources(scavenger_core PRIVATE openvino_usm_pool.cpp)
        target_compile_definitions(scavenger_core PUBLIC SCAVENGER_ENABLE_TIER2_USM)
        target_link_libraries(scavenger_core PUBLIC OpenCL::Headers OpenCL::HeadersCpp OpenCL::OpenCL)
    endif()

    # Phase M1: LlamaCudaBackend (local.nvidia.dgpu0). SCAVENGER_BUILD_CUDA
    # is PUBLIC (not PRIVATE) so llama_cuda_backend.hpp's own
    # #ifdef SCAVENGER_BUILD_CUDA build guard (see that header) also sees
    # the definition from any translation unit that includes it (e.g.
    # bindings/pybind_module.cpp), not just this library's own .cpp files.
    if(SCAVENGER_BUILD_CUDA)
        target_sources(scavenger_core PRIVATE llama_cuda_backend.cpp)
        target_compile_definitions(scavenger_core PUBLIC SCAVENGER_BUILD_CUDA)
    endif()
endif()

if(SCAVENGER_BUILD_IMAGE_GEN)
    # OpenVINO GenAI's own archive bundles a full, self-contained OpenVINO
    # runtime + CMake config (see docs/DEVELOPMENT.md's "Image generation
    # (OpenVINO GenAI)" section) - point OpenVINOGenAI_DIR (and
    # OpenVINO_DIR, if SCAVENGER_BUILD_BACKENDS is also ON) at that
    # archive's runtime/cmake/ so both find_package() calls resolve to the
    # same, mutually-compatible OpenVINO build rather than two independent
    # OpenVINO installs whose ABIs may not match.
    find_package(OpenVINOGenAI REQUIRED)

    target_sources(scavenger_core PRIVATE
        openvino_image_backend.cpp
    )
    target_link_libraries(scavenger_core PUBLIC openvino::genai)
endif()
