# Builds the unified gguf.cpp engine (LLM server + diffusion CLI + quantizer
# library) from the vendored source tree in ONE configure/build, and installs
# each artifact into the panel package that spawns/loads it:
#
#   gguf-server     -> src/gguf_cpp/server/bin      (spawned by the server panel)
#   diffusion       -> src/gguf_cpp/diffuser/bin    (spawned by the diffuser panel)
#   gguf_quantizer  -> src/gguf_cpp/editor/lib      (ctypes-loaded by the editor panel)
#
# The engine tree shares a single ggml kernels/ directory between all three, so
# the compute kernels compile once. Its own options are GGUF_CPP_*, so most of
# what this file does is shape that build for a wheel; see vendor/gguf.cpp/
# CMakeLists.txt for the engine itself and engine/README.md for how the tree is
# assembled.
cmake_minimum_required(VERSION 3.15)
project(gguf_cpp C CXX)

option(GGUF_CPP_BUILD "Build the unified gguf.cpp engine and install it alongside the python package" ON)

if(NOT GGUF_CPP_BUILD)
    return()
endif()

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()

# ── Locate the engine source tree ────────────────────────────────────────────
set(GGUF_CPP_ENGINE_DIR "" CACHE PATH "Path to the unified gguf.cpp engine source tree")
if(NOT GGUF_CPP_ENGINE_DIR AND DEFINED ENV{GGUF_CPP_ENGINE_DIR})
    set(GGUF_CPP_ENGINE_DIR "$ENV{GGUF_CPP_ENGINE_DIR}")
