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

EAPI=8

RUST_MIN_VER="1.91.0"

CRATES=""

inherit cargo systemd

DESCRIPTION="Compiler cache that wraps C, C++, and Rust compilers, with optional remote and distributed modes"
HOMEPAGE="https://github.com/mozilla/sccache"
SRC_URI="https://crates.io/api/v1/crates/sccache/${PV}/download -> sccache-${PV}.crate"
SRC_URI+=" https://github.com/0x6d6e647a/mndz-overlay-assets/releases/download/sccache-${PV}/sccache-${PV}-crates.tar.xz"

LICENSE="Apache-2.0"
# Dependent crate licenses
LICENSE+="
	Apache-2.0 BSD Boost-1.0 CC0-1.0 CDLA-Permissive-2.0 ISC MIT MPL-2.0
	Unicode-3.0 ZLIB
"
SLOT="0"
KEYWORDS="-* ~amd64 ~arm64 ~loong ~ppc64"
IUSE="+system-zstd symlink dist-client dist-server redis memcached s3 gcs azure gha webdav oss cos test"
RESTRICT="!test? ( test )"

DEPEND="
	system-zstd? ( app-arch/zstd:= )
	dist-server? ( dev-libs/openssl:= )
"
RDEPEND="
	${DEPEND}
	dist-server? ( sys-apps/bubblewrap )
"
# The HTTP backends build aws-lc-sys, which needs cmake. redis pulls rustls
# and is listed conservatively.
BDEPEND="
	system-zstd? ( virtual/pkgconfig )
	dist-server? ( virtual/pkgconfig )
	redis? ( dev-build/cmake )
	s3? ( dev-build/cmake )
	gcs? ( dev-build/cmake )
	azure? ( dev-build/cmake )
	gha? ( dev-build/cmake )
	webdav? ( dev-build/cmake )
	oss? ( dev-build/cmake )
	cos? ( dev-build/cmake )
	dist-client? ( dev-build/cmake )
"

# rust does not use *FLAGS from make.conf, silence portage warning
# /usr/bin/sccache
# /usr/bin/sccache-dist
QA_FLAGS_IGNORED="/usr/bin/sccache /usr/bin/sccache-dist"

src_configure() {
	# One flag per Cargo feature of the same name. The default build keeps
	# every backend off, so it is the local-disk cache only.
	local myfeatures=(
		$(usev dist-client)
		$(usev dist-server)
		$(usev redis)
		$(usev memcached)
		$(usev s3)
		$(usev gcs)
		$(usev azure)
		$(usev gha)
		$(usev webdav)
		$(usev oss)
		$(usev cos)
	)

	if use system-zstd; then
		export ZSTD_SYS_USE_PKG_CONFIG=1
	fi
	if use dist-server; then
		export OPENSSL_NO_VENDOR=1
	fi

	cargo_src_configure --no-default-features
}

src_test() {
	local CARGO_SKIP_TESTS=(
		# These need tests/test-crate, which the crates.io crate excludes.
		test_cache_hit_rate
		test_adv_cache_hit_rate
		test_rust_cargo_check
		test_rust_cargo_check_readonly
		test_rust_cargo_build
		test_rust_cargo_build_readonly
		test_rust_cargo_run_with_env_dep_parsing
		test_rust_cargo_cmd_readonly_preemtive_block
		test_rust_cargo_dep_info_from_other_checkout
		test_rust_cargo_incremental_refused_by_default
		test_rust_cargo_incremental_allowed_deps_cached
		test_rust_cargo_incremental_cached
		# Fail with gcc 16 in the minimal-feature host spike.
		test_assembler_affects_cache
	)
	cargo_src_test
}

src_install() {
	cargo_src_install

	dodoc README.md
	dodoc -r docs

	if use symlink; then
		# Only the C and C++ compilers. Rust callers set RUSTC_WRAPPER, and a
		# rustc shim named sccache mishandles cc-rs.
		local name
		for name in gcc g++ cc c++; do
			dosym ../../../bin/sccache "/usr/lib/sccache/bin/${name}"
		done
	fi

	if use dist-server; then
		newinitd "${FILESDIR}/sccache-scheduler.initd" sccache-scheduler
		newconfd "${FILESDIR}/sccache-scheduler.confd" sccache-scheduler
		newinitd "${FILESDIR}/sccache-server.initd" sccache-server
		newconfd "${FILESDIR}/sccache-server.confd" sccache-server
		systemd_dounit "${FILESDIR}/sccache-scheduler.service"
		systemd_dounit "${FILESDIR}/sccache-server.service"
		newdoc "${FILESDIR}/scheduler.conf" scheduler.conf.example
		newdoc "${FILESDIR}/server.conf" server.conf.example
	fi
}

pkg_postinst() {
	if use dist-server; then
		elog "The sccache-scheduler and sccache-server services refuse to start"
		elog "until /etc/sccache/scheduler.conf or /etc/sccache/server.conf exists."
		elog "Copy and edit scheduler.conf.example or server.conf.example from"
		elog "${EPREFIX}/usr/share/doc/${PF}. The build server runs as root and needs bubblewrap."
	fi
	if use symlink; then
		elog "Put ${EPREFIX}/usr/lib/sccache/bin first in PATH to cache gcc, g++, cc, and c++ calls."
	fi
}