# Host-side tools for the JoeyLib asset pipeline.
#
# These build with the host's default `cc` (no cross-toolchain) into
# build/tools/. Currently:
#   modloopend  -- inject an E8FF stop-marker pattern into a .MOD so
#                  the Amiga audio HAL can honor PlayMod(loop=false).
#                  joeymod invokes this transparently for Amiga output.
#   mkstlevel   -- host helper for Space Taxi level packaging.
#
# Asset baking (PNG -> .tbk / .spr) is handled by tools/assetbake/
# assetbake.py (Python). It is invoked directly from each platform's
# Makefile and does not need a C build step.

include $(dir $(lastword $(MAKEFILE_LIST)))/common.mk

BUILD_DIR := $(REPO_DIR)/build/tools
TOOLS_DIR := $(REPO_DIR)/tools

HOST_CC      ?= cc
HOST_CFLAGS  := -std=c99 -Wall -Wextra -Werror -O2

MODLOOPEND_SRC := $(TOOLS_DIR)/modloopend/modloopend.c
MODLOOPEND_BIN := $(BUILD_DIR)/modloopend
MKSTLEVEL_SRC  := $(REPO_DIR)/examples/spacetaxi/mkstlevel/mkstlevel.c
MKSTLEVEL_BIN  := $(BUILD_DIR)/mkstlevel

# spritebake -- offline sprite pre-compiler. Built ONCE PER TARGET on the
# host: it links the shared staging (spriteStage.c) + that target's emitter,
# compiled with -DJOEYLIB_PLATFORM_<port> so JOEY_SPRITE_SHIFT_COUNT and the
# emitted machine code match the target while running on the host. Because
# the emitters use only fixed/gated compile-time constants, host output is
# byte-identical to the target JIT (see tools/spritebake/spritebake.c).
SPRITEBAKE_SRC   := $(TOOLS_DIR)/spritebake/spritebake.c $(SRC_CG)/spriteStage.c
SPRITEBAKE_INC   := -I$(INCLUDE_DIR) -I$(SRC_CORE) -I$(SRC_CG)

.PHONY: all tools clean-tools modloopend mkstlevel \
        spritebake-dos spritebake-amiga spritebake-atarist spritebake-iigs
all tools: $(MODLOOPEND_BIN) $(MKSTLEVEL_BIN)
modloopend: $(MODLOOPEND_BIN)
mkstlevel:  $(MKSTLEVEL_BIN)
spritebake-dos:     $(BUILD_DIR)/spritebake-dos
spritebake-amiga:   $(BUILD_DIR)/spritebake-amiga
spritebake-atarist: $(BUILD_DIR)/spritebake-atarist
spritebake-iigs:    $(BUILD_DIR)/spritebake-iigs

$(MODLOOPEND_BIN): $(MODLOOPEND_SRC)
	@mkdir -p $(dir $@)
	$(HOST_CC) $(HOST_CFLAGS) $< -o $@

$(MKSTLEVEL_BIN): $(MKSTLEVEL_SRC)
	@mkdir -p $(dir $@)
	$(HOST_CC) $(HOST_CFLAGS) $< -o $@

$(BUILD_DIR)/spritebake-dos: $(SPRITEBAKE_SRC) $(SRC_DIR)/dos/spriteEmitX86.c
	@mkdir -p $(dir $@)
	$(HOST_CC) $(HOST_CFLAGS) -DJOEYLIB_PLATFORM_DOS $(SPRITEBAKE_INC) -I$(SRC_DIR)/dos $^ -o $@

$(BUILD_DIR)/spritebake-amiga: $(SPRITEBAKE_SRC) $(SRC_68K)/spriteEmitPlanar68k.c
	@mkdir -p $(dir $@)
	$(HOST_CC) $(HOST_CFLAGS) -DJOEYLIB_PLATFORM_AMIGA $(SPRITEBAKE_INC) -I$(SRC_DIR)/amiga -I$(SRC_68K) $^ -o $@

$(BUILD_DIR)/spritebake-atarist: $(SPRITEBAKE_SRC) $(SRC_68K)/spriteEmitInterleaved68k.c
	@mkdir -p $(dir $@)
	$(HOST_CC) $(HOST_CFLAGS) -DJOEYLIB_PLATFORM_ATARIST $(SPRITEBAKE_INC) -I$(SRC_DIR)/atarist -I$(SRC_68K) $^ -o $@

$(BUILD_DIR)/spritebake-iigs: $(SPRITEBAKE_SRC) $(SRC_DIR)/iigs/spriteEmitIigs.c
	@mkdir -p $(dir $@)
	$(HOST_CC) $(HOST_CFLAGS) -DJOEYLIB_PLATFORM_IIGS $(SPRITEBAKE_INC) $^ -o $@

clean-tools:
	rm -rf $(BUILD_DIR)
