[Concept,08/15] ulib: Add static-library build support

Message ID 20250905170132.182249-9-sjg@u-boot.org
State New
Headers
Series ulib: Provide test programs and documentation |

Commit Message

Simon Glass Sept. 5, 2025, 5:01 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

Sometimes it is more convenient to link an application directly with a
static library.

Add build rules to create libu-boot.a as a static library alongside the
existing shared library (libu-boot.so).

The static library is a fat archive containing all U-Boot object files
except for arch/sandbox/cpu/main.o since it contains main().

A temporary thin archive is used to collect all the objects.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <sjg@chromium.org>
---

 Makefile | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
  

Patch

diff --git a/Makefile b/Makefile
index 1a378ebbee3..bf571de9f06 100644
--- a/Makefile
+++ b/Makefile
@@ -1046,6 +1046,7 @@  INPUTS-$(CONFIG_X86) += u-boot-x86-start16.bin u-boot-x86-reset16.bin \
 ifdef CONFIG_CMDLINE
 ifneq ($(cc-name),clang)
 INPUTS-$(CONFIG_ULIB) += libu-boot.so test/ulib/ulib_test
+INPUTS-$(CONFIG_ULIB) += libu-boot.a
 endif
 endif
 
@@ -1869,6 +1870,20 @@  libu-boot.so: $(u-boot-init) $(u-boot-main) $(u-boot-keep-syms-lto) \
 		$(LIB_LDS) FORCE
 	$(call if_changed,libu-boot.so)
 
+# Build U-Boot as a static library
+# Create a fat archive with all object files (except arch/sandbox/cpu/main.o)
+# Avoid partial linking so as to preserve the linker-list sections
+quiet_cmd_libu-boot.a = AR      $@
+      cmd_libu-boot.a = rm -f $@ $@.tmp $@.objlist; \
+	$(AR) rcT $@.tmp $(u-boot-init) $(u-boot-main) \
+		$(u-boot-keep-syms-lto); \
+	$(AR) t $@.tmp | grep -v "arch/sandbox/cpu/main\.o$$" > $@.objlist; \
+	cat $@.objlist | xargs $(AR) rcs $@; \
+	rm -f $@.tmp $@.objlist
+
+libu-boot.a: $(u-boot-init) $(u-boot-main) $(u-boot-keep-syms-lto) FORCE
+	$(call if_changed,libu-boot.a)
+
 # Build ulib_test that links with shared library
 quiet_cmd_ulib_test = HOSTCC  $@
       cmd_ulib_test = $(HOSTCC) $(HOSTCFLAGS) \