# Copyright 2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

ROCM_VERSION="10.0"

if [[ ${PV} == 9999 ]]; then
	# The web UI's npm dependencies change with every commit, so the live
	# ebuild installs them from the checked-out lockfile in src_unpack
	NPM_PKGS=""
else
	# Generated from tools/ui/package-lock.json by scripts/npm-deps.py
	NPM_PKGS=""
	NPM_STUB_PKGS=""
	XDNA_COMMIT=""
fi

inherit cmake cuda flag-o-matic linux-info npm rocm
[[ ${PV} == 9999 ]] && inherit git-r3

DESCRIPTION="Port of Facebook's LLaMA model in C/C++, with its web UI"
HOMEPAGE="https://github.com/ggml-org/llama.cpp"
if [[ ${PV} == 9999 ]]; then
	EGIT_REPO_URI="https://github.com/ggml-org/llama.cpp.git"
else
	SRC_URI="
		https://github.com/ggml-org/llama.cpp/archive/refs/tags/v${PV}.tar.gz -> ${P}.gh.tar.gz
		xdna? (
			https://github.com/FireBurn/ggml-xdna1/archive/${XDNA_COMMIT}.tar.gz
				-> ggml-xdna1-${XDNA_COMMIT:0:10}.gh.tar.gz
		)
		${NPM_PKG_URIS}
	"
	S="${WORKDIR}/llama.cpp-${PV}"
fi

LICENSE="MIT"
# Web UI npm package licenses
LICENSE+=" 0BSD Apache-2.0 BSD BSD-2 CC0-1.0 ISC MIT MPL-2.0 Unlicense"
SLOT="0"
[[ ${PV} == 9999 ]] || KEYWORDS="~amd64"
IUSE="curl openblas +openmp blis hip cuda opencl vulkan xdna"
RESTRICT="mirror"

BDEPEND="
	${NPM_NODE_DEPEND}
	virtual/pkgconfig
"

CDEPEND="
	curl? ( net-misc/curl:= )
	openblas? ( sci-libs/openblas:= )
	openmp? ( llvm-runtimes/openmp:= )
	blis? ( sci-libs/blis:= )
	hip? ( >=dev-util/hip-10.0:= >=sci-libs/hipBLAS-10.0:= )
	cuda? ( dev-util/nvidia-cuda-toolkit:= )
"
DEPEND="${CDEPEND}
	opencl? ( dev-util/opencl-headers )
	vulkan? ( dev-util/vulkan-headers )
"
RDEPEND="${CDEPEND}
	dev-python/numpy
	opencl? ( dev-libs/opencl-icd-loader )
	vulkan? ( media-libs/vulkan-loader )
"

PATCHES=(
	"${FILESDIR}/0001-vulkan-handle-IQ4_XS-mat-vec-with-more-than-2-column.patch"
	"${FILESDIR}/0002-vulkan-share-the-K-V-pass-between-query-rows-in-coop.patch"
	"${FILESDIR}/0003-qwen35-optional-reduced-vocab-LM-head-for-MTP-drafti.patch"
	"${FILESDIR}/0004-vulkan-use-4-rows-for-legacy-quant-mat-vec-from-2-co.patch"
	"${FILESDIR}/0005-hip-use-short-vectors-for-bf16-WMMA-builtins-with-LL.patch"
	"${FILESDIR}/0006-server-keep-speculative-checkpoints-on-device.patch"
	"${FILESDIR}/0007-hip-keep-MMQ-WMMA-k-loops-rolled-to-avoid-VGPR-spill.patch"
	"${FILESDIR}/0008-cuda-use-8-lanes-per-state-column-in-gated-delta-net.patch"
	"${FILESDIR}/0009-hip-share-the-FA-vec-K-V-pass-between-the-Q-heads-of.patch"
	"${FILESDIR}/0010-vulkan-write-gated-delta-net-state-straight-into-the.patch"
	"${FILESDIR}/0011-vulkan-use-a-subgroup-reduction-in-rms_norm-and-keep.patch"
	"${FILESDIR}/0012-vulkan-dual-issue-friendly-int8-epilogue-in-cm1-mmq-.patch"
	"${FILESDIR}/0013-vulkan-keep-Q-fragments-in-registers-in-coopmat1-fla.patch"
	"${FILESDIR}/0014-vulkan-store-the-flash-attention-quantized-K-V-scrat.patch"
	"${FILESDIR}/0015-vulkan-concat-with-a-transposed-src1-through-shared-.patch"
	"${FILESDIR}/0016-vulkan-load-16-values-per-thread-for-q4_K-q5_K-q6_K-.patch"
	"${FILESDIR}/0017-vulkan-stage-large-writes-to-host-visible-VRAM-throu.patch"
	"${FILESDIR}/0018-vulkan-index-iq4-mxfp4-codebooks-as-a-constant-array.patch"
	"${FILESDIR}/0019-vulkan-use-the-float-mat-vec-for-single-tokens-with-.patch"
	"${FILESDIR}/0020-vulkan-hoist-loop-invariants-in-the-Q4_K-and-Q5_K-in.patch"
	"${FILESDIR}/0021-vulkan-keep-P-transposed-in-shared-memory-for-the-co.patch"
	"${FILESDIR}/0022-hip-allow-host-constexpr-use-of-physical-warp-size.patch"
)

