# Host tests for the GENIbus Arduino library.
#   make          core tests (spec vectors, simulated slaves) + Arduino layer
#   make asan     the same with AddressSanitizer / UBSan
#   make clean

CC      ?= cc
CXX     ?= c++
CORE    := ../../src/genibus
SRC     := ../../src
CSRCS   := $(CORE)/genibus.c $(CORE)/geni_units.c $(CORE)/geni_master.c $(CORE)/mp204.c
FLAGS   ?= -O1 -g
CFLAGS  += $(FLAGS) -std=c99 -Wall -Wextra -Werror -pedantic -I$(CORE)
CXXFLAGS+= $(FLAGS) -std=c++11 -Wall -Wextra -Werror -Ishim -I$(SRC)
BUILD   := build
SAN     := -fsanitize=address,undefined -fno-sanitize-recover=all

.PHONY: all test asan clean
all: test

$(BUILD)/core: test_genibus.c $(CSRCS) $(wildcard $(CORE)/*.h)
	@mkdir -p $(BUILD)
	$(CC) $(CFLAGS) $(XFLAGS) -o $@ test_genibus.c $(CSRCS) -lm

$(BUILD)/arduino: test_arduino.cpp $(SRC)/GENIbus.cpp $(SRC)/GENIbus.h shim/Arduino.h $(CSRCS) $(wildcard $(CORE)/*.h)
	@mkdir -p $(BUILD)/obj
	for f in $(CSRCS); do $(CC) $(CFLAGS) $(XFLAGS) -c $$f -o $(BUILD)/obj/$$(basename $$f .c).o || exit 1; done
	$(CXX) $(CXXFLAGS) $(XFLAGS) -o $@ test_arduino.cpp $(SRC)/GENIbus.cpp $(BUILD)/obj/*.o -lm

test: $(BUILD)/core $(BUILD)/arduino
	./$(BUILD)/core
	./$(BUILD)/arduino

asan:
	$(MAKE) clean
	$(MAKE) test XFLAGS="$(SAN)"

clean:
	rm -rf $(BUILD)
