diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2025-06-14 15:11:08 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2025-06-14 15:11:08 +0200 |
| commit | e3a1beda8665f4757aa7cab727fcf3b09419a451 (patch) | |
| tree | b27346d31cd74484295b99acf827d668513e37e1 | |
| parent | 47a60f0f1c71d6e5bca5d8ee72f0ad7c63e1817c (diff) | |
Embed Sysroot Into Binary
We now embed the PICO-w sysroot files (the WASM blob,
data pod and boot rom) into the final `picow` binary in
order to simplify distribution. This commit adds a new
`generate-sysroot` tool to achieve it.
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | lib/wamr/test/fibonacci.c | 8 | ||||
| -rw-r--r-- | platform/sdl/main.c | 11 | ||||
| -rw-r--r-- | src/sysroot.h | 16 | ||||
| -rw-r--r-- | tools/extract-data-pod.c | 2 | ||||
| -rw-r--r-- | tools/generate-sysroot.c | 69 |
7 files changed, 98 insertions, 13 deletions
@@ -3,6 +3,7 @@ carts/*.js carts/*.wasm carts/*.p8.png carts/*.p8.rom +src/sysroot.c system/ target/ tools/*.exe @@ -9,8 +9,8 @@ platform/sdl: platform/sdl/picow system ; platform/sdl/picow: platform/sdl/main.c src/picow.c src/png.c include/picow.h $(CC) -o $@ $< src/picow.c src/png.c -Iinclude/ -Ilib/ $$(pkg-config SDL3 --cflags --libs) -.PHONY: system -system: system/pico8.data.pod system/pico8.wasm system/boot.p8.rom ; +src/sysroot.c: tools/generate-sysroot.exe system/pico8.data.pod system/pico8.wasm system/boot.p8.rom + $< system > $@ system/pico8.data.pod: carts/pico8.js tools/extract-data-pod.exe @mkdir -p system diff --git a/lib/wamr/test/fibonacci.c b/lib/wamr/test/fibonacci.c index ddb5f9c..6effd51 100644 --- a/lib/wamr/test/fibonacci.c +++ b/lib/wamr/test/fibonacci.c @@ -10,12 +10,12 @@ __asm__(".global maxint\n" ".import_module maxint, env\n" ".import_name maxint, maxint\n" ".globaltype maxint, i32, immutable\n"); -static inline int getmaxint() { - int val; +static inline int32_t getmaxint() { + int32_t val; __asm__("global.get maxint\n" - "local.set %0" + "local.set %w0" : "=r"(val)); - return val; + return val > 0 ? val : INT32_MAX; } WASM_IMPORT("env", "lognumber") diff --git a/platform/sdl/main.c b/platform/sdl/main.c index f2dcb31..1087da8 100644 --- a/platform/sdl/main.c +++ b/platform/sdl/main.c @@ -9,8 +9,7 @@ static void usage(char const *name) { // clang-format off printf("Usage: %s [-s <sysroot>] [-c <carts>] [-v[v[v]]]\n", name); - printf(" -s <sysroot> The root directory with PICO-8 system files ('pico8.wasm',\n"); - printf(" 'pico8.data.pod', and 'boot.p8.rom'). Defaults to 'system'.\n"); + printf(" -b <boot> An alternate boot cart to use.\n"); printf(" -c <carts> Carts directory. Defaults to 'carts'.\n"); printf(" -v[v[v]] Increasingly verbose logging.\n"); // clang-format on @@ -267,18 +266,18 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { return FAIL("failed to initialize SDL"); } - char *sysroot = "system"; + char *boot = NULL; char *carts = "carts"; SDL_LogPriority logp = SDL_GetLogPriority(0); int c; - while ((c = getopt(argc, argv, "hs:c:v")) != -1) { + while ((c = getopt(argc, argv, "hb:c:v")) != -1) { switch (c) { case 'h': usage(argv[0]); return SDL_APP_SUCCESS; - case 's': - sysroot = optarg; + case 'b': + boot = optarg; break; case 'c': carts = optarg; diff --git a/src/sysroot.h b/src/sysroot.h new file mode 100644 index 0000000..e2e8ad1 --- /dev/null +++ b/src/sysroot.h @@ -0,0 +1,16 @@ +#ifndef _PICOW_SYSROOT_H_ +#define _PICOW_SYSROOT_H_ + +#include <stddef.h> +#include <stdint.h> + +struct pw_sysroot_data { + size_t len; + uint8_t data[]; +}; + +extern struct pw_sysroot_data pw_sysroot_pico8_wasm; +extern struct pw_sysroot_data pw_sysroot_pico8_data_pod; +extern struct pw_sysroot_data pw_sysroot_boot_rom; + +#endif diff --git a/tools/extract-data-pod.c b/tools/extract-data-pod.c index 50beadc..c9d8cc0 100644 --- a/tools/extract-data-pod.c +++ b/tools/extract-data-pod.c @@ -18,7 +18,7 @@ static inline void ensure(bool cond, char const *msg) { } } -int main(int argc, char *argv[argc + 1]) { +int main(int argc, char *argv[argc]) { if (argc != 3) { printf("Usage: %s <pico8.js> <data.pod>\n", argv[0]); return EXIT_FAILURE; diff --git a/tools/generate-sysroot.c b/tools/generate-sysroot.c new file mode 100644 index 0000000..7bd31dc --- /dev/null +++ b/tools/generate-sysroot.c @@ -0,0 +1,69 @@ +#include <stdbool.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> + +static inline void check(bool cond, char const *msg) { + if (!cond) { + perror(msg); + exit(EXIT_FAILURE); + } +} + +static void write_sysroot_data(char const *name, char const *file) { + FILE *f = fopen(file, "rb"); + check(f, "fopen(input)"); + + check(fseek(f, 0, SEEK_END) == 0, "fseek(end)"); + size_t size = ftell(f); + check(fseek(f, 0, SEEK_SET) == 0, "fseek(start)"); + + printf("struct pw_sysroot_data %s = {\n", name); + printf(" .len = %zu,\n", size); + printf(" .data = {\n"); + + size_t total = 0; + size_t n; + uint8_t data[16]; + while ((n = fread(data, 1, sizeof(data), f))) { + total += n; + + printf(" "); + for (size_t i = 0; i < n; i++) { + printf(" 0x%02x,", data[i]); + } + printf("\n"); + } + + printf(" },\n};\n"); + + check(total == size, "fread(input)"); + check(fclose(f) == 0, "fclose(input)"); +} + +int main(int argc, char *argv[argc]) { + if (argc != 2) { + printf("Usage: %s <sysroot>\n", argv[0]); + return EXIT_FAILURE; + } + + printf("// This file is automatically generated, do not edit by hand.\n"); + printf("// clang-format off\n"); + printf("#include \"sysroot.h\"\n"); + + struct { + char const *name; + char const *file; + } entries[] = { + {"pw_sysroot_pico8_wasm", "pico8.wasm"}, + {"pw_sysroot_pico8_data_pod", "pico8.data.pod"}, + {"pw_sysroot_boot_rom", "boot.p8.rom"}, + }; + for (size_t i = 0; i < sizeof entries / sizeof entries[0]; i++) { + char file[256]; + snprintf(file, sizeof(file), "%s/%s", argv[1], entries[i].file); + write_sysroot_data(entries[i].name, file); + } + + return EXIT_SUCCESS; +} |
