# Standalone, CI-friendly build for the FunASR llama.cpp runtime.
#
# Fetches a pinned llama.cpp (which provides ggml + llama) and builds the runtime
# binaries against it — no manual copying into a llama.cpp checkout, no hardcoded
# paths. Linux / macOS / Windows, x64 / arm64. Static libs -> self-contained binaries.
#
#   cmake -B build -DCMAKE_BUILD_TYPE=Release
#   cmake --build build -j               # binaries land in build/bin/
#
# Build against a local checkout instead of fetching:
#   -DFETCHCONTENT_SOURCE_DIR_LLAMA=/path/to/llama.cpp
cmake_minimum_required(VERSION 3.16)
project(funasr-llamacpp-sensevoice CXX C)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)   # static deps -> self-contained binaries
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "" FORCE)
endif()

include(FetchContent)
set(LLAMA_BUILD_TESTS    OFF CACHE BOOL "" FORCE)
set(LLAMA_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(LLAMA_BUILD_TOOLS    OFF CACHE BOOL "" FORCE)
set(LLAMA_BUILD_SERVER   OFF CACHE BOOL "" FORCE)
set(LLAMA_CURL           OFF CACHE BOOL "" FORCE)
FetchContent_Declare(llama
    GIT_REPOSITORY https://github.com/ggml-org/llama.cpp.git
    GIT_TAG        8086439a4cea94c71a5dfb8fe4ad1546aebd640f)
FetchContent_MakeAvailable(llama)

find_package(Threads REQUIRED)
set(FUNASR_COMMON ${CMAKE_CURRENT_SOURCE_DIR}/funasr-common)

function(funasr_add name src)
    add_executable(${name} ${src})
    target_include_directories(${name} PRIVATE ${FUNASR_COMMON})
    target_link_libraries(${name} PRIVATE ${ARGN} Threads::Threads)
    install(TARGETS ${name} RUNTIME DESTINATION bin)
endfunction()

funasr_add(llama-funasr-sensevoice funasr-sensevoice/funasr-sensevoice.cpp ggml)
funasr_add(llama-funasr-vad funasr-vad/funasr-vad.cpp ggml)
funasr_add(sensevoice-server sensevoice-server/sensevoice-server.cpp ggml)

enable_testing()
add_executable(server-utils-test tests/server_utils_test.cpp)
target_include_directories(server-utils-test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/sensevoice-server)
target_link_libraries(server-utils-test PRIVATE Threads::Threads)
set_target_properties(server-utils-test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tests)
add_test(NAME server-utils COMMAND server-utils-test)