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

EAPI=8

DISTUTILS_USE_PEP517=setuptools
PYTHON_COMPAT=( python3_{12..14} )
DISTUTILS_SINGLE_IMPL=1

inherit distutils-r1 cuda

DESCRIPTION="Modular primitives for high-performance differentiable rendering"
HOMEPAGE="https://github.com/NVlabs/nvdiffrast"
SRC_URI="https://github.com/NVlabs/nvdiffrast/archive/refs/tags/v${PV}.tar.gz -> ${P}.gh.tar.gz"

LICENSE="NVIDIA-nvdiffrast"
SLOT="0"
KEYWORDS="~amd64"
# The non-commercial NVIDIA license forbids mirroring and binary redistribution.
RESTRICT="bindist mirror"

# 0.4.0 builds _nvdiffrast_c through torch cpp_extension; pin CC/CXX to the
# CUDA-compatible GCC. ninja remains needed for the JIT-built GL plugin.
RDEPEND="
	sci-ml/caffe2[${PYTHON_SINGLE_USEDEP}]
	app-alternatives/ninja
	$(python_gen_cond_dep '
		dev-python/numpy[${PYTHON_USEDEP}]
	')
"
DEPEND="${RDEPEND}"
BDEPEND="
	app-alternatives/ninja
	$(python_gen_cond_dep '
		>=dev-python/setuptools-64[${PYTHON_USEDEP}]
		dev-python/wheel[${PYTHON_USEDEP}]
	')
"

src_prepare() {
	distutils-r1_src_prepare
}

src_compile() {
	local gccdir
	gccdir=$(cuda_gccdir) || die
	export CC="${gccdir}/gcc" CXX="${gccdir}/g++"
	# Respect TORCH_CUDA_ARCH_LIST; otherwise target the visible GPU.
	if [[ -z ${TORCH_CUDA_ARCH_LIST} ]]; then
		cuda_add_sandbox -w
		local native_cc
		native_cc=$(__nvcc_device_query 2>/dev/null)
		if [[ ${native_cc} =~ ^[0-9]{2,}$ ]]; then
			export TORCH_CUDA_ARCH_LIST="${native_cc%?}.${native_cc: -1}"
		else
			# Leaving it unset is not an option: with no device to query, torch
			# 2.13's cpp_extension collects an empty architecture list and then
			# indexes it, so the build dies with IndexError. PTX comes along
			# because this path serves builds that run elsewhere, and a 7.5 cubin
			# alone would not load on a newer GPU. verified 2026-09-16
			ewarn "No GPU is visible and TORCH_CUDA_ARCH_LIST is unset; building"
			ewarn "for compute capability 7.5 plus PTX, so the driver can JIT for"
			ewarn "a newer GPU. Set TORCH_CUDA_ARCH_LIST through Portage's"
			ewarn "package.env to target yours, then re-emerge ${PN}."
			export TORCH_CUDA_ARCH_LIST="7.5+PTX"
		fi
	fi
	export FORCE_CUDA=1 MAX_JOBS="${MAX_JOBS:-4}"

	distutils-r1_src_compile
}