# Project settings
TARGET   := cubit
SRCDIR   := src
BUILDDIR := builds
UNIW     := libs/unicode_width/unicode_width.c
GRAPHEME := libs/libgrapheme-2.0.2/src/*.c
CJSON    := libs/cjson/cJSON.c
TREE_SITTER_RUBY := libs/tree-sitter-ruby/libtree-sitter-ruby.a
TREE_SITTER_C    := libs/tree-sitter-c/libtree-sitter-c.a
TREE_SITTER      := libs/tree-sitter/libtree-sitter.a

# Sources
SRC := $(wildcard $(SRCDIR)/*.c) $(UNIW) $(GRAPHEME) $(CJSON)

# Linker flags (libraries you want to link against)
LIBS := $(TREE_SITTER) $(TREE_SITTER_RUBY) $(TREE_SITTER_C)

# Compiler settings
CC := gcc
CFLAGS   := -Wall -Wextra
DBGFLAGS := -O0 -g -fsanitize=address -fno-omit-frame-pointer
OPTFLAGS := -O3 -march=native -flto -fomit-frame-pointer

# Default target
all: release

# Debug/test build (with sanitizers, does NOT run automatically)
test: $(BUILDDIR)/$(TARGET)-tst
	@echo "Built test binary at $(BUILDDIR)/$(TARGET)-tst"

$(BUILDDIR)/$(TARGET)-tst: $(SRC)
	@mkdir -p $(BUILDDIR)
	env ASAN_OPTIONS=verbosity=1:halt_on_error=1 $(CC) $(CFLAGS) $(DBGFLAGS) -o $@ $^ $(LIBS)

# Release build (optimized, no sanitizers)
release: $(BUILDDIR)/$(TARGET)

$(BUILDDIR)/$(TARGET): $(SRC)
	@mkdir -p $(BUILDDIR)
	$(CC) $(CFLAGS) $(OPTFLAGS) -o $@ $^ $(LIBS)
	strip $@

# Clean
clean:
	rm -rf $(BUILDDIR)

.PHONY: all test release clean
