[Concept,10/13] x86: ulib: Add build infrastructure for example/

Message ID 20260214021317.816170-11-sjg@u-boot.org
State New
Headers
Series ulib: Support building examples for x86 |

Commit Message

Simon Glass Feb. 14, 2026, 2:13 a.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Add the kbuild and Makefile plumbing to compile and link ulib example
programs for x86:

- examples/ulib/Kbuild compiles demo objects via kbuild (not linked
  into u-boot itself)
- examples/Makefile hooks the ulib subdirectory for non-sandbox builds
- arch/x86/Makefile re-links u-boot with the example objects using the
  u-boot-link helper, so the example's strong main() overrides the
  weak default, then objcopy produces a flat binary

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

 arch/x86/Makefile    | 32 ++++++++++++++++++++++++++++++++
 examples/Makefile    |  4 ++++
 examples/ulib/Kbuild |  6 ++++++
 3 files changed, 42 insertions(+)
 create mode 100644 examples/ulib/Kbuild
  

Patch

diff --git a/arch/x86/Makefile b/arch/x86/Makefile
index e8813aa7e28..7df20c43de1 100644
--- a/arch/x86/Makefile
+++ b/arch/x86/Makefile
@@ -64,3 +64,35 @@  u-boot-x86-start16.bin: u-boot-x86-16bit.elf FORCE
 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
+	@:
+endif
diff --git a/examples/Makefile b/examples/Makefile
index 50bef4e6157..21c703d144b 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -8,4 +8,8 @@  endif
 
 subdir-$(EXAMPLES_STANDALONE) += standalone
 subdir-$(CONFIG_LEGACY_API) += api
+# Sandbox examples use the standalone Makefile; other archs use kbuild
+ifndef CONFIG_SANDBOX
+subdir-$(CONFIG_ULIB) += ulib
+endif
 endif
diff --git a/examples/ulib/Kbuild b/examples/ulib/Kbuild
new file mode 100644
index 00000000000..88d6a805b91
--- /dev/null
+++ b/examples/ulib/Kbuild
@@ -0,0 +1,6 @@ 
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Example objects for x86 platforms (compiled via kbuild, not linked into
+# u-boot).  The standalone Makefile is used for sandbox builds instead.
+
+extra-y += demo.o demo_helper.o