endif()
if(NOT GGUF_CPP_ENGINE_DIR AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/vendor/gguf.cpp/CMakeLists.txt")
    set(GGUF_CPP_ENGINE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/vendor/gguf.cpp")
endif()
if(NOT GGUF_CPP_ENGINE_DIR OR NOT EXISTS "${GGUF_CPP_ENGINE_DIR}/kernels/CMakeLists.txt")
    message(FATAL_ERROR
        "gguf.cpp engine source not found. Provide it via one of:\n"
        "  - the vendored tree at vendor/gguf.cpp (python scripts/vendor_engine.py)\n"
        "  - -DGGUF_CPP_ENGINE_DIR=/path/to/gguf.cpp or the same env var")
endif()
get_filename_component(GGUF_CPP_ENGINE_DIR "${GGUF_CPP_ENGINE_DIR}" ABSOLUTE)
message(STATUS "gguf-cpp: engine source at ${GGUF_CPP_ENGINE_DIR}")

# ── Option helper ────────────────────────────────────────────────────────────
# The engine declares its own GGUF_CPP_* options; declaring them here first
# wins, because option() leaves an existing cache entry alone. Values are read
# from the environment as well as -D, because `pip install` makes environment
# variables far easier to pass than CMake defines:
#   GGUF_CPP_CUDA=1 pip install .
#   CMAKE_ARGS="-DGGUF_CPP_CUDA=ON" pip install .
function(gguf_cpp_option name docstring default)
    if(DEFINED ENV{${name}} AND NOT "$ENV{${name}}" STREQUAL "")
        set(default "$ENV{${name}}")
    endif()
    option(${name} "${docstring}" "${default}")
endfunction()

# ── Components ───────────────────────────────────────────────────────────────
gguf_cpp_option(GGUF_CPP_SERVER    "Build the LLM server engine" ON)
gguf_cpp_option(GGUF_CPP_DIFFUSION "Build the diffusion engine"  ON)
gguf_cpp_option(GGUF_CPP_QUANTIZER "Build the quantizer engine"  ON)

# ── GPU / accelerator backends ───────────────────────────────────────────────
# One switch per backend drives the whole engine: the shared ggml kernels
# (server + diffusion) and, for CUDA/HIP/Metal, the quantizer's own device
# kernels. Anything else ggml supports can still be passed through as -DGGML_*.
gguf_cpp_option(GGUF_CPP_CUDA   "Build the engine with the CUDA backend"   OFF)
gguf_cpp_option(GGUF_CPP_HIP    "Build the engine with the HIP backend"    OFF)
gguf_cpp_option(GGUF_CPP_VULKAN "Build the engine with the Vulkan backend" OFF)
if(APPLE)  # matches the engine's own default; APPLE is "" elsewhere, not a valid default
    set(_gguf_metal_default ON)
else()
    set(_gguf_metal_default OFF)
endif()
gguf_cpp_option(GGUF_CPP_METAL  "Build the engine with the Metal backend"  ${_gguf_metal_default})
gguf_cpp_option(GGUF_CPP_BLAS   "Build the engine with the BLAS backend"   OFF)

# ── HTTPS / OpenSSL ──────────────────────────────────────────────────────────
# Off by default (the engine itself defaults it on). OpenSSL is only needed to
# *download* models over HTTPS (-hf / URL arguments); the panels always hand the
# engine local file paths, so the dependency buys nothing and costs portability:
# find_package(OpenSSL) happily picks up a foreign-ABI install — an MSYS2 /
# MinGW libcrypto next to an MSVC toolchain puts MinGW headers on cl.exe's
# include path and breaks httplib.cpp, download.cpp and hf-cache.cpp with
# "winnt.h: fatal error C1189: No supported target architecture".
gguf_cpp_option(GGUF_SERVER_OPENSSL
    "Link the LLM server against OpenSSL for HTTPS model downloads" OFF)

# Router mode (`--models`) spawns child servers via subprocess.h — harmless to
# keep and the GUI's custom-command box can use it.
gguf_cpp_option(GGUF_SERVER_SUBPROCESS
    "Support router mode (the LLM server spawns child servers)" ON)

# ── Windows / MinGW toolchain fixes ──────────────────────────────────────────
if(WIN32 AND NOT MSVC)
    # MinGW-w64's headers pin _WIN32_WINNT to a Windows 7-era value before the
    # engine gets a chance to set it; cpp-httplib then hard-errors and
    # ::CreateFile2 goes undeclared, which takes down httplib.cpp, download.cpp
    # and hf-cache.cpp. Set it up front, matching what the engine expects
    # everywhere else.
    add_compile_definitions(_WIN32_WINNT=0x0A00 WINVER=0x0A00 NTDDI_VERSION=0x0A000000)

    # A MinGW-built binary otherwise needs libstdc++-6.dll, libgcc_s_seh-1.dll
    # and libwinpthread-1.dll from the toolchain at run time. The wheel ships
    # self-contained binaries and Python spawns them without MSYS2 on PATH, so
    # link the runtime in.
    gguf_cpp_option(GGUF_CPP_MINGW_STATIC
        "Statically link the MinGW runtime into the engine binaries" ON)
    if(GGUF_CPP_MINGW_STATIC)
        add_link_options(-static-libgcc -static-libstdc++ -static)

        # ggml uses OpenMP, and CMake's FindOpenMP resolves it to
        # libgomp.dll.a — an import library that -static cannot override,
        # leaving the binary needing libgomp-1.dll. Point OpenMP at the static
        # archive instead; if there isn't one, drop OpenMP rather than ship a
        # half-static binary (ggml falls back to its own thread pool).
        if(NOT OpenMP_gomp_LIBRARY)
            set(_gguf_saved_suffixes ${CMAKE_FIND_LIBRARY_SUFFIXES})
            set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
            find_library(GGUF_CPP_LIBGOMP_STATIC NAMES gomp)
            set(CMAKE_FIND_LIBRARY_SUFFIXES ${_gguf_saved_suffixes})
            if(GGUF_CPP_LIBGOMP_STATIC)
                message(STATUS "gguf-cpp: static OpenMP runtime ${GGUF_CPP_LIBGOMP_STATIC}")
                set(OpenMP_gomp_LIBRARY "${GGUF_CPP_LIBGOMP_STATIC}"
                    CACHE FILEPATH "gguf-cpp: static OpenMP runtime" FORCE)
            else()
                message(STATUS "gguf-cpp: no static libgomp found, building without OpenMP")
                set(GGML_OPENMP OFF CACHE BOOL "gguf-cpp: keep the binaries self-contained" FORCE)
            endif()
        endif()
    endif()
endif()

# ── Shape the engine build for a single-file-per-artifact wheel ──────────────
# Static libs (ggml, llama, llama-common, mtmd, diffusion, …) linked straight
# into the executables keep the wheel to standalone binaries with no
# rpath/DLL handling. The quantizer stays a shared library by design (ctypes) —
# the engine sets CMAKE_POSITION_INDEPENDENT_CODE so it can link static ggml.
set(BUILD_SHARED_LIBS    OFF CACHE BOOL "gguf-cpp: static engine for single-file installs" FORCE)
set(SD_BUILD_SHARED_LIBS OFF CACHE BOOL "gguf-cpp: static diffusion library" FORCE)

# The engine's own install() rules would put artifacts in the wheel's data
# directory; ours below place them inside the package instead.
set(GGUF_CPP_INSTALL OFF CACHE BOOL "gguf-cpp: install rules are ours" FORCE)

set(GGUF_CPP_ENABLED_BACKENDS "")
foreach(backend IN ITEMS CUDA HIP VULKAN METAL BLAS)
    if(GGUF_CPP_${backend})
        list(APPEND GGUF_CPP_ENABLED_BACKENDS ${backend})
    endif()
endforeach()
if(GGUF_CPP_ENABLED_BACKENDS)
    string(REPLACE ";" ", " _backend_list "${GGUF_CPP_ENABLED_BACKENDS}")
    message(STATUS "gguf-cpp: accelerator backends requested: ${_backend_list}")
else()
    message(STATUS "gguf-cpp: CPU-only build (enable e.g. -DGGUF_CPP_CUDA=ON for GPU)")
endif()

# ── Embedded web UI ──────────────────────────────────────────────────────────
# The server engine embeds whatever static assets live in GGUF_SERVER_UI_DIR
# (default <engine>/llm/ui/dist). The tree ships none, so the engine is
# API-only — irrelevant here, because this package serves its own GUI.
if(NOT DEFINED CACHE{GGUF_SERVER_UI_DIR} AND DEFINED ENV{GGUF_SERVER_UI_DIR})
    set(GGUF_SERVER_UI_DIR "$ENV{GGUF_SERVER_UI_DIR}" CACHE PATH
        "directory with web UI assets to embed into the server engine")
endif()

# EXCLUDE_FROM_ALL keeps every target the engine defines out of the default
# build — the dependencies below pull in the three shipped artifacts and just
# the libraries they link against.
add_subdirectory("${GGUF_CPP_ENGINE_DIR}" "${CMAKE_BINARY_DIR}/gguf-cpp-engine" EXCLUDE_FROM_ALL)

add_custom_target(gguf_cpp_engine ALL)

if(GGUF_CPP_SERVER)
    if(NOT TARGET gguf-server)
        message(FATAL_ERROR "engine did not define the gguf-server target — is ${GGUF_CPP_ENGINE_DIR} the unified gguf.cpp source tree?")
    endif()
    add_dependencies(gguf_cpp_engine gguf-server)
endif()
if(GGUF_CPP_DIFFUSION)
    if(NOT TARGET diffusion-cli)
        message(FATAL_ERROR "engine did not define the diffusion-cli target")
    endif()
    add_dependencies(gguf_cpp_engine diffusion-cli)
endif()
if(GGUF_CPP_QUANTIZER)
    if(NOT TARGET gguf_quantizer)
        message(FATAL_ERROR "engine did not define the gguf_quantizer target")
    endif()
    add_dependencies(gguf_cpp_engine gguf_quantizer)
endif()

# ── Install into the panel packages ──────────────────────────────────────────
# Install into the wheel (SKBUILD_PLATLIB_DIR) and also into the source tree
# so editable installs (`pip install -e .`) find the artifacts — the same dual
# install the three standalone packages used.
set(_gguf_dests "${CMAKE_CURRENT_SOURCE_DIR}/src/gguf_cpp")
if(DEFINED SKBUILD_PLATLIB_DIR)
    list(APPEND _gguf_dests "${SKBUILD_PLATLIB_DIR}/gguf_cpp")
endif()

foreach(dest IN LISTS _gguf_dests)
    if(GGUF_CPP_SERVER)
        install(PROGRAMS $<TARGET_FILE:gguf-server> DESTINATION "${dest}/server/bin")
    endif()
    if(GGUF_CPP_DIFFUSION)
        install(PROGRAMS $<TARGET_FILE:diffusion-cli> DESTINATION "${dest}/diffuser/bin")
    endif()
    if(GGUF_CPP_QUANTIZER)
        install(TARGETS gguf_quantizer
            LIBRARY DESTINATION "${dest}/editor/lib"
            RUNTIME DESTINATION "${dest}/editor/lib"
            ARCHIVE DESTINATION "${dest}/editor/lib"
        )
    endif()
endforeach()
