diff mbox series

[v3,2/7] boot/grub2: add support to build multiple Grub2 configurations in the same build

Message ID 20210923155726.87851-3-kory.maincent@bootlin.com
State Accepted
Headers show
Series Add support for ISO9660 image compatible with Legacy and EFI BIOS | expand

Commit Message

Kory Maincent Sept. 23, 2021, 3:57 p.m. UTC
When Grub2 is build it is configured only for one boot set-up, BIOS Legacy,
EFI 32 bit or EFI 64 bit. It can not deal with several boot set-up on the
same image.

This patch allows to build Grub2 for different configurations simultaneously.
To cover Grub2 configuration of legacy BIOS platforms (32-bit), 32-bit EFI
BIOS and 64-bit EFI BIOS in the same build, multi-build system felt much more
reasonable to just extend the grub2 package into 3 packages.

We can no longer use autotools-package as a consequence of this multi-build, and
we have to resort to generic-package and a partial duplication of
the autotools-infra. Grub2 was already using custom option like --prefix or
--exec-prefix so this won't add much more weirdness.

We use a GRUB2_TUPLES list to describe all the configurations selected.
For each boot case described in the GRUB2_TUPLES list, it configures and
builds Grub2 in a separate folder named build-$(tuple).
We use a foreach loop to make actions on each tuple selected.

We have to separate the BR2_TARGET_GRUB2_BUILTIN_MODULES and the
BR2_TARGET_GRUB2_BUILTIN_CONFIG for each BIOS or EFI boot cases.

Signed-off-by: Kory Maincent <kory.maincent@bootlin.com>
---

Changes in v2:
Following the review from Yann E. Morin:
- Update the tuples configurations to makes the code more readable.
- Move "--target" and "--with-platform" parameters into the
  GRUB2_CONFIGURE_CMD.
- Fix startup.nsh installation.
- Fix Grub2 configuration to be sure that at least one platform is always enabled.
- Fix legacy builtin configurations to string in place of bool.

Changes in v3:
Following the review from Yann E. Morin:
- Update the tuples configurations to remove the conditions and makes it
  more readable.
- Update the tuples configurations for builtin config and modules options.
- Fix typo.
- Remove the generation of startup.nsh.

 Config.in.legacy                         |  24 ++++
 boot/grub2/Config.in                     |  53 +++++--
 boot/grub2/grub2.mk                      | 175 +++++++++++++----------
 support/testing/tests/fs/test_iso9660.py |   6 +-
 4 files changed, 169 insertions(+), 89 deletions(-)

Comments

Yann E. MORIN Oct. 6, 2021, 6:11 p.m. UTC | #1
Köry, All,

On 2021-09-23 17:57 +0200, Kory Maincent spake thusly:
[--SNIP--]
> We can no longer use autotools-package as a consequence of this multi-build, and
> we have to resort to generic-package and a partial duplication of
> the autotools-infra. Grub2 was already using custom option like --prefix or
> --exec-prefix so this won't add much more weirdness.

But this had an unfortunate side effect: the grub2 tools, like
grub-editenv, enabled with BR2_TARGET_GRUB2_INSTALL_TOOLS, are no
longer installed in the target now.

Indeed, it worked so far because there is a default target install
command for autotools packages, which does basivally call
    make install DESTDIR=$(TARGET_DIR)

But generic-package has no default install command, so declaring that
the package installs in target does nothing.

I see two options:

 1. provide a custom command for one of the enabled platforms; e.g.
    arbitrarily choose the first in the tuple and install in target
    from that one tuple

 2. revert to using an autotools package, but build for no platform at
    all and just install the tools. For the platforms, move the current
    foreach loops into post-build and post-install-image hooks

Care to look into that, please?

Regards,
Yann E. MORIN.

