#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2015-2016, 2026 InterGenJLU
#
# llama-cpp b8796 — LLM inference engine
# https://github.com/ggml-org/llama.cpp
#
# Builds: llama-server (HTTP API), llama-cli, libllama.so, libggml.so
#
# CPU-only build (no GGML_VULKAN passed -> defaults OFF; matches the .192
# validated config). ISA floor is set explicitly for portability: a distro
# binary must NOT be compiled for the build VM's CPU. GGML_NATIVE defaults ON
# (ggml/CMakeLists.txt), which bakes in -march=native = the builder's full ISA
# (the Ryzen 9 5900X has AVX2/FMA/BMI2) -> SIGILL on any target lacking those.
# We instead pin GGML_NATIVE=OFF + an explicit AVX2 floor (x86-64-v3 class),
# the exact set the .192 `build-a12safe` config validated on the target A12
# (AMD A12-9720P, Excavator: avx2/fma/f16c/bmi2/abm all present). FMA/F16C ride
# along with AVX2 in ggml; AVX512 stays OFF. Floor = Haswell-2013+/Excavator.

configure() {
    set -e
    mkdir -p build
    cd    build

    cmake .. \
        -DCMAKE_INSTALL_PREFIX=/usr \
        -DCMAKE_BUILD_TYPE=Release \
        -DLLAMA_BUILD_SERVER=ON \
        -DLLAMA_CURL=ON \
        -DBUILD_SHARED_LIBS=ON \
        -DCMAKE_INSTALL_LIBDIR=lib \
        -DGGML_NATIVE=OFF \
        -DGGML_AVX=ON \
        -DGGML_AVX2=ON \
        -DCMAKE_POLICY_VERSION_MINIMUM=3.5
}

build() {
    set -e
    cd build
    make -j${IGOS_JOBS}
}

do_install() {
    set -e
    cd build
    make DESTDIR="$DESTDIR" install
}
