CXX      := g++
CXXFLAGS := -std=c++17 -Wall -Wextra -Werror -O2 -fPIC
INCLUDES := -Iinclude
LIBS     := -lpthread

# NO SQLITE, and that is now a property of the whole tree rather than of its
# published half. This Makefile came onto the shared vendored chain on
# 2026-08-17 (INFRA-FIVE-TREES-LINK-SYSTEM-SQLITE3), and the daemon retirement
# later the same day (bcm/archive/trace-daemon-retire-2026-08-17/) removed the
# only two targets that had ever needed it — `traced`, which owned the
# persistence layer, and the integration test that drove it. libtrace itself
# never touched SQLite: src/Trace.cpp names it zero times and libtrace.so has no
# undefined sqlite symbols, which is exactly why a published MIT tree could come
# on-chain without a posture argument, and now why it needs no chain at all.
# Re-adding a database here means re-adding a dependency the tree has shed —
# `base/vendor/sqlite/sqlite.mk` is how, and the vendor-sqlite baseline asserts
# the absence, so it will be noticed.

SRC      := src/Trace.cpp
OBJ      := $(SRC:.cpp=.o)
LIB      := libtrace.a
SO       := libtrace.so
PREFIX   := /usr/local

UNIT_SRC := tests/test_libtrace.cpp
UNIT_BIN := test_libtrace

.PHONY: all lib clean install install-lib uninstall unit-test

all: $(LIB) $(SO)

lib: $(LIB) $(SO)

$(LIB): $(OBJ)
	ar rcs $@ $^

$(SO): $(OBJ)
	$(CXX) -shared -o $@ $^ $(LIBS)

src/%.o: src/%.cpp include/trace/Trace.h
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@

# Names $(LIB) DIRECTLY rather than `-L. -ltrace`
# (TRACE-TESTS-HAVE-NO-LIBRARY-PIN). With `-L.` and both artifacts present the
# linker prefers libtrace.so, the binary carried neither RPATH nor RUNPATH, and
# which copy the suite graded was decided by the ambient LD_LIBRARY_PATH —
# which on the build host had a trailing colon, so an empty element put the
# current directory on the search path. The suite passed either way, so nothing ever
# said which library it had tested. Naming the archive makes the answer a
# property of this rule instead of the environment: one file, statically
# linked, no search. Same fix antheos took 2026-08-07.
$(UNIT_BIN): $(UNIT_SRC) $(LIB)
	$(CXX) $(CXXFLAGS) $(INCLUDES) -o $@ $< $(LIB) -lpthread

unit-test: $(UNIT_BIN)
	./$(UNIT_BIN)

clean:
	rm -f $(OBJ) $(LIB) $(SO) $(UNIT_BIN)

# `install` and `install-lib` are the same operation and both are kept: the
# collector retirement removed the daemon half that used to make them differ,
# and `install-lib` is the name every caller and doc already uses.
install-lib: $(LIB) $(SO)
	install -d $(PREFIX)/lib $(PREFIX)/include/trace
	install -m 644 $(LIB) $(PREFIX)/lib/
	install -m 755 $(SO) $(PREFIX)/lib/
	install -m 644 include/trace/Trace.h $(PREFIX)/include/trace/
	ldconfig

install: install-lib

uninstall:
	rm -f $(PREFIX)/lib/$(LIB) $(PREFIX)/lib/$(SO) $(PREFIX)/include/trace/Trace.h
	rmdir --ignore-fail-on-non-empty $(PREFIX)/include/trace