> We use a GRUB2_TUPLES list to describe all the configurations selected.
> For each boot case described in the GRUB2_TUPLES list, it configures and
> builds Grub2 in a separate folder named build-$(tuple).
> We use a foreach loop to make actions on each tuple selected.
> 
> We have to separate the BR2_TARGET_GRUB2_BUILTIN_MODULES and the
> BR2_TARGET_GRUB2_BUILTIN_CONFIG for each BIOS or EFI boot cases.
> 
> Signed-off-by: Kory Maincent <kory.maincent@bootlin.com>
> ---
> 
> Changes in v2:
> Following the review from Yann E. Morin:
> - Update the tuples configurations to makes the code more readable.
> - Move "--target" and "--with-platform" parameters into the
>   GRUB2_CONFIGURE_CMD.
> - Fix startup.nsh installation.
> - Fix Grub2 configuration to be sure that at least one platform is always enabled.
> - Fix legacy builtin configurations to string in place of bool.
> 
> Changes in v3:
> Following the review from Yann E. Morin:
> - Update the tuples configurations to remove the conditions and makes it
>   more readable.
> - Update the tuples configurations for builtin config and modules options.
> - Fix typo.
> - Remove the generation of startup.nsh.
> 
>  Config.in.legacy                         |  24 ++++
>  boot/grub2/Config.in                     |  53 +++++--
>  boot/grub2/grub2.mk                      | 175 +++++++++++++----------
>  support/testing/tests/fs/test_iso9660.py |   6 +-
>  4 files changed, 169 insertions(+), 89 deletions(-)
> 
> diff --git a/Config.in.legacy b/Config.in.legacy
> index 35a11f4dc6..33cee0202b 100644
> --- a/Config.in.legacy
> +++ b/Config.in.legacy
> @@ -168,6 +168,30 @@ config BR2_KERNEL_HEADERS_5_12
>  
>  comment "Legacy options removed in 2021.08"
>  
> +config BR2_TARGET_GRUB2_BUILTIN_MODULES
> +	string "the grub2 builtin modules has been renamed"
> +	help
> +	  This option has been split to separate the builtin modules
> +	  between BR2_TARGET_GRUB2_BUILTIN_MODULES_PC and
> +	  BR2_TARGET_GRUB2_BUILTIN_MODULES_EFI.
> +
> +config BR2_TARGET_GRUB2_BUILTIN_MODULES_WRAP
> +	bool
> +	default y if BR2_TARGET_GRUB2_BUILTIN_MODULES != ""
> +	select BR2_LEGACY
> +
> +config BR2_TARGET_GRUB2_BUILTIN_CONFIG
> +	string "the grub2 builtin configuration has been renamed"
> +	help
> +	  This option has been split to separate the builtin configuration
> +	  between BR2_TARGET_GRUB2_BUILTIN_CONFIG_PC and
> +	  BR2_TARGET_GRUB2_BUILTIN_CONFIG_EFI.
> +
> +config BR2_TARGET_GRUB2_BUILTIN_CONFIG_WRAP
> +	bool
> +	default y if BR2_TARGET_GRUB2_BUILTIN_CONFIG != ""
> +	select BR2_LEGACY
> +
>  config BR2_PACKAGE_LIBMCRYPT
>  	bool "libmcrypt package was removed"
>  	select BR2_LEGACY
> diff --git a/boot/grub2/Config.in b/boot/grub2/Config.in
> index e45133999e..10bba6c5e4 100644
> --- a/boot/grub2/Config.in
> +++ b/boot/grub2/Config.in
> @@ -10,6 +10,13 @@ config BR2_TARGET_GRUB2
>  	bool "grub2"
>  	depends on BR2_TARGET_GRUB2_ARCH_SUPPORTS
>  	depends on BR2_USE_WCHAR
> +	select BR2_TARGET_GRUB2_I386_PC if \
> +		!BR2_TARGET_GRUB2_HAS_PTF && \
> +		(BR2_i386 || BR2_x86_64)
> +	select BR2_TARGET_GRUB2_ARM_UBOOT if \
> +		!BR2_TARGET_GRUB2_HAS_PTF && \
> +		BR2_arm
> +	select BR2_TARGET_GRUB2_ARM64_EFI if BR2_aarch64
>  	help
>  	  GNU GRUB is a Multiboot boot loader. It was derived from
>  	  GRUB, the GRand Unified Bootloader, which was originally
> @@ -25,10 +32,10 @@ config BR2_TARGET_GRUB2
>  
>  	  http://www.gnu.org/software/grub/
>  
> -if BR2_TARGET_GRUB2
> +config BR2_TARGET_GRUB2_HAS_PTF
> +	bool
>  
> -choice
> -	prompt "Platform"
> +if BR2_TARGET_GRUB2
>  
>  config BR2_TARGET_GRUB2_I386_PC
>  	bool "i386-pc"
> @@ -40,6 +47,7 @@ config BR2_TARGET_GRUB2_I386_PC
>  config BR2_TARGET_GRUB2_I386_EFI
>  	bool "i386-efi"
>  	depends on BR2_i386 || BR2_x86_64
> +	select BR2_TARGET_GRUB2_HAS_PTF
>  	help
>  	  Select this option if the platform you're targetting has a
>  	  32 bits EFI BIOS. Note that some x86-64 platforms use a 32
> @@ -48,6 +56,7 @@ config BR2_TARGET_GRUB2_I386_EFI
>  config BR2_TARGET_GRUB2_X86_64_EFI
>  	bool "x86-64-efi"
>  	depends on BR2_x86_64
> +	select BR2_TARGET_GRUB2_HAS_PTF
>  	help
>  	  Select this option if the platform you're targetting has a
>  	  64 bits EFI BIOS.
> @@ -63,6 +72,7 @@ config BR2_TARGET_GRUB2_ARM_UBOOT
>  config BR2_TARGET_GRUB2_ARM_EFI
>  	bool "arm-efi"
>  	depends on BR2_arm
> +	select BR2_TARGET_GRUB2_HAS_PTF
>  	help
>  	  Select this option if the platform you're targetting is an
>  	  ARM platform and you want to boot Grub 2 as an EFI
> @@ -76,10 +86,10 @@ config BR2_TARGET_GRUB2_ARM64_EFI
>  	  Aarch64 platform and you want to boot Grub 2 as an EFI
>  	  application.
>  
> -endchoice
> -
>  if BR2_TARGET_GRUB2_I386_PC || BR2_TARGET_GRUB2_ARM_UBOOT
>  
> +comment "Options for the x86 legacy BIOS or ARM U-Boot support"
> +
>  config BR2_TARGET_GRUB2_BOOT_PARTITION
>  	string "boot partition"
>  	default "hd0,msdos1"
> @@ -89,24 +99,43 @@ config BR2_TARGET_GRUB2_BOOT_PARTITION
>  	  first disk if using a legacy partition table, or 'hd0,gpt1'
>  	  if using GPT partition table.
>  
> -endif # BR2_TARGET_GRUB2_I386_PC || BR2_TARGET_GRUB2_ARM_UBOOT
> -
> -config BR2_TARGET_GRUB2_BUILTIN_MODULES
> +config BR2_TARGET_GRUB2_BUILTIN_MODULES_PC
>  	string "builtin modules"
> +	default BR2_TARGET_GRUB2_BUILTIN_MODULES if BR2_TARGET_GRUB2_BUILTIN_MODULES != "" # legacy
>  	default "boot linux ext2 fat squash4 part_msdos part_gpt normal biosdisk" if BR2_TARGET_GRUB2_I386_PC
> -	default "boot linux ext2 fat squash4 part_msdos part_gpt normal efi_gop" \
> -		if BR2_TARGET_GRUB2_I386_EFI || BR2_TARGET_GRUB2_X86_64_EFI || \
> -		   BR2_TARGET_GRUB2_ARM_EFI  || BR2_TARGET_GRUB2_ARM64_EFI
>  	default "linux ext2 fat part_msdos normal" if BR2_TARGET_GRUB2_ARM_UBOOT
>  
> -config BR2_TARGET_GRUB2_BUILTIN_CONFIG
> +config BR2_TARGET_GRUB2_BUILTIN_CONFIG_PC
> +	string "builtin config"
> +	default BR2_TARGET_GRUB2_BUILTIN_CONFIG if BR2_TARGET_GRUB2_BUILTIN_CONFIG != "" # legacy
> +	help
> +	  Path to a Grub 2 configuration file that will be embedded
> +	  into the Grub image itself. This allows to set the root
> +	  device and other configuration parameters, but however menu
> +	  entries cannot be described in this embedded configuration.
> +
> +endif # BR2_TARGET_GRUB2_I386_PC || BR2_TARGET_GRUB2_ARM_UBOOT
> +
> +if BR2_TARGET_GRUB2_I386_EFI || BR2_TARGET_GRUB2_X86_64_EFI || \
> +	BR2_TARGET_GRUB2_ARM_EFI  || BR2_TARGET_GRUB2_ARM64_EFI
> +
> +comment "Options for the EFI BIOS or ARM EFI support"
> +
> +config BR2_TARGET_GRUB2_BUILTIN_MODULES_EFI
> +	string "builtin modules"
> +	default BR2_TARGET_GRUB2_BUILTIN_MODULES if BR2_TARGET_GRUB2_BUILTIN_MODULES != "" # legacy
> +	default "boot linux ext2 fat squash4 part_msdos part_gpt normal efi_gop"
> +
> +config BR2_TARGET_GRUB2_BUILTIN_CONFIG_EFI
>  	string "builtin config"
> +	default BR2_TARGET_GRUB2_BUILTIN_CONFIG if BR2_TARGET_GRUB2_BUILTIN_CONFIG != "" # legacy
>  	help
>  	  Path to a Grub 2 configuration file that will be embedded
>  	  into the Grub image itself. This allows to set the root
>  	  device and other configuration parameters, but however menu
>  	  entries cannot be described in this embedded configuration.
>  
> +endif
>  config BR2_TARGET_GRUB2_INSTALL_TOOLS
>  	bool "install tools"
>  	help
> diff --git a/boot/grub2/grub2.mk b/boot/grub2/grub2.mk
> index ad2edf17aa..e01ebb2edb 100644
> --- a/boot/grub2/grub2.mk
> +++ b/boot/grub2/grub2.mk
> @@ -57,53 +57,65 @@ GRUB2_INSTALL_TARGET = NO
>  endif
>  GRUB2_CPE_ID_VENDOR = gnu
>  
> -GRUB2_BUILTIN_MODULES = $(call qstrip,$(BR2_TARGET_GRUB2_BUILTIN_MODULES))
> -GRUB2_BUILTIN_CONFIG = $(call qstrip,$(BR2_TARGET_GRUB2_BUILTIN_CONFIG))
> +GRUB2_BUILTIN_MODULES_PC = $(call qstrip,$(BR2_TARGET_GRUB2_BUILTIN_MODULES_PC))
> +GRUB2_BUILTIN_MODULES_EFI = $(call qstrip,$(BR2_TARGET_GRUB2_BUILTIN_MODULES_EFI))
> +GRUB2_BUILTIN_CONFIG_PC = $(call qstrip,$(BR2_TARGET_GRUB2_BUILTIN_CONFIG_PC))
> +GRUB2_BUILTIN_CONFIG_EFI = $(call qstrip,$(BR2_TARGET_GRUB2_BUILTIN_CONFIG_EFI))
>  GRUB2_BOOT_PARTITION = $(call qstrip,$(BR2_TARGET_GRUB2_BOOT_PARTITION))
>  
> -ifeq ($(BR2_TARGET_GRUB2_I386_PC),y)
> -GRUB2_IMAGE = $(BINARIES_DIR)/grub.img
> -GRUB2_CFG = $(TARGET_DIR)/boot/grub/grub.cfg
> -GRUB2_PREFIX = ($(GRUB2_BOOT_PARTITION))/boot/grub
> -GRUB2_TUPLE = i386-pc
> -GRUB2_TARGET = i386
> -GRUB2_PLATFORM = pc
> -else ifeq ($(BR2_TARGET_GRUB2_I386_EFI),y)
> -GRUB2_IMAGE = $(BINARIES_DIR)/efi-part/EFI/BOOT/bootia32.efi
> -GRUB2_CFG = $(BINARIES_DIR)/efi-part/EFI/BOOT/grub.cfg
> -GRUB2_PREFIX = /EFI/BOOT
> -GRUB2_TUPLE = i386-efi
> -GRUB2_TARGET = i386
> -GRUB2_PLATFORM = efi
> -else ifeq ($(BR2_TARGET_GRUB2_X86_64_EFI),y)
> -GRUB2_IMAGE = $(BINARIES_DIR)/efi-part/EFI/BOOT/bootx64.efi
> -GRUB2_CFG = $(BINARIES_DIR)/efi-part/EFI/BOOT/grub.cfg
> -GRUB2_PREFIX = /EFI/BOOT
> -GRUB2_TUPLE = x86_64-efi
> -GRUB2_TARGET = x86_64
> -GRUB2_PLATFORM = efi
> -else ifeq ($(BR2_TARGET_GRUB2_ARM_UBOOT),y)
> -GRUB2_IMAGE = $(BINARIES_DIR)/boot-part/grub/grub.img
> -GRUB2_CFG = $(BINARIES_DIR)/boot-part/grub/grub.cfg
> -GRUB2_PREFIX = ($(GRUB2_BOOT_PARTITION))/boot/grub
> -GRUB2_TUPLE = arm-uboot
> -GRUB2_TARGET = arm
> -GRUB2_PLATFORM = uboot
> -else ifeq ($(BR2_TARGET_GRUB2_ARM_EFI),y)
> -GRUB2_IMAGE = $(BINARIES_DIR)/efi-part/EFI/BOOT/bootarm.efi
> -GRUB2_CFG = $(BINARIES_DIR)/efi-part/EFI/BOOT/grub.cfg
> -GRUB2_PREFIX = /EFI/BOOT
> -GRUB2_TUPLE = arm-efi
> -GRUB2_TARGET = arm
> -GRUB2_PLATFORM = efi
> -else ifeq ($(BR2_TARGET_GRUB2_ARM64_EFI),y)
> -GRUB2_IMAGE = $(BINARIES_DIR)/efi-part/EFI/BOOT/bootaa64.efi
> -GRUB2_CFG = $(BINARIES_DIR)/efi-part/EFI/BOOT/grub.cfg
> -GRUB2_PREFIX = /EFI/BOOT
> -GRUB2_TUPLE = arm64-efi
> -GRUB2_TARGET = aarch64
> -GRUB2_PLATFORM = efi
> -endif
> +GRUB2_IMAGE_i386-pc = $(BINARIES_DIR)/grub.img
> +GRUB2_CFG_i386-pc = $(TARGET_DIR)/boot/grub/grub.cfg
> +GRUB2_PREFIX_i386-pc = ($(GRUB2_BOOT_PARTITION))/boot/grub
> +GRUB2_TARGET_i386-pc = i386
> +GRUB2_PLATFORM_i386-pc = pc
> +GRUB2_BUILTIN_CONFIG_i386-pc = $(GRUB2_BUILTIN_CONFIG_PC)
> +GRUB2_BUILTIN_MODULES_i386-pc = $(GRUB2_BUILTIN_MODULES_PC)
> +GRUB2_TUPLES-$(BR2_TARGET_GRUB2_I386_PC) += i386-pc
> +
> +GRUB2_IMAGE_i386-efi = $(BINARIES_DIR)/efi-part/EFI/BOOT/bootia32.efi
> +GRUB2_CFG_i386-efi = $(BINARIES_DIR)/efi-part/EFI/BOOT/grub.cfg
> +GRUB2_PREFIX_i386-efi = /EFI/BOOT
> +GRUB2_TARGET_i386-efi = i386
> +GRUB2_PLATFORM_i386-efi = efi
> +GRUB2_BUILTIN_CONFIG_i386-efi = $(GRUB2_BUILTIN_CONFIG_EFI)
> +GRUB2_BUILTIN_MODULES_i386-efi = $(GRUB2_BUILTIN_MODULES_EFI)
> +GRUB2_TUPLES-$(BR2_TARGET_GRUB2_I386_EFI) += i386-efi
> +
> +GRUB2_IMAGE_x86_64-efi = $(BINARIES_DIR)/efi-part/EFI/BOOT/bootx64.efi
> +GRUB2_CFG_x86_64-efi = $(BINARIES_DIR)/efi-part/EFI/BOOT/grub.cfg
> +GRUB2_PREFIX_x86_64-efi = /EFI/BOOT
> +GRUB2_TARGET_x86_64-efi = x86_64
> +GRUB2_PLATFORM_x86_64-efi = efi
> +GRUB2_BUILTIN_CONFIG_x86_64-efi = $(GRUB2_BUILTIN_CONFIG_EFI)
> +GRUB2_BUILTIN_MODULES_x86_64-efi = $(GRUB2_BUILTIN_MODULES_EFI)
> +GRUB2_TUPLES-$(BR2_TARGET_GRUB2_X86_64_EFI) += x86_64-efi
> +
> +GRUB2_IMAGE_arm-uboot = $(BINARIES_DIR)/boot-part/grub/grub.img
> +GRUB2_CFG_arm-uboot = $(BINARIES_DIR)/boot-part/grub/grub.cfg
> +GRUB2_PREFIX_arm-uboot = ($(GRUB2_BOOT_PARTITION))/boot/grub
> +GRUB2_TARGET_arm-uboot = arm
> +GRUB2_PLATFORM_arm-uboot = uboot
> +GRUB2_BUILTIN_CONFIG_arm-uboot = $(GRUB2_BUILTIN_CONFIG_PC)
> +GRUB2_BUILTIN_MODULES_arm-uboot = $(GRUB2_BUILTIN_MODULES_PC)
> +GRUB2_TUPLES-$(BR2_TARGET_GRUB2_ARM_UBOOT) += arm-uboot
> +
> +GRUB2_IMAGE_arm-efi = $(BINARIES_DIR)/efi-part/EFI/BOOT/bootarm.efi
> +GRUB2_CFG_arm-efi = $(BINARIES_DIR)/efi-part/EFI/BOOT/grub.cfg
> +GRUB2_PREFIX_arm-efi = /EFI/BOOT
> +GRUB2_TARGET_arm-efi = arm
> +GRUB2_PLATFORM_arm-efi = efi
> +GRUB2_BUILTIN_CONFIG_arm-efi = $(GRUB2_BUILTIN_CONFIG_EFI)
> +GRUB2_BUILTIN_MODULES_arm-efi = $(GRUB2_BUILTIN_MODULES_EFI)
> +GRUB2_TUPLES-$(BR2_TARGET_GRUB2_ARM_EFI) += arm-efi
> +
> +GRUB2_IMAGE_arm64-efi = $(BINARIES_DIR)/efi-part/EFI/BOOT/bootaa64.efi
> +GRUB2_CFG_arm64-efi = $(BINARIES_DIR)/efi-part/EFI/BOOT/grub.cfg
> +GRUB2_PREFIX_arm64-efi = /EFI/BOOT
> +GRUB2_TARGET_arm64-efi = aarch64
> +GRUB2_PLATFORM_arm64-efi = efi
> +GRUB2_BUILTIN_CONFIG_arm64-efi = $(GRUB2_BUILTIN_CONFIG_EFI)
> +GRUB2_BUILTIN_MODULES_arm64-efi = $(GRUB2_BUILTIN_MODULES_EFI)
> +GRUB2_TUPLES-$(BR2_TARGET_GRUB2_ARM64_EFI) += arm64-efi
>  
>  # Grub2 is kind of special: it considers CC, LD and so on to be the
>  # tools to build the host programs and uses TARGET_CC, TARGET_CFLAGS,
> @@ -127,18 +139,6 @@ GRUB2_CONF_ENV = \
>  	TARGET_OBJCOPY="$(TARGET_OBJCOPY)" \
>  	TARGET_STRIP="$(TARGET_CROSS)strip"
>  
> -GRUB2_CONF_OPTS = \
> -	--target=$(GRUB2_TARGET) \
> -	--with-platform=$(GRUB2_PLATFORM) \
> -	--prefix=/ \
> -	--exec-prefix=/ \
> -	--disable-grub-mkfont \
> -	--enable-efiemu=no \
> -	ac_cv_lib_lzma_lzma_code=no \
> -	--enable-device-mapper=no \
> -	--enable-libzfs=no \
> -	--disable-werror
> -
>  HOST_GRUB2_CONF_OPTS = \
>  	--disable-grub-mkfont \
>  	--enable-efiemu=no \
> @@ -147,26 +147,53 @@ HOST_GRUB2_CONF_OPTS = \
>  	--enable-libzfs=no \
>  	--disable-werror
>  
> -ifeq ($(BR2_TARGET_GRUB2_I386_PC),y)
> -define GRUB2_IMAGE_INSTALL_ELTORITO
> -	cat $(HOST_DIR)/lib/grub/$(GRUB2_TUPLE)/cdboot.img $(GRUB2_IMAGE) > \
> -		$(BINARIES_DIR)/grub-eltorito.img
> +define GRUB2_CONFIGURE_CMDS
> +	$(foreach tuple, $(GRUB2_TUPLES-y), \
> +		mkdir -p $(@D)/build-$(tuple) ; \
> +		cd $(@D)/build-$(tuple) ; \
> +		$(TARGET_CONFIGURE_OPTS) \
> +		$(TARGET_CONFIGURE_ARGS) \
> +		$(GRUB2_CONF_ENV) \
> +		../configure \
> +			--target=$(GRUB2_TARGET_$(tuple)) \
> +			--with-platform=$(GRUB2_PLATFORM_$(tuple)) \
> +			--host=$(GNU_TARGET_NAME) \
> +			--build=$(GNU_HOST_NAME) \
> +			--prefix=/ \
> +			--exec-prefix=/ \
> +			--disable-grub-mkfont \
> +			--enable-efiemu=no \
> +			ac_cv_lib_lzma_lzma_code=no \
> +			--enable-device-mapper=no \
> +			--enable-libzfs=no \
> +			--disable-werror
> +	)
> +endef
> +
> +define GRUB2_BUILD_CMDS
> +	$(foreach tuple, $(GRUB2_TUPLES-y), \
> +		$(TARGET_MAKE_ENV) $(MAKE) -C $(@D)/build-$(tuple)
> +	)
>  endef
> -endif
>  
>  define GRUB2_INSTALL_IMAGES_CMDS
> -	mkdir -p $(dir $(GRUB2_IMAGE))
> -	$(HOST_DIR)/usr/bin/grub-mkimage \
> -		-d $(@D)/grub-core/ \
> -		-O $(GRUB2_TUPLE) \
> -		-o $(GRUB2_IMAGE) \
> -		-p "$(GRUB2_PREFIX)" \
> -		$(if $(GRUB2_BUILTIN_CONFIG),-c $(GRUB2_BUILTIN_CONFIG)) \
> -		$(GRUB2_BUILTIN_MODULES)
> -	mkdir -p $(dir $(GRUB2_CFG))
> -	$(INSTALL) -D -m 0644 boot/grub2/grub.cfg $(GRUB2_CFG)
> -	$(GRUB2_IMAGE_INSTALL_ELTORITO)
> +	$(foreach tuple, $(GRUB2_TUPLES-y), \
> +		mkdir -p $(dir $(GRUB2_IMAGE_$(tuple))) ; \
> +		$(HOST_DIR)/usr/bin/grub-mkimage \
> +			-d $(@D)/build-$(tuple)/grub-core/ \
> +			-O $(tuple) \
> +			-o $(GRUB2_IMAGE_$(tuple)) \
> +			-p "$(GRUB2_PREFIX_$(tuple))" \
> +			$(if $(GRUB2_BUILTIN_CONFIG_$(tuple)), \
> +				-c $(GRUB2_BUILTIN_CONFIG_$(tuple))) \
> +			$(GRUB2_BUILTIN_MODULES_$(tuple)) ; \
> +		$(INSTALL) -D -m 0644 boot/grub2/grub.cfg $(GRUB2_CFG_$(tuple)) ; \
> +		$(if $(findstring $(GRUB2_PLATFORM_$(tuple)), pc), \
> +			cat $(HOST_DIR)/lib/grub/$(tuple)/cdboot.img $(GRUB2_IMAGE_$(tuple)) > \
> +				$(BINARIES_DIR)/grub-eltorito.img ; \
> +		) \
> +	)
>  endef
>  
> -$(eval $(autotools-package))
> +$(eval $(generic-package))
>  $(eval $(host-autotools-package))
> diff --git a/support/testing/tests/fs/test_iso9660.py b/support/testing/tests/fs/test_iso9660.py
> index 68f4840852..1b699e1fef 100644
> --- a/support/testing/tests/fs/test_iso9660.py
> +++ b/support/testing/tests/fs/test_iso9660.py
> @@ -54,7 +54,7 @@ class TestIso9660Grub2External(infra.basetest.BRTest):
>          # BR2_TARGET_ROOTFS_ISO9660_INITRD is not set
>          BR2_TARGET_GRUB2=y
>          BR2_TARGET_GRUB2_BOOT_PARTITION="cd"
> -        BR2_TARGET_GRUB2_BUILTIN_MODULES="boot linux ext2 fat part_msdos part_gpt normal biosdisk iso9660"
> +        BR2_TARGET_GRUB2_BUILTIN_MODULES_PC="boot linux ext2 fat part_msdos part_gpt normal biosdisk iso9660"
>          BR2_TARGET_ROOTFS_ISO9660_BOOT_MENU="{}"
>          """.format(infra.filepath("conf/grub2.cfg"))
>  
> @@ -75,7 +75,7 @@ class TestIso9660Grub2ExternalCompress(infra.basetest.BRTest):
>          BR2_TARGET_ROOTFS_ISO9660_TRANSPARENT_COMPRESSION=y
>          BR2_TARGET_GRUB2=y
>          BR2_TARGET_GRUB2_BOOT_PARTITION="cd"
> -        BR2_TARGET_GRUB2_BUILTIN_MODULES="boot linux ext2 fat part_msdos part_gpt normal biosdisk iso9660"
> +        BR2_TARGET_GRUB2_BUILTIN_MODULES_PC="boot linux ext2 fat part_msdos part_gpt normal biosdisk iso9660"
>          BR2_TARGET_ROOTFS_ISO9660_BOOT_MENU="{}"
>          """.format(infra.filepath("conf/grub2.cfg"))
>  
> @@ -95,7 +95,7 @@ class TestIso9660Grub2Internal(infra.basetest.BRTest):
>          BR2_TARGET_ROOTFS_ISO9660_INITRD=y
>          BR2_TARGET_GRUB2=y
>          BR2_TARGET_GRUB2_BOOT_PARTITION="cd"
> -        BR2_TARGET_GRUB2_BUILTIN_MODULES="boot linux ext2 fat part_msdos part_gpt normal biosdisk iso9660"
> +        BR2_TARGET_GRUB2_BUILTIN_MODULES_PC="boot linux ext2 fat part_msdos part_gpt normal biosdisk iso9660"
>          BR2_TARGET_ROOTFS_ISO9660_BOOT_MENU="{}"
>          """.format(infra.filepath("conf/grub2.cfg"))
>  
> -- 
> 2.25.1
> 
> _______________________________________________
> buildroot mailing list
> buildroot@lists.buildroot.org
> https://lists.buildroot.org/mailman/listinfo/buildroot
Kory Maincent Oct. 7, 2021, 8:23 a.m. UTC | #2
On Wed, 6 Oct 2021 20:11:04 +0200
"Yann E. MORIN" <yann.morin.1998@free.fr> wrote:

> Köry, All,
> 
> On 2021-09-23 17:57 +0200, Kory Maincent spake thusly:
> [--SNIP--]
> > We can no longer use autotools-package as a consequence of this
> > multi-build, and we have to resort to generic-package and a partial
> > duplication of the autotools-infra. Grub2 was already using custom option
> > like --prefix or --exec-prefix so this won't add much more weirdness.  
> 
> But this had an unfortunate side effect: the grub2 tools, like
> grub-editenv, enabled with BR2_TARGET_GRUB2_INSTALL_TOOLS, are no
> longer installed in the target now.

Doh, didn't thought of that!
 
> But generic-package has no default install command, so declaring that
> the package installs in target does nothing.
> 
> I see two options:
> 
>  1. provide a custom command for one of the enabled platforms; e.g.
>     arbitrarily choose the first in the tuple and install in target
>     from that one tuple
> 
>  2. revert to using an autotools package, but build for no platform at
>     all and just install the tools. For the platforms, move the current
>     foreach loops into post-build and post-install-image hooks
> 
> Care to look into that, please?

mmh that's tricky, because in the case we want to use the Grub modules it need
to be generated for each platforms.
For example we can face a case where we will have these three Grub modules
repositories for each platform:
target/lib/grub/i386-pc/
target/lib/grub/i386-efi/
target/lib/grub/x86_64-efi/

What about just adding the "make install" command in the foreach loop?
This will overwrite the binaries, the locale and the /etc configurations for
each "make" loop but we will have all the Grub modules repositories. I don't
think the overwrites will cause issues because it is just tools.

Regards,

Köry
Yann E. MORIN Oct. 7, 2021, 9:53 a.m. UTC | #3
Köry, All,

On 2021-10-07 10:23 +0200, Köry Maincent spake thusly:
> On Wed, 6 Oct 2021 20:11:04 +0200
> "Yann E. MORIN" <yann.morin.1998@free.fr> wrote:
> > On 2021-09-23 17:57 +0200, Kory Maincent spake thusly:
> > [--SNIP--]
> > > We can no longer use autotools-package as a consequence of this
> > > multi-build, and we have to resort to generic-package and a partial
> > > duplication of the autotools-infra. Grub2 was already using custom option
> > > like --prefix or --exec-prefix so this won't add much more weirdness.  
> > But this had an unfortunate side effect: the grub2 tools, like
> > grub-editenv, enabled with BR2_TARGET_GRUB2_INSTALL_TOOLS, are no
> > longer installed in the target now.
> Doh, didn't thought of that!

Neither did I...

[--SNIP--]
> > Care to look into that, please?
> 
> mmh that's tricky, because in the case we want to use the Grub modules it need
> to be generated for each platforms.
> For example we can face a case where we will have these three Grub modules
> repositories for each platform:
> target/lib/grub/i386-pc/
> target/lib/grub/i386-efi/
> target/lib/grub/x86_64-efi/
> 
> What about just adding the "make install" command in the foreach loop?
> This will overwrite the binaries, the locale and the /etc configurations for
> each "make" loop but we will have all the Grub modules repositories. I don't
> think the overwrites will cause issues because it is just tools.

I was thinking about something along the lines of:

    diff --git a/boot/grub2/grub2.mk b/boot/grub2/grub2.mk
    index e01ebb2edb..bb853cace6 100644
    --- a/boot/grub2/grub2.mk
    +++ b/boot/grub2/grub2.mk
    @@ -127,6 +127,9 @@ GRUB2_TUPLES-$(BR2_TARGET_GRUB2_ARM64_EFI) +=
    arm64-efi
     HOST_GRUB2_CONF_ENV = \
        CPP="$(HOSTCC) -E"
     
    +# Main build: only build tools
    +GRUB2_CONF_OPTS = --with-platform=none --blabla...
    +
     GRUB2_CONF_ENV = \
        CPP="$(TARGET_CC) -E" \
        TARGET_CC="$(TARGET_CC)" \
    @@ -147,7 +150,7 @@ HOST_GRUB2_CONF_OPTS = \
        --enable-libzfs=no \
        --disable-werror
     
    -define GRUB2_CONFIGURE_CMDS
    +define GRUB2_CONFIGURE_CMDS_PTF
        $(foreach tuple, $(GRUB2_TUPLES-y), \
            mkdir -p $(@D)/build-$(tuple) ; \
            cd $(@D)/build-$(tuple) ; \
    @@ -169,14 +172,16 @@ define GRUB2_CONFIGURE_CMDS
                --disable-werror
        )
     endef
    +GRUB2_POST_CONFIGURE_HOOKS += GRUB2_CONFIGURE_CMDS_PTF
     
    -define GRUB2_BUILD_CMDS
    +define GRUB2_BUILD_CMDS_PTF
        $(foreach tuple, $(GRUB2_TUPLES-y), \
            $(TARGET_MAKE_ENV) $(MAKE) -C $(@D)/build-$(tuple)
        )
     endef
    +GRUB2_POST_BUILD_HOOKS += GRUB2_BUILD_CMDS_PTF
     
    -define GRUB2_INSTALL_IMAGES_CMDS
    +define GRUB2_INSTALL_IMAGES_CMDS_PTF
        $(foreach tuple, $(GRUB2_TUPLES-y), \
            mkdir -p $(dir $(GRUB2_IMAGE_$(tuple))) ; \
            $(HOST_DIR)/usr/bin/grub-mkimage \
    @@ -194,6 +199,7 @@ define GRUB2_INSTALL_IMAGES_CMDS
            ) \
        )
     endef
    +GRUB2_POST_INSTALL_IMAGES_HOOKS += GRUB2_INSTALL_IMAGES_CMDS_PTF
     
    -$(eval $(generic-package))
    +$(eval $(autotools-package))
     $(eval $(host-autotools-package))

By using --with-platform=none in the main CONF_OPTS, we should be able
to disable building any of the "images", and just build the tools.

Totally untested, of course...

Alternatively, we could introduce a new package, grub2-tools, not unlike
uboot-tools, which sole responsibility would be to build and install
those tools.

Regards,
Yann E. MORIN.

> Regards,
> 
> Köry
Kory Maincent Oct. 7, 2021, 12:43 p.m. UTC | #4
On Thu, 7 Oct 2021 11:53:26 +0200
"Yann E. MORIN" <yann.morin.1998@free.fr> wrote:

> Köry, All,
> 
> On 2021-10-07 10:23 +0200, Köry Maincent spake thusly:
> > On Wed, 6 Oct 2021 20:11:04 +0200
> > "Yann E. MORIN" <yann.morin.1998@free.fr> wrote:  
> > > On 2021-09-23 17:57 +0200, Kory Maincent spake thusly:
> > > [--SNIP--]  
> > > > We can no longer use autotools-package as a consequence of this
> > > > multi-build, and we have to resort to generic-package and a partial
> > > > duplication of the autotools-infra. Grub2 was already using custom
> > > > option like --prefix or --exec-prefix so this won't add much more
> > > > weirdness.    
> > > But this had an unfortunate side effect: the grub2 tools, like
> > > grub-editenv, enabled with BR2_TARGET_GRUB2_INSTALL_TOOLS, are no
> > > longer installed in the target now.  
> > Doh, didn't thought of that!  
> 
> Neither did I...
> 
> [--SNIP--]
> > > Care to look into that, please?  
> > 
> > mmh that's tricky, because in the case we want to use the Grub modules it
> > need to be generated for each platforms.
> > For example we can face a case where we will have these three Grub modules
> > repositories for each platform:
> > target/lib/grub/i386-pc/
> > target/lib/grub/i386-efi/
> > target/lib/grub/x86_64-efi/
> > 
> > What about just adding the "make install" command in the foreach loop?
> > This will overwrite the binaries, the locale and the /etc configurations for
> > each "make" loop but we will have all the Grub modules repositories. I don't
> > think the overwrites will cause issues because it is just tools.  

> 
> By using --with-platform=none in the main CONF_OPTS, we should be able
> to disable building any of the "images", and just build the tools.
> 
> Totally untested, of course...

As I said if we do that we won't have all the Grub modules in the target
directory. I think these modules are quiet important, an user that wants to use
grub-mkimage will surely use them for example. 

Regards,

Köry,
Yann E. MORIN Oct. 7, 2021, 4:29 p.m. UTC | #5
Köry, All,

On 2021-10-07 14:43 +0200, Köry Maincent spake thusly:
> On Thu, 7 Oct 2021 11:53:26 +0200
> "Yann E. MORIN" <yann.morin.1998@free.fr> wrote:
[--SNIP--]
> > By using --with-platform=none in the main CONF_OPTS, we should be able
> > to disable building any of the "images", and just build the tools.
> > 
> > Totally untested, of course...
> As I said if we do that we won't have all the Grub modules in the target
> directory. I think these modules are quiet important, an user that wants to use
> grub-mkimage will surely use them for example. 

OK, I had a hard time understanding what you meant... But indeed,
BR2_TARGET_GRUB2_INSTALL_TOOLS *also* wants to install the modules in
target/

In this case, this should not be too complex either; we just need a
post-image hook that copies all modules into TARGET_DIR, like:

    diff --git a/boot/grub2/grub2.mk b/boot/grub2/grub2.mk
    index e01ebb2edb..0bb10d765d 100644
    --- a/boot/grub2/grub2.mk
    +++ b/boot/grub2/grub2.mk
    @@ -50,11 +50,6 @@ GRUB2_IGNORE_CVES += CVE-2019-14865
     # version available in Buildroot.
     GRUB2_IGNORE_CVES += CVE-2020-15705
     
    -ifeq ($(BR2_TARGET_GRUB2_INSTALL_TOOLS),y)
    -GRUB2_INSTALL_TARGET = YES
    -else
    -GRUB2_INSTALL_TARGET = NO
    -endif
     GRUB2_CPE_ID_VENDOR = gnu
     
     GRUB2_BUILTIN_MODULES_PC = $(call qstrip,$(BR2_TARGET_GRUB2_BUILTIN_MODULES_PC))
    @@ -127,6 +122,9 @@ GRUB2_TUPLES-$(BR2_TARGET_GRUB2_ARM64_EFI) += arm64-efi
     HOST_GRUB2_CONF_ENV = \
         CPP="$(HOSTCC) -E"
     
    +# Main build: only build tools
    +GRUB2_CONF_OPTS = --with-platform=none --blabla...
    +
     GRUB2_CONF_ENV = \
         CPP="$(TARGET_CC) -E" \
         TARGET_CC="$(TARGET_CC)" \
    @@ -147,7 +145,7 @@ HOST_GRUB2_CONF_OPTS = \
         --enable-libzfs=no \
         --disable-werror
     
    -define GRUB2_CONFIGURE_CMDS
    +define GRUB2_CONFIGURE_CMDS_PTF
         $(foreach tuple, $(GRUB2_TUPLES-y), \
             mkdir -p $(@D)/build-$(tuple) ; \
             cd $(@D)/build-$(tuple) ; \
    @@ -169,14 +167,16 @@ define GRUB2_CONFIGURE_CMDS
                 --disable-werror
         )
     endef
    +GRUB2_POST_CONFIGURE_HOOKS += GRUB2_CONFIGURE_CMDS_PTF
     
    -define GRUB2_BUILD_CMDS
    +define GRUB2_BUILD_CMDS_PTF
         $(foreach tuple, $(GRUB2_TUPLES-y), \
             $(TARGET_MAKE_ENV) $(MAKE) -C $(@D)/build-$(tuple)
         )
     endef
    +GRUB2_POST_BUILD_HOOKS += GRUB2_BUILD_CMDS_PTF
     
    -define GRUB2_INSTALL_IMAGES_CMDS
    +define GRUB2_INSTALL_IMAGES_CMDS_PTF
         $(foreach tuple, $(GRUB2_TUPLES-y), \
             mkdir -p $(dir $(GRUB2_IMAGE_$(tuple))) ; \
             $(HOST_DIR)/usr/bin/grub-mkimage \
    @@ -194,6 +194,23 @@ define GRUB2_INSTALL_IMAGES_CMDS
             ) \
         )
     endef
    +GRUB2_POST_INSTALL_IMAGES_HOOKS += GRUB2_INSTALL_IMAGES_CMDS_PTF
     
    -$(eval $(generic-package))
    +ifeq ($(BR2_TARGET_GRUB2_INSTALL_TOOLS),y)
    +GRUB2_INSTALL_TARGET = YES
    +define GRUB2_INSTALL_MODS_IN_TARGET
    +    mkdir -p $(TARGET_DIR)/boot
    +    $(foreach tuple, $(GRUB2_TUPLES-y), \
    +        cp -a $(GRUB2_IMAGE_$(tuple)) $(TARGET_DIR)/boot/
    +    )
    +endef
    +# **YES** this is a post-install-image hook that installs in target/
    +# **AND** we need it to be registered **LAST**, after all the per-tuple
    +# image-install hooks
    +GRUB2_POST_INSTALL_IMAGES_HOOKS += GRUB2_INSTALL_MODS_IN_TARGET
    +else
    +GRUB2_INSTALL_TARGET = NO
    +endif
    +
    +$(eval $(autotools-package))
     $(eval $(host-autotools-package))


NOTE: yes, I know that $(GRUB2_IMAGE_$(tuple)) is not exactly what we
need to copy, and that it will need some additional tweaking.

But then, maybe installing all the per-tuple to target/ might just
work in the end. That would not be nice, but as Thomas already said,
grub2 is already not nice anyway...

Also, I just noticed that we copy the cdboot.img from the host dir! Are
the modules all there?

Regards,
Yann E. MORIN.
Kory Maincent Oct. 8, 2021, 8:20 a.m. UTC | #6
On Thu, 7 Oct 2021 18:29:31 +0200
"Yann E. MORIN" <yann.morin.1998@free.fr> wrote:

> Köry, All,
> 
> On 2021-10-07 14:43 +0200, Köry Maincent spake thusly:
> > On Thu, 7 Oct 2021 11:53:26 +0200
> > "Yann E. MORIN" <yann.morin.1998@free.fr> wrote:  
> [--SNIP--]
> > > By using --with-platform=none in the main CONF_OPTS, we should be able
> > > to disable building any of the "images", and just build the tools.
> > > 
> > > Totally untested, of course...  
> > As I said if we do that we won't have all the Grub modules in the target
> > directory. I think these modules are quiet important, an user that wants to
> > use grub-mkimage will surely use them for example.   
> 
> OK, I had a hard time understanding what you meant... But indeed,
> BR2_TARGET_GRUB2_INSTALL_TOOLS *also* wants to install the modules in
> target/

Sorry I was maybe not clear enough.
I was speaking about all the .mod and .module files installed in the
/target/lib/grub/$(GRUB2_TUPLES-$(tuple)) directory when calling the "make
install" command. If we use none as platform they are not generated, therefore
installed.

Here is the list of installed file if I add "make install" in the foreach
loop: https://termbin.com/8b4w
Here is the list of installed file in the case of none
platform: https://termbin.com/x45h

> 
> In this case, this should not be too complex either; we just need a
> post-image hook that copies all modules into TARGET_DIR, like:

Yes it could works, but the installation of the module is not really proper, see
below.

> 
>     diff --git a/boot/grub2/grub2.mk b/boot/grub2/grub2.mk
>     index e01ebb2edb..0bb10d765d 100644
>     --- a/boot/grub2/grub2.mk
>     +++ b/boot/grub2/grub2.mk
>     @@ -50,11 +50,6 @@ GRUB2_IGNORE_CVES += CVE-2019-14865
>      # version available in Buildroot.
>      GRUB2_IGNORE_CVES += CVE-2020-15705
>      
>     -ifeq ($(BR2_TARGET_GRUB2_INSTALL_TOOLS),y)
>     -GRUB2_INSTALL_TARGET = YES
>     -else
>     -GRUB2_INSTALL_TARGET = NO
>     -endif
>      GRUB2_CPE_ID_VENDOR = gnu
>      
>      GRUB2_BUILTIN_MODULES_PC = $(call
> qstrip,$(BR2_TARGET_GRUB2_BUILTIN_MODULES_PC)) @@ -127,6 +122,9 @@
> GRUB2_TUPLES-$(BR2_TARGET_GRUB2_ARM64_EFI) += arm64-efi HOST_GRUB2_CONF_ENV =
> \ CPP="$(HOSTCC) -E"
>      
>     +# Main build: only build tools
>     +GRUB2_CONF_OPTS = --with-platform=none --blabla...
>     +
>      GRUB2_CONF_ENV = \
>          CPP="$(TARGET_CC) -E" \
>          TARGET_CC="$(TARGET_CC)" \
>     @@ -147,7 +145,7 @@ HOST_GRUB2_CONF_OPTS = \
>          --enable-libzfs=no \
>          --disable-werror
>      
>     -define GRUB2_CONFIGURE_CMDS
>     +define GRUB2_CONFIGURE_CMDS_PTF
>          $(foreach tuple, $(GRUB2_TUPLES-y), \
>              mkdir -p $(@D)/build-$(tuple) ; \
>              cd $(@D)/build-$(tuple) ; \
>     @@ -169,14 +167,16 @@ define GRUB2_CONFIGURE_CMDS
>                  --disable-werror
>          )
>      endef
>     +GRUB2_POST_CONFIGURE_HOOKS += GRUB2_CONFIGURE_CMDS_PTF
>      
>     -define GRUB2_BUILD_CMDS
>     +define GRUB2_BUILD_CMDS_PTF
>          $(foreach tuple, $(GRUB2_TUPLES-y), \
>              $(TARGET_MAKE_ENV) $(MAKE) -C $(@D)/build-$(tuple)
>          )
>      endef
>     +GRUB2_POST_BUILD_HOOKS += GRUB2_BUILD_CMDS_PTF
>      
>     -define GRUB2_INSTALL_IMAGES_CMDS
>     +define GRUB2_INSTALL_IMAGES_CMDS_PTF
>          $(foreach tuple, $(GRUB2_TUPLES-y), \
>              mkdir -p $(dir $(GRUB2_IMAGE_$(tuple))) ; \
>              $(HOST_DIR)/usr/bin/grub-mkimage \
>     @@ -194,6 +194,23 @@ define GRUB2_INSTALL_IMAGES_CMDS
>              ) \
>          )
>      endef
>     +GRUB2_POST_INSTALL_IMAGES_HOOKS += GRUB2_INSTALL_IMAGES_CMDS_PTF
>      
>     -$(eval $(generic-package))
>     +ifeq ($(BR2_TARGET_GRUB2_INSTALL_TOOLS),y)
>     +GRUB2_INSTALL_TARGET = YES
>     +define GRUB2_INSTALL_MODS_IN_TARGET
>     +    mkdir -p $(TARGET_DIR)/boot
>     +    $(foreach tuple, $(GRUB2_TUPLES-y), \
>     +        cp -a $(GRUB2_IMAGE_$(tuple)) $(TARGET_DIR)/boot/

Here you copy only the image generated, not the modules. It should more be like
that:
	$(foreach tuple, $(GRUB2_TUPLES-y), \
		mkdir -p $(TARGET_DIR)/usr/lib/$(tuple) \
		cp -a $(@D)/build-$(tuple)/grub-core/{*.mod,*.module} \
			$(TARGET_DIR)/usr/lib/$(tuple)

We might also need *.img file because cdboot.img is needed to create CDROM
drive image.

Or we also could "make install" each platform in a temporary folder then copy
the content of tmp-folder-$(tuple)/usr/lib/$(tuple) in the $(TARGET_DIR) but I
am not sure it is better than just "make install" directly in the $(TARGET_DIR).

>     +    )
>     +endef
>     +# **YES** this is a post-install-image hook that installs in target/
>     +# **AND** we need it to be registered **LAST**, after all the per-tuple
>     +# image-install hooks
>     +GRUB2_POST_INSTALL_IMAGES_HOOKS += GRUB2_INSTALL_MODS_IN_TARGET
>     +else
>     +GRUB2_INSTALL_TARGET = NO
>     +endif
>     +
>     +$(eval $(autotools-package))
>      $(eval $(host-autotools-package))
> 
> 
> NOTE: yes, I know that $(GRUB2_IMAGE_$(tuple)) is not exactly what we
> need to copy, and that it will need some additional tweaking.
> 
> But then, maybe installing all the per-tuple to target/ might just
> work in the end. That would not be nice, but as Thomas already said,
> grub2 is already not nice anyway...

Yeah, not sure what is the better solution. 

> Also, I just noticed that we copy the cdboot.img from the host dir! Are
> the modules all there?

You may have copy a wrong code because it won't install cdboot.img in the
$(TARGET_DIR) with the code below.

Regards,
Köry
Kory Maincent Oct. 11, 2021, 10:27 a.m. UTC | #7
Hello Yann,

On Thu, 7 Oct 2021 18:29:31 +0200
"Yann E. MORIN" <yann.morin.1998@free.fr> wrote:

> Köry, All,
> 
> On 2021-10-07 14:43 +0200, Köry Maincent spake thusly:
> > On Thu, 7 Oct 2021 11:53:26 +0200
> > "Yann E. MORIN" <yann.morin.1998@free.fr> wrote:  
> [--SNIP--]

> In this case, this should not be too complex either; we just need a
> post-image hook that copies all modules into TARGET_DIR, like:

Is something like the following code seems okay for you.
I use temporary install-$(tuple) folder for the different platforms.
We need to do the CONFIGURE_CMD_PTF as a PRE_CONFIGURE_HOOK because the
CONFIGURE_CMDS from autotools is configuring inside the GRUB2_SRCDIR folder.
Then if the ./configure has been run in the GRUB2_SRCDIR I can not run other
configure command without receive this error message:
  configure: error: source directory already configured; run "make distclean"
  there first

To follow the logic I move all the *_CMDS_PTF as PRE_*_HOOK.
I have tested it and it seems functioning.


diff --git a/boot/grub2/grub2.mk b/boot/grub2/grub2.mk
index e01ebb2edb..e339eed6de 100644
--- a/boot/grub2/grub2.mk
+++ b/boot/grub2/grub2.mk
@@ -139,6 +139,22 @@ GRUB2_CONF_ENV = \
        TARGET_OBJCOPY="$(TARGET_OBJCOPY)" \
        TARGET_STRIP="$(TARGET_CROSS)strip"
 
+GRUB2_CONF_COMMON_OPTS = \
+       --host=$(GNU_TARGET_NAME) \
+       --build=$(GNU_HOST_NAME) \
+       --prefix=/ \
+       --exec-prefix=/ \
+       --disable-grub-mkfont \
+       --enable-efiemu=no \
+       ac_cv_lib_lzma_lzma_code=no \
+       --enable-device-mapper=no \
+       --enable-libzfs=no \
+       --disable-werror
+
+GRUB2_CONF_OPTS = \
+       $(GRUB2_CONF_COMMON_OPTS) \
+       --with-platform=none
+
 HOST_GRUB2_CONF_OPTS = \
        --disable-grub-mkfont \
        --enable-efiemu=no \
@@ -147,7 +163,7 @@ HOST_GRUB2_CONF_OPTS = \
        --enable-libzfs=no \
        --disable-werror

-define GRUB2_CONFIGURE_CMDS
+define GRUB2_CONFIGURE_CMDS_PTF
        $(foreach tuple, $(GRUB2_TUPLES-y), \
                mkdir -p $(@D)/build-$(tuple) ; \
                cd $(@D)/build-$(tuple) ; \
@@ -157,26 +173,19 @@ define GRUB2_CONFIGURE_CMDS
                ../configure \
                        --target=$(GRUB2_TARGET_$(tuple)) \
                        --with-platform=$(GRUB2_PLATFORM_$(tuple)) \
-                       --host=$(GNU_TARGET_NAME) \
-                       --build=$(GNU_HOST_NAME) \
-                       --prefix=/ \
-                       --exec-prefix=/ \
-                       --disable-grub-mkfont \
-                       --enable-efiemu=no \
-                       ac_cv_lib_lzma_lzma_code=no \
-                       --enable-device-mapper=no \
-                       --enable-libzfs=no \
-                       --disable-werror
+                       $(GRUB2_CONF_COMMON_OPTS)
        )
 endef
+GRUB2_PRE_CONFIGURE_HOOKS += GRUB2_CONFIGURE_CMDS_PTF
 
-define GRUB2_BUILD_CMDS
+define GRUB2_BUILD_CMDS_PTF
        $(foreach tuple, $(GRUB2_TUPLES-y), \
                $(TARGET_MAKE_ENV) $(MAKE) -C $(@D)/build-$(tuple)
        )
 endef
+GRUB2_PRE_BUILD_HOOKS += GRUB2_BUILD_CMDS_PTF
 
-define GRUB2_INSTALL_IMAGES_CMDS
+define GRUB2_INSTALL_IMAGES_CMDS_PTF
        $(foreach tuple, $(GRUB2_TUPLES-y), \
                mkdir -p $(dir $(GRUB2_IMAGE_$(tuple))) ; \
                $(HOST_DIR)/usr/bin/grub-mkimage \
@@ -194,6 +203,20 @@ define GRUB2_INSTALL_IMAGES_CMDS
                ) \
        )
 endef
+GRUB2_PRE_INSTALL_IMAGES_HOOKS += GRUB2_INSTALL_CMDS_PTF
+
+ifeq ($(BR2_TARGET_GRUB2_INSTALL_TOOLS),y)
+GRUB2_INSTALL_TARGET = YES
+define GRUB2_INSTALL_MODS_IN_TARGET
+       $(foreach tuple, $(GRUB2_TUPLES-y), \
+               $(TARGET_MAKE_ENV) $(MAKE) DESTDIR=$(@D)/$(install-$(tuple))
install -C $(@D)/build-$(tuple) ; \
+               cp -a $(@D)/$(install-$(tuple))/lib/* $(TARGET_DIR)/lib/ ;
+       )
+endef
+GRUB2_POST_INSTALL_IMAGES_HOOKS += GRUB2_INSTALL_MODS_IN_TARGET
+else
+GRUB2_INSTALL_TARGET = NO
+endif
 
-$(eval $(generic-package))
+$(eval $(autotools-package))
 $(eval $(host-autotools-package))

Regards,
Köry
Yann E. MORIN Oct. 14, 2021, 8:02 p.m. UTC | #8
Köry, All,

Sorry for my late reply, I've been pretty busy here...

On 2021-10-11 12:27 +0200, Köry Maincent spake thusly:
> On Thu, 7 Oct 2021 18:29:31 +0200
> "Yann E. MORIN" <yann.morin.1998@free.fr> wrote:
> > On 2021-10-07 14:43 +0200, Köry Maincent spake thusly:
> > > On Thu, 7 Oct 2021 11:53:26 +0200
> > > "Yann E. MORIN" <yann.morin.1998@free.fr> wrote:  
> > [--SNIP--]
> 
> > In this case, this should not be too complex either; we just need a
> > post-image hook that copies all modules into TARGET_DIR, like:
> 
> Is something like the following code seems okay for you.
> I use temporary install-$(tuple) folder for the different platforms.
> We need to do the CONFIGURE_CMD_PTF as a PRE_CONFIGURE_HOOK because the
> CONFIGURE_CMDS from autotools is configuring inside the GRUB2_SRCDIR folder.
> Then if the ./configure has been run in the GRUB2_SRCDIR I can not run other
> configure command without receive this error message:
>   configure: error: source directory already configured; run "make distclean"
>   there first

Arg, I hadn't thought about this...

> To follow the logic I move all the *_CMDS_PTF as PRE_*_HOOK.
> I have tested it and it seems functioning.

But I'm afraid it is going to fail if you try to reconfigure the thing,
like:

    $ make grub2-reconfigure

So, in the end, your original proposal, to install each and every tuples
into target/, is probably the best option we have, and it can be as
simple as something like:

    diff --git a/boot/grub2/grub2.mk b/boot/grub2/grub2.mk
    index e01ebb2edb..a18696b6cc 100644
    --- a/boot/grub2/grub2.mk
    +++ b/boot/grub2/grub2.mk
    @@ -195,5 +195,13 @@ define GRUB2_INSTALL_IMAGES_CMDS
        )
     endef
     
    +ifeq ($(BR2_TARGET_GRUB2_INSTALL_TOOLS),y)
    +define GRUB2_INSTALL_TARGET_CMDS
    +	$(foreach tuple, $(GRUB2_TUPLES-y), \
    +		$(TARGET_MAKE_ENV) $(MAKE) -C $(@D)/build-$(tuple) DESTDIR=$(TARGET_DIR) install
    +	)
    +endef
    +endif
    +
     $(eval $(generic-package))
     $(eval $(host-autotools-package))

Yes, this will indeed install the same tools more than once, but this is
not a problem: it is _not_ a case of a package overwriting files from
another package, but overwriting its own files, so we don't care.

Test-built with:

    BR2_x86_pentium4=y
    BR2_TOOLCHAIN_EXTERNAL=y
    BR2_TARGET_GRUB2=y
    BR2_TARGET_GRUB2_I386_PC=y
    BR2_TARGET_GRUB2_I386_EFI=y
    BR2_TARGET_GRUB2_INSTALL_TOOLS=y

Side note, unrelated to this issue: we have a parallel build issue with
grub2:

    2021-10-14 21:45:52 cd . && /bin/sh ./config.status config-util.h
    2021-10-14 21:45:52 config.status: creating config-util.h
    2021-10-14 21:45:52 bison -d -p grub_script_yy -b grub_script ../grub-core/script/parser.y
    2021-10-14 21:45:52 ../grub-core/script/parser.y:92.1-12: warning: deprecated directive: ‘%pure-parser’, use ‘%define api.pure’ [-Wdeprecated]
    2021-10-14 21:45:52    92 | %pure-parser
    2021-10-14 21:45:52       | ^~~~~~~~~~~~
    2021-10-14 21:45:52       | %define api.pure
    2021-10-14 21:45:52 ../grub-core/script/parser.y: warning: fix-its can be applied.  Rerun with option '--update'. [-Wother]
    2021-10-14 21:45:52 /home/ymorin/dev/buildroot/O/host/bin/i686-linux-gcc -E -DHAVE_CONFIG_H -I. -I..  -Wall -W -DGRUB_UTIL=1 -D_FILE_OFFSET_BITS=64 -I./include -DGRUB_FILE="util/grub-fstest.c" -I. -I.. -I. -I.. -I../include -I./include -I../grub-core/lib/libgcrypt-grub/src/ -I./grub-core/lib/gnulib -I../grub-core/lib/gnulib  -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -Os -fno-stack-protector -D_FILE_OFFSET_BITS=64   -D'GRUB_MOD_INIT(x)=@MARKER@x@' ../util/grub-fstest.c ../grub-core/kern/emu/hostfs.c ../ grub-core/disk/host.c ../grub-core/osdep/init.c > grub_fstest.pp || (rm -f grub_fstest.pp; exit 1)
    2021-10-14 21:45:52 ../grub-core/kern/emu/hostfs.c:20:10: fatal error: config-util.h: No such file or directory
    2021-10-14 21:45:52    20 | #include <config-util.h>
    2021-10-14 21:45:52       |          ^~~~~~~~~~~~~~~
    2021-10-14 21:45:52 compilation terminated.
    2021-10-14 21:45:52 make[2]: *** [Makefile:13107: grub_fstest.pp] Error 1
    2021-10-14 21:45:52 make[2]: *** Waiting for unfinished jobs....

And re-starting the build succeeds.

But just looking at how config-util.h is actually created (in Makefile.in)
makes me cry:

   3389 config-util.h: stamp-h1
   3390     @test -f $@ || rm -f stamp-h1
   3391     @test -f $@ || $(MAKE) $(AM_MAKEFLAGS) stamp-h1
   3392
   3393 stamp-h1: $(srcdir)/config-util.h.in $(top_builddir)/config.status
   3394     @rm -f stamp-h1
   3395     cd $(top_builddir) && $(SHELL) ./config.status config-util.h

Whaaa?

But fortunately, it looks liek it is fixed with upstream commit
42f4054faf3c (Makefile: Make libgrub.pp depend on config-util.h).

Could you look into both the tools and the backport of the commit?

Regards,
Yann E. MORIN.
Yann E. MORIN Oct. 14, 2021, 8:27 p.m. UTC | #9
Köry, All,

On 2021-10-14 22:02 +0200, Yann E. MORIN spake thusly:
> Side note, unrelated to this issue: we have a parallel build issue with
> grub2:
[--SNIP--]
> But fortunately, it looks liek it is fixed with upstream commit
> 42f4054faf3c (Makefile: Make libgrub.pp depend on config-util.h).

And of course, since this commit touches Makefile.am, but we avoid very
hard re-running autoreconf for grub2, we also need to patch Makefile.in
(the same hunk applies quite OK to both file, fortunately).

Regards,
Yann E. MORIN.
Adam Duskett Oct. 14, 2021, 8:48 p.m. UTC | #10
Hello;

On Thu, Oct 14, 2021 at 1:02 PM Yann E. MORIN <yann.morin.1998@free.fr> wrote:
>
> Köry, All,
>
> Sorry for my late reply, I've been pretty busy here...
>
> On 2021-10-11 12:27 +0200, Köry Maincent spake thusly:
> > On Thu, 7 Oct 2021 18:29:31 +0200
> > "Yann E. MORIN" <yann.morin.1998@free.fr> wrote:
> > > On 2021-10-07 14:43 +0200, Köry Maincent spake thusly:
> > > > On Thu, 7 Oct 2021 11:53:26 +0200
> > > > "Yann E. MORIN" <yann.morin.1998@free.fr> wrote:
> > > [--SNIP--]
> >
> > > In this case, this should not be too complex either; we just need a
> > > post-image hook that copies all modules into TARGET_DIR, like:
> >
> > Is something like the following code seems okay for you.
> > I use temporary install-$(tuple) folder for the different platforms.
> > We need to do the CONFIGURE_CMD_PTF as a PRE_CONFIGURE_HOOK because the
> > CONFIGURE_CMDS from autotools is configuring inside the GRUB2_SRCDIR folder.
> > Then if the ./configure has been run in the GRUB2_SRCDIR I can not run other
> > configure command without receive this error message:
> >   configure: error: source directory already configured; run "make distclean"
> >   there first
>
> Arg, I hadn't thought about this...
>
> > To follow the logic I move all the *_CMDS_PTF as PRE_*_HOOK.
> > I have tested it and it seems functioning.
>
> But I'm afraid it is going to fail if you try to reconfigure the thing,
> like:
>
>     $ make grub2-reconfigure
>
> So, in the end, your original proposal, to install each and every tuples
> into target/, is probably the best option we have, and it can be as
> simple as something like:
>
>     diff --git a/boot/grub2/grub2.mk b/boot/grub2/grub2.mk
>     index e01ebb2edb..a18696b6cc 100644
>     --- a/boot/grub2/grub2.mk
>     +++ b/boot/grub2/grub2.mk
>     @@ -195,5 +195,13 @@ define GRUB2_INSTALL_IMAGES_CMDS
>         )
>      endef
>
>     +ifeq ($(BR2_TARGET_GRUB2_INSTALL_TOOLS),y)
>     +define GRUB2_INSTALL_TARGET_CMDS
>     +   $(foreach tuple, $(GRUB2_TUPLES-y), \
>     +           $(TARGET_MAKE_ENV) $(MAKE) -C $(@D)/build-$(tuple) DESTDIR=$(TARGET_DIR) install
>     +   )
>     +endef
>     +endif
>     +
>      $(eval $(generic-package))
>      $(eval $(host-autotools-package))
>
This indeed works properly!
Tested-by: Adam Duskett <aduskett@gmail.com>
> Yes, this will indeed install the same tools more than once, but this is
> not a problem: it is _not_ a case of a package overwriting files from
> another package, but overwriting its own files, so we don't care.
>
> Test-built with:
>
>     BR2_x86_pentium4=y
>     BR2_TOOLCHAIN_EXTERNAL=y
>     BR2_TARGET_GRUB2=y
>     BR2_TARGET_GRUB2_I386_PC=y
>     BR2_TARGET_GRUB2_I386_EFI=y
>     BR2_TARGET_GRUB2_INSTALL_TOOLS=y
>
> Side note, unrelated to this issue: we have a parallel build issue with
> grub2:
>
>     2021-10-14 21:45:52 cd . && /bin/sh ./config.status config-util.h
>     2021-10-14 21:45:52 config.status: creating config-util.h
>     2021-10-14 21:45:52 bison -d -p grub_script_yy -b grub_script ../grub-core/script/parser.y
>     2021-10-14 21:45:52 ../grub-core/script/parser.y:92.1-12: warning: deprecated directive: ‘%pure-parser’, use ‘%define api.pure’ [-Wdeprecated]
>     2021-10-14 21:45:52    92 | %pure-parser
>     2021-10-14 21:45:52       | ^~~~~~~~~~~~
>     2021-10-14 21:45:52       | %define api.pure
>     2021-10-14 21:45:52 ../grub-core/script/parser.y: warning: fix-its can be applied.  Rerun with option '--update'. [-Wother]
>     2021-10-14 21:45:52 /home/ymorin/dev/buildroot/O/host/bin/i686-linux-gcc -E -DHAVE_CONFIG_H -I. -I..  -Wall -W -DGRUB_UTIL=1 -D_FILE_OFFSET_BITS=64 -I./include -DGRUB_FILE="util/grub-fstest.c" -I. -I.. -I. -I.. -I../include -I./include -I../grub-core/lib/libgcrypt-grub/src/ -I./grub-core/lib/gnulib -I../grub-core/lib/gnulib  -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -Os -fno-stack-protector -D_FILE_OFFSET_BITS=64   -D'GRUB_MOD_INIT(x)=@MARKER@x@' ../util/grub-fstest.c ../grub-core/kern/emu/hostfs.c ../ grub-core/disk/host.c ../grub-core/osdep/init.c > grub_fstest.pp || (rm -f grub_fstest.pp; exit 1)
>     2021-10-14 21:45:52 ../grub-core/kern/emu/hostfs.c:20:10: fatal error: config-util.h: No such file or directory
>     2021-10-14 21:45:52    20 | #include <config-util.h>
>     2021-10-14 21:45:52       |          ^~~~~~~~~~~~~~~
>     2021-10-14 21:45:52 compilation terminated.
>     2021-10-14 21:45:52 make[2]: *** [Makefile:13107: grub_fstest.pp] Error 1
>     2021-10-14 21:45:52 make[2]: *** Waiting for unfinished jobs....
>
> And re-starting the build succeeds.
>
> But just looking at how config-util.h is actually created (in Makefile.in)
> makes me cry:
>
>    3389 config-util.h: stamp-h1
>    3390     @test -f $@ || rm -f stamp-h1
>    3391     @test -f $@ || $(MAKE) $(AM_MAKEFLAGS) stamp-h1
>    3392
>    3393 stamp-h1: $(srcdir)/config-util.h.in $(top_builddir)/config.status
>    3394     @rm -f stamp-h1
>    3395     cd $(top_builddir) && $(SHELL) ./config.status config-util.h
>
> Whaaa?
>
> But fortunately, it looks liek it is fixed with upstream commit
> 42f4054faf3c (Makefile: Make libgrub.pp depend on config-util.h).
>
> Could you look into both the tools and the backport of the commit?
>
> Regards,
> Yann E. MORIN.
>
> --
> .-----------------.--------------------.------------------.--------------------.
> |  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
> | +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
> | +33 561 099 427 `------------.-------:  X  AGAINST      |  \e/  There is no  |
> | http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
> '------------------------------^-------^------------------^--------------------'
Yann E. MORIN Oct. 14, 2021, 9:02 p.m. UTC | #11
Köry, All,

On 2021-10-14 22:02 +0200, Yann E. MORIN spake thusly:
> So, in the end, your original proposal, to install each and every tuples
> into target/, is probably the best option we have, [...]

Additionally, to test what was failing, I had instrumented the build
with the following patch (note that it mixes fixes, like no semi-colons
but separate lines, or using && not sime-colon, with the actual calls
to MESSAGE):

    diff --git a/boot/grub2/grub2.mk b/boot/grub2/grub2.mk
    index e01ebb2edb..5400538837 100644
    --- a/boot/grub2/grub2.mk
    +++ b/boot/grub2/grub2.mk
    @@ -149,8 +149,9 @@ HOST_GRUB2_CONF_OPTS = \
     
     define GRUB2_CONFIGURE_CMDS
     	$(foreach tuple, $(GRUB2_TUPLES-y), \
    -		mkdir -p $(@D)/build-$(tuple) ; \
    -		cd $(@D)/build-$(tuple) ; \
    +		@$(call MESSAGE,Configuring $(tuple))
    +		mkdir -p $(@D)/build-$(tuple)
    +		cd $(@D)/build-$(tuple) && \
     		$(TARGET_CONFIGURE_OPTS) \
     				$(TARGET_CONFIGURE_ARGS) \
     		$(GRUB2_CONF_ENV) \
    @@ -172,13 +173,15 @@ endef
     
     define GRUB2_BUILD_CMDS
     	$(foreach tuple, $(GRUB2_TUPLES-y), \
    +		@$(call MESSAGE,Building $(tuple))
     		$(TARGET_MAKE_ENV) $(MAKE) -C $(@D)/build-$(tuple)
     	)
     endef
     
     define GRUB2_INSTALL_IMAGES_CMDS
     	$(foreach tuple, $(GRUB2_TUPLES-y), \
    -		mkdir -p $(dir $(GRUB2_IMAGE_$(tuple))) ; \
    +		@$(call MESSAGE,Installing $(tuple) to images directory)
    +		mkdir -p $(dir $(GRUB2_IMAGE_$(tuple)))
     		$(HOST_DIR)/usr/bin/grub-mkimage \
     			-d $(@D)/build-$(tuple)/grub-core/ \
     			-O $(tuple) \
    @@ -186,12 +189,12 @@ define GRUB2_INSTALL_IMAGES_CMDS
     			-p "$(GRUB2_PREFIX_$(tuple))" \
     			$(if $(GRUB2_BUILTIN_CONFIG_$(tuple)), \
     				-c $(GRUB2_BUILTIN_CONFIG_$(tuple))) \
    -				$(GRUB2_BUILTIN_MODULES_$(tuple)) ; \
    -		$(INSTALL) -D -m 0644 boot/grub2/grub.cfg $(GRUB2_CFG_$(tuple)) ; \
    +				$(GRUB2_BUILTIN_MODULES_$(tuple))
    +		$(INSTALL) -D -m 0644 boot/grub2/grub.cfg $(GRUB2_CFG_$(tuple))
     		$(if $(findstring $(GRUB2_PLATFORM_$(tuple)), pc), \
     			cat $(HOST_DIR)/lib/grub/$(tuple)/cdboot.img $(GRUB2_IMAGE_$(tuple)) > \
    -				$(BINARIES_DIR)/grub-eltorito.img ; \
    -		) \
    +				$(BINARIES_DIR)/grub-eltorito.img \
    +		)
     	)
     endef
     

That way, it is easier to see what is going on, and the output when
filtered with brmake, looks nice ;-)

2021-10-14 22:52:36 >>> grub2 2.04 Extracting
2021-10-14 22:52:36 >>> grub2 2.04 Patching
2021-10-14 22:52:39 >>> grub2 2.04 Configuring
2021-10-14 22:52:39 >>> grub2 2.04 Configuring i386-pc
2021-10-14 22:52:56 >>> grub2 2.04 Configuring i386-efi
2021-10-14 22:53:13 >>> grub2 2.04 Building
2021-10-14 22:53:13 >>> grub2 2.04 Building i386-pc
2021-10-14 22:53:51 >>> grub2 2.04 Building i386-efi
2021-10-14 22:54:28 >>> grub2 2.04 Installing to target
2021-10-14 22:54:28 >>> grub2 2.04 Installing i386-pc to target
2021-10-14 22:54:31 >>> grub2 2.04 Installing i386-efi to target
2021-10-14 22:54:34 >>> grub2 2.04 Installing to images directory
2021-10-14 22:54:34 >>> grub2 2.04 Installing i386-pc to images directory
2021-10-14 22:54:34 >>> grub2 2.04 Installing i386-efi to images directory

Regards,
Yann E. MORIN.
Kory Maincent Oct. 15, 2021, 9:19 a.m. UTC | #12
Hello Yann,

On Thu, 14 Oct 2021 22:02:07 +0200
"Yann E. MORIN" <yann.morin.1998@free.fr> wrote:

> Köry, All,
> 
> Sorry for my late reply, I've been pretty busy here...

No problem, be a maintainer is a full-time job done on your own time and you
do great work! :)

> So, in the end, your original proposal, to install each and every tuples
> into target/, is probably the best option we have, and it can be as
> simple as something like:

Alright

> 
> Test-built with:
> 
>     BR2_x86_pentium4=y
>     BR2_TOOLCHAIN_EXTERNAL=y
>     BR2_TARGET_GRUB2=y
>     BR2_TARGET_GRUB2_I386_PC=y
>     BR2_TARGET_GRUB2_I386_EFI=y
>     BR2_TARGET_GRUB2_INSTALL_TOOLS=y
> 
> Side note, unrelated to this issue: we have a parallel build issue with
> grub2:
> 
>     2021-10-14 21:45:52 cd . && /bin/sh ./config.status config-util.h
>     2021-10-14 21:45:52 config.status: creating config-util.h
>     2021-10-14 21:45:52 bison -d -p grub_script_yy -b grub_script
> ../grub-core/script/parser.y 2021-10-14 21:45:52
> ../grub-core/script/parser.y:92.1-12: warning: deprecated directive:
> ‘%pure-parser’, use ‘%define api.pure’ [-Wdeprecated] 2021-10-14 21:45:52
> 92 | %pure-parser 2021-10-14 21:45:52       | ^~~~~~~~~~~~ 2021-10-14
> 21:45:52       | %define api.pure 2021-10-14 21:45:52
> ../grub-core/script/parser.y: warning: fix-its can be applied.  Rerun with
> option '--update'. [-Wother] 2021-10-14 21:45:52
> /home/ymorin/dev/buildroot/O/host/bin/i686-linux-gcc -E -DHAVE_CONFIG_H -I.
> -I..  -Wall -W -DGRUB_UTIL=1 -D_FILE_OFFSET_BITS=64 -I./include
> -DGRUB_FILE="util/grub-fstest.c" -I. -I.. -I. -I.. -I../include -I./include
> -I../grub-core/lib/libgcrypt-grub/src/ -I./grub-core/lib/gnulib
> -I../grub-core/lib/gnulib  -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
> -D_FILE_OFFSET_BITS=64 -Os -fno-stack-protector -D_FILE_OFFSET_BITS=64
> -D'GRUB_MOD_INIT(x)=@MARKER@x@' ../util/grub-fstest.c
> ../grub-core/kern/emu/hostfs.c ../ grub-core/disk/host.c
> ../grub-core/osdep/init.c > grub_fstest.pp || (rm -f grub_fstest.pp; exit 1)
> 2021-10-14 21:45:52 ../grub-core/kern/emu/hostfs.c:20:10: fatal error:
> config-util.h: No such file or directory 2021-10-14 21:45:52    20 | #include
> <config-util.h> 2021-10-14 21:45:52       |          ^~~~~~~~~~~~~~~
> 2021-10-14 21:45:52 compilation terminated. 2021-10-14 21:45:52 make[2]: ***
> [Makefile:13107: grub_fstest.pp] Error 1 2021-10-14 21:45:52 make[2]: ***
> Waiting for unfinished jobs....
> 
> And re-starting the build succeeds.

Weird, I do not face this build error with the same defconfig.

Regards,
Köry
Thomas Petazzoni Oct. 15, 2021, 9:28 a.m. UTC | #13
On Fri, 15 Oct 2021 11:19:39 +0200
Köry Maincent <kory.maincent@bootlin.com> wrote:

> > And re-starting the build succeeds.  
> 
> Weird, I do not face this build error with the same defconfig.

It's a parallel build issue, which means it's a race condition. It will
not happen all the time. Try on a build server with lots of CPU cores,
and do the build in a loop multiple times.

Thomas
Yann E. MORIN Oct. 15, 2021, 8:50 p.m. UTC | #14
Köry, All,

On 2021-10-14 22:02 +0200, Yann E. MORIN spake thusly:
[--SNIP--]
> Side note, unrelated to this issue: we have a parallel build issue with
> grub2:
>     2021-10-14 21:45:52 /home/ymorin/dev/buildroot/O/host/bin/i686-linux-gcc -E -DHAVE_CONFIG_H -I. -I..  -Wall -W -DGRUB_UTIL=1 -D_FILE_OFFSET_BITS=64 -I./include -DGRUB_FILE="util/grub-fstest.c" -I. -I.. -I. -I.. -I../include -I./include -I../grub-core/lib/libgcrypt-grub/src/ -I./grub-core/lib/gnulib -I../grub-core/lib/gnulib  -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -Os -fno-stack-protector -D_FILE_OFFSET_BITS=64   -D'GRUB_MOD_INIT(x)=@MARKER@x@' ../util/grub-fstest.c ../grub-core/kern/emu/hostfs.c ../ grub-core/disk/host.c ../grub-core/osdep/init.c > grub_fstest.pp || (rm -f grub_fstest.pp; exit 1)
>     2021-10-14 21:45:52 ../grub-core/kern/emu/hostfs.c:20:10: fatal error: config-util.h: No such file or directory
>     2021-10-14 21:45:52    20 | #include <config-util.h>
>     2021-10-14 21:45:52       |          ^~~~~~~~~~~~~~~
[--SNIP--]
> But fortunately, it looks liek it is fixed with upstream commit
> 42f4054faf3c (Makefile: Make libgrub.pp depend on config-util.h).

And apparently, this is not enough, because I still hit the issue, about
one in five builds, always when building i386-efi... :-(

But given how inventive the grub buildsystem is, I dread to ever have to
look at it once more... :-(

Regards,
Yann E. MORIN.
Adam Duskett Oct. 19, 2021, 4:30 p.m. UTC | #15
Hey Kory;

Do you plan on submitting a patch to fix this? If not, I can do so.
Just let me know!

Adam

On Fri, Oct 15, 2021 at 1:50 PM Yann E. MORIN <yann.morin.1998@free.fr> wrote:
>
> Köry, All,
>
> On 2021-10-14 22:02 +0200, Yann E. MORIN spake thusly:
> [--SNIP--]
> > Side note, unrelated to this issue: we have a parallel build issue with
> > grub2:
> >     2021-10-14 21:45:52 /home/ymorin/dev/buildroot/O/host/bin/i686-linux-gcc -E -DHAVE_CONFIG_H -I. -I..  -Wall -W -DGRUB_UTIL=1 -D_FILE_OFFSET_BITS=64 -I./include -DGRUB_FILE="util/grub-fstest.c" -I. -I.. -I. -I.. -I../include -I./include -I../grub-core/lib/libgcrypt-grub/src/ -I./grub-core/lib/gnulib -I../grub-core/lib/gnulib  -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -Os -fno-stack-protector -D_FILE_OFFSET_BITS=64   -D'GRUB_MOD_INIT(x)=@MARKER@x@' ../util/grub-fstest.c ../grub-core/kern/emu/hostfs.c ../ grub-core/disk/host.c ../grub-core/osdep/init.c > grub_fstest.pp || (rm -f grub_fstest.pp; exit 1)
> >     2021-10-14 21:45:52 ../grub-core/kern/emu/hostfs.c:20:10: fatal error: config-util.h: No such file or directory
> >     2021-10-14 21:45:52    20 | #include <config-util.h>
> >     2021-10-14 21:45:52       |          ^~~~~~~~~~~~~~~
> [--SNIP--]
> > But fortunately, it looks liek it is fixed with upstream commit
> > 42f4054faf3c (Makefile: Make libgrub.pp depend on config-util.h).
>
> And apparently, this is not enough, because I still hit the issue, about
> one in five builds, always when building i386-efi... :-(
>
> But given how inventive the grub buildsystem is, I dread to ever have to
> look at it once more... :-(
>
> Regards,
> Yann E. MORIN.
>
> --
> .-----------------.--------------------.------------------.--------------------.
> |  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
> | +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
> | +33 561 099 427 `------------.-------:  X  AGAINST      |  \e/  There is no  |
> | http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
> '------------------------------^-------^------------------^--------------------'
Kory Maincent Oct. 20, 2021, 3:58 p.m. UTC | #16
Hello Adam,

On Tue, 19 Oct 2021 09:30:10 -0700
Adam Duskett <aduskett@gmail.com> wrote:

> Hey Kory;
> 
> Do you plan on submitting a patch to fix this? If not, I can do so.
> Just let me know!

I was about to do it before Yann spot the parallel build error. As I did not
managed to face the error I have stopped my action on the patch. I can however
send the first part of it that fixing tools install, then wait the fix for the
parallel build issue.

Regards,
Köry

> 
> Adam
> 
> On Fri, Oct 15, 2021 at 1:50 PM Yann E. MORIN <yann.morin.1998@free.fr> wrote:
> >
> > Köry, All,
> >
> > On 2021-10-14 22:02 +0200, Yann E. MORIN spake thusly:
> > [--SNIP--]  
> > > Side note, unrelated to this issue: we have a parallel build issue with
> > > grub2:
> > >     2021-10-14 21:45:52
> > > /home/ymorin/dev/buildroot/O/host/bin/i686-linux-gcc -E -DHAVE_CONFIG_H
> > > -I. -I..  -Wall -W -DGRUB_UTIL=1 -D_FILE_OFFSET_BITS=64 -I./include
> > > -DGRUB_FILE="util/grub-fstest.c" -I. -I.. -I. -I.. -I../include
> > > -I./include -I../grub-core/lib/libgcrypt-grub/src/
> > > -I./grub-core/lib/gnulib -I../grub-core/lib/gnulib  -D_LARGEFILE_SOURCE
> > > -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -Os -fno-stack-protector
> > > -D_FILE_OFFSET_BITS=64   -D'GRUB_MOD_INIT(x)=@MARKER@x@'
> > > ../util/grub-fstest.c ../grub-core/kern/emu/hostfs.c ../
> > > grub-core/disk/host.c ../grub-core/osdep/init.c > grub_fstest.pp || (rm
> > > -f grub_fstest.pp; exit 1) 2021-10-14 21:45:52
> > > ../grub-core/kern/emu/hostfs.c:20:10: fatal error: config-util.h: No such
> > > file or directory 2021-10-14 21:45:52    20 | #include <config-util.h>
> > > 2021-10-14 21:45:52       |          ^~~~~~~~~~~~~~~  
> > [--SNIP--]  
> > > But fortunately, it looks liek it is fixed with upstream commit
> > > 42f4054faf3c (Makefile: Make libgrub.pp depend on config-util.h).  
> >
> > And apparently, this is not enough, because I still hit the issue, about
> > one in five builds, always when building i386-efi... :-(
> >
> > But given how inventive the grub buildsystem is, I dread to ever have to
> > look at it once more... :-(
> >
diff mbox series

Patch

diff --git a/Config.in.legacy b/Config.in.legacy
index 35a11f4dc6..33cee0202b 100644
--- a/Config.in.legacy
+++ b/Config.in.legacy
@@ -168,6 +168,30 @@  config BR2_KERNEL_HEADERS_5_12
 
 comment "Legacy options removed in 2021.08"
 
+config BR2_TARGET_GRUB2_BUILTIN_MODULES
+	string "the grub2 builtin modules has been renamed"
+	help
+	  This option has been split to separate the builtin modules
+	  between BR2_TARGET_GRUB2_BUILTIN_MODULES_PC and
+	  BR2_TARGET_GRUB2_BUILTIN_MODULES_EFI.
+
+config BR2_TARGET_GRUB2_BUILTIN_MODULES_WRAP
+	bool
+	default y if BR2_TARGET_GRUB2_BUILTIN_MODULES != ""
+	select BR2_LEGACY
+
+config BR2_TARGET_GRUB2_BUILTIN_CONFIG
+	string "the grub2 builtin configuration has been renamed"
+	help
+	  This option has been split to separate the builtin configuration
+	  between BR2_TARGET_GRUB2_BUILTIN_CONFIG_PC and
+	  BR2_TARGET_GRUB2_BUILTIN_CONFIG_EFI.
+
+config BR2_TARGET_GRUB2_BUILTIN_CONFIG_WRAP
+	bool
+	default y if BR2_TARGET_GRUB2_BUILTIN_CONFIG != ""
+	select BR2_LEGACY
+
 config BR2_PACKAGE_LIBMCRYPT
 	bool "libmcrypt package was removed"
 	select BR2_LEGACY
diff --git a/boot/grub2/Config.in b/boot/grub2/Config.in
index e45133999e..10bba6c5e4 100644
--- a/boot/grub2/Config.in
+++ b/boot/grub2/Config.in
@@ -10,6 +10,13 @@  config BR2_TARGET_GRUB2
 	bool "grub2"
 	depends on BR2_TARGET_GRUB2_ARCH_SUPPORTS
 	depends on BR2_USE_WCHAR
+	select BR2_TARGET_GRUB2_I386_PC if \
+		!BR2_TARGET_GRUB2_HAS_PTF && \
+		(BR2_i386 || BR2_x86_64)
+	select BR2_TARGET_GRUB2_ARM_UBOOT if \
+		!BR2_TARGET_GRUB2_HAS_PTF && \
+		BR2_arm
+	select BR2_TARGET_GRUB2_ARM64_EFI if BR2_aarch64
 	help
 	  GNU GRUB is a Multiboot boot loader. It was derived from
 	  GRUB, the GRand Unified Bootloader, which was originally
@@ -25,10 +32,10 @@  config BR2_TARGET_GRUB2
 
 	  http://www.gnu.org/software/grub/
 
-if BR2_TARGET_GRUB2
+config BR2_TARGET_GRUB2_HAS_PTF
+	bool
 
-choice
-	prompt "Platform"
+if BR2_TARGET_GRUB2
 
 config BR2_TARGET_GRUB2_I386_PC
 	bool "i386-pc"
@@ -40,6 +47,7 @@  config BR2_TARGET_GRUB2_I386_PC
 config BR2_TARGET_GRUB2_I386_EFI
 	bool "i386-efi"
 	depends on BR2_i386 || BR2_x86_64
+	select BR2_TARGET_GRUB2_HAS_PTF
 	help
 	  Select this option if the platform you're targetting has a
 	  32 bits EFI BIOS. Note that some x86-64 platforms use a 32
@@ -48,6 +56,7 @@  config BR2_TARGET_GRUB2_I386_EFI
 config BR2_TARGET_GRUB2_X86_64_EFI
 	bool "x86-64-efi"
 	depends on BR2_x86_64
+	select BR2_TARGET_GRUB2_HAS_PTF
 	help
 	  Select this option if the platform you're targetting has a
 	  64 bits EFI BIOS.
@@ -63,6 +72,7 @@  config BR2_TARGET_GRUB2_ARM_UBOOT
 config BR2_TARGET_GRUB2_ARM_EFI
 	bool "arm-efi"
 	depends on BR2_arm
+	select BR2_TARGET_GRUB2_HAS_PTF
 	help
 	  Select this option if the platform you're targetting is an
 	  ARM platform and you want to boot Grub 2 as an EFI
@@ -76,10 +86,10 @@  config BR2_TARGET_GRUB2_ARM64_EFI
 	  Aarch64 platform and you want to boot Grub 2 as an EFI
 	  application.
 
-endchoice
-
 if BR2_TARGET_GRUB2_I386_PC || BR2_TARGET_GRUB2_ARM_UBOOT
 
+comment "Options for the x86 legacy BIOS or ARM U-Boot support"
+
 config BR2_TARGET_GRUB2_BOOT_PARTITION
 	string "boot partition"
 	default "hd0,msdos1"
@@ -89,24 +99,43 @@  config BR2_TARGET_GRUB2_BOOT_PARTITION
 	  first disk if using a legacy partition table, or 'hd0,gpt1'
 	  if using GPT partition table.
 
-endif # BR2_TARGET_GRUB2_I386_PC || BR2_TARGET_GRUB2_ARM_UBOOT
-
-config BR2_TARGET_GRUB2_BUILTIN_MODULES
+config BR2_TARGET_GRUB2_BUILTIN_MODULES_PC
 	string "builtin modules"
+	default BR2_TARGET_GRUB2_BUILTIN_MODULES if BR2_TARGET_GRUB2_BUILTIN_MODULES != "" # legacy
 	default "boot linux ext2 fat squash4 part_msdos part_gpt normal biosdisk" if BR2_TARGET_GRUB2_I386_PC
-	default "boot linux ext2 fat squash4 part_msdos part_gpt normal efi_gop" \
-		if BR2_TARGET_GRUB2_I386_EFI || BR2_TARGET_GRUB2_X86_64_EFI || \
-		   BR2_TARGET_GRUB2_ARM_EFI  || BR2_TARGET_GRUB2_ARM64_EFI
 	default "linux ext2 fat part_msdos normal" if BR2_TARGET_GRUB2_ARM_UBOOT
 
-config BR2_TARGET_GRUB2_BUILTIN_CONFIG
+config BR2_TARGET_GRUB2_BUILTIN_CONFIG_PC
+	string "builtin config"
+	default BR2_TARGET_GRUB2_BUILTIN_CONFIG if BR2_TARGET_GRUB2_BUILTIN_CONFIG != "" # legacy
+	help
+	  Path to a Grub 2 configuration file that will be embedded
+	  into the Grub image itself. This allows to set the root
+	  device and other configuration parameters, but however menu
+	  entries cannot be described in this embedded configuration.
+
+endif # BR2_TARGET_GRUB2_I386_PC || BR2_TARGET_GRUB2_ARM_UBOOT
+
+if BR2_TARGET_GRUB2_I386_EFI || BR2_TARGET_GRUB2_X86_64_EFI || \
+	BR2_TARGET_GRUB2_ARM_EFI  || BR2_TARGET_GRUB2_ARM64_EFI
+
+comment "Options for the EFI BIOS or ARM EFI support"
+
+config BR2_TARGET_GRUB2_BUILTIN_MODULES_EFI
+	string "builtin modules"
+	default BR2_TARGET_GRUB2_BUILTIN_MODULES if BR2_TARGET_GRUB2_BUILTIN_MODULES != "" # legacy
+	default "boot linux ext2 fat squash4 part_msdos part_gpt normal efi_gop"
+
+config BR2_TARGET_GRUB2_BUILTIN_CONFIG_EFI
 	string "builtin config"
+	default BR2_TARGET_GRUB2_BUILTIN_CONFIG if BR2_TARGET_GRUB2_BUILTIN_CONFIG != "" # legacy
 	help
 	  Path to a Grub 2 configuration file that will be embedded
 	  into the Grub image itself. This allows to set the root
 	  device and other configuration parameters, but however menu
 	  entries cannot be described in this embedded configuration.
 
+endif
 config BR2_TARGET_GRUB2_INSTALL_TOOLS
 	bool "install tools"
 	help
diff --git a/boot/grub2/grub2.mk b/boot/grub2/grub2.mk
index ad2edf17aa..e01ebb2edb 100644
--- a/boot/grub2/grub2.mk
+++ b/boot/grub2/grub2.mk
@@ -57,53 +57,65 @@  GRUB2_INSTALL_TARGET = NO
 endif
 GRUB2_CPE_ID_VENDOR = gnu
 
-GRUB2_BUILTIN_MODULES = $(call qstrip,$(BR2_TARGET_GRUB2_BUILTIN_MODULES))
-GRUB2_BUILTIN_CONFIG = $(call qstrip,$(BR2_TARGET_GRUB2_BUILTIN_CONFIG))
+GRUB2_BUILTIN_MODULES_PC = $(call qstrip,$(BR2_TARGET_GRUB2_BUILTIN_MODULES_PC))
+GRUB2_BUILTIN_MODULES_EFI = $(call qstrip,$(BR2_TARGET_GRUB2_BUILTIN_MODULES_EFI))
+GRUB2_BUILTIN_CONFIG_PC = $(call qstrip,$(BR2_TARGET_GRUB2_BUILTIN_CONFIG_PC))
+GRUB2_BUILTIN_CONFIG_EFI = $(call qstrip,$(BR2_TARGET_GRUB2_BUILTIN_CONFIG_EFI))
 GRUB2_BOOT_PARTITION = $(call qstrip,$(BR2_TARGET_GRUB2_BOOT_PARTITION))
 
-ifeq ($(BR2_TARGET_GRUB2_I386_PC),y)
-GRUB2_IMAGE = $(BINARIES_DIR)/grub.img
-GRUB2_CFG = $(TARGET_DIR)/boot/grub/grub.cfg
-GRUB2_PREFIX = ($(GRUB2_BOOT_PARTITION))/boot/grub
-GRUB2_TUPLE = i386-pc
-GRUB2_TARGET = i386
-GRUB2_PLATFORM = pc
-else ifeq ($(BR2_TARGET_GRUB2_I386_EFI),y)
-GRUB2_IMAGE = $(BINARIES_DIR)/efi-part/EFI/BOOT/bootia32.efi
-GRUB2_CFG = $(BINARIES_DIR)/efi-part/EFI/BOOT/grub.cfg
-GRUB2_PREFIX = /EFI/BOOT
-GRUB2_TUPLE = i386-efi
-GRUB2_TARGET = i386
-GRUB2_PLATFORM = efi
-else ifeq ($(BR2_TARGET_GRUB2_X86_64_EFI),y)
-GRUB2_IMAGE = $(BINARIES_DIR)/efi-part/EFI/BOOT/bootx64.efi
-GRUB2_CFG = $(BINARIES_DIR)/efi-part/EFI/BOOT/grub.cfg
-GRUB2_PREFIX = /EFI/BOOT
-GRUB2_TUPLE = x86_64-efi
-GRUB2_TARGET = x86_64
-GRUB2_PLATFORM = efi
-else ifeq ($(BR2_TARGET_GRUB2_ARM_UBOOT),y)
-GRUB2_IMAGE = $(BINARIES_DIR)/boot-part/grub/grub.img
-GRUB2_CFG = $(BINARIES_DIR)/boot-part/grub/grub.cfg
-GRUB2_PREFIX = ($(GRUB2_BOOT_PARTITION))/boot/grub
-GRUB2_TUPLE = arm-uboot
-GRUB2_TARGET = arm
-GRUB2_PLATFORM = uboot
-else ifeq ($(BR2_TARGET_GRUB2_ARM_EFI),y)
-GRUB2_IMAGE = $(BINARIES_DIR)/efi-part/EFI/BOOT/bootarm.efi
-GRUB2_CFG = $(BINARIES_DIR)/efi-part/EFI/BOOT/grub.cfg
-GRUB2_PREFIX = /EFI/BOOT
-GRUB2_TUPLE = arm-efi
-GRUB2_TARGET = arm
-GRUB2_PLATFORM = efi
-else ifeq ($(BR2_TARGET_GRUB2_ARM64_EFI),y)
-GRUB2_IMAGE = $(BINARIES_DIR)/efi-part/EFI/BOOT/bootaa64.efi
-GRUB2_CFG = $(BINARIES_DIR)/efi-part/EFI/BOOT/grub.cfg
-GRUB2_PREFIX = /EFI/BOOT
-GRUB2_TUPLE = arm64-efi
-GRUB2_TARGET = aarch64
-GRUB2_PLATFORM = efi
-endif
+GRUB2_IMAGE_i386-pc = $(BINARIES_DIR)/grub.img
+GRUB2_CFG_i386-pc = $(TARGET_DIR)/boot/grub/grub.cfg
+GRUB2_PREFIX_i386-pc = ($(GRUB2_BOOT_PARTITION))/boot/grub
+GRUB2_TARGET_i386-pc = i386
+GRUB2_PLATFORM_i386-pc = pc
+GRUB2_BUILTIN_CONFIG_i386-pc = $(GRUB2_BUILTIN_CONFIG_PC)
+GRUB2_BUILTIN_MODULES_i386-pc = $(GRUB2_BUILTIN_MODULES_PC)
+GRUB2_TUPLES-$(BR2_TARGET_GRUB2_I386_PC) += i386-pc
+
+GRUB2_IMAGE_i386-efi = $(BINARIES_DIR)/efi-part/EFI/BOOT/bootia32.efi
+GRUB2_CFG_i386-efi = $(BINARIES_DIR)/efi-part/EFI/BOOT/grub.cfg
+GRUB2_PREFIX_i386-efi = /EFI/BOOT
+GRUB2_TARGET_i386-efi = i386
+GRUB2_PLATFORM_i386-efi = efi
+GRUB2_BUILTIN_CONFIG_i386-efi = $(GRUB2_BUILTIN_CONFIG_EFI)
+GRUB2_BUILTIN_MODULES_i386-efi = $(GRUB2_BUILTIN_MODULES_EFI)
+GRUB2_TUPLES-$(BR2_TARGET_GRUB2_I386_EFI) += i386-efi
+
+GRUB2_IMAGE_x86_64-efi = $(BINARIES_DIR)/efi-part/EFI/BOOT/bootx64.efi
+GRUB2_CFG_x86_64-efi = $(BINARIES_DIR)/efi-part/EFI/BOOT/grub.cfg
+GRUB2_PREFIX_x86_64-efi = /EFI/BOOT
+GRUB2_TARGET_x86_64-efi = x86_64
+GRUB2_PLATFORM_x86_64-efi = efi
+GRUB2_BUILTIN_CONFIG_x86_64-efi = $(GRUB2_BUILTIN_CONFIG_EFI)
+GRUB2_BUILTIN_MODULES_x86_64-efi = $(GRUB2_BUILTIN_MODULES_EFI)
+GRUB2_TUPLES-$(BR2_TARGET_GRUB2_X86_64_EFI) += x86_64-efi
+
+GRUB2_IMAGE_arm-uboot = $(BINARIES_DIR)/boot-part/grub/grub.img
+GRUB2_CFG_arm-uboot = $(BINARIES_DIR)/boot-part/grub/grub.cfg
+GRUB2_PREFIX_arm-uboot = ($(GRUB2_BOOT_PARTITION))/boot/grub
+GRUB2_TARGET_arm-uboot = arm
+GRUB2_PLATFORM_arm-uboot = uboot
+GRUB2_BUILTIN_CONFIG_arm-uboot = $(GRUB2_BUILTIN_CONFIG_PC)
+GRUB2_BUILTIN_MODULES_arm-uboot = $(GRUB2_BUILTIN_MODULES_PC)
+GRUB2_TUPLES-$(BR2_TARGET_GRUB2_ARM_UBOOT) += arm-uboot
+
+GRUB2_IMAGE_arm-efi = $(BINARIES_DIR)/efi-part/EFI/BOOT/bootarm.efi
+GRUB2_CFG_arm-efi = $(BINARIES_DIR)/efi-part/EFI/BOOT/grub.cfg
+GRUB2_PREFIX_arm-efi = /EFI/BOOT
+GRUB2_TARGET_arm-efi = arm
+GRUB2_PLATFORM_arm-efi = efi
+GRUB2_BUILTIN_CONFIG_arm-efi = $(GRUB2_BUILTIN_CONFIG_EFI)
+GRUB2_BUILTIN_MODULES_arm-efi = $(GRUB2_BUILTIN_MODULES_EFI)
+GRUB2_TUPLES-$(BR2_TARGET_GRUB2_ARM_EFI) += arm-efi
+
+GRUB2_IMAGE_arm64-efi = $(BINARIES_DIR)/efi-part/EFI/BOOT/bootaa64.efi
+GRUB2_CFG_arm64-efi = $(BINARIES_DIR)/efi-part/EFI/BOOT/grub.cfg
+GRUB2_PREFIX_arm64-efi = /EFI/BOOT
+GRUB2_TARGET_arm64-efi = aarch64
+GRUB2_PLATFORM_arm64-efi = efi
+GRUB2_BUILTIN_CONFIG_arm64-efi = $(GRUB2_BUILTIN_CONFIG_EFI)
+GRUB2_BUILTIN_MODULES_arm64-efi = $(GRUB2_BUILTIN_MODULES_EFI)
+GRUB2_TUPLES-$(BR2_TARGET_GRUB2_ARM64_EFI) += arm64-efi
 
 # Grub2 is kind of special: it considers CC, LD and so on to be the
 # tools to build the host programs and uses TARGET_CC, TARGET_CFLAGS,
@@ -127,18 +139,6 @@  GRUB2_CONF_ENV = \
 	TARGET_OBJCOPY="$(TARGET_OBJCOPY)" \
 	TARGET_STRIP="$(TARGET_CROSS)strip"
 
-GRUB2_CONF_OPTS = \
-	--target=$(GRUB2_TARGET) \
-	--with-platform=$(GRUB2_PLATFORM) \
-	--prefix=/ \
-	--exec-prefix=/ \
-	--disable-grub-mkfont \
-	--enable-efiemu=no \
-	ac_cv_lib_lzma_lzma_code=no \
-	--enable-device-mapper=no \
-	--enable-libzfs=no \
-	--disable-werror
-
 HOST_GRUB2_CONF_OPTS = \
 	--disable-grub-mkfont \
 	--enable-efiemu=no \
@@ -147,26 +147,53 @@  HOST_GRUB2_CONF_OPTS = \
 	--enable-libzfs=no \
 	--disable-werror
 
-ifeq ($(BR2_TARGET_GRUB2_I386_PC),y)
-define GRUB2_IMAGE_INSTALL_ELTORITO
-	cat $(HOST_DIR)/lib/grub/$(GRUB2_TUPLE)/cdboot.img $(GRUB2_IMAGE) > \
-		$(BINARIES_DIR)/grub-eltorito.img
+define GRUB2_CONFIGURE_CMDS
+	$(foreach tuple, $(GRUB2_TUPLES-y), \
+		mkdir -p $(@D)/build-$(tuple) ; \
+		cd $(@D)/build-$(tuple) ; \
+		$(TARGET_CONFIGURE_OPTS) \
+		$(TARGET_CONFIGURE_ARGS) \
+		$(GRUB2_CONF_ENV) \
+		../configure \
+			--target=$(GRUB2_TARGET_$(tuple)) \
+			--with-platform=$(GRUB2_PLATFORM_$(tuple)) \
+			--host=$(GNU_TARGET_NAME) \
+			--build=$(GNU_HOST_NAME) \
+			--prefix=/ \
+			--exec-prefix=/ \
+			--disable-grub-mkfont \
+			--enable-efiemu=no \
+			ac_cv_lib_lzma_lzma_code=no \
+			--enable-device-mapper=no \
+			--enable-libzfs=no \
+			--disable-werror
+	)
+endef
+
+define GRUB2_BUILD_CMDS
+	$(foreach tuple, $(GRUB2_TUPLES-y), \
+		$(TARGET_MAKE_ENV) $(MAKE) -C $(@D)/build-$(tuple)
+	)
 endef
-endif
 
 define GRUB2_INSTALL_IMAGES_CMDS
-	mkdir -p $(dir $(GRUB2_IMAGE))
-	$(HOST_DIR)/usr/bin/grub-mkimage \
-		-d $(@D)/grub-core/ \
-		-O $(GRUB2_TUPLE) \
-		-o $(GRUB2_IMAGE) \
-		-p "$(GRUB2_PREFIX)" \
-		$(if $(GRUB2_BUILTIN_CONFIG),-c $(GRUB2_BUILTIN_CONFIG)) \
-		$(GRUB2_BUILTIN_MODULES)
-	mkdir -p $(dir $(GRUB2_CFG))
-	$(INSTALL) -D -m 0644 boot/grub2/grub.cfg $(GRUB2_CFG)
-	$(GRUB2_IMAGE_INSTALL_ELTORITO)
+	$(foreach tuple, $(GRUB2_TUPLES-y), \
+		mkdir -p $(dir $(GRUB2_IMAGE_$(tuple))) ; \
+		$(HOST_DIR)/usr/bin/grub-mkimage \
+			-d $(@D)/build-$(tuple)/grub-core/ \
+			-O $(tuple) \
+			-o $(GRUB2_IMAGE_$(tuple)) \
+			-p "$(GRUB2_PREFIX_$(tuple))" \
+			$(if $(GRUB2_BUILTIN_CONFIG_$(tuple)), \
+				-c $(GRUB2_BUILTIN_CONFIG_$(tuple))) \
+			$(GRUB2_BUILTIN_MODULES_$(tuple)) ; \
+		$(INSTALL) -D -m 0644 boot/grub2/grub.cfg $(GRUB2_CFG_$(tuple)) ; \
+		$(if $(findstring $(GRUB2_PLATFORM_$(tuple)), pc), \
+			cat $(HOST_DIR)/lib/grub/$(tuple)/cdboot.img $(GRUB2_IMAGE_$(tuple)) > \
+				$(BINARIES_DIR)/grub-eltorito.img ; \
+		) \
+	)
 endef
 
-$(eval $(autotools-package))
+$(eval $(generic-package))
 $(eval $(host-autotools-package))
diff --git a/support/testing/tests/fs/test_iso9660.py b/support/testing/tests/fs/test_iso9660.py
index 68f4840852..1b699e1fef 100644
--- a/support/testing/tests/fs/test_iso9660.py
+++ b/support/testing/tests/fs/test_iso9660.py
@@ -54,7 +54,7 @@  class TestIso9660Grub2External(infra.basetest.BRTest):
         # BR2_TARGET_ROOTFS_ISO9660_INITRD is not set
         BR2_TARGET_GRUB2=y
         BR2_TARGET_GRUB2_BOOT_PARTITION="cd"
-        BR2_TARGET_GRUB2_BUILTIN_MODULES="boot linux ext2 fat part_msdos part_gpt normal biosdisk iso9660"
+        BR2_TARGET_GRUB2_BUILTIN_MODULES_PC="boot linux ext2 fat part_msdos part_gpt normal biosdisk iso9660"
         BR2_TARGET_ROOTFS_ISO9660_BOOT_MENU="{}"
         """.format(infra.filepath("conf/grub2.cfg"))
 
