# Build with MinGW: mingw32-make -C port -j8
#
# The game is the C++ in src/ and needs nothing else to build. To play it
# needs your own Castlevania III (USA) ROM in rom/ (any name).
#
# Reference build (developers, automatic when romap/ is present): also the
# original game's code decompiled to C++ (gen/), for --no-rewrites and the
# exact verification gate. The disassembly it comes from is made from romap/
# (the map, without the game) and your ROM (tools/romap.py).
CXX      := g++
PYTHON   := python
SDL_DIR  := third_party/SDL2-2.30.9/x86_64-w64-mingw32
BUILD    := build
.DEFAULT_GOAL := all

REFERENCE ?= $(if $(wildcard romap/manifest.txt),1,0)

# third_party/dr_libs/dr_mp3.h: https://github.com/mackron/dr_libs (public domain).
CXXFLAGS := -std=c++17 -O2 -g -Wall -Wextra -Isrc -I$(SDL_DIR)/include/SDL2 -isystem third_party/dr_libs -MMD -MP
CXXFLAGS_EXTRA ?=
CXXFLAGS += $(CXXFLAGS_EXTRA)
# Static libstdc++: Git's older libstdc++-6.dll is often first on PATH.
LDFLAGS  := -L$(SDL_DIR)/lib -static-libstdc++ -static-libgcc
LDLIBS   := -lSDL2

SRCS     := $(wildcard src/runtime/*.cpp src/platform/*.cpp src/verify/*.cpp src/recomp/*.cpp src/game/*.cpp src/names/*.cpp)
OBJS     := $(SRCS:%.cpp=$(BUILD)/%.o)

all: $(BUILD)/cv3.exe $(BUILD)/SDL2.dll

$(BUILD)/SDL2.dll: $(SDL_DIR)/bin/SDL2.dll
	cp $< $@

ifeq ($(REFERENCE),1)

CXXFLAGS += -DCV3_REFERENCE
# Generated code: one big goto-based function per component; skip -g and
# the unused-label warnings to keep compile times reasonable.
GEN_CXXFLAGS := -std=c++17 -O2 -Isrc -Igen -DCV3_REFERENCE -Wno-unused-label -Wno-unused-variable -MMD -MP

GEN_STAMP := gen/.stamp
# The disassembly, made from the map and the ROM into build/asm/.
ASM_DIR   := $(BUILD)/asm
ASM_STAMP := $(ASM_DIR)/.stamp
$(ASM_STAMP): $(wildcard romap/*) tools/romap.py
	$(PYTHON) tools/romap.py rebuild romap auto $(ASM_DIR)
	@echo > $@

# The recompiler reads the disassembly, the ROM (to verify it) and the
# REWRITE(...) registrations in src/game/. It only rewrites changed files.
RECOMP_INPUTS := $(wildcard tools/recomp/*.py src/game/*.cpp) $(ASM_STAMP)

# RECOMP_FLAGS=--dynamic-calls: every call through the dispatch table, for
# tools/bisect_decomp.py. A change of flags regenerates gen/.
RECOMP_FLAGS ?=
FLAGS_FILE := gen/.flags
$(FLAGS_FILE): FORCE
	@mkdir -p gen
	@echo '$(RECOMP_FLAGS)' | cmp -s - $@ || echo '$(RECOMP_FLAGS)' > $@

$(GEN_STAMP): $(RECOMP_INPUTS) $(FLAGS_FILE)
	$(PYTHON) tools/recomp/recomp.py --root $(ASM_DIR) --rom $(ASM_DIR)/rom.nes $(RECOMP_FLAGS)

# gen/ file names are only known after generation, so the exe rule
# re-expands them via a recursive make.
$(BUILD)/cv3.exe: $(OBJS) $(GEN_STAMP)
	@$(MAKE) --no-print-directory gen-objs
	$(CXX) $(LDFLAGS) -o $@ $(OBJS) $(patsubst %.cpp,$(BUILD)/%.o,$(wildcard gen/*.cpp gen/lowlevel/*.cpp)) $(LDLIBS)

GEN_OBJS = $(patsubst %.cpp,$(BUILD)/%.o,$(wildcard gen/*.cpp gen/lowlevel/*.cpp))
gen-objs: $(GEN_OBJS)

# Low-level copies of every routine are only for bisecting (cv3 --lowlevel).
$(BUILD)/gen/lowlevel/%.o: gen/lowlevel/%.cpp
	@mkdir -p $(dir $@)
	$(CXX) $(GEN_CXXFLAGS) -O1 -c -o $@ $<

$(BUILD)/gen/%.o: gen/%.cpp
	@mkdir -p $(dir $@)
	$(CXX) $(GEN_CXXFLAGS) -c -o $@ $<

else

$(BUILD)/cv3.exe: $(OBJS)
	$(CXX) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)

endif

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

# The CV3_REFERENCE define differs between the two builds: switching
# rebuilds everything.
REF_FILE := $(BUILD)/.reference
$(REF_FILE): FORCE
	@mkdir -p $(BUILD)
	@echo '$(REFERENCE)' | cmp -s - $@ || echo '$(REFERENCE)' > $@
$(OBJS): $(REF_FILE)

clean:
	rm -rf $(BUILD) gen

.PHONY: all clean gen-objs FORCE
FORCE:
-include $(OBJS:.o=.d) $(GEN_OBJS:.o=.d)
