From 428d1f78fd58811dbe0fe1d57e7100b6c8c2e432 Mon Sep 17 00:00:00 2001 From: aviralgarg05 Date: Wed, 3 Jun 2026 19:34:25 +0530 Subject: [PATCH] examples/sotest: support packaged shared library fixtures --- examples/sotest/Make.defs | 9 +- examples/sotest/main/Makefile | 14 ++- examples/sotest/main/mk_pkg_fixture_shared.py | 112 ++++++++++++++++++ examples/sotest/main/sotest_main.c | 98 +++++++++++++-- 4 files changed, 220 insertions(+), 13 deletions(-) create mode 100644 examples/sotest/main/mk_pkg_fixture_shared.py diff --git a/examples/sotest/Make.defs b/examples/sotest/Make.defs index c674e34e66e..17afaca4367 100644 --- a/examples/sotest/Make.defs +++ b/examples/sotest/Make.defs @@ -20,4 +20,11 @@ # ############################################################################ -include $(wildcard $(APPDIR)/examples/sotest/*/Make.defs) +ifeq ($(CONFIG_EXAMPLES_SOTEST),y) +include $(APPDIR)/examples/sotest/main/Make.defs +endif + +ifneq ($(CONFIG_EXAMPLES_SOTEST),n) +include $(APPDIR)/examples/sotest/modprint/Make.defs +include $(APPDIR)/examples/sotest/sotest/Make.defs +endif diff --git a/examples/sotest/main/Makefile b/examples/sotest/main/Makefile index 064b0b434b7..17461bb17ca 100644 --- a/examples/sotest/main/Makefile +++ b/examples/sotest/main/Makefile @@ -50,8 +50,20 @@ ifeq ($(CONFIG_EXAMPLES_SOTEST_BUILTINFS),y) ROMFSIMG = sotest_romfs.img ROMFSSRC = sotest_romfs.c ROMFSOBJ = $(ROMFSSRC:.c=$(OBJEXT)) +ifdef CONFIG_SYSTEM_NXPKG +PKGSHAREDINDEX = $(BINDIR)/shared-index.json +PKGSHAREDTEST = $(BINDIR)/pkgsotest.nsh +PKG_FIXTURE_GEN = $(APPDIR)/examples/sotest/main/mk_pkg_fixture_shared.py +PKG_FIXTURE_OUTPUTS = $(PKGSHAREDINDEX) $(PKGSHAREDTEST) + +$(PKG_FIXTURE_OUTPUTS) &: $(BINDIR)/modprint $(BINDIR)/sotest $(PKG_FIXTURE_GEN) + $(Q) python3 $(PKG_FIXTURE_GEN) \ + $(BINDIR)/modprint $(BINDIR)/sotest \ + $(PKGSHAREDINDEX) $(PKGSHAREDTEST) \ + $(CONFIG_ARCH) $(CONFIG_ARCH_BOARD) +endif -$(ROMFSIMG): +$(ROMFSIMG): $(PKG_FIXTURE_OUTPUTS) $(Q) genromfs -d $(BINDIR) -f $@ $(ROMFSSRC): $(ROMFSIMG) diff --git a/examples/sotest/main/mk_pkg_fixture_shared.py b/examples/sotest/main/mk_pkg_fixture_shared.py new file mode 100644 index 00000000000..dce6f256807 --- /dev/null +++ b/examples/sotest/main/mk_pkg_fixture_shared.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 +# +# SPDX-License-Identifier: Apache-2.0 + +import hashlib +import json +import pathlib +import sys + + +def sha256_file(path: pathlib.Path) -> str: + digest = hashlib.sha256() + with path.open("rb") as infile: + while True: + chunk = infile.read(4096) + if not chunk: + break + digest.update(chunk) + + return digest.hexdigest() + + +def write_shared_index(path: pathlib.Path, arch: str, compat: str, + modprint_digest: str, sotest_digest: str) -> None: + payload = { + "packages": [ + { + "name": "modprint", + "version": "1.0.0", + "arch": arch, + "compat": compat, + "artifact": "/mnt/sotest/romfs/modprint", + "sha256": modprint_digest, + "type": "shared-lib", + }, + { + "name": "modprint", + "version": "9.9.9", + "arch": "arm", + "compat": "stm32f4discovery", + "artifact": "/mnt/sotest/romfs/modprint", + "sha256": modprint_digest, + "type": "shared-lib", + }, + { + "name": "sotest", + "version": "1.0.0", + "arch": arch, + "compat": compat, + "artifact": "/mnt/sotest/romfs/sotest", + "sha256": sotest_digest, + "type": "shared-lib", + }, + { + "name": "sotest", + "version": "9.9.9", + "arch": "arm", + "compat": "stm32f4discovery", + "artifact": "/mnt/sotest/romfs/sotest", + "sha256": sotest_digest, + "type": "shared-lib", + }, + ] + } + + path.write_text(json.dumps(payload, separators=(",", ":")) + "\n", + encoding="utf-8") + + +def write_shared_script(path: pathlib.Path) -> None: + script = "\n".join( + [ + "mount -t tmpfs /etc", + "mount -t tmpfs /var", + "mkdir /etc/nxpkg", + "cp /mnt/sotest/romfs/shared-index.json /etc/nxpkg/index.json", + "nxpkg install modprint", + "nxpkg install sotest", + "cp /var/lib/nxpkg/pkgs/modprint/1.0.0/modprint /etc/m", + "cp /var/lib/nxpkg/pkgs/sotest/1.0.0/sotest /etc/s", + "sotest /etc/m /etc/s", + "nxpkg list", + "", + ] + ) + path.write_text(script, encoding="utf-8") + + +def main() -> int: + if len(sys.argv) != 7: + print( + "usage: mk_pkg_fixture_shared.py " + " ", + file=sys.stderr, + ) + return 1 + + modprint = pathlib.Path(sys.argv[1]) + sotest = pathlib.Path(sys.argv[2]) + shared_index = pathlib.Path(sys.argv[3]) + shared_script = pathlib.Path(sys.argv[4]) + arch = sys.argv[5] + compat = sys.argv[6] + + write_shared_index(shared_index, arch, compat, + sha256_file(modprint), sha256_file(sotest)) + write_shared_script(shared_script) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/examples/sotest/main/sotest_main.c b/examples/sotest/main/sotest_main.c index 0d399568fcd..6b2417ddb21 100644 --- a/examples/sotest/main/sotest_main.c +++ b/examples/sotest/main/sotest_main.c @@ -82,6 +82,21 @@ #define SOTEST_DEVPATH_FMT "/dev/ram%d" +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void sotest_show_usage(FAR const char *progname) +{ +#if CONFIG_LIBC_ELF_MAXDEPEND > 0 + fprintf(stderr, + "Usage: %s [--mount] [ ]\n", + progname); +#else + fprintf(stderr, "Usage: %s [--mount] []\n", progname); +#endif +} + /**************************************************************************** * Symbols from Auto-Generated Code ****************************************************************************/ @@ -104,7 +119,11 @@ extern const int g_sot_nexports; int main(int argc, FAR char *argv[]) { + FAR const char *modprint_path = NULL; + FAR const char *sotest_path = NULL; + bool mount_only = false; #ifdef CONFIG_EXAMPLES_SOTEST_BUILTINFS + bool mounted_builtinfs = false; char devname[32]; #endif #if CONFIG_LIBC_ELF_MAXDEPEND > 0 @@ -117,6 +136,38 @@ int main(int argc, FAR char *argv[]) #ifdef CONFIG_EXAMPLES_SOTEST_BUILTINFS struct boardioc_romdisk_s desc; #endif + +#if CONFIG_LIBC_ELF_MAXDEPEND > 0 + if (argc == 2 && strcmp(argv[1], "--mount") == 0) + { + mount_only = true; + } + else if (argc == 3) + { + modprint_path = argv[1]; + sotest_path = argv[2]; + } + else if (argc != 1) + { + sotest_show_usage(argv[0]); + return EXIT_FAILURE; + } +#else + if (argc == 2 && strcmp(argv[1], "--mount") == 0) + { + mount_only = true; + } + else if (argc == 2) + { + sotest_path = argv[1]; + } + else if (argc != 1) + { + sotest_show_usage(argv[0]); + return EXIT_FAILURE; + } +#endif + /* Set the shared library symbol table */ ret = dlsymtab((FAR struct symtab_s *)g_sot_exports, g_sot_nexports); @@ -127,6 +178,8 @@ int main(int argc, FAR char *argv[]) } #ifdef CONFIG_EXAMPLES_SOTEST_BUILTINFS + if (sotest_path == NULL || mount_only) + { /* Create a ROM disk for the ROMFS filesystem */ desc.minor = 0; /* Minor device number of the ROM disk. */ @@ -168,28 +221,48 @@ int main(int argc, FAR char *argv[]) devname, BINDIR, strerror(errno)); exit(EXIT_FAILURE); } + + mounted_builtinfs = true; + } #endif /* CONFIG_EXAMPLES_SOTEST_BUILTINFS */ + if (mount_only) + { + return EXIT_SUCCESS; + } + +#if CONFIG_LIBC_ELF_MAXDEPEND > 0 + if (modprint_path == NULL) + { + modprint_path = BINDIR "/modprint"; + } +#endif + + if (sotest_path == NULL) + { + sotest_path = BINDIR "/sotest"; + } + #if CONFIG_LIBC_ELF_MAXDEPEND > 0 /* Install the first test shared library. The first shared library only * verifies that symbols exported by one shared library can be used to * resolve undefined symbols in a second shared library. */ - handle1 = dlopen(BINDIR "/modprint", RTLD_NOW | RTLD_LOCAL); + handle1 = dlopen(modprint_path, RTLD_NOW | RTLD_LOCAL); if (handle1 == NULL) { - fprintf(stderr, "ERROR: dlopen(%s/modprint) failed\n", BINDIR); + fprintf(stderr, "ERROR: dlopen(%s) failed\n", modprint_path); exit(EXIT_FAILURE); } #endif /* Install the second test shared library */ - handle2 = dlopen(BINDIR "/sotest", RTLD_NOW | RTLD_LOCAL); + handle2 = dlopen(sotest_path, RTLD_NOW | RTLD_LOCAL); if (handle2 == NULL) { - fprintf(stderr, "ERROR: dlopen(%s/sotest) failed\n", BINDIR); + fprintf(stderr, "ERROR: dlopen(%s) failed\n", sotest_path); exit(EXIT_FAILURE); } @@ -288,15 +361,18 @@ int main(int argc, FAR char *argv[]) #endif #ifdef CONFIG_EXAMPLES_SOTEST_BUILTINFS - ret = umount(BINDIR); - if (ret < 0) + if (mounted_builtinfs) { - fprintf(stderr, "ERROR: umount(%s) failed: %d\n", - BINDIR, errno); - exit(EXIT_FAILURE); - } + ret = umount(BINDIR); + if (ret < 0) + { + fprintf(stderr, "ERROR: umount(%s) failed: %d\n", + BINDIR, errno); + exit(EXIT_FAILURE); + } - unlink(devname); + unlink(devname); + } #endif /* CONFIG_EXAMPLES_SOTEST_BUILTINFS */ return EXIT_SUCCESS;