# Amiga Assembly -- the repository's build.
#
#     make            build every disk image in every chapter
#     make ch03       build one chapter's disks
#     make check      run the first-day checklist
#     make verify     reassemble every source and compare byte for byte
#     make pic        look for addresses written down instead of worked out
#     make hunks      link every source and count what a loader must fix up
#     make clean      remove the binaries and the disk images
#
# There is nothing clever here. Every rule is the two commands chapter 4
# shows you, applied to every source in the tree.

VASM  ?= vasmm68k_mot
MKADF ?= python3 tools/mkadf.py

# ch43 has its own rule below: two of its sources are halves of one disk.
# ch51 and ch52 have their own rules as well, and for the opposite reason:
# from chapter 51 a disk is LAID OUT -- a bootblock, an index, and pieces on
# sector boundaries -- instead of being one binary dropped into a bootblock,
# so the Makefile stops at the binaries and the chapter's own script puts the
# image together. Without this, make would rebuild those images with mkadf
# and quietly throw the index away.
# And the appendices are built like chapters, because two of them carry a
# program: an appendix of this book is not a list copied out of a manual.
CHAPTERS := $(filter-out ch43 ch51 ch52,$(sort $(wildcard ch[0-9][0-9]) \
            $(wildcard app[A-L])))
SOURCES  := $(wildcard ch[0-9][0-9]/*.asm) $(wildcard app[A-L]/*.asm)
LAIDOUT  := $(filter ch51/% ch52/%,$(SOURCES:.asm=.adf))
DISKS    := $(filter-out ch43/title43.adf ch43/game43.adf $(LAIDOUT),\
            $(SOURCES:.asm=.adf)) ch43/veldrin.adf \
            ch51/index51.adf ch52/probe52.adf

# The .bin files are what verify.py reassembles against, so make must not
# treat them as intermediates and sweep them away after the .adf is built.
.PRECIOUS: %.bin

.PHONY: all check verify pic hunks clean $(CHAPTERS)
all: $(DISKS)

%.bin: %.asm
	$(VASM) -Fbin -o $@ $<

%.adf: %.bin
	$(MKADF) $< $@

# From chapter 42 the disk is a loader and a program with a seam between them,
# so it is built by a different tool. tools/mkadf.py is still chapter 4's and
# still what every disk before this one is made with.
MKGAME ?= python3 tools/mkgame.py

ch42/%.adf: ch42/%.bin
	$(MKGAME) $< $@

ch43/%.adf: ch43/%.bin
	$(MKGAME) $< $@

ch44/%.adf: ch44/%.bin
	$(MKGAME) $< $@

ch45/%.adf: ch45/%.bin
	$(MKGAME) $< $@

ch46/%.adf: ch46/%.bin
	$(MKGAME) $< $@

ch47/%.adf: ch47/%.bin
	$(MKGAME) $< $@

ch48/%.adf: ch48/%.bin
	$(MKGAME) $< $@

ch49/%.adf: ch49/%.bin
	$(MKGAME) $< $@

# And chapter 43's finished disk is two programs, so it is made by a third
# tool: the title screen goes on the first tracks with the loader in front of
# it, and the game goes on the tracks after that.
MKDISK ?= python3 tools/mkdisk.py

ch43/veldrin.adf: ch43/title43.bin ch43/game43.bin
	$(MKDISK) $^ $@

# title43 and game43 are halves of veldrin.adf and have no disk of their own,
# so chapter 43's disks are listed rather than taken from the sources.
CH43_DISKS := ch43/reuse43.adf ch43/hit43.adf ch43/panel43.adf \
              ch43/sound43.adf ch43/frame43.adf ch43/veldrin.adf

ch43: $(CH43_DISKS)

# And chapter 51's disks, and chapter 52's, are laid out rather than filled.
# One script builds all of a chapter's images at once, so the target is the
# one image that cannot be made any other way and the rest come with it.
ch51: ch51/index51.adf
ch52: ch52/probe52.adf

ch51/index51.adf: $(filter ch51/%,$(SOURCES:.asm=.bin))
	python3 ch51/disks51.py

ch52/probe52.adf: $(filter ch52/%,$(SOURCES:.asm=.bin))
	python3 ch52/disks52.py

$(CHAPTERS): %: ; @$(MAKE) $(patsubst %.asm,%.adf,$(wildcard $@/*.asm))

check:  ; python3 tools/checkup.py
verify: ; python3 verify.py
pic:    ; python3 tools/pic_check.py
hunks:  ; python3 ch15/hunks.py
clean:  ; rm -f ch[0-9][0-9]/*.bin ch[0-9][0-9]/*.adf \
                app[A-L]/*.bin app[A-L]/*.adf
