.POSIX:
# Dynagate LLM Gateway (C++20 port) - POSIX-compliant Makefile
# Targets: Linux, FreeBSD, macOS, Haiku (compatible with bmake, gmake, smake, etc.)
# Dependencies: libcurl (HTTP). JSON is handled natively in json.cpp
# (no extra or vendored dependency).
# Override compiler or flags if needed, e.g.:
#   make CXX=clang++ CPPFLAGS="-I/opt/homebrew/include -I." LDLIBS="-L/opt/homebrew/lib -lcurl -pthread"
#
# 32-bit build: make 32bit
# Or: make CXXFLAGS="-std=c++20 -O2 -m32" LDFLAGS="-m32"
# Large file support on 32-bit: make CPPFLAGS="-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE"

CXX = c++
STRIP = strip
CXXFLAGS = -std=c++20 -s -Wall -Wextra -Wpedantic -O2 -march=native -D_POSIX_C_SOURCE=200809L -D_DEFAULT_SOURCE -D_DARWIN_C_SOURCE
CPPFLAGS = -I/usr/local/include -I. -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
CURL_CFLAGS = $$(pkg-config --cflags libcurl 2>/dev/null || curl-config --cflags 2>/dev/null)
CURL_LIBS = $$(pkg-config --libs libcurl 2>/dev/null || curl-config --libs 2>/dev/null || echo '-lcurl')
LDFLAGS = -s
LDLIBS = -L/usr/local/lib $(CURL_LIBS) -pthread

# Location of the AddressSanitizer/UBSan runtime libraries plus a replacement
# libasan_preinit.o (so the runtime initializes before main()). On a normal
# system these ship with the compiler; override with
#   make debug ASANDIR=  (empty)  — the gcc driver finds them automatically.
ASANDIR = /tmp/asanlib

CORE_OBJS = json.o config.o handler.o server.o
OBJS = main.o $(CORE_OBJS)
TEST_OBJS = gateway_test.o $(CORE_OBJS)

.PHONY: all build clean test run install debug 32bit

all: build

build: dynagate

dynagate: $(OBJS)
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) -o dynagate $(OBJS) $(LDLIBS)
	$(STRIP) dynagate

main.o: main.cpp dynagate.hpp
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(CURL_CFLAGS) -c main.cpp

json.o: json.cpp dynagate.hpp
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(CURL_CFLAGS) -c json.cpp

config.o: config.cpp dynagate.hpp
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(CURL_CFLAGS) -c config.cpp

handler.o: handler.cpp dynagate.hpp
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(CURL_CFLAGS) -c handler.cpp

server.o: server.cpp dynagate.hpp
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(CURL_CFLAGS) -c server.cpp

gateway_test.o: gateway_test.cpp dynagate.hpp
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(CURL_CFLAGS) -c gateway_test.cpp

gateway_test: $(TEST_OBJS)
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) -o gateway_test $(TEST_OBJS) $(LDLIBS)

test: gateway_test
	./gateway_test

run: build
	./dynagate -port 8080 -csv models.csv

install: dynagate
	install -m 0755 dynagate $(DESTDIR)/usr/local/bin/dynagate

# Debug build with AddressSanitizer + UndefinedBehaviorSanitizer.
# Rebuilds ./dynagate — run `make clean && make` afterwards to restore the
# optimized build. ASANDIR points the gcc driver (-B) at the runtime libs and
# the replacement libasan_preinit.o, and sets an rpath so they load at run time.
debug:
	$(MAKE) CXXFLAGS="-std=c++20 -Wall -Wextra -Wpedantic -D_POSIX_C_SOURCE=200809L -D_DEFAULT_SOURCE -D_DARWIN_C_SOURCE -g -O0 -fsanitize=address,undefined -B$(ASANDIR) -Wl,-rpath,$(ASANDIR)" LDFLAGS="" STRIP=":" dynagate

# 32-bit build target
# Usage: make 32bit
# This builds a 32-bit binary with large file support enabled.
32bit:
	$(MAKE) CXXFLAGS="$(CXXFLAGS) -m32" LDFLAGS="$(LDFLAGS) -m32" dynagate

clean:
	rm -f $(OBJS) gateway_test.o dynagate gateway_test
