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

# Autogenerated by pycargoebuild 0.16.0

EAPI=8

CRATES="
	adler2@2.0.1
	aho-corasick@1.1.5
	anstream@1.0.0
	anstyle-parse@1.0.0
	anstyle-query@1.1.5
	anstyle-wincon@3.0.11
	anstyle@1.0.14
	anyhow@1.0.104
	ar_archive_writer@0.5.1
	async-trait@0.1.92
	auto_impl@1.3.0
	autocfg@1.5.1
	bit-set@0.8.0
	bit-vec@0.8.0
	bitflags@1.3.2
	bitflags@2.13.1
	bytes@1.12.1
	cc@1.4.3
	cfg-if@1.0.4
	clap@4.6.6
	clap_builder@4.6.6
	clap_complete@4.6.9
	clap_derive@4.6.4
	clap_lex@1.1.0
	colorchoice@1.0.5
	crc32fast@1.5.0
	crossbeam-utils@0.8.22
	dashmap@5.5.3
	dashmap@6.2.1
	diffy@0.5.1
	displaydoc@0.2.7
	equivalent@1.0.2
	errno@0.3.14
	fastrand@2.5.0
	find-msvc-tools@0.1.11
	flate2@1.1.9
	fnv@1.0.7
	foldhash@0.2.0
	form_urlencoded@1.2.2
	fsevent-sys@4.1.0
	futures-channel@0.3.34
	futures-core@0.3.34
	futures-executor@0.3.34
	futures-io@0.3.34
	futures-macro@0.3.34
	futures-sink@0.3.34
	futures-task@0.3.34
	futures-util@0.3.34
	futures@0.3.34
	getrandom@0.3.4
	getrandom@0.4.3
	hashbrown@0.14.5
	hashbrown@0.17.1
	heck@0.5.0
	httparse@1.10.1
	icu_collections@2.1.1
	icu_locale_core@2.1.1
	icu_normalizer@2.1.1
	icu_normalizer_data@2.1.1
	icu_properties@2.1.2
	icu_properties_data@2.1.2
	icu_provider@2.1.1
	idna@1.1.0
	idna_adapter@1.2.1
	indexmap@2.14.0
	indoc@2.0.7
	inotify-sys@0.1.8
	inotify@0.11.5
	is_terminal_polyfill@1.70.2
	itoa@1.0.18
	kqueue-sys@1.1.2
	kqueue@1.2.1
	lazy_static@1.5.0
	libc@0.2.189
	linux-raw-sys@0.12.1
	litemap@0.8.3
	lock_api@0.4.14
	log@0.4.33
	lsp-types@0.94.1
	matchers@0.2.0
	memchr@2.8.3
	miniz_oxide@0.8.9
	mio@1.2.2
	notify-types@2.1.0
	notify@8.2.0
	nu-ansi-term@0.50.3
	num-bigint@0.5.1
	num-integer@0.1.47
	num-traits@0.2.19
	object@0.37.3
	once_cell@1.21.4
	once_cell_polyfill@1.70.2
	parking_lot@0.12.5
	parking_lot_core@0.9.12
	percent-encoding@2.3.2
	pest@2.9.0
	pest_derive@2.9.0
	pest_generator@2.9.0
	pest_meta@2.9.0
	pin-project-internal@1.1.13
	pin-project-lite@0.2.17
	pin-project@1.1.13
	potential_utf@0.1.6
	ppv-lite86@0.2.21
	proc-macro2@1.0.107
	proptest@1.11.0
	psm@0.1.31
	quick-error@1.2.3
	quick-xml@0.41.0
	quote@1.0.47
	r-efi@5.3.0
	r-efi@6.0.0
	rand@0.9.5
	rand_chacha@0.9.0
	rand_core@0.9.5
	rand_xorshift@0.4.0
	redox_syscall@0.5.18
	regex-automata@0.4.18
	regex-syntax@0.8.11
	ropey@1.6.1
	rustix@1.1.4
	rustversion@1.0.23
	rusty-fork@0.3.1
	same-file@1.0.6
	scopeguard@1.2.0
	serde@1.0.229
	serde_core@1.0.229
	serde_derive@1.0.229
	serde_json@1.0.151
	serde_repr@0.1.21
	sharded-slab@0.1.7
	shlex@2.0.1
	signal-hook-registry@1.4.8
	simd-adler32@0.3.10
	slab@0.4.12
	smallvec@1.15.2
	stable_deref_trait@1.2.1
	stacker@0.1.25
	stats_alloc@0.1.10
	str_indices@0.4.4
	strsim@0.11.1
	syn@2.0.119
	syn@3.0.3
	sync_wrapper@1.0.2
	synstructure@0.13.2
	tempfile@3.27.0
	test-case-core@3.3.1
	test-case-macros@3.3.1
	test-case@3.3.1
	thiserror-impl@2.0.20
	thiserror@2.0.20
	thread_local@1.1.10
	tinystr@0.8.4
	tokio-macros@2.7.2
	tokio-util@0.7.19
	tokio@1.53.1
	tower-layer@0.3.3
	tower-lsp-macros@0.9.0
	tower-lsp@0.20.0
	tower-service@0.3.3
	tower@0.4.13
	tower@0.5.3
	tracing-attributes@0.1.31
	tracing-core@0.1.36
	tracing-log@0.2.0
	tracing-subscriber@0.3.23
	tracing@0.1.44
	typed-path@0.12.3
	ucd-trie@0.1.7
	unarray@0.1.4
	unicode-ident@1.0.24
	url@2.5.8
	utf8_iter@1.0.4
	utf8parse@0.2.2
	valuable@0.1.1
	wait-timeout@0.2.1
	walkdir@2.5.0
	wasi@0.11.1+wasi-snapshot-preview1
	wasip2@1.0.1+wasi-0.2.4
	winapi-util@0.1.11
	windows-link@0.2.1
	windows-sys@0.60.2
	windows-sys@0.61.2
	windows-targets@0.53.5
	windows_aarch64_gnullvm@0.53.1
	windows_aarch64_msvc@0.53.1
	windows_i686_gnu@0.53.1
	windows_i686_gnullvm@0.53.1
	windows_i686_msvc@0.53.1
	windows_x86_64_gnu@0.53.1
	windows_x86_64_gnullvm@0.53.1
	windows_x86_64_msvc@0.53.1
	wit-bindgen@0.46.0
	writeable@0.6.4
	yoke-derive@0.8.2
	yoke@0.8.3
	zerocopy-derive@0.8.56
	zerocopy@0.8.56
	zerofrom-derive@0.1.7
	zerofrom@0.1.8
	zerotrie@0.2.5
	zerovec-derive@0.11.4
	zerovec@0.11.7
	zip@8.6.0
	zlib-rs@0.6.7
	zmij@1.0.23
