# Host tests for script/lua_vesc_pkg.c: a package's settings (slot bounds and
# per-package isolation), page data both ways, and the motor's beeps and
# tones. The real bindings run in the real engine (script/script_lua.c and
# the vendored Lua) with two packages, against the stubs in stubs.h.
#
# Every firmware header the bindings include is stubs.h here: the stub
# headers are generated, one line each, so there is nothing to keep in step
# with the real ones but stubs.h itself.

TOP = ../..
CC = gcc
CFLAGS = -O1 -g -std=gnu99 -Wall -Wextra -I. -Iinc -I$(TOP) -I$(TOP)/script \
         -I$(TOP)/script/lua -I$(TOP)/comm
SAN = -fsanitize=address,undefined -fno-sanitize-recover=all
STUBS = conf_general commands comm_usb comm_can app mempools mcpwm_foc timeout
SOURCES = main.c $(TOP)/script/lua_vesc_pkg.c $(TOP)/script/script_lua.c \
          $(TOP)/script/script_pack.c $(wildcard $(TOP)/script/lua/*.c)

.PHONY: all run clean

all: test

inc/ch.h: Makefile
	@mkdir -p inc
	@for h in $(STUBS); do echo '#include "stubs.h"' > inc/$$h.h; done
	@printf '#include <stdint.h>\ntypedef uint32_t systime_t;\ntypedef int32_t msg_t;\n' > $@

test: $(SOURCES) stubs.h inc/ch.h $(wildcard $(TOP)/script/*.h $(TOP)/script/lua/*.h)
	$(CC) $(CFLAGS) $(SAN) -o $@ $(SOURCES) -lm

run: test
	./test

clean:
	rm -rf test inc
