# JoeyLib top-level Makefile.
#
# Usage:
#   make                        builds every target whose toolchain is installed
#   make iigs                   builds Apple IIgs only
#   make amiga                  builds Commodore Amiga only
#   make atarist                builds Atari ST only
#   make dos                    builds MS-DOS only
#   make x68000                 builds Sharp X68000 only
#   make dvx                    builds DVX (JoeyLib programs as DVX desktop programs)
#   make docs                   renders the manual to build/docs (HTML + PDF)
#   make verify                 builds every port, then runs every gate (needs emulators)
#   make verify-audio           runs every port's audio gate (needs emulators)
#   make verify-serial          runs every port's serial loopback gate
#   make clean                  removes all build outputs
#
# Cross tools come from ./install.sh, which records their paths in
# toolchains/config.mk; every makefile reads that file itself, so no
# environment setup is needed first.

# The toolchain paths and which ports are installed (HAVE_<PORT>,
# JOEY_PORTS_INSTALLED) come from make/common.mk, shared with every port's
# makefile and with make/project.mk.
include $(dir $(firstword $(MAKEFILE_LIST)))make/common.mk

ALL_TARGETS := $(JOEY_PORTS_INSTALLED)

.PHONY: all iigs iigs-disk iigs-verify amiga atarist dos x68000 dvx blank-check tools clean help status verify verify-audio verify-serial docs

all: $(ALL_TARGETS) tools
ifeq ($(ALL_TARGETS),)
	@echo "No toolchains detected. Run: ./install.sh install toolchains"
	@exit 1
endif

tools:
	@$(MAKE) -f $(REPO_DIR)/make/tools.mk

iigs:
	@$(MAKE) -f $(REPO_DIR)/make/iigs.mk

iigs-disk:
	@$(MAKE) -f $(REPO_DIR)/make/iigs.mk iigs-disk

iigs-verify:
	@$(MAKE) -f $(REPO_DIR)/make/iigs.mk iigs-verify

amiga:
	@$(MAKE) -f $(REPO_DIR)/make/amiga.mk

atarist:
	@$(MAKE) -f $(REPO_DIR)/make/atarist.mk

dos:
	@$(MAKE) -f $(REPO_DIR)/make/dos.mk

x68000:
	@$(MAKE) -f $(REPO_DIR)/make/x68000.mk

dvx:
	@$(MAKE) -f $(REPO_DIR)/make/dvx.mk

# Compile + link-check the new-port template (src/blank/blank.c) with the host
# cc. No toolchain needed; keeps the template from bit-rotting as the lib grows.
blank-check:
	@$(REPO_DIR)/scripts/check-blank.sh

# The manual: docs/Manual.adoc plus its chapter includes -> build/docs.
# Needs asciidoctor and asciidoctor-pdf, no cross toolchain.
# make docs FORMAT=html renders only the web version (much faster).
docs:
	@$(REPO_DIR)/scripts/build-docs.sh $(FORMAT)

# EVERYTHING: build every port, then run every gate -- host checks, UBER
# golden hashes, the IIgs example boots, input, audio, serial, the X68000
# mouse, IIgs save / serial loss / game disks -- sequentially, with a
# per-gate summary. Same exit codes as below. Takes a subset of ports:
# make verify PORTS="host dos"
verify:
	@$(REPO_DIR)/scripts/verify-all.sh all $(PORTS)

# Every port's audio gate, run sequentially (the recording gates grade
# fixed time windows, so a competing emulator can slide a phase boundary).
# Exits 1 on a real failure and 3 if a gate SKIPPED for an unbuilt port --
# non-zero on purpose, so a run that tested nothing cannot look green.
# Takes a subset: make verify-audio PORTS="atarist amiga"
verify-audio:
	@$(REPO_DIR)/scripts/verify-all.sh audio $(PORTS)

# The host check of the shared serial layer, then every port's loopback gate
# (banner out, probe echoed back), sequentially, with the same exit codes.
# Takes a subset: make verify-serial PORTS="host dos"
verify-serial:
	@$(REPO_DIR)/scripts/verify-all.sh serial $(PORTS)

clean:
	rm -rf $(REPO_DIR)/build

status:
	@echo "Toolchain availability:"
	@echo "  iigs:    $(if $(HAVE_IIGS),yes,NO)"
	@echo "  amiga:   $(if $(HAVE_AMIGA),yes,NO)"
	@echo "  atarist: $(if $(HAVE_ATARIST),yes,NO)"
	@echo "  dos:     $(if $(HAVE_DOS),yes,NO)"
	@echo "  x68000:  $(if $(HAVE_X68000),yes,NO)"
	@echo "  dvx:     $(if $(HAVE_DVX),yes,NO)"

help:
	@echo "Targets: iigs amiga atarist dos x68000 dvx all clean status help"
	@echo "         iigs-disk iigs-verify (IIgs MAME visual check)"
	@echo "         blank-check (host-cc smoke test of the new-port template + gnu89 lint)"
	@echo "         verify (build every port, then run every gate; PORTS= subset)"
	@echo "         verify-audio (all 5 audio gates; PORTS=\"atarist amiga\" for a subset)"
	@echo "         verify-serial (host check + all 5 serial loopback gates; PORTS= subset)"
	@echo "         docs (manual to build/docs; FORMAT=html for HTML only)"
	@echo "Install toolchains first: ./install.sh install toolchains"
