# Desktop build. ROM-derived data lives in build/ and is never committed.
# Mac: brew install sdl3. Linux / Steam Deck: see tools/deck/build.sh (Steam Runtime container).
CXX ?= clang++
CXXFLAGS ?= -std=c++20 -O2 -g -Wall -Wno-unused-variable -Wno-unused-but-set-variable -Wno-unused-function
# (no fused multiply-adds, on every platform: the V60's and the TGP's floating point is the game's state, and an
# ARM64 Mac must work out the same numbers as an x86-64 PC for netplay)
CXXFLAGS += -ffp-contract=off
CXXFLAGS += -Ithird_party/glm -Isrc/core/shim_include $(EXTRA_CFLAGS)
LDLIBS ?= -lz
# the compiler for tools run during the build (m68kmake): the target's own unless cross-compiling
HOSTCC ?= $(CC)
SDL_CFLAGS ?= $(shell pkg-config --cflags sdl3 2>/dev/null || echo -I/opt/homebrew/include)
SDL_LIBS ?= $(shell pkg-config --libs sdl3 2>/dev/null || echo -L/opt/homebrew/lib -lSDL3)
FT_CFLAGS ?= $(shell pkg-config --cflags freetype2 2>/dev/null || echo -I/opt/homebrew/include/freetype2)
FT_LIBS ?= $(shell pkg-config --libs freetype2 2>/dev/null || echo -L/opt/homebrew/lib -lfreetype)
ARCH ?= $(shell uname -m)
ifneq ($(filter arm64 aarch64,$(ARCH)),)
CORO_ASM = src/rt/coro_arm64.S
else ifeq ($(TARGET_OS),windows)
CORO_ASM = src/rt/coro_win64.S
else
CORO_ASM = src/rt/coro_x86_64.S
endif

CORE = src/core/v60_device.cpp src/core/mb86233.cpp
# online play: netplay (src/net, VF3's), the game as a session's Game, the Online screens
NET = src/net/session.cpp src/net/link.cpp src/net/stun.cpp src/net/room_link.cpp src/net/http.cpp src/net/watch.cpp
# the menus' languages (src/host/i18n.cpp, text.cpp and lang/*.inc: docs/i18n-design.md, VF3's)
I18N = src/host/i18n.cpp src/host/text.cpp
ONLINE = src/host/online.cpp src/host/online_room.cpp src/host/links.cpp src/host/vf1_netplay.cpp $(I18N) $(NET)
SOUND = src/machine/m1audio.cpp src/machine/multipcm.cpp third_party/ymfm/ymfm_opn.cpp third_party/ymfm/ymfm_adpcm.cpp third_party/ymfm/ymfm_ssg.cpp
MUSASHI_OBJ = $(OBJ)/musashi/m68kcpu.o $(OBJ)/musashi/m68kops.o $(OBJ)/musashi/m68kdasm.o $(OBJ)/musashi/softfloat.o
MACHINE = src/machine/model1.cpp src/machine/model1_tgp.cpp src/machine/model1_tgp_lle.cpp src/machine/model1_video.cpp src/machine/model1_sky.cpp src/machine/model1_interp.cpp src/machine/model1_gpu.cpp src/machine/model1_edge_aa.cpp src/machine/model1io.cpp src/machine/model1_state.cpp
Z80_OBJ = $(OBJ)/z80/z80.o
DASM = third_party/mame/v60/v60d.cpp
# the recompile done on the player's machine: analysis + native code (sljit, BSD-2)
NATIVE = src/rt/analyse.cpp src/rt/native.cpp $(DASM)
SLJIT_OBJ = $(OBJ)/third_party/sljit/sljitLir.o

