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

EAPI=8

DISTUTILS_EXT=1
DISTUTILS_USE_PEP517=standalone
PYTHON_COMPAT=( python3_{12..14} )

inherit distutils-r1

MY_TAG="cuda-core-v${PV}"
# Gitlink of cuda_core/third_party/cccl at ${MY_TAG}: a CCCL 3.6.0 development
# commit. build_hooks.py always compiles against this pinned libcu++ and never
# the CTK's (CUDA 13.5 ships CCCL 3.5.0), so fetch the pin rather than unbundle.
# verified 2026-11-19
CCCL_COMMIT="ba8eb88fb78254a07112e60d4e74cac84038fac8"

DESCRIPTION="cuda.core: pythonic CUDA module"
HOMEPAGE="
	https://github.com/NVIDIA/cuda-python
	https://nvidia.github.io/cuda-python/cuda-core/
	https://pypi.org/project/cuda-core/
"

# cuda-core uses cuda-core-v<PV> monorepo tags, like cuda-pathfinder's
# namespaced tags; cuda-bindings uses bare v<PV>. # verified 2026-09-03
# Upstream's uploaded release tarball, not GitHub's generated tag archive:
# .git_archival.txt is export-subst and lists every ref on the commit (PR
# refs and release branches too), so the generated bytes drift. The release
# asset is not immutable (immutable: false; release-upload.yml uses
# --clobber), so the Manifest hash remains the pin. verified 2026-11-09
SRC_URI="
	https://github.com/NVIDIA/cuda-python/releases/download/${MY_TAG}/cuda-python-${MY_TAG}.tar.gz
		-> ${P}.tar.gz
	https://github.com/NVIDIA/cccl/archive/${CCCL_COMMIT}.tar.gz
		-> cccl-${CCCL_COMMIT}.tar.gz
"
S="${WORKDIR}/cuda-python-${MY_TAG}/cuda_core"

# libcu++ headers from CCCL are compiled into the extension modules.
LICENSE="Apache-2.0 Apache-2.0-with-LLVM-exceptions"
SLOT="0"
KEYWORDS="~amd64"

# build_hooks.py reads cuda.h through cuda.pathfinder and generates Cython.
# cuda-bindings is also a build dependency because .pyx files cimport its .pxd
# headers, despite upstream declaring it only in runtime extras. From 1.3.0
# the cu13 extra sets a cuda-bindings floor that build_hooks.py and the
# import-time check both enforce.
RDEPEND="
	>=dev-python/cuda-pathfinder-1.4.2[${PYTHON_USEDEP}]
	>=dev-python/cuda-bindings-13.5.0[${PYTHON_USEDEP}]
	dev-python/numpy[${PYTHON_USEDEP}]
	dev-util/nvidia-cuda-toolkit:=
"
DEPEND="${RDEPEND}"
BDEPEND="
	>=dev-python/cuda-pathfinder-1.5[${PYTHON_USEDEP}]
	>=dev-python/cuda-bindings-13.5.0[${PYTHON_USEDEP}]
	>=dev-python/cython-3.2.5[${PYTHON_USEDEP}]
	<dev-python/cython-3.3[${PYTHON_USEDEP}]
	>=dev-python/setuptools-scm-8[${PYTHON_USEDEP}]
	>=dev-python/setuptools-80[${PYTHON_USEDEP}]
"

src_unpack() {
	unpack "${P}.tar.gz"

	# build_hooks.py includes only libcudacxx/include from the submodule, and
	# checks for <cuda/tma> there; extract just that tree into the empty
	# gitlink directory.
	local dest="${S}/third_party/cccl"
	mkdir -p "${dest}" || die
	tar -xzf "${DISTDIR}/cccl-${CCCL_COMMIT}.tar.gz" -C "${dest}" \
		--strip-components=1 "cccl-${CCCL_COMMIT}/libcudacxx/include" || die
	[[ -f ${dest}/libcudacxx/include/cuda/tma ]] ||
		die "pinned CCCL lacks <cuda/tma>; build_hooks.py would reject it"
}

# build_hooks.py requires CUDA_HOME to locate cuda.h.
export CUDA_HOME=/opt/cuda

# build_hooks.py lints the handwritten C++ with the clang-tidy==22.1.8 wheel
# it lists in build-system.requires, or with ${sys.prefix}/bin/clang-tidy if
# the host happens to have one, and fails the build on its findings. A style
# check is not a build step; skip it. verified 2026-11-23
export CUDA_PYTHON_SKIP_CLANG_TIDY=1

# The archive lacks monorepo Git metadata, so provide the literal version.
# A v-prefixed value leaks into cuda.core.__version__ despite metadata
# normalization and breaks consumers. # verified 2026-06-10
export SETUPTOOLS_SCM_PRETEND_VERSION_FOR_CUDA_CORE="${PV}"