# Copyright 2026 Kelsey TheDreamer
# Distributed under the terms of the GNU General Public License v2

EAPI=8

inherit toolchain-funcs

DESCRIPTION="Intentionally useless C++ compiler torture benchmark (Clang 22, -O3 -flto=full)"
HOMEPAGE="https://codeberg.org/kelseythedreamer/headspace-repo"
SRC_URI=""

LICENSE="BSD-2"
SLOT="0"
KEYWORDS="~amd64"

IUSE="+die 4GiB 8GiB 16GiB 32GiB 64GiB 128GiB 256GiB"

BDEPEND="
	llvm-core/clang:22
	llvm-core/llvm:22
	dev-lang/python
	dev-build/make
"
RDEPEND=""
DEPEND="${RDEPEND}"

S="${WORKDIR}/clang-torture-${PV}"

# Helper: determine selected workload size in GiB.
# Dies with the exact messages required by the spec if selection is invalid.
clang_torture_target() {
	local count=0
	local target=""

	if use 4GiB; then ((count++)); target=4; fi
	if use 8GiB; then ((count++)); target=8; fi
	if use 16GiB; then ((count++)); target=16; fi
	if use 32GiB; then ((count++)); target=32; fi
	if use 64GiB; then ((count++)); target=64; fi
	if use 128GiB; then ((count++)); target=128; fi
	if use 256GiB; then ((count++)); target=256; fi

	if [[ ${count} -eq 0 ]]; then
		die "select exactly one workload size: 4GiB, 8GiB, 16GiB, 32GiB, 64GiB, 128GiB, or 256GiB"
	elif [[ ${count} -gt 1 ]]; then
		die "select exactly one workload size"
	fi

	echo "${target}"
}

pkg_setup() {
	# Validate USE selection early so the user gets the die message before any work.
	clang_torture_target >/dev/null
}

src_unpack() {
	# Source tarball is stored in FILESDIR (mirrors games-misc/mewo pattern).
	# This avoids hardcoding /home/aubrey and keeps the overlay self-contained.
	if [[ -f "${FILESDIR}/${P}.tar.gz" ]]; then
		tar xzf "${FILESDIR}/${P}.tar.gz" -C "${WORKDIR}" || die "unpack failed"
	else
		die "missing ${FILESDIR}/${P}.tar.gz — run 'tar czf ${P}.tar.gz -C /path/to/clang-torture clang-torture-${PV}' and place it in files/"
	fi
}

src_prepare() {
	default
}

src_compile() {
	local target
	target=$(clang_torture_target) || die

	einfo "Generating ${target} GiB workload (this will create GiBs of real C++: templates, constexpr, concepts, LTO IR)..."
	# Use the project's generator directly; respects TARGET_GIB mapping.
	# Do not create a second copy of the generated tree — generate in place.
	python3 generator.py --target-gib "${target}" --out-dir generated || die "generator failed"

	einfo "Building torture binary with -O3 -flto=full (Clang 22, full LTO)..."
	# Respect Gentoo's MAKEOPTS via emake, but ensure Clang 22 is used.
	# Makefile uses CXX ?= clang++ so we pass CXX explicitly; toolchain-funcs provides the correct binary.
	# Do not replace -O3 with -O4/-Ofast and do not drop -flto=full — Makefile enforces it.
	emake CXX="clang++" build || die "build failed"
}

src_test() {
	# Project's own validation: executable must exit 0 with no output.
	# Cheap to run, does not execute the workload itself.
	emake CXX="clang++" check || die "check failed (binary did not return 0)"
}

src_install() {
	dobin torture || die "dobin failed"
	dodoc README.md
	# Optionally install documentation about the generator; keep package minimal.
	# Do not install the GiBs of generated source — they are build-time only and would bloat the image.
}