Skip to content

Commit c0ea45a

Browse files
committed
examples/sotest: support packaged shared library fixtures
Signed-off-by: aviralgarg05 <gargaviral99@gmail.com>
1 parent 97802bd commit c0ea45a

4 files changed

Lines changed: 220 additions & 13 deletions

File tree

examples/sotest/Make.defs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,11 @@
2020
#
2121
############################################################################
2222

23-
include $(wildcard $(APPDIR)/examples/sotest/*/Make.defs)
23+
ifeq ($(CONFIG_EXAMPLES_SOTEST),y)
24+
include $(APPDIR)/examples/sotest/main/Make.defs
25+
endif
26+
27+
ifneq ($(CONFIG_EXAMPLES_SOTEST),n)
28+
include $(APPDIR)/examples/sotest/modprint/Make.defs
29+
include $(APPDIR)/examples/sotest/sotest/Make.defs
30+
endif

examples/sotest/main/Makefile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,20 @@ ifeq ($(CONFIG_EXAMPLES_SOTEST_BUILTINFS),y)
5050
ROMFSIMG = sotest_romfs.img
5151
ROMFSSRC = sotest_romfs.c
5252
ROMFSOBJ = $(ROMFSSRC:.c=$(OBJEXT))
53+
ifdef CONFIG_SYSTEM_NXPKG
54+
PKGSHAREDINDEX = $(BINDIR)/shared-index.json
55+
PKGSHAREDTEST = $(BINDIR)/pkgsotest.nsh
56+
PKG_FIXTURE_GEN = $(APPDIR)/examples/sotest/main/mk_pkg_fixture_shared.py
57+
PKG_FIXTURE_OUTPUTS = $(PKGSHAREDINDEX) $(PKGSHAREDTEST)
58+
59+
$(PKG_FIXTURE_OUTPUTS) &: $(BINDIR)/modprint $(BINDIR)/sotest $(PKG_FIXTURE_GEN)
60+
$(Q) python3 $(PKG_FIXTURE_GEN) \
61+
$(BINDIR)/modprint $(BINDIR)/sotest \
62+
$(PKGSHAREDINDEX) $(PKGSHAREDTEST) \
63+
$(CONFIG_ARCH) $(CONFIG_ARCH_BOARD)
64+
endif
5365

54-
$(ROMFSIMG):
66+
$(ROMFSIMG): $(PKG_FIXTURE_OUTPUTS)
5567
$(Q) genromfs -d $(BINDIR) -f $@
5668

5769
$(ROMFSSRC): $(ROMFSIMG)
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/usr/bin/env python3
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import hashlib
6+
import json
7+
import pathlib
8+
import sys
9+
10+
11+
def sha256_file(path: pathlib.Path) -> str:
12+
digest = hashlib.sha256()
13+
with path.open("rb") as infile:
14+
while True:
15+
chunk = infile.read(4096)
16+
if not chunk:
17+
break
18+
digest.update(chunk)
19+
20+
return digest.hexdigest()
21+
22+
23+
def write_shared_index(path: pathlib.Path, arch: str, compat: str,
24+
modprint_digest: str, sotest_digest: str) -> None:
25+
payload = {
26+
"packages": [
27+
{
28+
"name": "modprint",
29+
"version": "1.0.0",
30+
"arch": arch,
31+
"compat": compat,
32+
"artifact": "/mnt/sotest/romfs/modprint",
33+
"sha256": modprint_digest,
34+
"type": "shared-lib",
35+
},
36+
{
37+
"name": "modprint",
38+
"version": "9.9.9",
39+
"arch": "arm",
40+
"compat": "stm32f4discovery",
41+
"artifact": "/mnt/sotest/romfs/modprint",
42+
"sha256": modprint_digest,
43+
"type": "shared-lib",
44+
},
45+
{
46+
"name": "sotest",
47+
"version": "1.0.0",
48+
"arch": arch,
49+
"compat": compat,
50+
"artifact": "/mnt/sotest/romfs/sotest",
51+
"sha256": sotest_digest,
52+
"type": "shared-lib",
53+
},
54+
{
55+
"name": "sotest",
56+
"version": "9.9.9",
57+
"arch": "arm",
58+
"compat": "stm32f4discovery",
59+
"artifact": "/mnt/sotest/romfs/sotest",
60+
"sha256": sotest_digest,
61+
"type": "shared-lib",
62+
},
63+
]
64+
}
65+
66+
path.write_text(json.dumps(payload, separators=(",", ":")) + "\n",
67+
encoding="utf-8")
68+
69+
70+
def write_shared_script(path: pathlib.Path) -> None:
71+
script = "\n".join(
72+
[
73+
"mount -t tmpfs /etc",
74+
"mount -t tmpfs /var",
75+
"mkdir /etc/nxpkg",
76+
"cp /mnt/sotest/romfs/shared-index.json /etc/nxpkg/index.json",
77+
"nxpkg install modprint",
78+
"nxpkg install sotest",
79+
"cp /var/lib/nxpkg/pkgs/modprint/1.0.0/modprint /etc/m",
80+
"cp /var/lib/nxpkg/pkgs/sotest/1.0.0/sotest /etc/s",
81+
"sotest /etc/m /etc/s",
82+
"nxpkg list",
83+
"",
84+
]
85+
)
86+
path.write_text(script, encoding="utf-8")
87+
88+
89+
def main() -> int:
90+
if len(sys.argv) != 7:
91+
print(
92+
"usage: mk_pkg_fixture_shared.py <modprint-bin> <sotest-bin> "
93+
"<shared-index-json> <pkgsotest-nsh> <arch> <compat>",
94+
file=sys.stderr,
95+
)
96+
return 1
97+
98+
modprint = pathlib.Path(sys.argv[1])
99+
sotest = pathlib.Path(sys.argv[2])
100+
shared_index = pathlib.Path(sys.argv[3])
101+
shared_script = pathlib.Path(sys.argv[4])
102+
arch = sys.argv[5]
103+
compat = sys.argv[6]
104+
105+
write_shared_index(shared_index, arch, compat,
106+
sha256_file(modprint), sha256_file(sotest))
107+
write_shared_script(shared_script)
108+
return 0
109+
110+
111+
if __name__ == "__main__":
112+
raise SystemExit(main())

examples/sotest/main/sotest_main.c

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@
8282

8383
#define SOTEST_DEVPATH_FMT "/dev/ram%d"
8484

85+
/****************************************************************************
86+
* Private Functions
87+
****************************************************************************/
88+
89+
static void sotest_show_usage(FAR const char *progname)
90+
{
91+
#if CONFIG_LIBC_ELF_MAXDEPEND > 0
92+
fprintf(stderr,
93+
"Usage: %s [--mount] [<modprint-path> <sotest-path>]\n",
94+
progname);
95+
#else
96+
fprintf(stderr, "Usage: %s [--mount] [<sotest-path>]\n", progname);
97+
#endif
98+
}
99+
85100
/****************************************************************************
86101
* Symbols from Auto-Generated Code
87102
****************************************************************************/
@@ -104,7 +119,11 @@ extern const int g_sot_nexports;
104119

105120
int main(int argc, FAR char *argv[])
106121
{
122+
FAR const char *modprint_path = NULL;
123+
FAR const char *sotest_path = NULL;
124+
bool mount_only = false;
107125
#ifdef CONFIG_EXAMPLES_SOTEST_BUILTINFS
126+
bool mounted_builtinfs = false;
108127
char devname[32];
109128
#endif
110129
#if CONFIG_LIBC_ELF_MAXDEPEND > 0
@@ -117,6 +136,38 @@ int main(int argc, FAR char *argv[])
117136
#ifdef CONFIG_EXAMPLES_SOTEST_BUILTINFS
118137
struct boardioc_romdisk_s desc;
119138
#endif
139+
140+
#if CONFIG_LIBC_ELF_MAXDEPEND > 0
141+
if (argc == 2 && strcmp(argv[1], "--mount") == 0)
142+
{
143+
mount_only = true;
144+
}
145+
else if (argc == 3)
146+
{
147+
modprint_path = argv[1];
148+
sotest_path = argv[2];
149+
}
150+
else if (argc != 1)
151+
{
152+
sotest_show_usage(argv[0]);
153+
return EXIT_FAILURE;
154+
}
155+
#else
156+
if (argc == 2 && strcmp(argv[1], "--mount") == 0)
157+
{
158+
mount_only = true;
159+
}
160+
else if (argc == 2)
161+
{
162+
sotest_path = argv[1];
163+
}
164+
else if (argc != 1)
165+
{
166+
sotest_show_usage(argv[0]);
167+
return EXIT_FAILURE;
168+
}
169+
#endif
170+
120171
/* Set the shared library symbol table */
121172

122173
ret = dlsymtab((FAR struct symtab_s *)g_sot_exports, g_sot_nexports);
@@ -127,6 +178,8 @@ int main(int argc, FAR char *argv[])
127178
}
128179

129180
#ifdef CONFIG_EXAMPLES_SOTEST_BUILTINFS
181+
if (sotest_path == NULL || mount_only)
182+
{
130183
/* Create a ROM disk for the ROMFS filesystem */
131184

132185
desc.minor = 0; /* Minor device number of the ROM disk. */
@@ -168,28 +221,48 @@ int main(int argc, FAR char *argv[])
168221
devname, BINDIR, strerror(errno));
169222
exit(EXIT_FAILURE);
170223
}
224+
225+
mounted_builtinfs = true;
226+
}
171227
#endif /* CONFIG_EXAMPLES_SOTEST_BUILTINFS */
172228

