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

# Autogenerated by pycargoebuild 0.17.2

EAPI=8

CRATES="
	addr2line@0.24.2
	adler2@2.0.1
	aho-corasick@1.0.2
	android-tzdata@0.1.1
	android_system_properties@0.1.5
	anstream@0.6.13
	anstyle-parse@0.2.1
	anstyle-query@1.0.0
	anstyle-wincon@3.0.2
	anstyle@1.0.6
	assert_cmd@2.2.2
	assert_matches@1.5.0
	async-broadcast@0.7.2
	async-channel@2.5.0
	async-executor@1.13.3
	async-io@2.6.0
	async-lock@3.4.2
	async-process@2.5.0
	async-recursion@1.1.1
	async-signal@0.2.13
	async-task@4.7.1
	async-trait@0.1.89
	atomic-waker@1.1.2
	autocfg@1.1.0
	backtrace@0.3.75
	bindgen@0.72.1
	bit-set@0.8.0
	bit-vec@0.8.0
	bitflags@2.9.3
	block-buffer@0.12.0
	blocking@1.6.2
	bstr@1.5.0
	bumpalo@3.13.0
	byteorder@1.4.3
	bytes@1.11.1
	cc@1.2.19
	cexpr@0.6.0
	cfg-if@1.0.0
	cfg_aliases@0.2.1
	chacha20@0.10.2
	chrono@0.4.31
	clang-sys@1.6.1
	clap@4.5.13
	clap_builder@4.5.13
	clap_derive@4.5.13
	clap_lex@0.7.4
	colorchoice@1.0.0
	concurrent-queue@2.5.0
	const-oid@0.10.2
	core-foundation-sys@0.8.4
	cpufeatures@0.3.0
	crc-catalog@2.2.0
	crc32fast@1.4.2
	crc@3.0.1
	crossbeam-utils@0.8.21
	crypto-common@0.2.2
	data-encoding@2.4.0
	devicemapper-sys@0.3.3
	devicemapper@0.34.8
	difflib@0.4.0
	digest@0.11.3
	either@1.8.1
	endi@1.1.1
	enumflags2@0.7.12
	enumflags2_derive@0.7.12
	env_filter@0.1.0
	env_logger@0.11.2
	equivalent@1.0.1
	errno@0.3.10
	event-listener-strategy@0.5.4
	event-listener@5.4.2
	fastrand@2.3.0
	flate2@1.1.2
	float-cmp@0.10.0
	fnv@1.0.7
	futures-channel@0.3.31
	futures-core@0.3.31
	futures-executor@0.3.31
	futures-io@0.3.31
	futures-lite@2.6.1
	futures-macro@0.3.31
	futures-sink@0.3.31
	futures-task@0.3.31
	futures-util@0.3.31
	futures@0.3.31
	getrandom@0.3.1
	getrandom@0.4.3
	gimli@0.31.1
	glob@0.3.1
	hashbrown@0.16.1
	heck@0.5.0
	hermit-abi@0.5.2
	hex@0.4.3
	humantime@2.1.0
	hybrid-array@0.4.12
	iana-time-zone-haiku@0.1.2
	iana-time-zone@0.1.57
	indexmap@2.13.0
	io-uring@0.7.10
	itertools@0.10.5
	itertools@0.15.0
	itoa@1.0.6
	js-sys@0.3.83
	lazy_static@1.4.0
	libblkid-rs-sys@0.3.3
	libblkid-rs@0.4.2
	libc@0.2.180
	libcryptsetup-rs-sys@0.7.0
	libcryptsetup-rs@0.16.1
	libloading@0.7.4
	libudev-sys@0.1.4
	libudev@0.3.0
	linux-raw-sys@0.11.0
	linux-raw-sys@0.12.1
	log@0.4.27
	loopdev-3@0.5.3
	memchr@2.7.6
	memoffset@0.9.0
	minimal-lexical@0.2.1
	miniz_oxide@0.8.9
	mio@1.0.3
	nix@0.31.1
	nom@7.1.3
	normalize-line-endings@0.3.0
	num-traits@0.2.15
	object@0.36.7
	once_cell@1.19.0
	ordered-stream@0.2.0
	parking@2.2.1
	per-thread-mutex@0.1.4
	pin-project-lite@0.2.16
	pin-utils@0.1.0
	piper@0.2.4
	pkg-config@0.3.31
	polling@3.11.0
	ppv-lite86@0.2.17
	predicates-core@1.0.6
	predicates-tree@1.0.9
	predicates@3.1.4
	pretty-hex@0.4.1
	prettyplease@0.2.22
	proc-macro-crate@3.4.0
	proc-macro2@1.0.106
	procfs-core@0.18.0
	procfs@0.18.0
	proptest@1.7.0
	quick-error@1.2.3
	quote@1.0.37
	r-efi@6.0.0
	rand@0.10.1
	rand@0.9.4
	rand_chacha@0.9.0
	rand_core@0.10.0
	rand_core@0.9.1
	rand_xorshift@0.4.0
	regex-automata@0.1.10
	regex-syntax@0.7.2
	regex-syntax@0.8.6
	regex@1.8.4
	retry@2.0.0
	rustc-demangle@0.1.24
	rustc-hash@2.1.1
	rustix@1.1.2
	rustversion@1.0.18
	rusty-fork@0.3.0
	ryu@1.0.13
	semver@1.0.17
	serde@1.0.228
	serde_core@1.0.228
	serde_derive@1.0.228
	serde_json@1.0.97
	serde_repr@0.1.20
	sha2@0.11.0
	shlex@1.3.0
	signal-hook-registry@1.4.1
	slab@0.4.11
	socket2@0.6.0
	strsim@0.11.1
	strum@0.28.0
	strum_macros@0.28.0
	syn@2.0.116
	tempfile@3.23.0
	termios@0.3.3
	termtree@0.4.1
	tokio-macros@2.5.0
	tokio@1.47.1
	toml_datetime@0.7.5+spec-1.1.0
	toml_edit@0.23.10+spec-1.0.0
	toml_parser@1.0.6+spec-1.1.0
	tracing-attributes@0.1.31
	tracing-core@0.1.36
	tracing@0.1.44
	typenum@1.20.1
	uds_windows@1.1.0
	unarray@0.1.4
	unicode-ident@1.0.9
	utf8parse@0.2.1
	uuid@1.19.0
	wait-timeout@0.2.0
	wasi@0.11.0+wasi-snapshot-preview1
	wasi@0.13.3+wasi-0.2.2
	wasm-bindgen-macro-support@0.2.106
	wasm-bindgen-macro@0.2.106
	wasm-bindgen-shared@0.2.106
	wasm-bindgen@0.2.106
	winapi-i686-pc-windows-gnu@0.4.0
	winapi-x86_64-pc-windows-gnu@0.4.0
	winapi@0.3.9
	windows-link@0.2.1
	windows-sys@0.48.0
	windows-sys@0.52.0
	windows-sys@0.59.0
	windows-sys@0.61.2
	windows-targets@0.48.0
	windows-targets@0.52.6
	windows@0.48.0
	windows_aarch64_gnullvm@0.48.0
	windows_aarch64_gnullvm@0.52.6
	windows_aarch64_msvc@0.48.0
	windows_aarch64_msvc@0.52.6
	windows_i686_gnu@0.48.0
	windows_i686_gnu@0.52.6
	windows_i686_gnullvm@0.52.6
	windows_i686_msvc@0.48.0
	windows_i686_msvc@0.52.6
	windows_x86_64_gnu@0.48.0
	windows_x86_64_gnu@0.52.6
	windows_x86_64_gnullvm@0.48.0
	windows_x86_64_gnullvm@0.52.6
	windows_x86_64_msvc@0.48.0
	windows_x86_64_msvc@0.52.6
	winnow@0.7.14
	winnow@1.0.1
	wit-bindgen-rt@0.33.0
	zbus@5.17.0
	zbus_macros@5.17.0
	zbus_names@4.3.3
	zerocopy-derive@0.8.18
	zerocopy@0.8.18
	zvariant@5.13.0
	zvariant_derive@5.13.0
	zvariant_utils@3.5.0
