diff --git a/Makefile b/Makefile index da984c41b..6cbc1e1df 100644 --- a/Makefile +++ b/Makefile @@ -41,11 +41,6 @@ all: crystal crystal: pokecrystal.gbc crystal11: pokecrystal11.gbc -# Build tools when building the rom -ifeq (,$(filter clean tools,$(MAKECMDGOALS))) -Makefile: tools ; -endif - clean: rm -f $(roms) $(crystal_obj) $(crystal11_obj) $(roms:.gbc=.map) $(roms:.gbc=.sym) $(MAKE) clean -C tools/ @@ -56,13 +51,28 @@ compare: $(roms) tools: $(MAKE) -C tools/ -%11.o: dep = $(shell tools/scan_includes $(@D)/$*.asm) -%11.o: %.asm $$(dep) - $(RGBASM) -D CRYSTAL11 -o $@ $< -%.o: dep = $(shell tools/scan_includes $(@D)/$*.asm) -%.o: %.asm $$(dep) - $(RGBASM) -o $@ $< +$(crystal11_obj): RGBASMFLAGS = -D CRYSTAL11 + +# The dep rules have to be explicit or else missing files won't be reported. +# As a side effect, they're evaluated immediately instead of when the rule is invoked. +# It doesn't look like $(shell) can be deferred so there might not be a better way. +define DEP +$1: $2 $$(shell tools/scan_includes $2) + $$(RGBASM) $$(RGBASMFLAGS) -o $$@ $$< +endef + +# Build tools when building the rom. +# This has to happen before the rules are processed, since that's when scan_includes is run. +ifeq (,$(filter clean tools,$(MAKECMDGOALS))) + +$(info $(shell $(MAKE) -C tools)) + +$(foreach obj, $(crystal11_obj), $(eval $(call DEP,$(obj),$(obj:11.o=.asm)))) +$(foreach obj, $(crystal_obj), $(eval $(call DEP,$(obj),$(obj:.o=.asm)))) + +endif + pokecrystal11.gbc: $(crystal11_obj) pokecrystal.link $(RGBLINK) -n pokecrystal11.sym -m pokecrystal11.map -l pokecrystal.link -o $@ $(crystal11_obj)