# Room left in the LispBM extension table for packages.
#
#   check.py  counts the firmware's registrations (the board with the most)
#             against EXTENSION_STORAGE_SIZE and wants RESERVE slots left;
#   main.c    builds the real lispBM/lispif.c and LispBM reader on the host,
#             registers that many extensions, and loads a package that reads
#             an `ext-` symbol in its constant section, as dash_esc 2.8 does.
#
# -m32 and the stand-in headers as tests/lispif_stats (gcc_multi).
TOP = ../..
LISPBM = $(TOP)/lispBM/lispBM
STUBS = ../lispif_stats

LBM_SRC = $(addprefix $(LISPBM)/src/, env.c fundamental.c heap.c lbm_memory.c \
          print.c stack.c symrepr.c tokpar.c extensions.c lispbm.c eval_cps.c \
          lbm_channel.c lbm_c_interop.c lbm_custom_type.c lbm_flat_value.c \
          lbm_prof.c lbm_defrag_mem.c lbm_image.c \
          extensions/array_extensions.c extensions/math_extensions.c \
          extensions/string_extensions.c extensions/mutex_extensions.c \
          extensions/lbm_dyn_lib.c) \
          $(LISPBM)/platform/linux/src/platform_mutex.c \
          $(LISPBM)/platform/linux/src/platform_timestamp.c

SOURCES = main.c $(TOP)/lispBM/lispif.c $(TOP)/util/buffer.c $(LBM_SRC) \
          $(TOP)/script/lbm_pkgs.c $(TOP)/script/pkg_table.c $(TOP)/script/script_pack.c

FW_EXTENSIONS := $(shell python3 check.py --need)

CC = gcc
LBM_OPT = -DLBM_USE_DYN_FUNS -DLBM_USE_DYN_MACROS -DLBM_USE_DYN_LOOPS \
          -DLBM_USE_TIME_QUOTA -DLBM_USE_ERROR_LINENO -DLBM_USE_MACRO_REST_ARGS
CFLAGS = -m32 -O1 -g -std=gnu99 -DNO_STM32 $(LBM_OPT) -DFW_EXTENSIONS=$(FW_EXTENSIONS) \
         -I$(STUBS) -I$(TOP)/lispBM -I$(LISPBM) -I$(LISPBM)/include \
         -I$(LISPBM)/platform/linux/include -I$(TOP) -I$(TOP)/util -I$(TOP)/script
LIBS = -lm -lpthread

TARGET = test

.PHONY: default all clean run
default: $(TARGET)
all: default

$(TARGET): $(SOURCES) check.py $(TOP)/lispBM/lispif_vesc_extensions.c
	$(CC) $(CFLAGS) $(SOURCES) $(LIBS) -o $@

clean:
	rm -f $(TARGET)

run: $(TARGET)
	python3 check.py
	timeout 60 ./$(TARGET)
