CC ?= cc
PKG_CONFIG ?= pkg-config
CFLAGS ?= -O2 -g
CPPFLAGS ?=
WARN = -std=c99 -Wall -Wextra -Wpedantic
CORE = src/core.c src/app.c src/render.c src/audio.c src/fm_synth.c src/music.c
OBJECTS = build/core.o build/app.o build/render.o build/audio.o build/fm_synth.o build/music.o build/controller.o build/main.o
SDL_CFLAGS = $(shell $(PKG_CONFIG) --cflags sdl3 2>/dev/null)
SDL_LIBS = $(shell $(PKG_CONFIG) --libs sdl3 2>/dev/null)

.PHONY: all clean check sanitizers headless check-sdl
all: glyph-division

check-sdl:
	@$(PKG_CONFIG) --atleast-version=3.2.0 sdl3 || { \
	 echo 'SDL3 development files not found.'; \
	 echo 'EndeavourOS / Arch: sudo pacman -S --needed base-devel pkgconf sdl3'; \
	 echo 'Debian/Ubuntu with SDL3 packages: sudo apt install build-essential pkg-config libsdl3-dev'; \
	 echo 'Core tests need no SDL: make check'; exit 1; }

build:
	mkdir -p build

build/%.o: src/%.c | build
	$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -MMD -MP -Isrc -c $< -o $@

build/main.o build/controller.o: build/%.o: src/%.c | build check-sdl
	$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) $(SDL_CFLAGS) -MMD -MP -Isrc -c $< -o $@

glyph-division: $(OBJECTS)
	$(CC) $(LDFLAGS) $^ $(SDL_LIBS) -lm $(LDLIBS) -o $@

headless: glyph-division-headless
glyph-division-headless: $(CORE) src/headless.c src/core.h src/app.h src/audio.h src/fm_synth.h src/music.h src/font.h
	$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -Isrc $(CORE) src/headless.c -lm $(LDFLAGS) $(LDLIBS) -o $@

build/test-core: $(CORE) tests/test.c src/core.h src/app.h src/audio.h src/fm_synth.h src/music.h src/font.h | build
	$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -Isrc $(CORE) tests/test.c -lm $(LDFLAGS) $(LDLIBS) -o $@

check: build/test-core
	./build/test-core

build/test-sanitize: $(CORE) tests/test.c src/core.h src/app.h src/audio.h src/fm_synth.h src/music.h src/font.h | build
	$(CC) $(CPPFLAGS) -O1 -g $(WARN) -fno-omit-frame-pointer -fsanitize=address,undefined -fno-pie -no-pie -Isrc $(CORE) tests/test.c -lm -o $@

sanitizers: build/test-sanitize
	ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 UBSAN_OPTIONS=halt_on_error=1 ./build/test-sanitize

clean:
	rm -rf build glyph-division glyph-division-headless

-include $(OBJECTS:.o=.d)

AUDIO_CORE = src/audio.c src/music.c src/fm_synth.c
AUDIO_HEADERS = src/audio.h src/music.h src/fm_synth.h src/core.h
.PHONY: check-audio sanitize-audio
build/test-audio: $(AUDIO_CORE) $(AUDIO_HEADERS) tests/test_audio.c | build
	$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -Isrc $(AUDIO_CORE) tests/test_audio.c -lm $(LDFLAGS) $(LDLIBS) -o $@
check-audio: build/test-audio
	./build/test-audio
build/test-audio-sanitize: $(AUDIO_CORE) $(AUDIO_HEADERS) tests/test_audio.c | build
	$(CC) $(CPPFLAGS) -O1 -g $(WARN) -fno-omit-frame-pointer -fsanitize=address,undefined -fno-pie -no-pie -Isrc $(AUDIO_CORE) tests/test_audio.c -lm -o $@
sanitize-audio: build/test-audio-sanitize
	ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 UBSAN_OPTIONS=halt_on_error=1 ./build/test-audio-sanitize

