[Concept,05/17] ulib: Extract common example build rules for demo

Message ID 20260216013511.4079770-6-sjg@u-boot.org
State New
Headers
Series ulib: Add multi-arch demo and EFI app support |

Commit Message

Simon Glass Feb. 16, 2026, 1:34 a.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

The example build rules (link command, object lists, objcopy, and
final-target logic) are duplicated in each arch Makefile. As more
architectures gain ulib support, this duplication grows.

Extract the common rules into scripts/Makefile.ulib-example, so that
each arch's Makefile sets EXAMPLE_ARCH (and optionally
EXAMPLE_APPEND_DTB or EXAMPLE_POST_LINK), then includes the shared
file. Convert x86 to use it.

Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 arch/x86/Makefile             | 31 ++-------------------
 scripts/Makefile.ulib-example | 51 +++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 29 deletions(-)
 create mode 100644 scripts/Makefile.ulib-example
  

Patch

diff --git a/arch/x86/Makefile b/arch/x86/Makefile
index 7df20c43de1..661e75fbdaa 100644
--- a/arch/x86/Makefile
+++ b/arch/x86/Makefile
@@ -65,34 +65,7 @@  u-boot-x86-reset16.bin: u-boot-x86-16bit.elf FORCE
 	$(call if_changed,objcopy)
 endif
 
-# x86 example targets: re-link U-Boot with example objects providing main()
-#
-# The example .o files are compiled via kbuild (examples/ulib/Kbuild).
-# This re-links u-boot with those objects so the example's strong main()
-# overrides the weak one in board_r.c, using the shared u-boot-link helper.
 ifdef CONFIG_EXAMPLES
-INPUTS-$(CONFIG_ULIB) += examples_x86
-
-PHONY += examples_x86
-
-X86_EXAMPLES := demo
-
-quiet_cmd_u-boot-example = LD      $@
-      cmd_u-boot-example = $(call u-boot-link,$(example-objs),$@.map)
-
-# Per-example object lists (matches examples/ulib/Makefile:demo_objs)
-example-demo-objs := examples/ulib/demo.o examples/ulib/demo_helper.o
-
-# Link each example ELF (depends on u-boot to ensure archives exist)
-examples/ulib/demo: $(example-demo-objs) u-boot FORCE
-	$(eval example-objs := $(example-demo-objs))
-	$(call if_changed,u-boot-example)
-
-# Binary targets (same objcopy flags as u-boot-nodtb.bin)
-OBJCOPYFLAGS_demo-nodtb.bin = $(OBJCOPYFLAGS_u-boot-nodtb.bin)
-examples/ulib/demo-nodtb.bin: examples/ulib/demo FORCE
-	$(call if_changed,objcopy)
-
-examples_x86: $(foreach e,$(X86_EXAMPLES),examples/ulib/$(e)-nodtb.bin) FORCE
-	@:
+EXAMPLE_ARCH := x86
+include scripts/Makefile.ulib-example
 endif
diff --git a/scripts/Makefile.ulib-example b/scripts/Makefile.ulib-example
new file mode 100644
index 00000000000..fd7c60e4483
--- /dev/null
+++ b/scripts/Makefile.ulib-example
@@ -0,0 +1,51 @@ 
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Common build rules for ulib example programs.
+#
+# Re-links U-Boot with example objects providing main(). The example .o
+# files are compiled via kbuild (examples/ulib/Kbuild). This re-links
+# u-boot with those objects so the example's strong main() overrides the
+# weak one in board_r.c, using the shared u-boot-link helper.
+#
+# Required variables (set by arch Makefile before including):
+#   EXAMPLE_ARCH        - architecture name (x86, arm, riscv, ...)
+#
+# Optional variables:
+#   EXAMPLE_APPEND_DTB  - set to y to append DTB to the binary
+#   EXAMPLE_POST_LINK   - extra recipe line run after linking the ELF
+
+INPUTS-$(CONFIG_ULIB) += examples_$(EXAMPLE_ARCH)
+PHONY += examples_$(EXAMPLE_ARCH)
+
+ULIB_EXAMPLES := demo
+
+quiet_cmd_u-boot-example = LD      $@
+      cmd_u-boot-example = $(call u-boot-link,$(example-objs),$@.map)
+
+# Per-example object lists (matches examples/ulib/Kbuild)
+example-demo-objs := examples/ulib/demo.o examples/ulib/demo_helper.o
+
+# Link each example ELF (depends on u-boot to ensure archives exist)
+examples/ulib/demo: $(example-demo-objs) u-boot FORCE
+	$(eval example-objs := $(example-demo-objs))
+	$(call if_changed,u-boot-example)
+	$(EXAMPLE_POST_LINK)
+
+# Binary target (without DTB)
+OBJCOPYFLAGS_demo-nodtb.bin = $(OBJCOPYFLAGS_u-boot-nodtb.bin)
+examples/ulib/demo-nodtb.bin: examples/ulib/demo FORCE
+	$(call if_changed,objcopy)
+
+ifeq ($(EXAMPLE_APPEND_DTB),y)
+# Binary with DTB appended
+examples/ulib/demo.bin: examples/ulib/demo-nodtb.bin dts/dt.dtb FORCE
+	$(call if_changed,cat)
+
+examples_$(EXAMPLE_ARCH): \
+		$(foreach e,$(ULIB_EXAMPLES),examples/ulib/$(e).bin) FORCE
+	@:
+else
+examples_$(EXAMPLE_ARCH): \
+		$(foreach e,$(ULIB_EXAMPLES),examples/ulib/$(e)-nodtb.bin) FORCE
+	@:
+endif