# ============================================================================
# Universal Translator — build all faces + the REAL analog substrate.
#
#   make              build everything against the real Kuramoto-wave substrate
#   make test         build + run all three harnesses (demo, alpine, substrate)
#   make FIXEDPOINT=1 build the substrate libm-free (freestanding kernel path)
#   make PLACEHOLDER=1 A/B against the old toy phi_lattice.asm substrate
#   make clean
#
# NO GAPS: the default build wires face_settle to hdgl_substrate_real.c —
# Kuramoto phase-coupled oscillators + 𝓛ₙ(z)/Ω(tick) + Dₙ(r) + CV consensus,
# extracted from phi_pool_kuramoto.c and hdgl_analog_v30.c. It converges to a
# real phase-coherence LOCK (see `make test`).
# ============================================================================
CC      ?= gcc
CSTD    ?= -std=c99
CFLAGS  ?= $(CSTD) -O2 -Wall -Wextra
BUILD   := build

CORE    := core/face_operator.c
SUBSRC  := substrate/hdgl_substrate_real.c
SUBLIB  := -lm

ifdef FIXEDPOINT
  CFLAGS += -DHDGL_SUBSTRATE_FIXEDPOINT
  SUBLIB :=
endif

# A/B: build the operator against the old toy asm substrate instead.
ifdef PLACEHOLDER
  SUBOBJ := $(BUILD)/phi_lattice.o
  SUBLIB :=
  # operator must include phi_lattice.h in this mode:
  $(warning PLACEHOLDER build: edit core/face_operator.c include back to phi_lattice.h)
else
  SUBOBJ := $(BUILD)/hdgl_substrate_real.o
endif

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

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

$(BUILD)/hdgl_substrate_real.o: $(SUBSRC) substrate/hdgl_substrate_real.h | $(BUILD)
	$(CC) $(CFLAGS) -c $< -o $@

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

$(BUILD)/face_operator.o: $(CORE) | $(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 $(SUBOBJ) | $(BUILD)
	$(CC) $(CFLAGS) $(BUILD)/face_operator.o $(BUILD)/face_demo.o tests/test_face.c $(SUBOBJ) $(SUBLIB) -o $@

$(BUILD)/test_alpine: $(BUILD)/face_operator.o $(BUILD)/face_alpine.o tests/test_alpine.c $(SUBOBJ) | $(BUILD)
	$(CC) $(CFLAGS) $(BUILD)/face_operator.o $(BUILD)/face_alpine.o tests/test_alpine.c $(SUBOBJ) $(SUBLIB) -o $@

$(BUILD)/test_substrate_real: tests/test_substrate_real.c $(BUILD)/hdgl_substrate_real.o | $(BUILD)
	$(CC) $(CFLAGS) tests/test_substrate_real.c $(BUILD)/hdgl_substrate_real.o $(SUBLIB) -o $@

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

clean:
	rm -rf $(BUILD)