"

# bindgen runs unconditionally: devicemapper-sys, libblkid-rs-sys and
# libcryptsetup-rs-sys generate their bindings from the system headers, and
# USE=systemd adds stratisd's own bindings to sd-daemon.h.
LLVM_COMPAT=( {20..23} )
# Cargo.toml: [workspace.package] rust-version ("LOWEST SUPPORTED RUST
# TOOLCHAIN"). rust-toolchain.toml pins 1.98.0 for upstream's own CI only.
RUST_MIN_VER="1.87.0"

inherit cargo llvm-r2 systemd udev

DESCRIPTION="Daemon that manages Stratis storage pools and filesystems"
HOMEPAGE="
	https://stratis-storage.github.io/
	https://github.com/stratis-storage/stratisd
"
SRC_URI="
	https://github.com/stratis-storage/${PN}/archive/refs/tags/${PN}-v${PV}.tar.gz
		-> ${P}.tar.gz
	${CARGO_CRATE_URIS}
"
S="${WORKDIR}/${PN}-${PN}-v${PV}"

LICENSE="MPL-2.0"
# Dependent crate licenses
LICENSE+=" Apache-2.0 BSD ISC MIT MPL-2.0 Unicode-DFS-2016"
SLOT="0"
KEYWORDS="~amd64"
# No clevis flag yet: app-crypt/clevis is not in ::gentoo and will be a
# separate package. stratisd looks the clevis tools up at run time, so
# Clevis/Tang/TPM2 binding starts working once they are installed; the
# 50stratis-clevis dracut module is not installed until that package exists.
IUSE="dracut systemd"
# The 50stratis dracut module is systemd-only: it runs stratisd-min as a
# systemd unit, needs the stratis-setup-generator systemd generator (built
# only with systemd_compat) and asks for passphrases through
# systemd-ask-password.
REQUIRED_USE="dracut? ( systemd )"
# The suites need root, loop devices and real block devices; the few that do
# not cannot be told apart by feature, only by name (see "make test").
RESTRICT="test"

