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

EAPI=8

ROCM_VERSION="7.2"

inherit cmake cuda linux-info rocm toolchain-funcs

REPO="https://github.com/handy-computer/transcribe.cpp"
MY_PN="${PN/-/.}"

if [[ ${PV} = *9999* ]]; then
	inherit git-r3
	EGIT_REPO_URI="${REPO}.git"
else
	SRC_URI="${REPO}/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz"
	KEYWORDS="~amd64 ~arm64"
	# the tarball extracts to transcribe.cpp-${PV}, not ${P}
	S="${WORKDIR}/transcribe.cpp-${PV}"
fi

DESCRIPTION="GGUF speech-to-text inference for 16+ model families"
HOMEPAGE="https://github.com/handy-computer/transcribe.cpp"
LICENSE="MIT"
SLOT="0"

# GGML CPU flags
X86_CPU_FLAGS=(
	amx_bf16
	amx_int8
	amx_tile
	avx
	avx2
	avx512_bf16
	avx512_vnni
	avx512f
	avx512vbmi
	avx_vnni
	bmi2
	f16c
	fma3
	sse4_2
)
CPU_FLAGS=( "${X86_CPU_FLAGS[@]/#/cpu_flags_x86_}" )

IUSE="cuda +examples hip openblas +openmp tools vulkan ${CPU_FLAGS[*]}"

unset X86_CPU_FLAGS CPU_FLAGS

# the tools link against the examples' common library
REQUIRED_USE="tools? ( examples )"

COMMON_DEPEND="
	cuda? ( dev-util/nvidia-cuda-toolkit:= )
	hip? (
		>=dev-util/hip-${ROCM_VERSION}:=
		>=sci-libs/hipBLAS-${ROCM_VERSION}:=
		>=sci-libs/rocBLAS-${ROCM_VERSION}:=
	)
	openblas? ( sci-libs/openblas:= )
	openmp? ( llvm-runtimes/openmp:= )
	vulkan? ( media-libs/vulkan-loader )
"
DEPEND="${COMMON_DEPEND}
	vulkan? (
		dev-util/spirv-headers
		dev-util/vulkan-headers
	)
"
RDEPEND="${COMMON_DEPEND}"
BDEPEND="
	vulkan? (
		dev-util/spirv-headers
		dev-util/vulkan-headers
		media-libs/shaderc
	)
"
DOCS=( CONTRIBUTING.md README.md THIRD-PARTY-LICENSES.md )

pkg_pretend() {
	[[ ${MERGE_TYPE} != binary ]] && use openmp && tc-check-openmp

	if use cuda && [[ -z ${CUDA_ARCHITECTURES} ]]; then
		einfo "CUDA_ARCHITECTURES wasn't set, so transcribe-cpp builds its default set of"
		einfo "CUDA compute capabilities (Maxwell through Blackwell, depending on the"
		einfo "CUDA toolkit version)."
		einfo "Set it to your GPU's compute capability to skip unused targets, e.g."
		einfo 'CUDA_ARCHITECTURES="75-real" (several are separated with ";": "75-real;80-real")'
		einfo "  find: nvidia-smi --query-gpu=compute_cap --format=csv,noheader"
		einfo "  suffixes: 75 (PTX+SASS), 75-real (SASS only), 75-virtual (PTX only)"
	fi
}

pkg_setup() {
	if use hip; then
		linux-info_pkg_setup
		if linux-info_get_any_version && linux_config_exists; then
			if ! linux_chkconfig_present HSA_AMD_SVM; then
				ewarn "To use ROCm/HIP, you need to have HSA_AMD_SVM option enabled in your kernel."
			fi
		fi
	fi

	[[ ${MERGE_TYPE} != binary ]] && use openmp && tc-check-openmp
}

src_prepare() {
	# inherit cuda leaves a src_prepare stub that runs cuda_sanitize
	# unconditionally (dying without the CUDA toolkit); defining it here
	# overrides the stub
	use cuda && cuda_src_prepare
	cmake_src_prepare
}

