[Concept,10/18] ulib: Create a test program for the shared library

Message ID 20250904130459.848794-11-sjg@u-boot.org
State New
Headers
Series ulib: Introduce building U-Boot as a shared library |

Commit Message

Simon Glass Sept. 4, 2025, 1:04 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

Provide a host program which can use the shared library. For now this
operates similarly to sandbox itself, but future work will make it more
flexible.

Leave it out of the build, since there are a few other pieces needed.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 Makefile              |  9 +++++++++
 test/ulib/Makefile    | 12 ++++++++++++
 test/ulib/ulib_test.c | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+)
 create mode 100644 test/ulib/Makefile
 create mode 100644 test/ulib/ulib_test.c
  

Patch

diff --git a/Makefile b/Makefile
index c4f3065d2e6..a0a9ce3196b 100644
--- a/Makefile
+++ b/Makefile
@@ -1863,6 +1863,15 @@  quiet_cmd_libu-boot.so = LD      $@
 libu-boot.so: $(u-boot-init) $(u-boot-main) $(u-boot-keep-syms-lto) FORCE
 	$(call if_changed,libu-boot.so)
 
+# Build ulib_test that links with shared library
+quiet_cmd_ulib_test = HOSTCC  $@
+      cmd_ulib_test = $(HOSTCC) $(HOSTCFLAGS) \
+	-I$(srctree)/arch/sandbox/include -o $@ $< -L$(obj) -lu-boot \
+	-Wl,-rpath,$(obj)
+
+test/ulib/ulib_test: test/ulib/ulib_test.o libu-boot.so FORCE
+	$(call if_changed,ulib_test)
+
 quiet_cmd_sym ?= SYM     $@
       cmd_sym ?= $(OBJDUMP) -t $< > $@
 u-boot.sym: u-boot FORCE
diff --git a/test/ulib/Makefile b/test/ulib/Makefile
new file mode 100644
index 00000000000..ae58e01fccd
--- /dev/null
+++ b/test/ulib/Makefile
@@ -0,0 +1,12 @@ 
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright 2025 Simon Glass <sjg@chromium.org>
+
+obj-y += ulib_test.o
+
+# Link with the shared library
+HOSTCFLAGS_ulib_test.o += -I$(srctree)/arch/sandbox/include
+HOSTLDLIBS_ulib_test += -L$(obj)/../.. -lu-boot -Wl,-rpath,$(obj)/../..
+
+# Ensure shared library is built first
+$(obj)/ulib_test: $(obj)/../../libu-boot.so
diff --git a/test/ulib/ulib_test.c b/test/ulib/ulib_test.c
new file mode 100644
index 00000000000..dd9cc0bb978
--- /dev/null
+++ b/test/ulib/ulib_test.c
@@ -0,0 +1,36 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Test application for U-Boot shared library
+ *
+ * This demonstrates linking against libu-boot.so
+ *
+ * Copyright 2025 Canonical
+ * Written by Simon Glass <simon.glass@canonical.com>
+ */
+
+/* Use system headers, not U-Boot headers */
+#include <stdio.h>
+#include <string.h>
+#include <init.h>
+#include <asm/global_data.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+int main(int argc, char *argv[])
+{
+	struct global_data data;
+	int ret;
+
+	printf("Calling U-Boot initialization via shared library...\n");
+
+	/* init global data */
+	memset(&data, '\0', sizeof(data));
+
+	ret = sandbox_init(argc, argv, &data);
+
+	/* Do pre- and post-relocation init */
+	board_init_f(gd->flags);
+	board_init_r(data.new_gd, 0);
+
+	return ret;
+}