OBJ ?= build/obj
BIN ?= build/bin
# the version and date shown in the menu: the git tag (or tag-commits-hash between releases) and the
# date of the last commit. Builds without git (a container) are handed them in the environment.
VF_VERSION ?= $(shell git describe --tags --always 2>/dev/null || echo dev)
VF_DATE ?= $(shell git log -1 --format=%cd --date=format:'%b %-d %Y' 2>/dev/null)
# (the commit too: what a GPU failure is remembered by, src/host/main.cpp's BUILD)
VF_COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null)
# and the lobby the room codes use (VF3's, shared: vf3-recomp/tools/lobby): its https address, .lobby-url's (one line, kept out of git) if unset or
# empty, "none" for none
ifeq ($(strip $(VF_LOBBY_URL)),)
VF_LOBBY_URL := $(strip $(shell cat .lobby-url 2>/dev/null))
endif
ifeq ($(VF_LOBBY_URL),none)
VF_LOBBY_URL :=
endif
# rewritten only when they change, so main is rebuilt for a new version and not otherwise
$(OBJ)/version.h: FORCE
	@mkdir -p $(OBJ)
	@printf '#define VF_VERSION "%s"\n#define VF_DATE "%s"\n#define VF_COMMIT "%s"\n#define VF_LOBBY_URL "%s"\n' "$(VF_VERSION)" "$(VF_DATE)" "$(VF_COMMIT)" "$(VF_LOBBY_URL)" > $@.tmp
	@cmp -s $@.tmp $@ && rm -f $@.tmp || mv $@.tmp $@
FORCE:
objs = $(patsubst %.cpp,$(OBJ)/%.o,$(1))

all: $(BIN)/vf $(BIN)/vfrun $(BIN)/vfdis $(BIN)/vfcov

RT = src/rt/rec_rt.cpp src/rt/coro.cpp
GEN = build/gen/vf_rec.cpp

# Musashi: m68kmake generates the opcode tables from m68k_in.c
$(BIN)/m68kmake: third_party/musashi/m68kmake.c
	@mkdir -p $(dir $@)
	$(HOSTCC) -O1 -w $< -o $@
build/musashi/m68kops.c: $(BIN)/m68kmake third_party/musashi/m68k_in.c
	@mkdir -p build/musashi
	$(BIN)/m68kmake build/musashi third_party/musashi/m68k_in.c >/dev/null
MUSASHI_CFLAGS = -O2 -w -Ithird_party/musashi -Ibuild/musashi
$(OBJ)/musashi/m68kops.o: build/musashi/m68kops.c
	@mkdir -p $(dir $@)
	$(CC) $(MUSASHI_CFLAGS) -c $< -o $@
$(OBJ)/musashi/m68kcpu.o: third_party/musashi/m68kcpu.c build/musashi/m68kops.c
	@mkdir -p $(dir $@)
	$(CC) $(MUSASHI_CFLAGS) -c $< -o $@
$(OBJ)/musashi/m68kdasm.o: third_party/musashi/m68kdasm.c build/musashi/m68kops.c
	@mkdir -p $(dir $@)
	$(CC) $(MUSASHI_CFLAGS) -c $< -o $@
$(OBJ)/musashi/softfloat.o: third_party/musashi/softfloat/softfloat.c
	@mkdir -p $(dir $@)
	$(CC) $(MUSASHI_CFLAGS) -c $< -o $@

$(SLJIT_OBJ): third_party/sljit/sljitLir.c
	@mkdir -p $(dir $@)
	$(CC) -O2 -w -DSLJIT_CONFIG_AUTO=1 -c $< -o $@

$(OBJ)/z80/z80.o: third_party/z80/z80.c
	@mkdir -p $(dir $@)
	$(CC) -O2 -w -c $< -o $@

$(OBJ)/%.o: %.S
	@mkdir -p $(dir $@)
	$(CC) -c $< -o $@

$(BIN)/v60recomp: $(call objs,tools/recomp/v60recomp.cpp src/rt/analyse.cpp $(DASM))
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@

