# libwalkenv_speech (walkenv_speech.dll on Windows): speech to text as a plugin (../speech_abi.h). Built by the main
# project as an ExternalProject (cmake/speech.cmake) so whisper.cpp and ggml stay out of the engine's own build: the
# runners carry no speech code; projects with speech off never load this, and their exports leave it out.
# Inputs: OUT_DIR (where the plugin goes), WHISPER_SOURCE (optional: a whisper.cpp checkout instead of the download).
# whisper.cpp is pinned to the release below (MIT); change it here and nowhere else.
cmake_minimum_required(VERSION 3.16)
project(walkenv_speech C CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_C_VISIBILITY_PRESET hidden)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
if(WALKENV_CL_JOBS)  # Visual Studio: compile in parallel like the main build (env/CMakeLists.txt)
  add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:/MP${WALKENV_CL_JOBS}>)
endif()
include(FetchContent)
if(POLICY CMP0135)
  cmake_policy(SET CMP0135 NEW)
endif()
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)

set(WALKENV_WHISPER_VERSION v1.9.5)
# whisper.cpp: the library only (no examples, tests, server), static; ggml's CPU backend built for a baseline CPU
# (GGML_NATIVE off: a plugin built here runs on any Mac / any x86-64 with AVX2), Metal on macOS with its shaders
# embedded (one file to ship), no OpenMP (no runtime library to carry).
foreach(off WHISPER_BUILD_TESTS WHISPER_BUILD_EXAMPLES WHISPER_BUILD_SERVER WHISPER_SDL2 WHISPER_CURL WHISPER_ALL_WARNINGS
            WHISPER_BUILD_IS_DEV GGML_NATIVE GGML_OPENMP GGML_BLAS GGML_LLAMAFILE GGML_CPU_KLEIDIAI GGML_AVX512)
  set(${off} OFF CACHE BOOL "" FORCE)
endforeach()
if(APPLE)
  set(GGML_METAL ON CACHE BOOL "" FORCE)
  set(GGML_METAL_EMBED_LIBRARY ON CACHE BOOL "" FORCE)
  set(GGML_ACCELERATE ON CACHE BOOL "" FORCE)
else()
  set(GGML_METAL OFF CACHE BOOL "" FORCE)
  set(GGML_ACCELERATE OFF CACHE BOOL "" FORCE)
endif()
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64|amd64")
  foreach(on GGML_AVX GGML_AVX2 GGML_FMA GGML_F16C)
    set(${on} ON CACHE BOOL "" FORCE)
  endforeach()
endif()
if(WHISPER_SOURCE)
  set(FETCHCONTENT_SOURCE_DIR_WHISPER ${WHISPER_SOURCE})
endif()
FetchContent_Declare(whisper URL https://github.com/ggml-org/whisper.cpp/archive/refs/tags/${WALKENV_WHISPER_VERSION}.tar.gz)
FetchContent_MakeAvailable(whisper)

add_library(walkenv_speech MODULE walkenv_speech.cpp)
target_link_libraries(walkenv_speech PRIVATE whisper)
target_compile_definitions(walkenv_speech PRIVATE WALKENV_WHISPER_VERSION="${WALKENV_WHISPER_VERSION}")
# libwalkenv_speech.dylib / libwalkenv_speech.so / walkenv_speech.dll (speech_plugin.cpp looks for these)
if(WIN32)
  set_target_properties(walkenv_speech PROPERTIES PREFIX "" SUFFIX .dll)
else()
  set_target_properties(walkenv_speech PROPERTIES PREFIX lib SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
endif()
if(OUT_DIR)
  set_target_properties(walkenv_speech PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${OUT_DIR})
  foreach(config Debug Release RelWithDebInfo MinSizeRel)
    string(TOUPPER ${config} CONFIG)
    set_target_properties(walkenv_speech PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${CONFIG} ${OUT_DIR})
  endforeach()
endif()
# Only walkenv_speech_open leaves the plugin: its ggml never meets anything else's.
if(APPLE)
  target_link_options(walkenv_speech PRIVATE -Wl,-exported_symbol,_walkenv_speech_open -Wl,-dead_strip)
elseif(MINGW)
  target_link_options(walkenv_speech PRIVATE -static -static-libgcc -static-libstdc++ -Wl,--exclude-all-symbols $<$<CONFIG:Release>:-s>)
elseif(UNIX)
  target_link_options(walkenv_speech PRIVATE -Wl,--exclude-libs,ALL -Wl,-Bsymbolic -Wl,--gc-sections)
endif()