@@ -75,7 +75,7 @@  class TestIso9660Grub2ExternalCompress(infra.basetest.BRTest):
         BR2_TARGET_ROOTFS_ISO9660_TRANSPARENT_COMPRESSION=y
         BR2_TARGET_GRUB2=y
         BR2_TARGET_GRUB2_BOOT_PARTITION="cd"
-        BR2_TARGET_GRUB2_BUILTIN_MODULES="boot linux ext2 fat part_msdos part_gpt normal biosdisk iso9660"
+        BR2_TARGET_GRUB2_BUILTIN_MODULES_PC="boot linux ext2 fat part_msdos part_gpt normal biosdisk iso9660"
         BR2_TARGET_ROOTFS_ISO9660_BOOT_MENU="{}"
         """.format(infra.filepath("conf/grub2.cfg"))
 
@@ -95,7 +95,7 @@  class TestIso9660Grub2Internal(infra.basetest.BRTest):
         BR2_TARGET_ROOTFS_ISO9660_INITRD=y
         BR2_TARGET_GRUB2=y
         BR2_TARGET_GRUB2_BOOT_PARTITION="cd"
-        BR2_TARGET_GRUB2_BUILTIN_MODULES="boot linux ext2 fat part_msdos part_gpt normal biosdisk iso9660"
+        BR2_TARGET_GRUB2_BUILTIN_MODULES_PC="boot linux ext2 fat part_msdos part_gpt normal biosdisk iso9660"
         BR2_TARGET_ROOTFS_ISO9660_BOOT_MENU="{}"
         """.format(infra.filepath("conf/grub2.cfg"))