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

EAPI=8

inherit multilib toolchain-funcs

DESCRIPTION="Ulm's Oberon compiler and libraries for Linux on x86 and x86-64"
HOMEPAGE="https://github.com/norayr/ulmo"
SRC_URI="https://github.com/norayr/ulmo/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz"

LICENSE="GPL-2+ LGPL-2+"
SLOT="0"
KEYWORDS="~amd64 ~x86"
IUSE="test"
RESTRICT="!test? ( test )"

RDEPEND="
	dev-lang/perl
	sys-devel/binutils
	virtual/libelf:=
"
DEPEND="virtual/libelf:="
BDEPEND="
	dev-build/make
	dev-lang/perl
	sys-devel/binutils
"

# These executables are generated by the Oberon backend, not the C compiler.
QA_FLAGS_IGNORED="usr/lib.*/ulmo/(amd64|i386)/(ulmoc|obtofgen)"

ulmo_make() {
	emake -j1 \
		ARCH="${ARCH}" \
		PREFIX="${EPREFIX}/usr" \
		LIBDIR="${EPREFIX}/usr/$(get_libdir)" \
		CC="$(tc-getCC)" AR="$(tc-getAR)" \
		AS="$(tc-getAS)" LD="$(tc-getLD)" \
		"$@"
}

src_compile() {
	# The C converter accepts the normal compiler/linker flags. The Oberon
	# programs invoke ld directly, so they cannot accept GCC's -Wl flags.
	ulmo_make CFLAGS="${CFLAGS}" LDFLAGS="${LDFLAGS}" build/root/lib/ulmo/tof2elf
	ulmo_make LDFLAGS= all
}

src_test() {
	ulmo_make LDFLAGS= check check-runtime check-cli
}

src_install() {
	ulmo_make LDFLAGS= DESTDIR="${D}" install
	dodoc readme.md "release-notes-${PV}" WANTED
	doman man/man3/*.3
}