# Template file for 'grub'
pkgname=grub
version=2.12
revision=2
hostmakedepends="python3 pkg-config flex freetype-devel font-unifont-bdf help2man
 automake gettext-devel-tools"
makedepends="libusb-compat-devel ncurses-devel freetype-devel liblzma-devel
 device-mapper-devel fuse-devel"
depends="os-prober"
conf_files="/etc/default/grub /etc/grub.d/*"
short_desc="GRand Unified Bootloader 2"
maintainer="Orphaned <orphan@voidlinux.org>"
license="GPL-3.0-or-later"
homepage="https://www.gnu.org/software/grub/"
changelog="https://git.savannah.gnu.org/cgit/grub.git/plain/NEWS"
distfiles="${GNU_SITE}/grub/grub-${version}.tar.xz"
checksum=f3c97391f7c4eaa677a78e090c7e97e6dc47b16f655f04683ebd37bef7fe0faa

archs="i686* x86_64* aarch64* ppc ppc-musl ppc64*"
nopie=yes

subpackages="grub-utils"
# _SUPPLEMENTARY_BUILDS is a list of <TARGET>-<PLATFORN> version of grub to build
case "$XBPS_TARGET_MACHINE" in
	x86_64*)
		_NATIVE_PLATFORM=pc
		_SUPPLEMENTARY_BUILDS="i386-efi x86_64-efi i386-coreboot x86_64-xen"
		subpackages+=" grub-i386-efi grub-x86_64-efi grub-i386-coreboot grub-xen"
		;;
	i686*)
		CFLAGS="-D_FILE_OFFSET_BITS=64"
		hostmakedepends+=" cross-x86_64-linux-musl"
		configure_args+=" ac_cv_sizeof_off_t=8"
		_NATIVE_PLATFORM=pc
		_SUPPLEMENTARY_BUILDS="i386-efi x86_64-efi i386-coreboot i386-xen"
		subpackages+=" grub-i386-efi grub-x86_64-efi grub-i386-coreboot grub-xen"
		;;
	aarch64*)
		_NATIVE_PLATFORM=efi
		subpackages+=" grub-arm64-efi"
		;;
	ppc*)
		_NATIVE_PLATFORM=ieee1275
		subpackages+=" grub-powerpc-ieee1275"
		;;
esac

pre_configure() {
	autoreconf -fi

	# XXX: empty file missing from 2.12 release
	touch grub-core/extra_deps.lst
}

do_configure() {
	# workaround for https://savannah.gnu.org/bugs/?60458
	# some more info: https://www.linuxquestions.org/questions/showthread.php?p=6257712
	# grub 2.06 reboots immediately when compiled with -O2,
	# only affects legacy BIOS
	export CFLAGS="${CFLAGS/-O2/-Os}"
	export CXXFLAGS="${CXXFLAGS/-O2/-Os}"

	unset CC AS LD RANLIB CPP
	local freestanding_cflags="-fno-stack-protector"

	# building with altivec generates broken grub core
	case "$XBPS_TARGET_MACHINE" in
		ppc*) freestanding_cflags+=" -mno-altivec" ;;
	esac
	CFLAGS+=" $freestanding_cflags"

	configure_args+=" --enable-device-mapper --enable-cache-stats --enable-nls
		--enable-grub-mkfont --enable-grub-mount --disable-werror
		--sbindir=/usr/bin"

	# build tools
	msg_normal "Configuring grub tools...\n"
	mkdir $wrksrc/build
	cd $wrksrc/build
	../configure --host=${XBPS_TARGET_MACHINE} ${configure_args} \
		${_NATIVE_PLATFORM:+--with-platform=${_NATIVE_PLATFORM}}

	for _SUPP_BUILD in ${_SUPPLEMENTARY_BUILDS}; do
		_TARGET=${_SUPP_BUILD%%-*}
		_PLATFORM=${_SUPP_BUILD##*-}
		msg_normal "Configuring ${_TARGET} ${_PLATFORM} grub...\n"
		mkdir $wrksrc/${_PLATFORM}_${_TARGET}_build
		cd $wrksrc/${_PLATFORM}_${_TARGET}_build
		if [ "$_TARGET" = x86_64 ] &&
			[ "${XBPS_TARGET_MACHINE%-musl}" = i686 ]; then
			_TARGET=x86_64-linux-musl
		fi
		CFLAGS="$freestanding_cflags" \
		../configure --host=${XBPS_TARGET_MACHINE} \
			--target=${_TARGET} \
			--with-platform=${_PLATFORM} ${configure_args} \
			--disable-efiemu \
			--libdir=/usr/lib
	done
}

