ROM := pokegold-spaceworld.gb
CORRECTEDROM := $(ROM:%.gb=%-correctheader.gb)
BASEROM := baserom.gb

DIRS := home engine data gfx audio maps scripts ram slack
FILES :=

BUILD := build

rwildcard = $(foreach d, $(wildcard $1*), $(filter $(subst *, %, $2), $d) $(call rwildcard, $d/, $2))
ASMFILES := $(call rwildcard, $(DIRS), *.asm) $(FILES)
OBJS := $(patsubst %.asm, $(BUILD)/%.o, $(ASMFILES))
OBJS += $(BUILD)/shim.o


### Build tools

ifeq (,$(shell which sha1sum))
SHA1 := shasum
else
SHA1 := sha1sum
endif

PYTHON := python3

RGBDS ?=
RGBASM  ?= $(RGBDS)rgbasm
RGBFIX  ?= $(RGBDS)rgbfix
RGBGFX  ?= $(RGBDS)rgbgfx
RGBLINK ?= $(RGBDS)rgblink

RGBASMFLAGS  ?= -Weverything -Wtruncation=1
RGBLINKFLAGS ?= -Weverything -Wtruncation=1
RGBFIXFLAGS  ?= -Weverything
RGBGFXFLAGS  ?= -Weverything

tools/gfx :=


### Build targets

.SECONDEXPANSION:

.PHONY: all
all: $(ROM) $(CORRECTEDROM) compare

.PHONY: compare
compare: $(ROM) $(CORRECTEDROM)
	@$(SHA1) -c roms.sha1

.PHONY: tools
tools tools/pkmncompress tools/gfx tools/scan_includes:
	"$(MAKE)" -C tools/

# Remove files generated by the build process.
.PHONY: clean
clean:
	rm -rf $(ROM) $(CORRECTEDROM) $(ROMS:.gb=.sym) $(ROMS:.gb=.map) $(BUILD)
	"$(MAKE)" -C tools clean

# Remove generated files except for graphics.
.PHONY: tidy
tidy:
	rm -rf $(ROM) $(CORRECTEDROM) $(ROMS:.gb=.sym) $(ROMS:.gb=.map) $(OBJS) $(BUILD)/shim.asm rgbdscheck.o

# Visualize disassembly progress.
.PHONY: coverage
coverage: $(ROM:.gb=.map)
	$(PYTHON) utils/coverage.py $<


### Build products

rgbdscheck.o: rgbdscheck.asm
	$(RGBASM) -o $@ $<

%.map: %.gb

$(CORRECTEDROM): RGBFIXFLAGS += -f hg -m 0x10 -Wno-overwrite
$(CORRECTEDROM): %-correctheader.gb: %.gb
	cp $< $@
	cp $(<:.gb=.sym) $(@:.gb=.sym)
	$(RGBFIX) $(RGBFIXFLAGS) $@

RGBLINKFLAGS += -d

$(ROM): RGBFIXFLAGS += -f lh -k 01 -l 0x33 -m 0x03 -p 0 -r 3
$(ROM): poke%-spaceworld.gb: layout.link $(OBJS) | $(BASEROM)
	$(RGBLINK) $(RGBLINKFLAGS) -l layout.link -n $(@:.gb=.sym) -m $(@:.gb=.map) -O $(BASEROM) -o $@ $(filter-out $<, $^)
	$(RGBFIX) $(RGBFIXFLAGS) -t "POKEMON2$(shell echo $* | cut -d _ -f 1 | tr '[:lower:]' '[:upper:]')" $@

$(BASEROM):
	@echo "Please obtain a copy of Gold_debug.sgb and put it in this directory as $@"
	@exit 1

$(BUILD)/shim.asm: shim.sym | $$(dir $$@)
	$(PYTHON) tools/make_shim.py $< > $@


### Misc file-specific graphics rules
include gfx/gfx.mk
include slack/slack.mk


### Catch-all build target rules

RGBASMFLAGS += -E -i $(BUILD)/ -DGOLD

$(BUILD)/%.o: $(BUILD)/%.asm | $$(dir $$@) rgbdscheck.o
	$(RGBASM) $(RGBASMFLAGS) $(OUTPUT_OPTION) $<

$(BUILD)/%.o: %.asm | $$(dir $$@) rgbdscheck.o
	$(RGBASM) $(RGBASMFLAGS) $(OUTPUT_OPTION) $<

$(BUILD)/%.d: %.asm | $$(dir $$@) tools/scan_includes
	@tools/scan_includes -b $(BUILD)/ -i $(BUILD)/ -i ./ -o $@ -t $(@:.d=.o) $<

.PRECIOUS: $(BUILD)/%.pic
$(BUILD)/%.pic: $(BUILD)/%.2bpp tools/pkmncompress | $$(dir $$@)
	tools/pkmncompress $< $@

.PRECIOUS: $(BUILD)/%.2bpp
$(BUILD)/%.2bpp: %.png tools/gfx | $$(dir $$@)
	$(RGBGFX) -c dmg $(RGBGFXFLAGS) $(OUTPUT_OPTION) $<
	tools/gfx $(tools/gfx) $(OUTPUT_OPTION) $@

.PRECIOUS: $(BUILD)/%.1bpp
$(BUILD)/%.1bpp: %.1bpp.png tools/gfx | $$(dir $$@)
	$(RGBGFX) -c dmg $(RGBGFXFLAGS) -d1 $(OUTPUT_OPTION) $<
	tools/gfx $(tools/gfx) -d1 $(OUTPUT_OPTION) $@

.PRECIOUS: $(BUILD)/%.tilemap
$(BUILD)/%.tilemap: %.png | $$(dir $$@)
	$(RGBGFX) -c dmg $(RGBGFXFLAGS) -t $@ $<

.PRECIOUS: $(BUILD)/%.sgb.tilemap
export LC_ALL=C
$(BUILD)/%.sgb.tilemap: %.bin | $$(dir $$@)
	tr < $< -d '\000' > $@

.PRECIOUS: %/
%/:
	mkdir -p $@


### Scan .asm files for INCLUDE dependencies

ifeq (,$(filter clean tools,$(MAKECMDGOALS)))
-include $(patsubst %.o, %.d, $(OBJS))
endif
