CC ?= cc
OPT ?= -O2 -g
WARN = -Wall -Wextra
CFLAGS = $(OPT) $(WARN) -std=gnu11
CPPFLAGS = -Isrc
LDLIBS = -lm

# Parity depends on the compiler not reassociating or contracting the
# reductions differently from the reference; -ffast-math is deliberately
# absent.
#
# No architecture flags are needed or wanted. On x86 the AVX2 kernels carry
# their own instruction set on the functions and are selected against the
# running processor, so a default build is both safe on a host without AVX2
# and fast on one with it.
#
# THREADS=0 drops the pthread dependency; the window loop then runs on the
# calling thread whatever --threads asks for.
# EMBED=0 leaves the weight blob out of the binary, which then needs
# --weights pyseg_weights.bin.
# STATIC=1 links a self-contained binary, for a container that carries
# nothing but the executable.

THREADS ?= 1
EMBED ?= 1

ifeq ($(THREADS),1)
  CPPFLAGS += -DPYSEG_THREADS
  LDLIBS += -lpthread
endif
ifeq ($(STATIC),1)
  LDFLAGS += -static
endif

BUILD := build
LIB := $(BUILD)/libpyseg.a
BIN := pyseg_cli

LIB_SRCS := src/pyseg.c src/kernels.c src/weights.c src/slide.c
ifeq ($(EMBED),1)
  CPPFLAGS += -DPYSEG_EMBED_WEIGHTS
  LIB_SRCS += src/weights_blob.c
endif
LIB_OBJS := $(LIB_SRCS:%.c=$(BUILD)/%.o)
CLI_SRCS := src/pyseg_cli.c src/wav.c
CLI_OBJS := $(CLI_SRCS:%.c=$(BUILD)/%.o)

TESTS := test_kernels test_parity test_robust test_slide
TEST_BINS := $(TESTS:%=$(BUILD)/%)
TEST_CPPFLAGS := $(CPPFLAGS) -Itests

BENCH := $(BUILD)/bench

WEIGHTS := pyseg_weights.bin
GOLDEN := $(BUILD)/golden.bin
SLIDE_GOLDEN := $(BUILD)/slide_golden.bin
AUDIO ?= testdata_aepyx.wav
PY := PYTHONPATH=tools .venv/bin/python
PYTHON ?= python3

.PHONY: all test bench clean weights golden verify

all: $(LIB) $(BIN)

$(LIB): $(LIB_OBJS)
	@rm -f $@
	ar rcs $@ $(LIB_OBJS)

$(BIN): $(CLI_OBJS) $(LIB)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(CLI_OBJS) $(LIB) $(LDLIBS)

$(BUILD)/%.o: %.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) $(CPPFLAGS) -MMD -MP -c $< -o $@

# The generated weight array is data, not code; optimising it only costs
# compile time.
$(BUILD)/src/weights_blob.o: src/weights_blob.c
	@mkdir -p $(dir $@)
	$(CC) -O0 -g0 -std=gnu11 -c $< -o $@

$(BUILD)/test_%: tests/test_%.c $(LIB) tests/test_util.h
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) $(TEST_CPPFLAGS) $(LDFLAGS) $< $(LIB) -o $@ $(LDLIBS)

$(BENCH): bench/bench.c $(LIB)
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $< $(LIB) -o $@ $(LDLIBS)

# Regenerates the weight blob from the checkpoint in the Hugging Face cache.
# The blob is checked in, so this is only needed after a layout change; the
# embedded copy is derived from it by a plain-stdlib script that runs anywhere,
# including inside the container build.
weights:
	$(PY) tools/export_weights.py .
	$(MAKE) src/weights_blob.c

src/weights_blob.c: $(WEIGHTS) tools/embed.py
	$(PYTHON) tools/embed.py $(WEIGHTS) $@

golden: $(GOLDEN) $(SLIDE_GOLDEN)

$(GOLDEN): golden.npz tools/make_golden.py
	@mkdir -p $(BUILD)
	$(PY) tools/make_golden.py chunks $< $@

$(SLIDE_GOLDEN): $(AUDIO) tools/make_golden.py
	@mkdir -p $(BUILD)
	$(PY) tools/make_golden.py slide $< $@

golden.npz: $(AUDIO) tools/baseline.py
	$(PY) tools/baseline.py golden $< $@

test: $(TEST_BINS) $(GOLDEN) $(SLIDE_GOLDEN)
	@rc=0; \
	$(BUILD)/test_kernels || rc=1; \
	$(BUILD)/test_parity $(GOLDEN) || rc=1; \
	$(BUILD)/test_robust || rc=1; \
	$(BUILD)/test_slide $(SLIDE_GOLDEN) || rc=1; \
	if [ $$rc -eq 0 ]; then echo "all ok"; else echo "not ok"; exit 1; fi

bench: $(BENCH) $(GOLDEN)
	$(BENCH) $(GOLDEN)

# End-to-end against pyannote's own pipelines; needs the Python environment.
verify: $(BIN)
	$(PY) tools/verify_cli.py $(AUDIO)

clean:
	rm -rf $(BUILD) $(BIN)

-include $(LIB_OBJS:.o=.d) $(CLI_OBJS:.o=.d)
