cmake_minimum_required(VERSION 3.20)
project(searchi_gemma_worker LANGUAGES C CXX)
if(NOT DEFINED LLAMA_CPP_DIR OR NOT EXISTS "${LLAMA_CPP_DIR}/include/llama.h")
    message(FATAL_ERROR "Supply -DLLAMA_CPP_DIR=<pinned patched llama.cpp b11461 source>")
endif()
set(CMAKE_CXX_STANDARD 17)
if(MSVC)
    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>" CACHE STRING "Standalone worker static MSVC runtime" FORCE)
endif()
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Static local worker runtime" FORCE)
set(GGML_OPENMP OFF CACHE BOOL "Use bounded native llama thread pools without external OpenMP runtime" FORCE)
set(GGML_NATIVE OFF CACHE BOOL "Portable CPU kernels")
# GPU backends are explicit production build options. The Windows VM builds
# with all of these OFF; runtime device selection always retains CPU fallback.
set(GGML_CUDA OFF CACHE BOOL "Production CUDA backend")
set(GGML_VULKAN OFF CACHE BOOL "Production Vulkan backend")
set(GGML_METAL OFF CACHE BOOL "Production Metal backend")
set(SEARCHI_ACCELERATION "cpu" CACHE STRING "cpu, auto, metal, vulkan, or cuda")
if(SEARCHI_ACCELERATION STREQUAL "cpu")
    set(GGML_CUDA OFF CACHE BOOL "CPU test build" FORCE)
    set(GGML_VULKAN OFF CACHE BOOL "CPU test build" FORCE)
    set(GGML_METAL OFF CACHE BOOL "CPU test build" FORCE)
elseif(SEARCHI_ACCELERATION STREQUAL "metal" OR (SEARCHI_ACCELERATION STREQUAL "auto" AND APPLE))
    set(GGML_METAL ON CACHE BOOL "Production Metal backend" FORCE)
elseif(SEARCHI_ACCELERATION STREQUAL "cuda")
    set(GGML_CUDA ON CACHE BOOL "Production CUDA backend" FORCE)
elseif(SEARCHI_ACCELERATION STREQUAL "vulkan")
    set(GGML_VULKAN ON CACHE BOOL "Production Vulkan backend" FORCE)
elseif(SEARCHI_ACCELERATION STREQUAL "auto")
    find_package(Vulkan QUIET)
    if(Vulkan_FOUND)
        set(GGML_VULKAN ON CACHE BOOL "Available production Vulkan backend" FORCE)
    else()
        message(STATUS "No production acceleration SDK detected; retaining CPU backend")
    endif()
else()
    message(FATAL_ERROR "Unknown SEARCHI_ACCELERATION=${SEARCHI_ACCELERATION}")
endif()
set(LLAMA_BUILD_COMMON OFF CACHE BOOL "No extra common runtime" FORCE)
set(LLAMA_BUILD_TOOLS OFF CACHE BOOL "No runtime tools" FORCE)
set(LLAMA_BUILD_SERVER OFF CACHE BOOL "No HTTP server" FORCE)
set(LLAMA_BUILD_EXAMPLES OFF CACHE BOOL "No examples" FORCE)
set(LLAMA_BUILD_TESTS OFF CACHE BOOL "No upstream tests in worker build" FORCE)
set(LLAMA_BUILD_MTMD OFF CACHE BOOL "Text worker; media encoder follows separately" FORCE)
set(LLAMA_OPENSSL OFF CACHE BOOL "No network runtime dependencies" FORCE)
add_subdirectory("${LLAMA_CPP_DIR}" llama-build)
add_executable(searchi-gemma-worker worker.cpp)
target_include_directories(searchi-gemma-worker PRIVATE "${LLAMA_CPP_DIR}/vendor")
target_link_libraries(searchi-gemma-worker PRIVATE llama)
target_compile_definitions(searchi-gemma-worker PRIVATE SEARCHI_LLAMA_TAG="b11461-no-logits-v1")
install(TARGETS searchi-gemma-worker RUNTIME DESTINATION bin)