do_build() {
	# XXX remove the strip wrapper
	rm ${XBPS_WRAPPERDIR}/strip

	msg_normal "Building grub tools...\n"
	cd $wrksrc/build
	make ${makejobs}

	for _SUPP_BUILD in ${_SUPPLEMENTARY_BUILDS}; do
		_TARGET=${_SUPP_BUILD%%-*}
		_PLATFORM=${_SUPP_BUILD##*-}
		msg_normal "Building ${_TARGET} ${_PLATFORM} grub...\n"
		cd $wrksrc/${_PLATFORM}_${_TARGET}_build
		make ${makejobs}
	done
}

do_install() {
	# XXX remove the strip wrapper
	rm ${XBPS_WRAPPERDIR}/strip

	for _SUPP_BUILD in ${_SUPPLEMENTARY_BUILDS}; do
		_TARGET=${_SUPP_BUILD%%-*}
		_GRUB_TARGET=${_TARGET}
		case "${_GRUB_TARGET}" in
		aarch64*)
			_GRUB_TARGET=arm64
			;;
		esac
		_PLATFORM=${_SUPP_BUILD##*-}
		cd $wrksrc/${_PLATFORM}_${_TARGET}_build
		make DESTDIR=$DESTDIR/${_PLATFORM}_${_TARGET} install
		# Remove non-platform specific files
		rm -rf ${DESTDIR}/${_PLATFORM}_${_TARGET}/{boot,etc,usr/{share,bin}}
		rm -f ${DESTDIR}/${_PLATFORM}_${_TARGET}/usr/lib/grub/${_GRUB_TARGET}-${_PLATFORM}/${_GRUB_TARGET}-*
		rm -f ${DESTDIR}/${_PLATFORM}_${_TARGET}/usr/lib/grub/${_GRUB_TARGET}-${_PLATFORM}/*.{module,image}
		cp -r ${DESTDIR}/${_PLATFORM}_${_TARGET}/* ${DESTDIR}
		rm -rf ${DESTDIR}/${_PLATFORM}_${_TARGET}
	done

	cd $wrksrc/build
	make DESTDIR=$DESTDIR install

	# Required to compress info files.
	vmkdir usr/share/info
	touch -f ${DESTDIR}/usr/share/info/dir

	vinstall ${FILESDIR}/grub.default 644 etc/default grub
	# Kernel hooks.
	vinstall ${FILESDIR}/kernel.d/grub.post 750 \
		etc/kernel.d/post-install 50-grub
	vinstall ${FILESDIR}/kernel.d/grub.post 750 \
		etc/kernel.d/post-remove 50-grub

	# update-grub for noobs.
	printf "#!/bin/sh\ngrub-mkconfig -o /boot/grub/grub.cfg\nexit \$?\n" >> \
		${DESTDIR}/usr/bin/update-grub
	chmod 755 ${DESTDIR}/usr/bin/update-grub

	vmkdir usr/share/bash-completion/completions
	mv ${DESTDIR}/etc/bash_completion.d/grub \
		${DESTDIR}/usr/share/bash-completion/completions

	# Remove useless tools
	rm ${DESTDIR}/usr/bin/grub-ofpathname
	rm ${DESTDIR}/usr/bin/grub-sparc64-setup
}

grub-utils_package() {
	short_desc+=" - additional utilities"
	depends="grub>=${version}"
	pkg_install() {
		vmove usr/bin/grub-menulst2cfg
		vmove usr/bin/grub-fstest
		vmove usr/bin/grub-mkfont
	}
}
grub-x86_64-efi_package() {
	depends="grub>=$version dosfstools efibootmgr"
	short_desc+=" - x86_64 EFI support"
	pkg_install() {
		vmove usr/lib/grub/x86_64-efi
	}
}
grub-i386-efi_package() {
	depends="grub>=$version dosfstools efibootmgr"
	short_desc+=" - i386 EFI support"
	pkg_install() {
		vmove usr/lib/grub/i386-efi
	}
}
grub-i386-coreboot_package() {
	depends="grub>=$version"
	short_desc+=" - i386 coreboot support"
	pkg_install() {
		vmove usr/lib/grub/i386-coreboot
	}
}
grub-xen_package() {
	depends="grub>=$version"
	short_desc+=" - Xen PV support"
	pkg_install() {
		case "$XBPS_TARGET_MACHINE" in
			x86_64*)
				vmove usr/lib/grub/x86_64-xen
				;;
			i686*)
				vmove usr/lib/grub/i386-xen
				;;
		esac
	}
}
grub-arm64-efi_package() {
	depends="grub>=$version dosfstools efibootmgr"
	short_desc+=" - arm64 EFI support"
	pkg_install() {
		vmove usr/lib/grub/arm64-efi
	}
}
grub-powerpc-ieee1275_package() {
	depends="grub>=$version powerpc-utils"
	short_desc+=" - powerpc Open Firmware support"
	pkg_install() {
		vmove usr/lib/grub/powerpc-ieee1275
	}
}