"

inherit cargo edo shell-completion

DESCRIPTION="Rust toolchain for Event-B: parser, static checker, CLI, and language server"
HOMEPAGE="https://github.com/eventb-rossi/rossi"
SRC_URI="
	https://github.com/eventb-rossi/rossi/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz
	${CARGO_CRATE_URIS}
"

LICENSE="|| ( Apache-2.0 MIT )"
# Dependent crate licenses
LICENSE+="
	Apache-2.0 Apache-2.0-with-LLVM-exceptions CC0-1.0 ISC MIT
	Unicode-3.0 ZLIB
"
SLOT="0"
KEYWORDS="~amd64"
IUSE="pgo test"
RESTRICT="!test? ( test )"

# dev-lang/rust[system-llvm] guarantees a matching llvm-core/llvm:<slot> is
# installed (rust's own DEPEND pins that slot), which is required to merge
# PGO profiles with a compatible llvm-profdata. Bundled-LLVM rust builds and
# dev-lang/rust-bin ship no llvm-profdata at all, so pgo is unsupported there.
BDEPEND="pgo? ( dev-lang/rust[system-llvm] )"

# These tests guard that the in-repo tree-sitter editor grammar
# (editors/tree-sitter-eventb, a git submodule absent from the release tarball)
# stays in sync with the canonical token tables. They check files this package
# neither builds nor installs, so skip them rather than fetch the submodule.
CARGO_SKIP_TESTS=(
	editor_grammars_are_up_to_date
	generated_nodes_are_listed_in_token_choice
)

