# Template file for 'yaboot'
pkgname=yaboot
version=1.3.17
revision=1
archs="ppc64 ppc64-musl ppc ppc-musl"
create_wrksrc=yes
build_style=gnu-makefile
hostmakedepends="autoconf automake"
depends="powerpc-utils hfsutils"
short_desc="Bootloader for PowerMac and IBM CHRP computers"
maintainer="Orphaned <orphan@voidlinux.org>"
license="GPL-2.0-or-later"
homepage="https://wiki.debian.org/YaBoot"
_e2fsprogs_ver="1.41.14"
distfiles="https://cdimage.debian.org/cdimage/cd-sources/yaboot_${version}.orig.tar.gz
 $KERNEL_SITE/kernel/people/tytso/e2fsprogs/e2fsprogs-${_e2fsprogs_ver}.tar.gz"
checksum="2fb738d8fd48b94ab2534a4fdf63738ad02c1a30f4b2add91f837baff8fa2aa1
 3f8ac1fedd7c4bec480afcbe4acabdd4ac59ec0446a0fd50c8975cd0aad7b176"
# Don't want stripped yaboot bits to leak into -dbg
nostrip_files=yaboot

# This is a safe choice available on all cross-hosts and ppc64 itself
# We need to compile yaboot binary itself, as well as e2fsprogs libraries,
# using a 32-bit ppc compiler; 64-bit with -m32 won't do because of libgcc etc.
case "$XBPS_TARGET_MACHINE" in
	ppc64*) _need_cross=powerpc-linux-musl ;;
esac

if [ -n "$_need_cross" ]; then
	hostmakedepends+=" cross-${_need_cross}"
fi

# No testsuite provided
do_check() {
	:
}

post_extract() {
	mv yaboot-${version}/* .
}

post_patch() {
	# Add support for DESTDIR in order to not leak files
	sed -i 's/^ROOT =/ROOT = $(DESTDIR)/' Makefile
}

# This builds e2fsprogs libraries, needed by yaboot for ext* support
# We need to build this using the potential 32-bit cross compiler, with
# specific minimal flags, so we can't just makedepend on it...
_build_e2fsprogs() {
	pushd e2fsprogs-${_e2fsprogs_ver}

	if [ ! -f Makefile ]; then
		# Regenerate autotools
		autoreconf -isf

		# Explicitly disable things we definitely don't need
		local _args="--disable-testio-debug --disable-debugfs --disable-tls"
		local _ecc="$CC" _ear="$AR"
		if [ -n "$_need_cross" -o -n "$CROSS_BUILD" ]; then
			# using a crosscompiler, set up configure/compiler for that
			_args+=" --host=${_need_cross:-$XBPS_CROSS_TRIPLET}"
			_args+=" --build=$XBPS_TRIPLET"
			_ecc="${_need_cross:-$XBPS_CROSS_TRIPLET}-gcc"
			_ear="${_need_cross:-$XBPS_CROSS_TRIPLET}-ar"
		fi

		# We need to keep flags synchronized with yaboot itself
		local _ecflags="-Os -fno-stack-protector -no-pie"
		_ecflags+=" -fgnu89-inline -fno-builtin-malloc -DNDEBUG"

		CFLAGS="$_ecflags" CC="$_ecc" AR="$_ear" ./configure ${_args}
	fi

	# Avoid building the whole thing, build only the two libs we need
	cd lib/et
	make ${makejobs}
	cd ../ext2fs
	make ${makejobs}

	cd ..
	cp libcom_err.a libext2fs.a ${wrksrc}

	popd
}

pre_build() {
	# Add toolchain prefix when cross-compiling; for userland tools
	# always, for yaboot itself when cross-compiling to ppc32
	if [ "$CROSS_BUILD" ]; then
		sed -i "s/^CROSS =/CROSS = ${XBPS_CROSS_TRIPLET}-/" Makefile
	fi

	# When using a separate cross-compiler (ppc64 target), override YBCC
	if [ -n "$_need_cross" ]; then
		sed -i "s/^YBCC =.*/YBCC = ${_need_cross}-gcc/" Makefile
	fi

	# These are needed by yaboot core
	_build_e2fsprogs
}

post_install() {
	# Yaboot is nostrip_files so do it manually (and ignore -dbg)
	if [ -n "$_need_cross" ]; then
		${_need_cross}-strip --remove-section=.comment \
			${DESTDIR}/usr/lib/yaboot/yaboot
	else
		$STRIP --remove-section=.comment ${DESTDIR}/usr/lib/yaboot/yaboot
	fi
}
