# ============================================================================
# Universal Translator — build all faces on the REAL asm substrate.
#
#   make        build everything (operator + dialects + phi_lattice.asm)
#   make test   build + run the demo and alpine face harnesses
#   make clean
#
# The substrate is substrate/phi_lattice.asm — extracted logic-for-logic from
# hdgl_router64.asm's boot-verified .phi_tick/.phi_consensus (Router64 reports
# real LOCK/UNLOCK on real and QEMU boot). face_settle drives it directly.
# Requires nasm >= 2.14 and gcc.
# ============================================================================
CC      ?= gcc
CSTD    ?= -std=c99
CFLAGS  ?= $(CSTD) -O2 -Wall -Wextra
NASM    ?= nasm
BUILD   := build

.PHONY: all test clean
all: $(BUILD)/test_face $(BUILD)/test_alpine

$(BUILD):
	@mkdir -p $(BUILD)

$(BUILD)/phi_lattice.o: substrate/phi_lattice.asm | $(BUILD)
	$(NASM) -f elf64 $< -o $@

$(BUILD)/face_operator.o: core/face_operator.c core/face_node.h substrate/phi_lattice.h | $(BUILD)
	$(CC) $(CFLAGS) -c $< -o $@

$(BUILD)/face_demo.o: dialects/demo/face_demo_dialect.c | $(BUILD)
	$(CC) $(CFLAGS) -c $< -o $@

$(BUILD)/face_alpine.o: dialects/alpine/face_alpine_dialect.c | $(BUILD)
	$(CC) $(CFLAGS) -c $< -o $@

$(BUILD)/test_face: $(BUILD)/face_operator.o $(BUILD)/face_demo.o tests/test_face.c $(BUILD)/phi_lattice.o | $(BUILD)
	$(CC) $(CFLAGS) $^ -o $@ -z noexecstack

$(BUILD)/test_alpine: $(BUILD)/face_operator.o $(BUILD)/face_alpine.o tests/test_alpine.c $(BUILD)/phi_lattice.o | $(BUILD)
	$(CC) $(CFLAGS) $^ -o $@ -z noexecstack

test: all
	@echo "-- demo face (asm substrate) --"   && $(BUILD)/test_face   | tail -6
	@echo "-- alpine face (asm substrate) --" && $(BUILD)/test_alpine | tail -5

clean:
	rm -rf $(BUILD)
