From 93a202d1fa6cd05b494bbf7175d79c6da075f333 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Thu, 29 Oct 2026 16:09:56 +0100 Subject: Prebuild System Files In order to make it easier to build `picow` on systems _without_ PICO-8 support (which what this project targets...), we added a system for producing a `system.tar.gz` tarball that contains the generated system source required for building PICO-w (namely, the `pico8.wasm` transpiled to C, and the `sysroot` with the embedded data files needed by the application). I also moved away from `a b c: ...` style recipes, as they run multiple times in parallelized builds. I made it compatible with older `make`s, and it will hopefully generally work. --- Makefile | 38 +++++++++++++++++++++++++++++++------- tools/extract-data-pod.c | 13 ++++++++++--- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 6ed6297..fa4909a 100644 --- a/Makefile +++ b/Makefile @@ -5,11 +5,12 @@ AR ?= ar HOSTCC ?= cc PICO8 ?= pico8 WASM2C ?= wasm2c +TAR ?= tar ARCH ?= $(shell uname -m) CFLAGS ?= -std=c99 -Wall -Wextra -O2 -MMD -Isrc -Ilib -I$(SYSTEM) \ - $(shell pkg-config sdl3 --cflags) -HOSTCFLAGS ?= -std=c99 -Wall -Wextra -O2 -MMD + $(shell pkg-config sdl3 --cflags) +HOSTCFLAGS ?= -std=c99 -Wall -Wextra -O2 LDFLAGS ?= $(shell pkg-config sdl3 --libs) -lm W2CFLAGS ?= --disable-tail-call @@ -20,6 +21,7 @@ SYSTEM := build/system SRC := $(wildcard src/*.c) $(SYSTEM)/pico8.c $(SYSTEM)/sysroot.c OBJ := $(patsubst %.c,$(OUT)/%.o,$(SRC:build/%=%)) +SYS := $(addprefix $(SYSTEM)/,pico8.c pico8.h sysroot.c sysroot.h wasm-rt.h) .PHONY: all all: $(TARGET)/picow ; @@ -37,17 +39,37 @@ $(OUT)/system/%.o: $(SYSTEM)/%.c @ mkdir -p $(dir $@) @ $(CC) $(CFLAGS) -Wno-unused -Wno-unused-parameter -c $< -o $@ -$(SYSTEM)/pico8.h $(SYSTEM)/pico8.c: $(SYSTEM)/pico8.wasm +ifdef SYSTEMTAR + +$(SYS): $(SYSTEM)/.systemtar ; +$(SYSTEM)/.systemtar: $(SYSTEMTAR) + @ echo "TAR\t$(SYS)" + @ mkdir -p $(dir $@) + @ $(TAR) -xzf $< -C $(dir $@) + @ touch -c $(SYS) + @ touch $@ + +else + +$(SYSTEM)/system.tar.gz: $(SYS) + @ echo "TAR\t$@" + @ $(TAR) -czf $@ -C $(SYSTEM) $(^:$(SYSTEM)/%=%) + +$(SYSTEM)/pico8.h $(SYSTEM)/pico8.c $(SYSTEM)/wasm-rt.h: $(SYSTEM)/.pico8 ; +$(SYSTEM)/.pico8: $(SYSTEM)/pico8.wasm @ echo "WASM2C\t$(SYSTEM)/pico8.h $(SYSTEM)/pico8.c" @ $(WASM2C) $(W2CFLAGS) $< -o $(SYSTEM)/pico8.c @ echo '#include "wasmrt.h"' > $(SYSTEM)/wasm-rt.h + @ touch $@ -$(SYSTEM)/sysroot.h $(SYSTEM)/sysroot.c: $(TOOLS)/generate-sysroot \ - $(SYSTEM)/pico8.data.pod \ - $(SYSTEM)/unknown.p8.png \ - $(SYSTEM)/font.png +$(SYSTEM)/sysroot.h $(SYSTEM)/sysroot.c: $(SYSTEM)/.sysroot ; +$(SYSTEM)/.sysroot: $(TOOLS)/generate-sysroot \ + $(SYSTEM)/pico8.data.pod \ + $(SYSTEM)/unknown.p8.png \ + $(SYSTEM)/font.png @ echo "TOOL\t$(SYSTEM)/sysroot.h $(SYSTEM)/sysroot.c" @ $< $(SYSTEM) + @ touch $@ $(SYSTEM)/pico8.data.pod: $(TOOLS)/extract-data-pod $(SYSTEM)/pico8.js @ echo "TOOL\t$@" @@ -68,6 +90,8 @@ $(SYSTEM)/%.png: carts/%.p8 @ mkdir -p $(dir $@) @ $(PICO8) $< -export $@ >/dev/null +endif + $(TOOLS)/%: tools/%.c @ echo "CC\t$@" @ mkdir -p $(dir $@) diff --git a/tools/extract-data-pod.c b/tools/extract-data-pod.c index 50beadc..9a7bbdb 100644 --- a/tools/extract-data-pod.c +++ b/tools/extract-data-pod.c @@ -27,7 +27,10 @@ int main(int argc, char *argv[argc + 1]) { FILE *f = fopen(argv[1], "r"); check(f, "fopen(input)"); - char buffer[1024 * 1024]; + // 1 MiB is quite large for the stack, we also assume that the data.pod + // lives entirely within the first part of the file. This is true for the + // current version, and we can change this if it ever changes. + static char buffer[1024 * 1024]; size_t n = fread(buffer, 1, sizeof(buffer) - 1, f); check(n, "fread(input)"); buffer[n] = '\0'; @@ -46,16 +49,20 @@ int main(int argc, char *argv[argc + 1]) { for (char *s = strtok(start, sep); s; s = strtok(NULL, sep)) { long b = strtol(s, &s, 10); - ensure(b < 256 && *s == '\0', "failed to read byte"); + ensure(b >= 0 && b < 256 && *s == '\0', "failed to read byte"); + ensure((size_t)(dataptr - data) < sizeof(data), "data pod too large"); *(dataptr++) = (uint8_t)b; } } + size_t datalen = (size_t)(dataptr - data); + ensure(datalen, "failed to decode data.pod"); + FILE *o = fopen(argv[2], "wb"); check(o, "fopen(output)"); - size_t datalen = (size_t)(dataptr - data); check(datalen == fwrite(data, 1, datalen, o), "fwrite(output)"); + check(!fclose(o), "fclose(output)"); return EXIT_SUCCESS; } -- cgit v1.2.3