pkg_setup() {
	if use hip || use xdna; then
		linux-info_pkg_setup
	fi
	if use hip && linux-info_get_any_version && linux_config_exists; then
		linux_chkconfig_present HSA_AMD_SVM ||
			ewarn "ROCm/HIP requires CONFIG_HSA_AMD_SVM in the kernel."
	fi
	if use xdna; then
		if linux-info_get_any_version && linux_config_exists; then
			if ! linux_chkconfig_present DRM_AMDXDNA; then
				ewarn "To use the XDNA1 backend, you likely need the AMD XDNA DRM driver enabled"
				ewarn "(CONFIG_DRM_AMDXDNA) or built out-of-tree."
			fi
		fi
	fi
}

src_unpack() {
	if [[ ${PV} == 9999 ]]; then
		git-r3_src_unpack
		if use xdna; then
			local xdna_uri="https://github.com/FireBurn/ggml-xdna1.git"
			git-r3_fetch "${xdna_uri}"
			git-r3_checkout "${xdna_uri}" "${WORKDIR}/ggml-xdna1"
		fi

		# A live ebuild may use the network here; the build then runs offline
		pushd "${S}/tools/ui" >/dev/null || die
		HOME="${T}/npm-home" npm ci --ignore-scripts --no-audit --no-fund \
			--cache "${T}/npm-cache" || die "npm ci failed"
		popd >/dev/null || die
	else
		npm_src_unpack
		if use xdna; then
			mv "${WORKDIR}/ggml-xdna1-${XDNA_COMMIT}" "${WORKDIR}/ggml-xdna1" || die
		fi
	fi
}

src_prepare() {
	use cuda && cuda_src_prepare

	if use xdna; then
		# Fix hardcoded library installation path to match llama.cpp's isolation
		# so `libggml-xdna1.so` ends up in `/usr/$(get_libdir)/llama.cpp/` instead of `/usr/lib/`
		sed -i -e "s|DESTINATION lib|DESTINATION $(get_libdir)/llama.cpp|g" \
			"${WORKDIR}/ggml-xdna1/CMakeLists.txt" || die

		# ggml added alloc_buffer_n and get_alloc_size_n to the buffer type
		# interface; the backend's positional initialiser predates them.
		if ! grep -q 'alloc_buffer_n' "${WORKDIR}/ggml-xdna1/src/ggml-xdna1.cpp"; then
			sed -i \
				-e '/\/\* \.alloc_buffer   = \*\/ xdna1_buft_alloc,/a\        /* .alloc_buffer_n   = */ NULL,' \
				-e '/\/\* \.get_alloc_size = \*\/ NULL,/a\        /* .get_alloc_size_n = */ NULL,' \
				"${WORKDIR}/ggml-xdna1/src/ggml-xdna1.cpp" || die
		fi
	fi

	cmake_src_prepare
}

