From: Simon Glass <simon.glass@canonical.com>
Generalise the per-example link, objcopy and cat rules in
Makefile.ulib-example from demo-specific to foreach+eval over the
ULIB_EXAMPLES list. This is a pure refactoring that produces identical
build output but makes it straightforward to add further examples.
Co-developed-by: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---
scripts/Makefile.ulib-example | 48 +++++++++++++++++++++++++----------
1 file changed, 34 insertions(+), 14 deletions(-)
@@ -25,32 +25,52 @@ quiet_cmd_u-boot-example = LD $@
# 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)
+# Generate link rule for each example
+define example_link_rule
+examples/ulib/$(1): $$(example-$(1)-objs) u-boot FORCE
+ $$(eval example-objs := $$(example-$(1)-objs))
+ $$(call if_changed,u-boot-example)
+ $$(EXAMPLE_POST_LINK)
+endef
+
+$(foreach e,$(ULIB_EXAMPLES),$(eval $(call example_link_rule,$(e))))
ifeq ($(CONFIG_EFI_APP),y)
# EFI: embed DTB and convert to PE binary
-OBJCOPYFLAGS_demo-app.efi := $(OBJCOPYFLAGS_EFI)
-examples/ulib/demo-app.efi: examples/ulib/demo dts/dt.dtb FORCE
- $(if $(CONFIG_OF_SEPARATE),$(call if_changed,embeddtb))
- $(call if_changed,zobjcopy)
+$(foreach e,$(ULIB_EXAMPLES),\
+ $(eval OBJCOPYFLAGS_$(e)-app.efi := $$(OBJCOPYFLAGS_EFI)))
+
+define example_efi_rule
+examples/ulib/$(1)-app.efi: examples/ulib/$(1) dts/dt.dtb FORCE
+ $$(if $$(CONFIG_OF_SEPARATE),$$(call if_changed,embeddtb))
+ $$(call if_changed,zobjcopy)
+endef
+
+$(foreach e,$(ULIB_EXAMPLES),$(eval $(call example_efi_rule,$(e))))
examples_$(EXAMPLE_ARCH): \
$(foreach e,$(ULIB_EXAMPLES),examples/ulib/$(e)-app.efi) FORCE
@:
else
# 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)
+$(foreach e,$(ULIB_EXAMPLES),\
+ $(eval OBJCOPYFLAGS_$(e)-nodtb.bin = $$(OBJCOPYFLAGS_u-boot-nodtb.bin)))
+
+define example_nodtb_rule
+examples/ulib/$(1)-nodtb.bin: examples/ulib/$(1) FORCE
+ $$(call if_changed,objcopy)
+endef
+
+$(foreach e,$(ULIB_EXAMPLES),$(eval $(call example_nodtb_rule,$(e))))
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)
+define example_bin_rule
+examples/ulib/$(1).bin: examples/ulib/$(1)-nodtb.bin dts/dt.dtb FORCE
+ $$(call if_changed,cat)
+endef
+
+$(foreach e,$(ULIB_EXAMPLES),$(eval $(call example_bin_rule,$(e))))
examples_$(EXAMPLE_ARCH): \
$(foreach e,$(ULIB_EXAMPLES),examples/ulib/$(e).bin) FORCE