# every coverage run in build/cov counts (their union)
COVS = $(wildcard build/cov/*.txt)
comma = ,
empty =
space = $(empty) $(empty)
build/gen/vf_prog.txt: $(BIN)/v60recomp build/vf/maincpu.bin $(COVS)
	@mkdir -p $(dir $@)
	$(BIN)/v60recomp build/vf/maincpu.bin $(subst $(space),$(comma),$(COVS)) $@

$(GEN): build/gen/vf_prog.txt tools/recomp/emit.py
	python3 tools/recomp/emit.py build/vf/maincpu.bin build/gen/vf_prog.txt $@

$(OBJ)/build/gen/vf_rec.o: $(GEN)
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) -O1 -Isrc/rt -c $< -o $@

$(BIN)/vflock: $(call objs,tools/vflock.cpp $(CORE) $(MACHINE) $(SOUND) $(RT) $(NATIVE)) $(SLJIT_OBJ) $(MUSASHI_OBJ) $(Z80_OBJ) $(patsubst %.S,$(OBJ)/%.o,$(CORO_ASM)) $(OBJ)/build/gen/vf_rec.o
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDLIBS)

rec: $(BIN)/vflock

# netplay's rollback soak test (tools/vf1soak.cpp): the native code made at start-up, as the game's
$(BIN)/vf1soak: $(call objs,tools/vf1soak.cpp src/rt/rec_table_none.cpp $(CORE) $(MACHINE) $(SOUND) $(RT) $(NATIVE)) $(SLJIT_OBJ) $(MUSASHI_OBJ) $(Z80_OBJ) $(patsubst %.S,$(OBJ)/%.o,$(CORO_ASM))
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDLIBS)
# nettest: the session on a simulated network with a stand-in game (no ROM)
$(BIN)/nettest: $(call objs,$(NET) tools/nettest.cpp)
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDLIBS)

# watchsim: watching a match, simulated (no ROM): the players the same with 0, 1 or 50 watchers, each watcher's game theirs
$(BIN)/watchsim: $(call objs,$(NET) tools/watchsim.cpp)
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDLIBS)

# delaytest: Auto's input delay against simulated pings (full speed at any; raised and lowered as it should)
$(BIN)/delaytest: $(call objs,$(NET) tools/delaytest.cpp)
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDLIBS)

# roomtest: room links against a TURN server on 127.0.0.1:3478 (coturn: tools/roomtest.cpp)
$(BIN)/roomtest: $(call objs,$(NET) tools/roomtest.cpp)
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDLIBS)

# wsfuzz: the WebSocket frame reader against anything a server could send, with AddressSanitizer and UBSan (VF3's)
$(BIN)/wsfuzz: src/net/http.cpp tools/wsfuzz.cpp
	@mkdir -p $(dir $@)
	$(CXX) -std=c++20 -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer $^ -o $@ -lpthread

# netfuzz: the session against a peer who sends anything, with AddressSanitizer and UBSan (its own objects)
$(BIN)/netfuzz: $(NET) tools/netfuzz.cpp
	@mkdir -p $(dir $@)
	$(CXX) -std=c++20 -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer $^ -o $@ $(LDLIBS)

# vf1net: two boards through a simulated network
# snapfuzz: a watcher's snapshot loading against a hostile host's (spoilt real snapshots: vf1net --watch-snap --snap-out
# DIR), with AddressSanitizer and UBSan, its own objects (make snapfuzz: build/asan/bin/snapfuzz)
ASAN_FLAGS = -std=c++20 -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer -Wno-unused-function
snapfuzz:
	CXXFLAGS="$(ASAN_FLAGS)" $(MAKE) OBJ=build/asan/obj BIN=build/asan/bin build/asan/bin/snapfuzz
.PHONY: snapfuzz
$(BIN)/snapfuzz: $(call objs,tools/snapfuzz.cpp src/host/vf1_netplay.cpp src/rt/rec_table_none.cpp $(NET) $(CORE) $(MACHINE) $(SOUND) $(RT) $(NATIVE)) $(SLJIT_OBJ) $(MUSASHI_OBJ) $(Z80_OBJ) $(patsubst %.S,$(OBJ)/%.o,$(CORO_ASM))
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDLIBS) -lpthread

$(BIN)/vf1net: $(call objs,tools/vf1net.cpp src/host/vf1_netplay.cpp src/rt/rec_table_none.cpp $(NET) $(CORE) $(MACHINE) $(SOUND) $(RT) $(NATIVE)) $(SLJIT_OBJ) $(MUSASHI_OBJ) $(Z80_OBJ) $(patsubst %.S,$(OBJ)/%.o,$(CORO_ASM))
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDLIBS)

$(OBJ)/%.o: %.cpp
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) -MMD -c $< -o $@

$(OBJ)/tools/vfrun.o $(OBJ)/tools/vfscript.o: CXXFLAGS += $(SDL_CFLAGS)
$(BIN)/vfrun: $(call objs,tools/vfrun.cpp src/host/stb_image_impl.cpp $(CORE) $(MACHINE) $(SOUND) $(RT)) $(MUSASHI_OBJ) $(Z80_OBJ) $(patsubst %.S,$(OBJ)/%.o,$(CORO_ASM)) $(OBJ)/build/gen/vf_rec.o
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDLIBS) $(SDL_LIBS)

# RmlUi (MIT): the in-game menu. Its core and the SDL backend, built with FreeType.
RML_SRC = $(wildcard third_party/rmlui/Source/Core/*.cpp third_party/rmlui/Source/Core/*/*.cpp) \
	third_party/rmlui/Backends/RmlUi_Platform_SDL.cpp third_party/rmlui/Backends/RmlUi_Renderer_SDL.cpp
