# krang -- POSIX make.
#
#   make            build krang
#   make check      build and run the offline tests (no network, no model)
#   make clean
#
# The engine is ../splinter/libsplinter.so. Build it first with
# `make -C ../splinter`; its headers are not installed by `make install`.

CC	?= cc
SPLINTER ?= ../splinter
CFLAGS	?= -std=c99 -O2 -Wall -Wextra -Werror -D_POSIX_C_SOURCE=200809L
CPPFLAGS = -Isrc -I$(SPLINTER)/include -I$(SPLINTER)/src/backend \
	   -I$(SPLINTER)/vendor/libmcp
LDFLAGS	?=
LIBS	= -L$(SPLINTER) -lsplinter -lpthread
RPATH	= -Wl,-rpath,'$$ORIGIN/../splinter' -Wl,-rpath,'$$ORIGIN/../lib'
TEST_RPATH = -Wl,-rpath,'$$ORIGIN/../../splinter' \
	     -Wl,-rpath,'$$ORIGIN/../../lib'

BIN	= krang
LIB	= libkrang.a
SRC	= src/util.c src/config.c src/log.c src/session.c src/birth.c \
	  src/stuck.c src/state.c src/chat.c src/journal.c src/goal.c \
	  src/replay.c src/mind.c src/daemon.c src/sandbox.c src/httpd.c
OBJ	= $(SRC:.c=.o)
TESTS	= tests/test_krang tests/test_engine tests/test_server \
	  tests/test_sandbox tests/test_sandbox_engine tests/test_daemon \
	  tests/test_timeout tests/test_replay tests/test_mind

all: $(BIN)

$(BIN): src/main.o $(LIB)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ src/main.o $(LIB) $(LIBS) $(RPATH)

$(LIB): $(OBJ)
	rm -f $@
	ar rcs $@ $(OBJ)

src/%.o: src/%.c src/krang.h
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

src/main.o: src/main.c src/krang.h
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

tests/test_krang: tests/test_krang.c $(LIB)
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_krang.c $(LIB) \
	    $(LIBS) $(TEST_RPATH)

tests/test_engine: tests/test_engine.c $(LIB)
	$(CC) $(CFLAGS) $(CPPFLAGS) \
	    -DKRANG_TEST_PLUGIN='"$(abspath $(SPLINTER))/plugins/clock/clock.so"' \
	    -o $@ tests/test_engine.c $(LIB) $(LIBS) $(TEST_RPATH)

tests/test_server: tests/test_server.c $(LIB)
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_server.c $(LIB) \
	    $(LIBS) $(TEST_RPATH)

tests/test_sandbox: tests/test_sandbox.c $(LIB)
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_sandbox.c $(LIB) \
	    $(LIBS) $(TEST_RPATH)

tests/test_sandbox_engine: tests/test_sandbox_engine.c $(LIB)
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_sandbox_engine.c $(LIB) \
	    $(LIBS) $(TEST_RPATH)

tests/test_daemon: tests/test_daemon.c $(LIB)
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_daemon.c $(LIB) \
	    $(LIBS) $(TEST_RPATH)

tests/test_timeout: tests/test_timeout.c $(LIB)
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_timeout.c $(LIB) \
	    $(LIBS) $(TEST_RPATH)

tests/test_replay: tests/test_replay.c $(LIB)
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_replay.c $(LIB) \
	    $(LIBS) $(TEST_RPATH)

tests/test_mind: tests/test_mind.c $(LIB)
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_mind.c $(LIB) \
	    $(LIBS) $(TEST_RPATH)

check: $(TESTS)
	@echo "== tests/test_krang"; ./tests/test_krang
	@echo "== tests/test_engine"; ./tests/test_engine
	@echo "== tests/test_server"; ./tests/test_server
	@echo "== tests/test_sandbox"; ./tests/test_sandbox
	@echo "== tests/test_sandbox_engine"; ./tests/test_sandbox_engine
	@echo "== tests/test_daemon"; ./tests/test_daemon
	@echo "== tests/test_timeout"; ./tests/test_timeout
	@echo "== tests/test_replay"; ./tests/test_replay
	@echo "== tests/test_mind"; ./tests/test_mind
	@echo "all tests passed"

check-root: $(BIN)
	tests/root-integration.sh

# The sanitizer build reuses the same object paths, so it starts and ends with
# a clean tree; a plain `make` after it must not link instrumented objects.
check-asan:
	$(MAKE) clean
	$(MAKE) CFLAGS="$(CFLAGS) -g -fsanitize=address,undefined" \
	    LDFLAGS="-fsanitize=address,undefined" check
	$(MAKE) clean

install: $(BIN)
	install -d $(DESTDIR)$(PREFIX)/bin $(DESTDIR)$(PREFIX)/lib \
	    $(DESTDIR)$(PREFIX)/share/krang
	install -m 0755 $(BIN) $(DESTDIR)$(PREFIX)/bin/krang
	install -m 0644 $(SPLINTER)/libsplinter.so \
	    $(DESTDIR)$(PREFIX)/lib/libsplinter.so
	install -m 0644 krang.conf.example \
	    $(DESTDIR)$(PREFIX)/share/krang/krang.conf.example
	install -m 0644 share/krang.service \
	    $(DESTDIR)$(PREFIX)/share/krang/krang.service

clean:
	rm -f $(BIN) $(LIB) $(OBJ) src/main.o $(TESTS)

.PHONY: all check check-asan check-root install clean
