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

# Autogenerated by pycargoebuild 0.15.0

EAPI=8

CRATES="
	aho-corasick@1.1.4
	android_system_properties@0.1.5
	anstream@0.6.21
	anstyle-parse@0.2.7
	anstyle-query@1.1.5
	anstyle-wincon@3.0.11
	anstyle@1.0.13
	anyhow@1.0.100
	autocfg@1.5.0
	base64@0.21.7
	bitflags@2.10.0
	bumpalo@3.19.1
	camino@1.2.2
	cargo-platform@0.1.9
	cargo_metadata@0.15.4
	cc@1.2.53
	cfg-if@1.0.4
	cfg_aliases@0.2.1
	chrono@0.4.43
	clap@4.5.54
	clap_builder@4.5.54
	clap_derive@4.5.49
	clap_lex@0.7.7
	colorchoice@1.0.4
	core-foundation-sys@0.8.7
	errno@0.3.14
	fastrand@2.3.0
	find-msvc-tools@0.1.8
	getrandom@0.3.4
	heck@0.5.0
	hermit-abi@0.5.2
	hidreport@0.6.0
	hut@0.5.0
	iana-time-zone-haiku@0.1.2
	iana-time-zone@0.1.64
	is-terminal@0.4.17
	is_terminal_polyfill@1.70.2
	itoa@1.0.17
	js-sys@0.3.85
	libbpf-cargo@0.24.8
	libbpf-rs@0.24.8
	libbpf-sys@1.6.2+v1.6.2
	libc@0.2.180
	libudev-sys@0.1.4
	linkme-impl@0.3.35
	linkme@0.3.35
	linux-raw-sys@0.11.0
	log@0.4.29
	memchr@2.7.6
	memmap2@0.5.10
	memoffset@0.9.1
	mio@0.8.11
	nix@0.30.1
	num-traits@0.2.19
	once_cell@1.21.3
	once_cell_polyfill@1.70.2
	pkg-config@0.3.32
	proc-macro2@1.0.106
	quote@1.0.43
	r-efi@5.3.0
	regex-automata@0.4.13
	regex-syntax@0.8.8
	regex@1.12.2
	rustix@1.1.3
	rustversion@1.0.22
	semver@1.0.27
	serde@1.0.228
	serde_core@1.0.228
	serde_derive@1.0.228
	serde_json@1.0.149
	shlex@1.3.0
	stderrlog@0.6.0
	strsim@0.11.1
	syn@2.0.114
	tempfile@3.24.0
	termcolor@1.1.3
	thiserror-impl@1.0.69
	thiserror-impl@2.0.18
	thiserror@1.0.69
	thiserror@2.0.18
	thread_local@1.1.9
	udev@0.7.0
	unicode-ident@1.0.22
	utf8parse@0.2.2
	vsprintf@2.0.0
	wasi@0.11.1+wasi-snapshot-preview1
	wasip2@1.0.2+wasi-0.2.9
	wasm-bindgen-macro-support@0.2.108
	wasm-bindgen-macro@0.2.108
	wasm-bindgen-shared@0.2.108
	wasm-bindgen@0.2.108
	winapi-util@0.1.11
	windows-core@0.62.2
	windows-implement@0.60.2
	windows-interface@0.59.3
	windows-link@0.2.1
	windows-result@0.4.1
	windows-strings@0.5.1
	windows-sys@0.48.0
	windows-sys@0.61.2
	windows-targets@0.48.5
	windows_aarch64_gnullvm@0.48.5
	windows_aarch64_msvc@0.48.5
	windows_i686_gnu@0.48.5
	windows_i686_msvc@0.48.5
	windows_x86_64_gnu@0.48.5
	windows_x86_64_gnullvm@0.48.5
	windows_x86_64_msvc@0.48.5
	wit-bindgen@0.51.0
	zmij@1.0.16
"
RUST_MIN_VER="1.82.0"
PYTHON_COMPAT=( python3_{12..15} )

inherit cargo linux-info meson python-any-r1 udev