src_configure() {
	# Force enable the Web UI macros
	append-cppflags -DLLAMA_BUILD_UI=1 -DLLAMA_BUILD_WEBUI=1

	local mycmakeargs=(
		-DLLAMA_BUILD_TESTS=OFF
		-DLLAMA_BUILD_SERVER=ON
		-DLLAMA_BUILD_UI=ON
		# Point CMake to the directory where we will build the UI assets
		-DUI_SOURCE_DIR="${S}/tools/ui"
		-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON
		-DGGML_NATIVE=0
		-DGGML_RPC=ON
		-DLLAMA_CURL=$(usex curl ON OFF)
		-DBUILD_NUMBER="1"
		-DGGML_CUDA=$(usex cuda ON OFF)
		-DGGML_OPENCL=$(usex opencl ON OFF)
		-DGGML_OPENMP=$(usex openmp ON OFF)
		-DGGML_VULKAN=$(usex vulkan ON OFF)

		# avoid clashing with whisper.cpp
		-DCMAKE_INSTALL_LIBDIR="${EPREFIX}/usr/$(get_libdir)/llama.cpp"
		-DCMAKE_INSTALL_RPATH="${EPREFIX}/usr/$(get_libdir)/llama.cpp"
	)

	if use openblas ; then
		mycmakeargs+=( -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS )
	fi

	if use blis ; then
		mycmakeargs+=( -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=FLAME )
	fi

	if use hip; then
		rocm_use_hipcc
		mycmakeargs+=( -DGGML_HIP=ON -DAMDGPU_TARGETS="$(get_amdgpu_flags)" )
	fi

	if use cuda; then
		local -x CUDAHOSTCXX="$(cuda_gccdir)"
		cuda_add_sandbox
		addpredict "/dev/char/"
	fi

	# Configure llama.cpp
	local CMAKE_USE_DIR="${S}"
	local BUILD_DIR="${WORKDIR}/${P}_build"
	cmake_src_configure

	# Configure ggml-xdna1
	if use xdna; then
		einfo "Configuring ggml-xdna1 backend..."
		local CMAKE_USE_DIR="${WORKDIR}/ggml-xdna1"
		local BUILD_DIR="${WORKDIR}/ggml-xdna1_build"
		local mycmakeargs=(
			-DGGML_SOURCE_DIR="${S}/ggml"
			-DCMAKE_INSTALL_PREFIX="${EPREFIX}/usr"
		)
		cmake_src_configure
	fi
}

src_compile() {
	# 1. Build the Web UI assets from source
	einfo "Building Web UI assets using npm..."
	pushd "${S}/tools/ui" > /dev/null || die

	# Releases install the UI's dependencies from the offline registry
	if [[ ${PV} != 9999 ]]; then
		npm_with_registry npm ci --ignore-scripts
	fi
	# Generates the dist/ directory CMake embeds
	HOME="${T}/npm-home" npm run build --offline || die

	popd > /dev/null || die

	# 2. Run the standard C++ build for llama.cpp
	local CMAKE_USE_DIR="${S}"
	local BUILD_DIR="${WORKDIR}/${P}_build"
	cmake_src_compile

	# 3. Build ggml-xdna1
	if use xdna; then
		einfo "Building ggml-xdna1 backend..."
		local CMAKE_USE_DIR="${WORKDIR}/ggml-xdna1"
		local BUILD_DIR="${WORKDIR}/ggml-xdna1_build"
		cmake_src_compile
	fi
}

src_install() {
	# 1. Install llama.cpp
	local CMAKE_USE_DIR="${S}"
	local BUILD_DIR="${WORKDIR}/${P}_build"
	cmake_src_install
	dobin "${BUILD_DIR}/bin/ggml-rpc-server"

	# 2. Install ggml-xdna1
	if use xdna; then
		einfo "Installing ggml-xdna1 backend..."
		local CMAKE_USE_DIR="${WORKDIR}/ggml-xdna1"
		local BUILD_DIR="${WORKDIR}/ggml-xdna1_build"
		cmake_src_install
	fi
}

pkg_postinst() {
	if use xdna; then
		elog ""
		elog "You have installed the AMD Ryzen AI NPU backend (ggml-xdna1)."
		elog "Requirements for hardware offloading:"
		elog "  1. You must load the 'amdxdna' Linux DRM driver."
		elog "  2. You must raise RLIMIT_MEMLOCK (e.g. ulimit -l unlimited or via pam_limits)."
		elog "     Without this, the device heap allocation will fail with EAGAIN."
		elog "  3. Use '--device XDNA1 -ngl 0' to run models on the NPU."
		elog "  4. Set 'export GGML_BACKEND_PATH=/usr/$(get_libdir)/llama.cpp/libggml-xdna1.so'"
		elog "     so the plugin is properly dynamically loaded by llama-cli or llama-server."
		elog ""
	fi
}