# Template file for 'grub'
pkgname=grub
version=2.14
revision=0
short_desc="Bootloader with support for BIOS and UEFI"
maintainer="finwo <finwo@pm.me>"
license="GPL-3.0-or-later"
homepage="https://www.gnu.org/software/grub/"
# NOTE: gnu publishes PGP .sig only, no sha256 file - TOFU pin.
distfiles="https://ftp.gnu.org/gnu/grub/grub-2.14.tar.xz"
checksum=bc8d3c73535b8838d8c8e2654d73edc4e6ae8c8acdb45d5df5dc9a1547446d43
# xz: grub-install links liblzma (readelf-proven, not guessed).
depends="xz"
makedepends="xz"
hostmakedepends="bison flex"

# Notes:
# - x86_64-efi ONLY in v1 (not i386-pc): grub 2.14's pc kernel.img links
#   .text at 0x9074 instead of the required 0x9000 under host binutils
#   2.44 (headers counted where old ld overlapped them), and grub-mkimage
#   fail-louds on it. Objects compile fine (-m32 codegen works); only the
#   flat core layout is wrong. Revisit when the toolchain allows: older
#   binutils, upstream fix, or a verified link flag - NOT by shipping
#   artifacts that build unbootable images. Modern whiteboxes and QEMU-OVMF
#   are UEFI anyway; legacy BIOS is a follow-up, not a blocker.
# - Single out-of-tree build. --disable-nls (no localized boot menus) and
#   --disable-werror (host gcc is newer than grub's vintage).
# - Inclusive install, then prune (grub-install needs its helpers at
#   runtime; cherry-picking risks breaking it). Pruned: info docs, themes,
#   locales, grub-mkconfig (UNOS writes a static grub.cfg - deterministic,
#   no probing). grub-script-check ships to validate that static config.
#   Man pages kept (tinyssh precedent).
# - Upstream defaults are trimmed ONLY where auto-detection would create
#   host-dependent runtime deps (same lesson as iproute2's scrub): no
#   custom console fonts (serial only, drops freetype), no TPM-sealed boot
#   (drops tasn1), no FUSE mount, no ZFS, no device-mapper.
# - CPPFLAGS/LDFLAGS put the sysroot first: configure "guesses" liblzma
#   via try-link, which would otherwise take host headers.
# - No `make check` (needs qemu RVA); CI story pending.
do_configure() {
  mkdir -p "${WORK}/build-efi"
  cd "${WORK}/build-efi"
  CPPFLAGS="-I${SYSROOT}/usr/include" LDFLAGS="-L${SYSROOT}/usr/lib" \
    "${WRKSRC}/configure" --prefix=/usr --with-platform=efi \
    --disable-nls --disable-werror \
    --disable-grub-mkfont --disable-grub-mount --disable-grub-protect \
    --disable-libzfs --disable-device-mapper
}

do_build() {
  make -C "${WORK}/build-efi" -j"$(nproc)"
  [ -x "${WORK}/build-efi/grub-mkimage" ] || die "grub: efi grub-mkimage missing"
}

do_install() {
  # install-strip: upstream binaries ship with debug_info; strip programs.
  # (.mod files install as data and stay unstripped - measured acceptable.)
  make -C "${WORK}/build-efi" install-strip DESTDIR="${DESTDIR}"
  # prune: docs, themes, locales - serial/console boot needs none of them
  rm -rf "${DESTDIR}/usr/share/info" "${DESTDIR}/usr/share/grub/themes" \
    "${DESTDIR}/usr/share/locale"
  # no probing config generation on a switch (see Notes)
  rm -f "${DESTDIR}"/usr/sbin/grub-mkconfig "${DESTDIR}"/usr/share/man/man8/grub-mkconfig.8
  [ -x "${DESTDIR}/usr/sbin/grub-install" ] || die "grub: grub-install missing"
  [ -d "${DESTDIR}/usr/lib/grub/x86_64-efi" ] || die "grub: x86_64-efi modules missing"
}
