# Makefile — Standalone co_* test harness
#
# Sources:
#   ../../mux/lib/color_ops.c        — Ragel-generated co_* functions
#   ../../ragel/unicode_tables.c     — Case mapping DFA tables (real OTT)
#   tables_extra.c                   — GCB/width/extpict tables (extracted)
#   co_console_width.c               — Console width function
#   test_color_ops.c                 — Test harness
#
# Build: make
# Run:   ./test_color_ops [-v] [-f <suite>] [-s <seed>]

CC       = gcc
# -MMD -MP: emit header prerequisites next to each object (#1952).  The rules
# below list headers by hand, which is correct today but drifts the moment a
# source picks up an #include nobody remembers to add here -- and a missed
# header means editing it rebuilds nothing and the suite reports green
# against the previous version.  The hand-written lists stay: they are what
# makes the FIRST build correct, before any .d exists.
CFLAGS   = -O2 -Wall -Wextra -std=c11 -g -MMD -MP
INCLUDES = -I../../mux/include -I../../ragel

TARGET   = test_color_ops

# Object files
OBJS = test_color_ops.o color_ops.o unicode_tables.o tables_extra.o tables_ascii.o co_console_width.o co_nearest_color.o

all: $(TARGET)

$(TARGET): $(OBJS)
	$(CC) $(CFLAGS) -o $@ $(OBJS)

test_color_ops.o: test_color_ops.c ../../mux/include/color_ops.h
	$(CC) $(CFLAGS) $(INCLUDES) -c test_color_ops.c

color_ops.o: ../../mux/lib/color_ops.c ../../mux/include/color_ops.h ../../mux/include/unicode_tables_c.h
	$(CC) $(CFLAGS) $(INCLUDES) -c ../../mux/lib/color_ops.c -o color_ops.o

unicode_tables.o: ../../ragel/unicode_tables.c ../../ragel/unicode_tables.h
	$(CC) $(CFLAGS) -I../../ragel -c ../../ragel/unicode_tables.c -o unicode_tables.o

# Extract width, GCB, and ExtPict tables from rv64 source.
# These arrays are not in ragel/unicode_tables.c.
tables_extra.c: ../../mux/rv64/src/unicode_tables.c
	@echo "/* Auto-extracted GCB/width/extpict tables from rv64/src/unicode_tables.c */" > $@
	@echo "#include <stddef.h>" >> $@
	@echo "#include \"unicode_tables_c.h\"" >> $@
	sed -n '/^const.*tr_widths_itt/,$$p' ../../mux/rv64/src/unicode_tables.c >> $@

tables_extra.o: tables_extra.c ../../mux/include/unicode_tables_c.h
	$(CC) $(CFLAGS) -I../../mux/include -c tables_extra.c

# Extract tr_ascii_* DFA tables from utf8tables.cpp (C-compatible arrays).
tables_ascii.c: ../../mux/lib/utf8tables.cpp
	@echo "/* Auto-extracted tr_ascii DFA tables from utf8tables.cpp */" > $@
	sed -n '/^const.*tr_ascii_itt/,/^};/p' ../../mux/lib/utf8tables.cpp >> $@
	sed -n '/^const.*tr_ascii_sot/,/^};/p' ../../mux/lib/utf8tables.cpp >> $@
	sed -n '/^const.*tr_ascii_sbt/,/^};/p' ../../mux/lib/utf8tables.cpp >> $@

tables_ascii.o: tables_ascii.c
	$(CC) $(CFLAGS) -c tables_ascii.c

co_console_width.o: co_console_width.c
	$(CC) $(CFLAGS) -c co_console_width.c

co_nearest_color.o: co_nearest_color.c ../../mux/include/color_ops.h
	$(CC) $(CFLAGS) $(INCLUDES) -c co_nearest_color.c

test: $(TARGET)
	./$(TARGET)

verbose: $(TARGET)
	./$(TARGET) -v

clean:
	rm -f $(OBJS) $(OBJS:.o=.d) $(TARGET) tables_extra.c tables_ascii.c

# Generated by -MMD; absent on the first build, hence the leading '-'.
-include $(wildcard *.d)

.PHONY: all test verbose clean
