aboutsummaryrefslogtreecommitdiffstats
path: root/Makefile
blob: e9d44be290a16aa44d1859aa1d9112f36146b416 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
CC      := gcc
CFLAGS  := -std=c11 -O2 -Wall -Wextra -Wno-unused-parameter -g -D_POSIX_C_SOURCE=200809L
LDFLAGS :=
SRCDIR  := src
BUILD   := build
# The native binary is every .c except the wasm host layer.
SRCS    := $(filter-out $(SRCDIR)/wasm.c,$(wildcard $(SRCDIR)/*.c))
OBJS    := $(patsubst $(SRCDIR)/%.c,$(BUILD)/%.o,$(SRCS))
BIN     := $(BUILD)/sl0pboy

# ---- wasm ----
# Only the portable core plus the wasm host layer: render/kitty/control/main
# are terminal-and-POSIX bound and have no meaning in a browser.
# zig cc carries its own wasm backend, linker and wasi-libc, so there is no
# emscripten dependency. Reactor model = no main(), exports stay callable.
WASM_SRCS := $(addprefix $(SRCDIR)/,cpu.c ppu.c timer.c gb.c cart.c wasm.c)
WASM_OUT  := web/sl0pboy.wasm
WASM_CC   := zig cc
# --stack-size: zig defaults the wasm stack to 16MB, which lands in the
# module's *initial* memory - so every Game Boy on the page cost 16.25MB
# before running an instruction. The machine is a static and the C code has no
# deep recursion, so 256KB is plenty: 260 pages -> 8 pages per instance.
WASM_FLAGS := -target wasm32-wasi -mexec-model=reactor -O2 -std=c11 \
              -Wall -Wextra -Wno-unused-parameter \
              -Wl,--no-entry -Wl,--export-dynamic -Wl,--strip-all \
              -Wl,-z,stack-size=262144

.PHONY: all clean run wasm

all: $(BIN)

wasm: $(WASM_OUT)

$(WASM_OUT): $(WASM_SRCS) $(wildcard $(SRCDIR)/*.h)
	@mkdir -p $(dir $@)
	$(WASM_CC) $(WASM_FLAGS) $(WASM_SRCS) -o $@
	@echo "built $@ ($$(du -h $@ | cut -f1))"

$(BIN): $(OBJS)
	$(CC) $(CFLAGS) $(OBJS) -o $@ $(LDFLAGS)

$(BUILD)/%.o: $(SRCDIR)/%.c | $(BUILD)
	$(CC) $(CFLAGS) -c $< -o $@

$(BUILD):
	mkdir -p $(BUILD)

clean:
	rm -rf $(BUILD) $(WASM_OUT)

run: $(BIN)
	$(BIN) $(ROM)