# zxdream — ZX81 BASIC interpreter with swappable UIs
CC      ?= gcc
CFLAGS  ?= -Wall -Wextra -Werror -std=c17 -g -fno-omit-frame-pointer -fsanitize=address,undefined
UI      ?= tty
CPPFLAGS += -DUNITY_INCLUDE_DOUBLE

# Install prefix per running user: root -> /usr/local, else ~/.local.
# Always overridable via PREFIX=; DESTDIR is honored for staged installs.
PREFIX  ?= $(shell [ "$$(id -u)" = 0 ] && echo /usr/local || echo $(HOME)/.local)
BINDIR  ?= $(PREFIX)/bin
# setfont(1) default search path on Debian; pass FONTDIR= to override.
FONTDIR ?= $(PREFIX)/share/consolefonts

CORE_SRC := src/core/screen.c
CORE_INC := src/core vendor/unity/src
BASIC_SRC := src/core/basic/token.c src/core/basic/expr.c src/core/basic/eval.c src/core/basic/exec.c src/core/basic/run.c src/core/basic/pload.c
MCP_SRC := src/mcp/srv.c src/mcp/tools.c
MCP_INC := -I src/mcp -I vendor/mcp-core/src -I vendor/cJSON/src
TEST_BINS := bin/test-screen bin/test-token bin/test-expr bin/test-eval bin/test-teletype bin/test-pload bin/test-numstr bin/test-pframe bin/test-timing bin/test-runext bin/test-mcp
FONTS     := fonts/zxdream-8x8.psf fonts/zxdream-32x32.psf fonts/zxdream-40x40.psf

# Place a freshly linked binary with a guaranteed exec bit: link to $@.tmp
# (gcc creates it with the exec bit), then mv (rename) it into place. A
# rename never touches permissions, so mounts that reject chmod
# (EOPNOTSUPP) cannot break the build. The final test -x fails loudly
# instead of leaving a silently unrunnable binary behind.
LINKED = @mv -f $@.tmp $@ && test -x $@ || { echo "ERROR: $@ has no exec bit after build (umask stripped +x?)"; false; }

.PHONY: all test backends fonts install uninstall clean run zip

all: bin/zxdream-$(UI) bin/console-test.sh bin/zxdream-mcp

# build every UI backend at once
backends: bin/zxdream-tty bin/zxdream-ncurses bin/zxdream-sdl3 bin/zxdream-raw bin/zxdream-mcp

bin/zxdream-tty: $(CORE_SRC) $(BASIC_SRC) src/ui/tty/main.c
	@mkdir -p bin
	$(CC) $(CFLAGS) $(CPPFLAGS) -I src/core/basic -I src/core $(CORE_SRC) $(BASIC_SRC) src/ui/tty/main.c -o $@.tmp -lm
	@$(LINKED)

bin/zxdream-sdl3: $(CORE_SRC) $(BASIC_SRC) src/ui/pframe.c src/ui/sdl3/main.c
	@mkdir -p bin
	$(CC) $(CFLAGS) $(CPPFLAGS) -I src/core/basic -I src/core -I src/ui $(CORE_SRC) $(BASIC_SRC) src/ui/pframe.c src/ui/sdl3/main.c -o $@.tmp -lm $(shell pkg-config --libs sdl3 2>/dev/null)
	@$(LINKED)

bin/zxdream-ncurses: $(CORE_SRC) $(BASIC_SRC) src/ui/ncurses/main.c
	@mkdir -p bin
	$(CC) $(CFLAGS) $(CPPFLAGS) -I src/core/basic -I src/core $(CORE_SRC) $(BASIC_SRC) src/ui/ncurses/main.c -o $@.tmp -lm -lncursesw
	@$(LINKED)

bin/zxdream-raw: $(CORE_SRC) $(BASIC_SRC) src/ui/raw/main.c
	@mkdir -p bin
	$(CC) $(CFLAGS) $(CPPFLAGS) -I src/core/basic -I src/core $(CORE_SRC) $(BASIC_SRC) src/ui/raw/main.c -o $@.tmp -lm
	@$(LINKED)
# MCP 2.0 stdio server (docs/mcp-server-spec.md); vendored mcp-core + cJSON
bin/zxdream-mcp: $(CORE_SRC) $(BASIC_SRC) $(MCP_SRC) vendor/mcp-core/src/mcpcore.c vendor/cJSON/src/cJSON.c src/mcp/main.c
	@mkdir -p bin
	$(CC) $(CFLAGS) $(CPPFLAGS) -D_POSIX_C_SOURCE=200809L -I src/core/basic -I src/core $(MCP_INC) $(CORE_SRC) $(BASIC_SRC) $(MCP_SRC) vendor/mcp-core/src/mcpcore.c vendor/cJSON/src/cJSON.c src/mcp/main.c -o $@.tmp -lm -pthread
	@$(LINKED)