RML_CFLAGS = -DRMLUI_STATIC_LIB -DRMLUI_FONT_ENGINE_FREETYPE -DRMLUI_SDL_VERSION_MAJOR=3 -Ithird_party/rmlui/Include -Ithird_party/rmlui/Backends
$(OBJ)/third_party/rmlui/%.o: CXXFLAGS += $(RML_CFLAGS) $(FT_CFLAGS) $(SDL_CFLAGS) -w
$(OBJ)/src/host/stb_image_impl.o: CXXFLAGS += -w
$(OBJ)/src/host/main.o $(OBJ)/src/host/menu.o $(OBJ)/src/host/online.o $(OBJ)/src/host/online_room.o $(OBJ)/src/host/i18n.o $(OBJ)/tools/langtest.o $(OBJ)/tools/menutest.o: CXXFLAGS += $(SDL_CFLAGS) $(RML_CFLAGS)
$(OBJ)/src/host/main.o $(OBJ)/src/host/online.o: $(OBJ)/version.h
$(OBJ)/src/host/main.o $(OBJ)/src/host/online.o: CXXFLAGS += -I$(OBJ)
# menutest: the Online pages drawn offscreen to pictures (SDL_VIDEO_DRIVER=offscreen), no ROM (VF3's)
$(BIN)/menutest: $(call objs,tools/menutest.cpp src/host/menu.cpp $(ONLINE) src/rt/rec_table_none.cpp $(CORE) $(MACHINE) $(SOUND) $(RT) $(NATIVE) $(RML_SRC)) $(SLJIT_OBJ) $(MUSASHI_OBJ) $(Z80_OBJ) $(patsubst %.S,$(OBJ)/%.o,$(CORO_ASM))
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDLIBS) $(SDL_LIBS) $(FT_LIBS)

# langtest: the strings' tables (src/host/lang): every ID in every language once, the same {places} as the English, each
# language's letters in the menus' fonts, the settings' language codes; no ROM, no window (VF3's)
$(BIN)/langtest: $(call objs,$(I18N) tools/langtest.cpp)
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(SDL_LIBS) $(FT_LIBS)
$(OBJ)/tools/langtest.o: CXXFLAGS += $(FT_CFLAGS)

# updatetest: the update check's versions compared and GitHub's answers read (src/host/update_check.h); no network
$(BIN)/updatetest: $(call objs,tools/updatetest.cpp)
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDLIBS) -lpthread

# The 3D on the GPU in the game (src/gpu, Display > 3D renderer): SDL_GPU everywhere (Direct3D 12, Vulkan or
# Metal: gpu3d_sdlgpu.cpp, its shaders precompiled in gpu3d_shaders.h by tools/gpu/shaders.sh), on the device of
# SDL's gpu renderer, drawing straight into its textures. (The Mac had its own Metal code, gpu3d_metal.mm, on SDL's
# Metal renderer, where CRT colours, brightness and gamma can't run: GPU3D=metal builds it still.) GPU3D=none: the
# CPU's only
GPU3D ?= sdlgpu
ifeq ($(GPU3D),metal)
GPU3D_OBJ = $(OBJ)/src/gpu/gpu3d_metal.o
GPU3D_LIBS = -framework Metal -framework Foundation -framework QuartzCore
GPU3D_DEF = -DVF_GPU3D_METAL=1
endif
ifeq ($(GPU3D),sdlgpu)
GPU3D_OBJ = $(OBJ)/src/gpu/gpu3d_sdlgpu.o
$(OBJ)/src/gpu/gpu3d_sdlgpu.o: CXXFLAGS += $(SDL_CFLAGS)
GPU3D_DEF = -DVF_GPU3D_SDL=1
endif
$(OBJ)/%.o: %.mm
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) -fobjc-arc -MMD -c $< -o $@
$(OBJ)/src/host/main.o: CXXFLAGS += $(GPU3D_DEF)
$(BIN)/vf: $(call objs,src/host/main.cpp src/host/menu.cpp $(ONLINE) src/host/romset.cpp src/host/stb_image_impl.cpp $(CORE) $(MACHINE) $(SOUND) $(RT) $(RML_SRC)) $(MUSASHI_OBJ) $(Z80_OBJ) $(patsubst %.S,$(OBJ)/%.o,$(CORO_ASM)) $(OBJ)/build/gen/vf_rec.o $(GPU3D_OBJ)
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDLIBS) $(SDL_LIBS) $(FT_LIBS) $(GPU3D_LIBS)