MY_PV=${PV/_p/-}
MY_P=${PN}-${MY_PV}
DESCRIPTION="Automatic HID-BPF loader based on udev events for device workarounds"
HOMEPAGE="https://libevdev.pages.freedesktop.org/udev-hid-bpf/"
SRC_URI="
	https://gitlab.freedesktop.org/libevdev/udev-hid-bpf/-/archive/${MY_PV}/${MY_P}.tar.bz2
	${CARGO_CRATE_URIS}
"
S="${WORKDIR}"/${MY_P}

LICENSE="GPL-2"
# Dependent crate licenses
LICENSE+=" BSD-2 MIT Unicode-3.0"
SLOT="0"
KEYWORDS="~amd64"
IUSE="test userhacks"
RESTRICT="!test? ( test )"

DEPEND="
	dev-libs/libbpf:=
	virtual/libudev:=
"
RDEPEND="${DEPEND}"
BDEPEND="
	>=dev-util/bpftool-7.0.0
	llvm-core/clang[llvm_targets_BPF]
	test? (
		$(python_gen_any_dep '
			dev-python/pytest-xdist[${PYTHON_USEDEP}]
		')
	)
"

# BPF
QA_EXECSTACK="
	usr/lib/firmware/hid/bpf/*
	lib/firmware/hid/bpf/*
"

PATCHES=(
	"${FILESDIR}"/${PN}-2.2.0_p20251121-cargo-no-bundled-libbpf.patch
	"${FILESDIR}"/${PN}-2.3.0_p20260703-clang23.patch
)

python_check_deps() {
	python_has_version "dev-python/pytest-xdist[${PYTHON_USEDEP}]"
}

pkg_pretend() {
	local options=(
		BPF DEBUG_INFO_BTF DEBUG_INFO_BTF_MODULES
		# hidraw: https://docs.kernel.org/hid/hid-bpf.html#tracing
		HID_BPF HIDRAW BPF_EVENTS TRACING BPF_SYSCALL
		# tools/testing/selftests/hid/config
		BPF_JIT BPF_KPROBE_OVERRIDE BPF_LSM
		BPF_PRELOAD BPF_PRELOAD_UMD BPF_KPROBE_OVERRIDE
		BPF_STREAM_PARSER CGROUP_BPF DYNAMIC_FTRACE_WITH_DIRECT_CALLS
		FPROBE FTRACE_SYSCALLS FUNCTION_TRACER HID
		INPUT_EVDEV UHID LEDS_CLASS_MULTICOLOR
		USB USB_HID HID_APPLE HID_ITE HID_MULTITOUCH
		HID_PLAYSTATION PLAYSTATION_FF HID_SONY
		SONY_FF HID_WACOM
	)

	local CONFIG_CHECK f
	for f in "${options[@]}"; do
		CONFIG_CHECK+=" ~${f}"
	done

	check_extra_config
}

pkg_setup() {
	use test && python-any-r1_pkg_setup
}

src_configure() {
	# cargo_env vs meson, plus cargo test wants build to be a subdir
	BUILD_DIR="${S}"/build

	local bpf_types=(
		# https://gitlab.freedesktop.org/libevdev/udev-hid-bpf#quirks-user-hack-testing-and-stable
		# 'stable' isn't on by default, but README says:
		#   Distributions that package udev-hid-bpf should package these stable quirks.
		stable
		testing
		$(usev userhacks)
	)

	local emesonargs=(
		# Can't yet use GCC: https://gcc.gnu.org/PR121419
		-Dbpf-compiler=clang
		-Dbpfs="$(IFS=,; echo "${bpf_types[*]}")"
		-Dudevdir="${EPREFIX}$(get_udevdir)"
		$(meson_feature test tests)
	)

	cargo_env meson_src_configure
	ln -s "${CARGO_HOME}" "${BUILD_DIR}/cargo-home" || die
}

src_compile() {
	cargo_env meson_src_compile
}

src_test() {
	# Breakage w/ click-8.3 and mysterious missing _fields_
	local -x PYTEST_ADDOPTS="-k 'not (test_sync_with_kernel_tree.py or TestTUXEDOSirius16Gen1andGen2)'"
	cargo_env meson_src_test
}

src_install() {
	cargo_env meson_src_install

	# BPF objects
	dostrip -x /usr/lib/firmware /lib/firmware
}

pkg_postrm() {
	udev_reload
}

pkg_postinst() {
	udev_reload
}