# Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License.
# See LICENSE.md for license information.
# SPDX-License-Identifier: MIT
#
# 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 (runs on every ST)
#   make ste                    builds the Atari STe variant (STe and Mega STe only)
#   make dos                    builds MS-DOS only
#   make x68000                 builds Sharp X68000 only
#   make dvx                    builds DVX (JoeyLib programs as DVX desktop programs)
#   make sdl3                   builds SDL3 (JoeyLib programs as native windowed programs)
#   make web                    builds the web port (JoeyLib programs as web pages)
#   make macos                  builds macOS (JoeyLib programs as universal Mac apps)
#   make win32                  builds Win32 (Windows 95 through today, as .EXE programs)
#   make win16                  builds Win16 (Windows 3.1 on a 386, as .EXE programs)
#   make psp                    builds the PSP port (JoeyLib programs as PSP homebrew EBOOT.PBPs)
#   make haiku                  builds Haiku x86_64 (the Be API backend, gcc 13)
#   make haiku32                builds Haiku 32-bit x86 (the Be API backend, gcc 2.95, BeOS ABI)
#   make beos                   builds BeOS R5 x86 (the same backend and compiler, R5's own headers and libraries)
#   make tools                  builds the host tools (the asset, icon, sprite, and music converters) into build/tools
#   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-conform         grades every recording port against the audio contract
#   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-game-disks iigs-verify amiga atarist ste dos x68000 dvx sdl3 web macos win32 win16 psp haiku haiku32 beos blank-check tools clean help status verify verify-audio verify-conform 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-game-disks:
	@$(MAKE) -f $(REPO_DIR)/make/iigs.mk iigs-game-disks

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

ste:
	@$(MAKE) -f $(REPO_DIR)/make/ste.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

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

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

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

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

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

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

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

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

beos:
	@$(MAKE) -f $(REPO_DIR)/make/beos.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)

# Every recording port against the one audio contract (AUDPAR, graded by
# tests/gate/audioGate.c), sequentially, with the same exit codes.
# Takes a subset: make verify-conform PORTS="sdl3 dos"
verify-conform:
	@$(REPO_DIR)/scripts/verify-all.sh conform $(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 "  ste:     $(if $(HAVE_STE),yes,NO)"
	@echo "  dos:     $(if $(HAVE_DOS),yes,NO)"
	@echo "  x68000:  $(if $(HAVE_X68000),yes,NO)"
	@echo "  dvx:     $(if $(HAVE_DVX),yes,NO)"
	@echo "  sdl3:    $(if $(HAVE_SDL3),yes,NO)"
	@echo "  web:     $(if $(HAVE_WEB),yes,NO)"
	@echo "  macos:   $(if $(HAVE_MACOS),yes,NO)"
	@echo "  win32:   $(if $(HAVE_WIN32),yes,NO)"
	@echo "  win16:   $(if $(HAVE_WIN16),yes,NO)"
	@echo "  psp:     $(if $(HAVE_PSP),yes,NO)"
	@echo "  haiku:   $(if $(HAVE_HAIKU),yes,NO)"
	@echo "  haiku32: $(if $(HAVE_HAIKU32),yes,NO)"
	@echo "  beos:    $(if $(HAVE_BEOS),yes,NO)"

help:
	@echo "Targets: iigs amiga atarist ste dos x68000 dvx sdl3 web macos win32 win16 psp haiku haiku32 beos all clean status help"
	@echo "         iigs-disk (the first examples that fit one 800 KB disk) iigs-game-disks (a bootable disk for every example)"
	@echo "         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 (every port's audio gate; PORTS=\"atarist amiga\" for a subset)"
	@echo "         verify-conform (every recording port against the audio contract; PORTS= subset)"
	@echo "         verify-serial (host check + every port's serial loopback gate; PORTS= subset)"
	@echo "         docs (manual to build/docs; FORMAT=html for HTML only)"
	@echo "Install toolchains first: ./install.sh install toolchains"