229+
if (mount_only)
230+
{
231+
return EXIT_SUCCESS;
232+
}
233+
234+
#if CONFIG_LIBC_ELF_MAXDEPEND > 0
235+
if (modprint_path == NULL)
236+
{
237+
modprint_path = BINDIR "/modprint";
238+
}
239+
#endif
240+
241+
if (sotest_path == NULL)
242+
{
243+
sotest_path = BINDIR "/sotest";
244+
}
245+
173246
#if CONFIG_LIBC_ELF_MAXDEPEND > 0
174247
/* Install the first test shared library. The first shared library only
175248
* verifies that symbols exported by one shared library can be used to
176249
* resolve undefined symbols in a second shared library.
177250
*/
178251

179-
handle1 = dlopen(BINDIR "/modprint", RTLD_NOW | RTLD_LOCAL);
252+
handle1 = dlopen(modprint_path, RTLD_NOW | RTLD_LOCAL);
180253
if (handle1 == NULL)
181254
{
182-
fprintf(stderr, "ERROR: dlopen(%s/modprint) failed\n", BINDIR);
255+
fprintf(stderr, "ERROR: dlopen(%s) failed\n", modprint_path);
183256
exit(EXIT_FAILURE);
184257
}
185258
#endif
186259

187260
/* Install the second test shared library */
188261

