CC ?= cc
ZIG ?= zig
WASM2JS ?= wasm2js
CFLAGS = -std=c99 -O2 -Wall -Wextra -Werror -Wno-unused-function -fno-strict-aliasing -Iinclude
EXPORTS = u_init u_set u_get u_enable u_enabled u_transport u_hold u_held u_scene u_revision u_composition_hash u_capture u_recall u_clear u_render u_output u_state_bytes u_events u_limited u_rate u_responses u_body_limited
.PHONY: all native wasm compat test clean
all: native
build:
	mkdir -p build
src/tables.inc: tools/generate_tables.py
	python3 tools/generate_tables.py
native: build/render build/libuniverse.so
build/render: src/universe.c src/gestures.inc src/tables.inc tools/render.c include/universe.h | build
	$(CC) $(CFLAGS) src/universe.c tools/render.c -o $@
build/libuniverse.so: src/universe.c src/gestures.inc src/tables.inc include/universe.h | build
	$(CC) $(CFLAGS) -shared -fPIC src/universe.c -o $@
wasm: build/universe.wasm
compat: build/universe-js.mjs
build/universe-js.mjs: build/universe.wasm
	$(WASM2JS) $< -O2 -o $@
build/universe.wasm: src/universe.c src/gestures.inc src/tables.inc include/universe.h | build
	$(ZIG) cc -target wasm32-freestanding -std=c99 -O2 -fno-builtin -nostdlib -Iinclude src/universe.c -Wl,--no-entry -Wl,--export-memory -Wl,-z,stack-size=16384 -Wl,--initial-memory=524288 -Wl,--max-memory=524288 $(foreach e,$(EXPORTS),-Wl,--export=$(e)) -o $@
test: native
	python3 tests/check_core.py
	python3 tests/check_resonance.py
	python3 tests/check_nine_presets.py
clean:
	rm -f build/render build/libuniverse.so build/universe.wasm
