cmake_minimum_required(VERSION 3.24)
project(lumi-whisper-worker VERSION 0.1.0 LANGUAGES CXX)

include(FetchContent)
option(LUMI_WHISPER_CUDA "Build whisper.cpp with CUDA support" OFF)

set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
set(WHISPER_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(WHISPER_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(WHISPER_BUILD_SERVER OFF CACHE BOOL "" FORCE)
set(GGML_CUDA ${LUMI_WHISPER_CUDA} CACHE BOOL "" FORCE)
# Build a portable AVX2 host path instead of inheriting the build machine's
# AVX-512 features. The GPU package must run on ordinary RTX 3060-era hosts.
set(GGML_NATIVE OFF CACHE BOOL "" FORCE)
set(GGML_AVX ON CACHE BOOL "" FORCE)
set(GGML_AVX2 ON CACHE BOOL "" FORCE)
set(GGML_AVX512 OFF CACHE BOOL "" FORCE)
if(LUMI_WHISPER_CUDA)
  # The RTX 3060 production baseline and RTX 3080 Ti development host are
  # both Ampere (SM 8.6). Include PTX for forward compatibility as well.
  set(LUMI_CUDA_ARCHITECTURES "86-real;86-virtual" CACHE STRING "CUDA architectures included in the portable worker")
  set(CMAKE_CUDA_ARCHITECTURES "${LUMI_CUDA_ARCHITECTURES}" CACHE STRING "" FORCE)
endif()

FetchContent_Declare(
  whisper
  GIT_REPOSITORY https://github.com/ggml-org/whisper.cpp.git
  GIT_TAG f049fff95a089aa9969deb009cdd4892b3e74916
  GIT_SHALLOW FALSE
)
FetchContent_Declare(
  json
  URL https://github.com/nlohmann/json/releases/download/v3.12.0/json.tar.xz
  URL_HASH SHA256=42f6e95cad6ec532fd372391373363b62a14af6d771056dbfc86160e6dfff7aa
)
FetchContent_MakeAvailable(whisper json)

add_executable(lumi-whisper-worker src/main.cpp)
target_compile_features(lumi-whisper-worker PRIVATE cxx_std_20)
target_link_libraries(lumi-whisper-worker PRIVATE whisper nlohmann_json::nlohmann_json)
if(LUMI_WHISPER_CUDA)
  target_compile_definitions(lumi-whisper-worker PRIVATE LUMI_BACKEND_CUDA=1)
endif()
if(MSVC)
  target_compile_options(lumi-whisper-worker PRIVATE /W4 /EHsc)
else()
  target_compile_options(lumi-whisper-worker PRIVATE -Wall -Wextra -Wpedantic)
endif()
install(TARGETS lumi-whisper-worker RUNTIME DESTINATION bin)