# The release build: no generated C++ in it (nothing made from the ROM). The game is recompiled
# to native code from the player's ROM when it starts (src/rt/native.cpp, assets/vf_map.txt).
$(OBJ)/src/host/main_native.o: src/host/main.cpp $(OBJ)/version.h
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $(SDL_CFLAGS) $(RML_CFLAGS) $(GPU3D_DEF) -I$(OBJ) -DVF_NATIVE -MMD -c $< -o $@
$(BIN)/vf-native: $(OBJ)/src/host/main_native.o $(call objs,src/host/menu.cpp $(ONLINE) src/host/romset.cpp src/host/stb_image_impl.cpp src/rt/rec_table_none.cpp $(CORE) $(MACHINE) $(SOUND) $(RT) $(NATIVE) $(RML_SRC)) $(SLJIT_OBJ) $(MUSASHI_OBJ) $(Z80_OBJ) $(patsubst %.S,$(OBJ)/%.o,$(CORO_ASM)) $(GPU3D_OBJ)
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDLIBS) $(SDL_LIBS) $(FT_LIBS) $(GPU3D_LIBS)
native: $(BIN)/vf-native

$(BIN)/vfcov: $(call objs,tools/vfcov.cpp $(CORE) $(MACHINE) $(SOUND)) $(MUSASHI_OBJ) $(Z80_OBJ)
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDLIBS)

$(BIN)/vfscript: $(call objs,tools/vfscript.cpp src/host/stb_image_impl.cpp $(CORE) $(MACHINE) $(SOUND) $(RT)) $(MUSASHI_OBJ) $(Z80_OBJ) $(patsubst %.S,$(OBJ)/%.o,$(CORO_ASM)) $(OBJ)/build/gen/vf_rec.o
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDLIBS) $(SDL_LIBS)

$(BIN)/vfdis: $(call objs,tools/vfdis.cpp $(DASM))
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@

regions: build/vf/maincpu.bin
build/vf/maincpu.bin: roms/vf.zip tools/romset.py
	python3 tools/romset.py roms/vf.zip build/vf

clean:
	rm -rf build/obj build/bin

-include $(shell find $(OBJ) -name '*.d' 2>/dev/null)
# The tests, always from this tree's sources: langtest (the strings' tables and fonts), menutest (every page drawn and
# checked, English; tools/langcheck.py for every language) and updatetest (the update check's versions and answers).
# The default make builds none, and a menutest built before a change passes for a tree it isn't: run them through here
test: $(BIN)/langtest $(BIN)/menutest $(BIN)/updatetest
	$(BIN)/langtest
	$(BIN)/updatetest
	mkdir -p build/menutest
	SDL_VIDEO_DRIVER=offscreen SDL_RENDER_DRIVER=software SDL_AUDIO_DRIVER=dummy $(BIN)/menutest build/menutest

.PHONY: all clean regions test

# vf1watchsound: a late watcher's match sound after its catch-up against a from-the-start watcher's (join points swept)
$(BIN)/vf1watchsound: $(call objs,tools/vf1watchsound.cpp src/host/vf1_netplay.cpp src/rt/rec_table_none.cpp $(NET) $(CORE) $(MACHINE) $(SOUND) $(RT) $(NATIVE)) $(SLJIT_OBJ) $(MUSASHI_OBJ) $(Z80_OBJ) $(patsubst %.S,$(OBJ)/%.o,$(CORO_ASM))
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDLIBS)
