Files
2026-04-26 16:35:12 +00:00

95 lines
2.9 KiB
Bash

#!/usr/bin/env bash
# cxos/vendor/llama-cpp/build.sh — build the pinned llama.cpp for CxLLM-Arch.
# shellcheck disable=SC2012
#
# Prerequisites:
# * fetch.sh has run and verified the source under src/llama.cpp-<ver>/
# * Host has cmake, ninja-build, gcc (and optional Vulkan/CUDA/HIP/OpenCL
# SDKs for the corresponding backends).
#
# Usage:
# build.sh # CPU only
# build.sh --backend vulkan # +Vulkan
# build.sh --backend cuda # +CUDA
# build.sh --backend hip # +ROCm/HIP
# build.sh --backend opencl # +OpenCL
# build.sh --backend vulkan --backend cuda # multiple
# build.sh --dry # print plan; no compile
#
# Outputs (relative to repo root):
# dist/cxllm-arch/llama-cpp/include/...
# dist/cxllm-arch/llama-cpp/lib/libllama.a
# dist/cxllm-arch/llama-cpp/lib/libggml.a
set -euo pipefail
BACKENDS=()
DRY=0
JOBS="${JOBS:-$(nproc 2>/dev/null || echo 2)}"
while (( "$#" )); do
case "$1" in
--backend) BACKENDS+=("$2"); shift 2 ;;
--dry) DRY=1; shift ;;
--jobs) JOBS="$2"; shift 2 ;;
-h|--help)
sed -n '2,18p' "$0" | sed 's/^# \{0,1\}//'
exit 0 ;;
*) echo "build.sh: unknown arg: $1" >&2; exit 2 ;;
esac
done
HERE="$(cd "$(dirname "$0")" && pwd)"
REPO_ROOT="$(cd "$HERE/../../.." && pwd)"
PINNED="$HERE/PINNED.json"
VERSION="$(python3 -c 'import json,sys;print(json.load(open(sys.argv[1]))["version"])' "$PINNED")"
EXTRACTED_DIR="$(python3 -c 'import json,sys;print(json.load(open(sys.argv[1]))["extracted_dir"])' "$PINNED")"
SRC="$HERE/src/$EXTRACTED_DIR"
OUT="$REPO_ROOT/dist/cxllm-arch/llama-cpp"
CMAKE_FLAGS=(
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX="$OUT"
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DLLAMA_BUILD_TESTS=OFF
-DLLAMA_BUILD_EXAMPLES=OFF
-DLLAMA_BUILD_SERVER=OFF
)
for b in "${BACKENDS[@]:-}"; do
case "$b" in
cpu) ;;
vulkan) CMAKE_FLAGS+=(-DGGML_VULKAN=ON) ;;
cuda) CMAKE_FLAGS+=(-DGGML_CUDA=ON) ;;
hip|rocm) CMAKE_FLAGS+=(-DGGML_HIP=ON) ;;
opencl) CMAKE_FLAGS+=(-DGGML_OPENCL=ON) ;;
"") ;;
*) echo "build.sh: unknown backend: $b" >&2; exit 2 ;;
esac
done
echo "==> llama.cpp ${VERSION}"
echo " src : ${SRC}"
echo " out : ${OUT}"
echo " flags : ${CMAKE_FLAGS[*]}"
echo " jobs : ${JOBS}"
if [[ "$DRY" == "1" ]]; then
echo "==> dry-run; not compiling"
exit 0
fi
if [[ ! -d "$SRC" ]]; then
echo "build.sh: source not extracted; run fetch.sh first" >&2
exit 1
fi
BUILD="$SRC/build"
mkdir -p "$BUILD" "$OUT"
cmake -S "$SRC" -B "$BUILD" -G Ninja "${CMAKE_FLAGS[@]}"
cmake --build "$BUILD" --parallel "$JOBS"
cmake --install "$BUILD"
echo "==> built: $OUT/lib/$(ls "$OUT/lib" 2>/dev/null | head -1 || echo '<install>')"