189-
handle2 = dlopen(BINDIR "/sotest", RTLD_NOW | RTLD_LOCAL);
262+
handle2 = dlopen(sotest_path, RTLD_NOW | RTLD_LOCAL);
190263
if (handle2 == NULL)
191264
{
192-
fprintf(stderr, "ERROR: dlopen(%s/sotest) failed\n", BINDIR);
265+
fprintf(stderr, "ERROR: dlopen(%s) failed\n", sotest_path);
193266
exit(EXIT_FAILURE);
194267
}
195268

@@ -288,15 +361,18 @@ int main(int argc, FAR char *argv[])
288361
#endif
289362

290363
#ifdef CONFIG_EXAMPLES_SOTEST_BUILTINFS
291-
ret = umount(BINDIR);
292-
if (ret < 0)
364+
if (mounted_builtinfs)
293365
{
294-
fprintf(stderr, "ERROR: umount(%s) failed: %d\n",
295-
BINDIR, errno);
296-
exit(EXIT_FAILURE);
297-
}
366+
ret = umount(BINDIR);
367+
if (ret < 0)
368+
{
369+
fprintf(stderr, "ERROR: umount(%s) failed: %d\n",
370+
BINDIR, errno);
371+
exit(EXIT_FAILURE);
372+
}
298373

299-
unlink(devname);
374+
unlink(devname);
375+
}
300376
#endif /* CONFIG_EXAMPLES_SOTEST_BUILTINFS */
301377

302378
return EXIT_SUCCESS;

0 commit comments

Comments
 (0)