.PHONY: check-controller sanitize-controller
CONTROLLER_CORE = $(CORE) src/controller.c
build/test-controller: $(CONTROLLER_CORE) tests/test_controller.c src/controller.h src/app.h src/core.h src/music.h src/font.h | build check-sdl
	$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) $(SDL_CFLAGS) -Isrc $(CONTROLLER_CORE) tests/test_controller.c $(SDL_LIBS) -lm $(LDFLAGS) $(LDLIBS) -o $@
check-controller: build/test-controller
	SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software SDL_AUDIODRIVER=dummy ./build/test-controller
build/test-controller-sanitize: $(CONTROLLER_CORE) tests/test_controller.c src/controller.h src/app.h src/core.h src/music.h src/font.h | build check-sdl
	$(CC) $(CPPFLAGS) -O1 -g $(WARN) $(SDL_CFLAGS) -fno-omit-frame-pointer -fsanitize=address,undefined -fno-pie -no-pie -Isrc $(CONTROLLER_CORE) tests/test_controller.c $(SDL_LIBS) -lm -o $@
sanitize-controller: build/test-controller-sanitize
	SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software SDL_AUDIODRIVER=dummy ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 UBSAN_OPTIONS=halt_on_error=1 ./build/test-controller-sanitize

.PHONY: check-rebinding sanitize-rebinding
build/test-rebinding: $(CONTROLLER_CORE) tests/test_rebinding.c src/controller.h src/app.h src/core.h src/music.h src/font.h | build check-sdl
	$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) $(SDL_CFLAGS) -Isrc $(CONTROLLER_CORE) tests/test_rebinding.c $(SDL_LIBS) -lm $(LDFLAGS) $(LDLIBS) -o $@
check-rebinding: build/test-rebinding
	SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software SDL_AUDIODRIVER=dummy ./build/test-rebinding
build/test-rebinding-sanitize: $(CONTROLLER_CORE) tests/test_rebinding.c src/controller.h src/app.h src/core.h src/music.h src/font.h | build check-sdl
	$(CC) $(CPPFLAGS) -O1 -g $(WARN) $(SDL_CFLAGS) -fno-omit-frame-pointer -fsanitize=address,undefined -fno-pie -no-pie -Isrc $(CONTROLLER_CORE) tests/test_rebinding.c $(SDL_LIBS) -lm -o $@
sanitize-rebinding: build/test-rebinding-sanitize
	SDL_VIDEODRIVER=offscreen SDL_RENDER_DRIVER=software SDL_AUDIODRIVER=dummy ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 UBSAN_OPTIONS=halt_on_error=1 ./build/test-rebinding-sanitize

# Balance experiments: no SDL, display, audio device, or player saves required.
# Completion of the experiment is NOT a PASS verdict on human difficulty.
.PHONY: balance-report balance-smoke balance-sanitize
build/balance-player: src/core.c src/core.h tests/balance_player.c | build
	$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -Isrc src/core.c tests/balance_player.c -lm $(LDFLAGS) $(LDLIBS) -o $@
balance-report: build/balance-player
	python3 tests/run_balance.py --binary build/balance-player --out balance-results
balance-smoke: build/balance-player
	python3 tests/run_balance.py --binary build/balance-player --out balance-smoke-results --smoke
build/balance-player-sanitize: src/core.c src/core.h tests/balance_player.c | build
	$(CC) $(CPPFLAGS) -O1 -g $(WARN) -fno-omit-frame-pointer -fsanitize=address,undefined -fno-pie -no-pie -Isrc src/core.c tests/balance_player.c -lm -o $@
balance-sanitize: build/balance-player-sanitize
	ASAN_OPTIONS=detect_leaks=1:halt_on_error=1 UBSAN_OPTIONS=halt_on_error=1 python3 tests/run_balance.py --binary build/balance-player-sanitize --out balance-sanitizer-results --smoke --workers 2

.PHONY: balance-upper
balance-upper: build/balance-player
	python3 tests/run_balance.py --binary build/balance-player --out balance-upper-results --upper

.PHONY: windows-x64 windows-installer
windows-x64:
	./windows/build-mingw.sh "$(SDL3_SOURCE_ARCHIVE)"
windows-installer:
	./windows/build-installer.sh