bin/test-screen: $(CORE_SRC) tests/test_screen.c vendor/unity/src/unity.c
	@mkdir -p bin
	$(CC) $(CFLAGS) $(CPPFLAGS) -I src/core -I vendor/unity/src $(CORE_SRC) tests/test_screen.c vendor/unity/src/unity.c -o $@.tmp
	@$(LINKED)

bin/test-token: src/core/basic/token.c tests/test_token.c vendor/unity/src/unity.c
	@mkdir -p bin
	$(CC) $(CFLAGS) $(CPPFLAGS) -I src/core/basic -I vendor/unity/src src/core/basic/token.c tests/test_token.c vendor/unity/src/unity.c -o $@.tmp
	@$(LINKED)

bin/test-expr: src/core/basic/token.c src/core/basic/expr.c tests/test_expr.c vendor/unity/src/unity.c
	@mkdir -p bin
	$(CC) $(CFLAGS) $(CPPFLAGS) -I src/core/basic -I vendor/unity/src src/core/basic/token.c src/core/basic/expr.c tests/test_expr.c vendor/unity/src/unity.c -o $@.tmp
	@$(LINKED)

bin/test-eval: $(CORE_SRC) $(BASIC_SRC) tests/test_eval.c vendor/unity/src/unity.c
	@mkdir -p bin
	$(CC) $(CFLAGS) $(CPPFLAGS) -I src/core/basic -I src/core -I vendor/unity/src $(CORE_SRC) $(BASIC_SRC) tests/test_eval.c vendor/unity/src/unity.c -o $@.tmp -lm
	@$(LINKED)

# MCP tool tests (docs/mcp-server-spec.md 7): tools against fixture
# programs, single-threaded + one threaded pause/step/stop run.
bin/test-mcp: $(CORE_SRC) $(BASIC_SRC) $(MCP_SRC) vendor/mcp-core/src/mcpcore.c vendor/cJSON/src/cJSON.c tests/test_mcp.c vendor/unity/src/unity.c
	@mkdir -p bin
	$(CC) $(CFLAGS) $(CPPFLAGS) -D_POSIX_C_SOURCE=200809L -I src/core/basic -I src/core -I vendor/unity/src $(MCP_INC) $(CORE_SRC) $(BASIC_SRC) $(MCP_SRC) vendor/mcp-core/src/mcpcore.c vendor/cJSON/src/cJSON.c tests/test_mcp.c vendor/unity/src/unity.c -o $@.tmp -lm -pthread
	@$(LINKED)

# runext tests: external eval ownership + statement hook (MCP server,
# docs/mcp-server-spec.md C1/C2); wired into `make test`.
bin/test-runext: $(CORE_SRC) $(BASIC_SRC) tests/test_runext.c vendor/unity/src/unity.c
	@mkdir -p bin
	$(CC) $(CFLAGS) $(CPPFLAGS) -I src/core/basic -I src/core -I vendor/unity/src $(CORE_SRC) $(BASIC_SRC) tests/test_runext.c vendor/unity/src/unity.c -o $@.tmp -lm
	@$(LINKED)

# p-loader tests (docs/p-loader-spec.md); wired into `make test`.
bin/test-pload: $(CORE_SRC) $(BASIC_SRC) tests/test_pload.c vendor/unity/src/unity.c
	@mkdir -p bin
	$(CC) $(CFLAGS) $(CPPFLAGS) -I src/core/basic -I vendor/unity/src $(CORE_SRC) $(BASIC_SRC) tests/test_pload.c vendor/unity/src/unity.c -o $@.tmp -lm
	@$(LINKED)

# Appendix C number printing (docs/semantics-decisions.md)
bin/test-numstr: $(CORE_SRC) src/core/basic/token.c src/core/basic/expr.c src/core/basic/eval.c tests/test_numstr.c vendor/unity/src/unity.c
	@mkdir -p bin
	$(CC) $(CFLAGS) $(CPPFLAGS) -I src/core/basic -I src/core -I vendor/unity/src $(CORE_SRC) src/core/basic/token.c src/core/basic/expr.c src/core/basic/eval.c tests/test_numstr.c vendor/unity/src/unity.c -o $@.tmp -lm
	@$(LINKED)

bin/test-teletype: $(CORE_SRC) $(BASIC_SRC) tests/test_teletype.c vendor/unity/src/unity.c
	@mkdir -p bin
	$(CC) $(CFLAGS) $(CPPFLAGS) -I src/core/basic -I src/core -I vendor/unity/src $(CORE_SRC) $(BASIC_SRC) tests/test_teletype.c vendor/unity/src/unity.c -o $@.tmp -lm
	@$(LINKED)