DOCS=( README.md )

src_compile() {
	if ! use pgo; then
		cargo_src_compile
		return
	fi

	# Two-phase profile-guided optimization build, mirroring upstream's
	# release workflow (.github/workflows/release.yml): instrument+train the
	# CLI, merge the collected profile, then rebuild both binaries using it.
	# The two cargo_src_compile calls must differ ONLY in the trailing
	# -Cprofile-generate/-Cprofile-use flag: any other RUSTFLAGS difference
	# changes rustc's -Cmetadata hash and silently breaks profile matching.
	local llvm_ver
	llvm_ver=$(rustc --version --verbose | sed -n 's/^LLVM version: \([0-9]*\).*/\1/p')
	local profdata="/usr/lib/llvm/${llvm_ver}/bin/llvm-profdata"
	[[ -x ${profdata} ]] || die "llvm-profdata not found at ${profdata}; USE=pgo requires dev-lang/rust[system-llvm] and a matching llvm-core/llvm:${llvm_ver}"

	local pgodir="${T}/pgo-profiles"
	local rustflags_base="${RUSTFLAGS}"

	# Drop profiles left over from an earlier attempt (FEATURES=keepwork), so
	# llvm-profdata cannot merge in data from a differently-built binary.
	rm -rf "${pgodir}" || die

	# RUSTFLAGS must be assigned as a plain (non-local, non-prefix) variable.
	# cargo_env() folds ${RUSTFLAGS} into CARGO_TARGET_<TRIPLE>_RUSTFLAGS and
	# then unsets RUSTFLAGS in the subshell it runs cargo in. `unset` pops only
	# the innermost binding, so a `RUSTFLAGS=... cargo_src_compile` prefix
	# assignment -- or a `local RUSTFLAGS` -- would re-expose the make.conf
	# value, which cargo prefers over CARGO_TARGET_<TRIPLE>_RUSTFLAGS. That
	# discards the whole eclass flag string: no instrumentation, and no
	# LDFLAGS, linker or -Cstrip=none either.
	RUSTFLAGS="${rustflags_base} -Cprofile-generate=${pgodir}"
	cargo_src_compile --bin rossi

	edo bash "${S}"/scripts/pgo-train.sh "$(cargo_target_dir)/rossi"

	# An uninstrumented binary trains silently and writes nothing; say so here
	# rather than letting llvm-profdata fail with a bare ENOENT on ${pgodir}.
	local profraws=( "${pgodir}"/*.profraw )
	[[ -e ${profraws[0]} ]] ||
		die "PGO training produced no profile data in ${pgodir}"

	edo "${profdata}" merge -o "${T}"/pgo-merged.profdata "${pgodir}"

	RUSTFLAGS="${rustflags_base} -Cprofile-use=${T}/pgo-merged.profdata"
	cargo_src_compile --bin rossi --bin eventb-language-server

	RUSTFLAGS="${rustflags_base}"
}

src_install() {
	# Both binaries come from the cargo build(s) src_compile runs; install them
	# from the shared target dir rather than `cargo install` per crate, which
	# would re-run the LTO release build for each.
	dobin "$(cargo_target_dir)"/{rossi,eventb-language-server}

	einstalldocs

	# Example Event-B models for reference.
	docinto examples
	dodoc crates/rossi/examples/*.eventb

	# Shell completions, generated by the freshly built CLI. clap_complete emits
	# a `#compdef rossi` header for zsh, so the zsh file must be named `_rossi`.
	local rossi_bin="$(cargo_target_dir)/rossi"
	"${rossi_bin}" completions bash > "${T}"/rossi.bash || die
	"${rossi_bin}" completions zsh > "${T}"/_rossi || die
	"${rossi_bin}" completions fish > "${T}"/rossi.fish || die
	newbashcomp "${T}"/rossi.bash rossi
	dozshcomp "${T}"/_rossi
	dofishcomp "${T}"/rossi.fish
}