# plugin_memory - SCORPIOX BOT open-source knowledge-graph memory connector
#
# Targets:
#   make            native build  -> ./plugin_memory
#   make musl       static musl x86_64 -> ./plugin_memory.musl
#   make arm64      static arm64 musl (aarch64 cross) -> ./plugin_memory.arm64
#   make win64      MinGW-w64 static -> ./plugin_memory.exe
#   make macos      Apple clang -> ./plugin_memory (run this on macOS)
#   make ui         regenerate src/ui_html.h from src/ui.html
#   make asan       ASan/UBSan + _FORTIFY_SOURCE=2 build -> ./plugin_memory.asan
#   make test       build + run tests/run.sh
#   make e2e        build + run tests/e2e.py (Playwright)
#   make clean
#
# The binary is four translation units plus the vendored yyjson amalgamation:
#   src/plugin_memory.c  entry, --meta, the --mcp JSON-RPC loop
#   src/graph.c          JSONL storage, atomic save, the store, shared helpers
#   src/tools.c          the nine tools, shared by --mcp and the HTTP API
#   src/api.c            the CGI JSON actions and the HTML page
#   vendor/yyjson.c      vendored JSON parser/writer (zero runtime deps)

CC            ?= cc
SRCS          := src/plugin_memory.c src/graph.c src/tools.c src/api.c vendor/yyjson.c
CFLAGS        ?= -O2 -std=c99 -Wall -Wextra -pedantic -Wno-overlength-strings -I src -I vendor
MUSL_CC       ?= musl-gcc
MUSL_CFLAGS   := -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -I src -I vendor -static
ARM_CC        ?= aarch64-linux-gnu-gcc
ARM_CFLAGS    := -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -I src -I vendor -static
WIN_CC        ?= x86_64-w64-mingw32-gcc
WIN_CFLAGS    := -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -I src -I vendor -static
UI_SRC        := src/ui.html
UI_HDR        := src/ui_html.h
BIN           := plugin_memory
EXE           := plugin_memory.exe

.PHONY: all native musl arm64 win64 macos ui asan test e2e clean help

all: native

$(UI_HDR): $(UI_SRC) tools/gen_ui.py
	@echo "==> regenerating $(UI_HDR)"
	python3 tools/gen_ui.py

ui: $(UI_HDR)

native: $(BIN)

$(BIN): $(SRCS) $(UI_HDR)
	@echo "==> native build ($(CC))"
	$(CC) $(CFLAGS) -o $(BIN) $(SRCS)

# Static musl build - the universal Linux artifact (Alpine, Debian, Arch, Omarchy)
musl: $(SRCS) $(UI_HDR)
	@echo "==> static musl build ($(MUSL_CC))"
	$(MUSL_CC) $(MUSL_CFLAGS) -o $(BIN).musl $(SRCS)

# Static aarch64 build - Raspberry Pi 5 / ARM64 servers
arm64: $(SRCS) $(UI_HDR)
	@echo "==> static arm64 build ($(ARM_CC))"
	$(ARM_CC) $(ARM_CFLAGS) -o $(BIN).arm64 $(SRCS)

# Native Win64 static PE - runs on any Windows 10/11 x64 node
win64: $(SRCS) $(UI_HDR)
	@echo "==> MinGW-w64 build ($(WIN_CC))"
	$(WIN_CC) $(WIN_CFLAGS) -o $(EXE) $(SRCS)

# Apple clang build (run this on macOS)
macos: $(SRCS) $(UI_HDR)
	@echo "==> Apple clang build"
	$(CC) $(CFLAGS) -o $(BIN) $(SRCS)

# Address/undefined sanitizers plus fortify. This is the build that finds the
# overlap, use-after-free and truncation bugs the -O2 suite cannot see, so it
# runs in tests/run.sh, not just by hand.
asan: $(SRCS) $(UI_HDR)
	@echo "==> sanitizer build"
	$(CC) -O1 -g -std=c99 -Wall -Wextra -I src -I vendor \
	  -fsanitize=address,undefined -fno-omit-frame-pointer \
	  -D_FORTIFY_SOURCE=2 -o $(BIN).asan $(SRCS)

test: native
	@sh tests/run.sh

e2e: native
	@python3 tests/e2e.py

clean:
	rm -f $(BIN) $(EXE) $(BIN).musl $(BIN).arm64 $(BIN).macos-arm64 \
	  $(BIN).macos-x86_64 $(BIN).asan
	rm -rf bin

help:
	@echo "make | make musl | make arm64 | make win64 | make macos | make ui | make asan | make test | make e2e | make clean"