src_configure() {
	local mycmakeargs=(
		-DTRANSCRIBE_BUILD_EXAMPLES=$(usex examples)
		-DTRANSCRIBE_BUILD_TOOLS=$(usex tools)
		-DTRANSCRIBE_USE_OPENMP=$(usex openmp)
		-DTRANSCRIBE_USE_SYSTEM_BLAS=$(usex openblas)
		-DTRANSCRIBE_VULKAN=$(usex vulkan)
		-DTRANSCRIBE_CUDA=$(usex cuda)
		-DTRANSCRIBE_HIP=$(usex hip)
		# the uv-gated fixture tests would otherwise run "uv run" at build
		# time, which resolves a Python environment from the network; keep
		# the build hermetic by not registering them
		-DTRANSCRIBE_UV=uv-NOTFOUND

		# avoid clashing with sci-ml/ggml
		-DCMAKE_INSTALL_INCLUDEDIR="include/${MY_PN}"
		-DCMAKE_INSTALL_LIBDIR="$(get_libdir)/${MY_PN}"
		-DCMAKE_INSTALL_RPATH="\$ORIGIN/../$(get_libdir)/${MY_PN};\$ORIGIN"
		# some CMake/nvcc combos pass the host system includes of consumed
		# imported targets (CUDAToolkit) as -isystem, which puts /usr/include
		# ahead of the C++ standard headers and breaks gcc-16's
		# #include_next <math.h>
		-DCMAKE_NO_SYSTEM_FROM_IMPORTED=ON
	)

	# build the bundled ggml for the declared ISA extensions, not
	# -march=native
	mycmakeargs+=(
		-DGGML_NATIVE=OFF
		-DGGML_LTO="$(tc-is-lto && echo ON || echo OFF)"
		-DGGML_OPENMP=$(usex openmp)

		# CPU ISA flags
		-DGGML_SSE42=$(usex cpu_flags_x86_sse4_2)
		-DGGML_AVX=$(usex cpu_flags_x86_avx)
		-DGGML_AVX_VNNI=$(usex cpu_flags_x86_avx_vnni)
		-DGGML_AVX2=$(usex cpu_flags_x86_avx2)
		-DGGML_BMI2=$(usex cpu_flags_x86_bmi2)
		-DGGML_FMA=$(usex cpu_flags_x86_fma3)
		-DGGML_F16C=$(usex cpu_flags_x86_f16c)
		-DGGML_AVX512=$(usex cpu_flags_x86_avx512f)
		-DGGML_AVX512_VBMI=$(usex cpu_flags_x86_avx512vbmi)
		-DGGML_AVX512_VNNI=$(usex cpu_flags_x86_avx512_vnni)
		-DGGML_AVX512_BF16=$(usex cpu_flags_x86_avx512_bf16)
		-DGGML_AMX_TILE=$(usex cpu_flags_x86_amx_tile)
		-DGGML_AMX_INT8=$(usex cpu_flags_x86_amx_int8)
		-DGGML_AMX_BF16=$(usex cpu_flags_x86_amx_bf16)
	)

	if use cuda; then
		local -x CUDAHOSTCXX="$(cuda_gccdir)"
		cuda_add_sandbox
		addpredict "/dev/char/"
		# CUDA_ARCHITECTURES: restrict the compute capabilities (see
		# pkg_pretend); the bundled ggml otherwise uses its own default
		# arch list
		if [[ -n ${CUDA_ARCHITECTURES} ]]; then
			mycmakeargs+=( -DCMAKE_CUDA_ARCHITECTURES="${CUDA_ARCHITECTURES}" )
		fi
		# nvcc hard-fails on host compilers newer than its support table
		# (e.g. gcc 16 with CUDA 13); -allow-unsupported-compiler is a
		# no-op for supported compilers
		mycmakeargs+=( -DCMAKE_CUDA_FLAGS=-allow-unsupported-compiler )
	fi

	if use hip; then
		rocm_use_clang
		mycmakeargs+=(
			-DGPU_TARGETS="$(get_amdgpu_flags)"
			-DCMAKE_HIP_FLAGS="${CXXFLAGS}"
			# cmake.eclass clears per-build-type flags for C/CXX only;
			# drop CMake's "-O2 -g -DNDEBUG" for HIP
			"-DCMAKE_HIP_FLAGS_${CMAKE_BUILD_TYPE^^}="
		)
	fi

	cmake_src_configure
}

src_test() {
	# the ctest suite runs without model files (the real-model tests
	# stay off)
	cmake_src_test --output-on-failure
}