# libvkserve_engine: the multi-sequence engine, loaded by src/vkserve/engine.py through ctypes.
# It builds compute-ml from source (COMPUTE_ML_DIR, default ../compute-ml next to this repo) and
# compiles compute-ml's LLM core (examples/llm) into the same library.
cmake_minimum_required(VERSION 3.21)
project(vkserve_engine LANGUAGES C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

set(COMPUTE_ML_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../compute-ml" CACHE PATH "compute-ml source tree")
get_filename_component(COMPUTE_ML_DIR "${COMPUTE_ML_DIR}" ABSOLUTE)
if(NOT EXISTS "${COMPUTE_ML_DIR}/examples/llm/llm.c")
    message(FATAL_ERROR "compute-ml not found at ${COMPUTE_ML_DIR}; pass -DCOMPUTE_ML_DIR=<path>")
endif()

# The engine calls the multi-sequence KV API of compute-ml's LLM core. A checkout older than
# 37bb499 (compute-ml 0.4.54) lacks it and fails with nine implicit-declaration errors.
file(READ "${COMPUTE_ML_DIR}/examples/llm/llm.h" CML_LLM_HEADER)
foreach(sym cml_llm_forward_batch cml_llm_kv_create cml_llm_kv_free cml_llm_kv_len
            cml_llm_kv_reset cml_llm_kv_truncate)
    string(FIND "${CML_LLM_HEADER}" "${sym}" found)
    if(found EQUAL -1)
        message(FATAL_ERROR
            "compute-ml at ${COMPUTE_ML_DIR} is too old: examples/llm/llm.h has no ${sym}. "
            "vkserve needs compute-ml 37bb499 (0.4.54) or newer. "
            "Update it: git -C ${COMPUTE_ML_DIR} pull")
    endif()
endforeach()
add_subdirectory("${COMPUTE_ML_DIR}" compute-ml EXCLUDE_FROM_ALL)
find_package(Vulkan REQUIRED)

add_library(vkserve_engine SHARED engine.c devices.c
            "${COMPUTE_ML_DIR}/examples/llm/llm.c" "${COMPUTE_ML_DIR}/examples/whisper/json.c")
target_include_directories(vkserve_engine PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
                           "${COMPUTE_ML_DIR}/examples/llm" "${COMPUTE_ML_DIR}/examples/whisper")
target_link_libraries(vkserve_engine PRIVATE compute_ml Vulkan::Vulkan)
if(NOT MSVC)
    target_link_libraries(vkserve_engine PRIVATE m)
endif()
set_target_properties(vkserve_engine PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