COMMON_DEPEND="
	>=sys-apps/util-linux-2.32.0
	>=sys-fs/cryptsetup-2.3.0:=
	virtual/libudev:=
	systemd? ( sys-apps/systemd:= )
"
# devicemapper-sys only reads libdevmapper.h and dm-ioctl.h; the binaries talk
# to device-mapper through ioctls and do not link libdevmapper.
DEPEND="
	${COMMON_DEPEND}
	sys-fs/lvm2
"
RDEPEND="
	${COMMON_DEPEND}
	sys-apps/dbus
	sys-block/thin-provisioning-tools
	sys-fs/xfsprogs
	dracut? ( sys-kernel/dracut )
"
# a2x builds the two man pages from asciidoc sources.
BDEPEND="
	$(llvm_gen_dep '
		llvm-core/clang:${LLVM_SLOT}
	')
	app-text/asciidoc
	app-text/docbook-xsl-stylesheets
	dev-libs/libxslt
	virtual/pkgconfig
"

QA_FLAGS_IGNORED="
	usr/bin/stratis-min
	usr/bin/stratisd-tools
	usr/bin/stratis-dumpmetadata
	usr/bin/stratis-predict-usage
	usr/bin/stratis-decode-dm
	usr/libexec/stratisd
	usr/libexec/stratisd-min
	usr/lib/udev/stratis-base32-decode
	usr/lib/udev/stratis-str-cmp
	usr/lib/systemd/system-generators/stratis-setup-generator
	usr/lib/systemd/system-generators/stratis-clevis-setup-generator
"

pkg_setup() {
	llvm-r2_pkg_setup
	rust_pkg_setup
}

src_prepare() {
	default

	# The dracut module names the udev helpers by absolute path. Point it at
	# this system's udev directory when that is not /usr/lib/udev; guarded,
	# because a sed that matches nothing exits 0 and would ship a module whose
	# check() fails on every host.
	local udevdir=$(get_udevdir)
	if [[ ${udevdir} != /usr/lib/udev ]]; then
		local f=dracut/50stratis/module-setup.sh.in
		grep -qF '/usr/lib/udev/stratis-str-cmp' "${f}" ||
			die "${f}: /usr/lib/udev anchor not found"
		sed -i "s|/usr/lib/udev/|${udevdir}/|g" "${f}" || die
		grep -qF "${udevdir}/stratis-str-cmp" "${f}" ||
			die "${f}: udev directory was not rewritten"
	fi
}

src_compile() {
	# Cargo.toml denies every rustc warning, so a lint added by a newer
	# toolchain would fail the build. Cap them, as upstream's CAP_LINTS does.
	local -x RUSTFLAGS="${RUSTFLAGS} --cap-lints=warn"

	# One cargo pass per feature set, the same sets the Makefile's build-*
	# targets use (the Makefile itself is not run: it would call cargo without
	# the eclass environment). Each binary keeps exactly upstream's features:
	# the dbus_enabled and min features change engine behaviour, so they are
	# never merged into a single pass.
	local systemd_compat=$(usev systemd ,systemd_compat)

	# stratisd: default features (dbus_enabled, engine); never links libsystemd.
	cargo_src_compile --bin stratisd
	cargo_src_compile --bin stratisd-tools \
		--no-default-features --features engine,extras
	# stratisd-min + stratis-min: the JSON-RPC pair used in early boot, and the
	# only command-line client this package ships. systemd_compat adds the
	# sd_notify() readiness signal the Type=notify units wait for.
	cargo_src_compile --bin stratis-min --bin stratisd-min \
		--no-default-features --features "engine,min${systemd_compat}"
	# stratis-utils: predict-usage, decode-dm and, with systemd_compat, the
	# two systemd generators (without it they exit with an error).
	cargo_src_compile --bin stratis-utils \
		--no-default-features --features "dbus_enabled,engine${systemd_compat}"
	# udev helpers. Upstream links them with +crt-static; they are linked
	# dynamically here, since dracut copies their libraries into the initramfs
	# and the real root already has them.
	cargo_src_compile --bin stratis-str-cmp --bin stratis-base32-decode \
		--no-default-features --features udev_scripts

	local page
	for page in stratisd stratis-dumpmetadata; do
		a2x -L -f manpage "docs/${page}.txt" || die "a2x failed for ${page}"
	done
}

src_install() {
	# Reuse the Makefile's install targets, which never invoke cargo, so a bump
	# picks up upstream's layout (hard links included) without rewriting it.
	# They read the binaries from target/$(PROFILEDIR).
	local target_dir=$(cargo_target_dir)
	[[ ${target_dir} == target/* ]] ||
		die "cargo target directory ${target_dir} is not under target/"
	local -a mymakeargs=(
		DESTDIR="${D}"
		PREFIX="${EPREFIX}/usr"
		LIBEXECDIR="${EPREFIX}/usr/libexec"
		UDEVDIR="${EPREFIX}$(get_udevdir)"
		UNITDIR="$(systemd_get_systemunitdir)"
		UNITEXECDIR="$(systemd_get_utildir)"
		UNITGENDIR="$(systemd_get_systemgeneratordir)"
		DRACUTDIR="${EPREFIX}/usr/lib/dracut"
		PROFILEDIR="${target_dir#target/}"
	)
	local -a targets=(
		install-binaries
		install-daemons
		install-dbus-cfg
		install-udev-binaries
	)
	use systemd && targets+=( install-systemd-cfg install-fstab-script )
	use dracut && targets+=( install-dracut-cfg )
	emake "${mymakeargs[@]}" "${targets[@]}"

	if ! use systemd; then
		# install-binaries always links the two generators into the systemd
		# generator directory; without systemd_compat they only print an error.
		rm -r "${D}$(systemd_get_systemgeneratordir)" ||
			die "systemd generator directory not found"
		# Drop the parent too when nothing else of systemd's is left in it.
		find "${D}$(dirname "$(systemd_get_systemgeneratordir)")" \
			-maxdepth 0 -empty -delete || die
	fi
	if use dracut; then
		# Needs app-crypt/clevis, which is not packaged yet (see IUSE).
		rm -r "${ED}/usr/lib/dracut/modules.d/50stratis-clevis" ||
			die "50stratis-clevis dracut module not found"
	fi

	udev_dorules udev/61-stratisd.rules
	doman docs/stratisd.8 docs/stratis-dumpmetadata.8

	# The systemd units above are gated behind USE=systemd; the OpenRC script
	# never is (overlay policy: no systemd-only daemons). It mirrors
	# stratisd.service only. What has no OpenRC counterpart, and why:
	#  - stratisd-min-postinitrd.service and stratis-fstab-setup{,-with-network}@
	#    .service: the systemd boot path that starts the pools named in
	#    /etc/fstab (x-systemd.requires=stratis-fstab-setup@<pool-uuid>) before
	#    local-fs-pre.target, prompting through systemd-ask-password. OpenRC has
	#    no per-mount unit dependency to hang them on; under OpenRC the pools
	#    are set up by stratisd itself once its init script runs.
	#  - stratis-setup-generator and stratis-clevis-setup-generator: systemd
	#    generators, a concept OpenRC does not have. They serve root-on-Stratis
	#    in a systemd initramfs (USE=dracut).
	newinitd "${FILESDIR}"/stratisd.initd stratisd
	newconfd "${FILESDIR}"/stratisd.confd stratisd

	dodoc README.md CHANGES.txt
}

pkg_postinst() {
	udev_reload

	elog "stratisd is controlled over D-Bus (org.storage.stratis3). The usual"
	elog "client, the Python 'stratis' command (stratis-cli), is a separate"
	elog "project; stratis-min, installed here, talks only to stratisd-min."
	elog
	elog "OpenRC: rc-update add stratisd default"
	use systemd && elog "systemd: systemctl enable --now stratisd.service"
	elog
	elog "Under OpenRC, mark Stratis filesystems in /etc/fstab 'noauto' and mount"
	elog "them once stratisd is running: localmount runs before D-Bus, hence"
	elog "before stratisd, and the systemd-only stratis-fstab-setup units that"
	elog "order these mounts have no OpenRC equivalent."
	if use dracut; then
		elog
		elog "Root on Stratis: the 50stratis dracut module needs a systemd"
		elog "initramfs and the kernel parameter stratis.rootfs.pool_uuid=<UUID>."
	fi
	if ! has_version app-crypt/clevis; then
		elog
		elog "Clevis/Tang/TPM2 pool binding needs app-crypt/clevis, not"
		elog "installed; passphrase (keyring) encryption works without it."
	fi
}

pkg_postrm() {
	udev_reload
}