# pframe: pixel renderer + PNG writer (tests/test_pframe.c)
bin/test-pframe: src/core/screen.c src/ui/pframe.c tests/test_pframe.c vendor/unity/src/unity.c
	@mkdir -p bin
	$(CC) $(CFLAGS) $(CPPFLAGS) -I src/core -I src/ui -I vendor/unity/src src/core/screen.c src/ui/pframe.c tests/test_pframe.c vendor/unity/src/unity.c -o $@.tmp
	@$(LINKED)

# FAST/SLOW timing (SPEC.md 4.5 M4; semantics-decisions.md "Timing")
bin/test-timing: $(CORE_SRC) $(BASIC_SRC) tests/test_timing.c vendor/unity/src/unity.c
	@mkdir -p bin
	$(CC) $(CFLAGS) $(CPPFLAGS) -I src/core/basic -I src/core -I vendor/unity/src $(CORE_SRC) $(BASIC_SRC) tests/test_timing.c vendor/unity/src/unity.c -o $@.tmp -lm
	@$(LINKED)

# PSF2 console fonts for the native Linux console (see tools/mkfont.c):
# 8x8 = original cell, 32x32 = 4x (old-kernel fallback), 40x40 = 5x,
# which fills a 1920x1080 console: 24 rows x 40 px + 120 px border.
bin/mkfont: tools/mkfont.c src/ui/font.h
	@mkdir -p bin
	$(CC) $(CFLAGS) $(CPPFLAGS) -I src/core $< -o $@.tmp
	@$(LINKED)

fonts: bin/mkfont $(FONTS)

bin/console-test.sh: tools/console-test.sh
	@mkdir -p bin
	@rm -f $@ # fresh copy: cp applies source mode & ~umask at create time
	@cp $< $@
	@chmod 755 $@ 2>/dev/null || true # some FS mounts reject chmod (EOPNOTSUPP); the create-time mode already has +x
	@test -x $@ || { echo "ERROR: $@ has no exec bit after copy (mount rejects chmod/create-mode?)"; false; }

fonts/zxdream-%.psf: bin/mkfont
	@mkdir -p fonts
	./bin/mkfont fonts

install: backends fonts
	install -d $(DESTDIR)$(BINDIR) $(DESTDIR)$(FONTDIR)
	install -m 755 bin/zxdream-tty bin/zxdream-ncurses bin/zxdream-sdl3 bin/zxdream-raw bin/zxdream-mcp bin/console-test.sh $(DESTDIR)$(BINDIR)/
	install -m 644 $(FONTS) $(DESTDIR)$(FONTDIR)/
	@echo "Installed backends to $(DESTDIR)$(BINDIR), fonts to $(DESTDIR)$(FONTDIR)"
	@echo "Console: setfont $(FONTDIR)/zxdream-40x40.psf"

uninstall:
	rm -f $(DESTDIR)$(BINDIR)/zxdream-tty $(DESTDIR)$(BINDIR)/zxdream-ncurses $(DESTDIR)$(BINDIR)/zxdream-sdl3 $(DESTDIR)$(BINDIR)/zxdream-raw $(DESTDIR)$(BINDIR)/console-test.sh
	rm -f $(DESTDIR)$(FONTDIR)/zxdream-8x8.psf $(DESTDIR)$(FONTDIR)/zxdream-32x32.psf $(DESTDIR)$(FONTDIR)/zxdream-40x40.psf

test: $(TEST_BINS) bin/zxdream-sdl3 bin/zxdream-mcp
	./bin/test-screen
	./bin/test-teletype
	./bin/test-token
	./bin/test-pframe
	@ASAN_OPTIONS=detect_leaks=0 ./bin/test-expr
	@ASAN_OPTIONS=detect_leaks=0 ./bin/test-eval
	./bin/test-runext
	./bin/test-mcp
	@ASAN_OPTIONS=detect_leaks=0 ./bin/test-timing
	@ASAN_OPTIONS=detect_leaks=0 SDL_VIDEODRIVER=dummy ./bin/zxdream-sdl3 --selftest
	@ASAN_OPTIONS=detect_leaks=0 python3 tests/test_pty_keys.py bin/zxdream-tty bin/zxdream-ncurses bin/zxdream-raw

run: bin/zxdream-$(UI)
	./bin/zxdream-$(UI) $(PROG)

clean:
	rm -rf bin fonts

# Distribution archive from HEAD, without regression data (difftest
# results). Always runs clean first (Martin, 2026-12-01); note:
# vendor/unity is untracked, so 'make test' inside the unpacked zip
# needs Unity installed at vendor/unity/src.
ZIP_OUT  ?= ../zxdream.zip
ZIP_EXCLUDE := tools/difftest/results
zip: clean
	git archive -o $(ZIP_OUT) --prefix=zxdream/ HEAD -- . \
		$(foreach p,$(ZIP_EXCLUDE),':(exclude)$(p)')
