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

EAPI=8

# 0.16.0 moved the build backend from setuptools to scikit_build_core.build
# (pyproject.toml build-system); the standalone setuptools path is gone.
DISTUTILS_USE_PEP517=scikit-build-core
DISTUTILS_SINGLE_IMPL=1
DISTUTILS_EXT=1
PYTHON_COMPAT=( python3_{12..14} )

inherit distutils-r1

DESCRIPTION="Decode/encode video and audio into PyTorch tensors via FFmpeg"
HOMEPAGE="
	https://github.com/meta-pytorch/torchcodec
	https://pypi.org/project/torchcodec/
"
SRC_URI="
	https://github.com/meta-pytorch/torchcodec/archive/refs/tags/v${PV}.tar.gz
		-> ${P}.gh.tar.gz
"

LICENSE="BSD-2"
SLOT="0"
KEYWORDS="~amd64 ~arm64"
IUSE="cuda"

# Upstream supports FFmpeg majors 4..8 (compiled separately and dlopen'd
# at runtime); ::gentoo's media-video/ffmpeg covers the live 7.x/8.x slot.
RDEPEND="
	sci-ml/pytorch[${PYTHON_SINGLE_USEDEP}]
	media-video/ffmpeg:=
	cuda? (
		dev-util/nvidia-cuda-toolkit:=
	)
"
DEPEND="${RDEPEND}"
BDEPEND="
	$(python_gen_cond_dep '
		dev-python/pybind11[${PYTHON_USEDEP}]
	')
	dev-build/cmake
"

# Tests pull a video corpus from S3.
RESTRICT="test"

python_compile() {
	# 0.16.0's scikit-build-core backend drives cmake and maps these env vars
	# to cmake defines ([tool.scikit-build.cmake.define] in pyproject.toml); the
	# torchcodec_version provider still honors BUILD_VERSION for the version.
	export CMAKE_BUILD_TYPE=Release
	export BUILD_VERSION="${PV}"

	# 0.16.0 added image decoders (JPEG/PNG/WebP/AVIF/GIF/HEIC), ON by default
	# and *required* -- a missing codec lib fails the build loudly. Keep the
	# 0.15.0 video-only scope: TORCHCODEC_BUILD_IMAGE=0 disables all of them and
	# the decode_* image ops raise at runtime instead. verified 2026-08-14
	export TORCHCODEC_BUILD_IMAGE=0

	# Upstream defaults to vendoring FFmpeg from S3 to skirt the wheel-
	# distribution licensing question; we link against media-video/ffmpeg
	# instead and have to ack the opt-out env var. Self-built local
	# install is not redistributing a binary, so no GPL concerns.
	export I_CONFIRM_THIS_IS_NOT_A_LICENSE_VIOLATION=1

	# CUDA 13.x nvcc rejects gcc>15. Caffe2's cmake config is included
	# unconditionally by find_package(Torch) and tries to enable_language(CUDA)
	# whenever it finds /opt/cuda — even for our CPU-only build path. Pin
	# the host compiler always; this requires gcc-15 installed alongside
	# whatever newer gcc is the active system slot.
	export CUDAHOSTCXX="/usr/bin/g++-15"

	if use cuda; then
		export ENABLE_CUDA=1
	else
		export ENABLE_CUDA=
	fi

	distutils-r1_python_compile
}