# A native library unloaded without undoing what it set must not leave the
# firmware calling into it. Builds the real lispBM/lispif_c_lib.c on the
# host against stub/ and LispBM, and loads fake libraries through it; see
# main.c.
#
#   make run                                  this tree
#   make run LIBSRC=/old/tree/lispBM/lispif_c_lib.c   another file (the
#                                             hardening's fail-before check)
#
# -m32 because the firmware stores function addresses as uint32_t, as on
# the board. Needs gcc_multi (the dev shell's gcc); check.sh skips without
# 32-bit headers. The functions the file reaches and the test does not
# provide are generated as stubs that abort when called (stubs.c), from the
# link's own list of what is missing.
TOP = ../..
LISPBM = $(TOP)/lispBM/lispBM
LIBSRC ?= $(TOP)/lispBM/lispif_c_lib.c

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

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
INC = -Istub -I$(dir $(LIBSRC)) -I$(TOP) -I$(LISPBM)/include -I$(LISPBM)/platform/linux/include
CFLAGS = -m32 -O1 -g -std=gnu99 -DNO_STM32 $(LBM_OPT) $(INC)
LIBS = -lm -lpthread

.PHONY: all run clean
all: test

stub/decls.h: gen_decls.py stub/world.h $(LIBSRC)
	python3 gen_decls.py $(LIBSRC) stub/world.h > $@

lib.o: $(LIBSRC) stub/decls.h $(wildcard stub/*.h)
	$(CC) $(CFLAGS) -Wall -c $(LIBSRC) -o lib.tmp.o
	objcopy --globalize-symbol=cif lib.tmp.o $@
	rm -f lib.tmp.o

main.o: main.c stub/decls.h $(wildcard stub/*.h)
	$(CC) $(CFLAGS) -Wall -Wextra -Wno-unused-parameter -c main.c -o $@

lbm.a: $(LBM_SRC)
	mkdir -p lbm_obj
	rm -f lbm_obj/*.o $@
	for f in $(LBM_SRC); do $(CC) $(CFLAGS) -c $$f -o lbm_obj/$$(basename $$f .c).o || exit 1; done
	ar rcs $@ lbm_obj/*.o

# What the link is missing, from the link's own errors, as aborting stubs
stubs.c: lib.o main.o lbm.a
	$(CC) -m32 main.o lib.o lbm.a $(LIBS) -o /dev/null 2>&1 \
	  | sed -n "s/.*undefined reference to \`\([A-Za-z_0-9]*\)'.*/\1/p" | sort -u \
	  | awk 'BEGIN { print "// Generated by the Makefile: do not edit"; \
	                 print "#include <stdio.h>"; print "#include <stdlib.h>"; \
	                 print "static void none(const char *n) { printf(\"FAIL: %s called\\n\", n); exit(3); }" } \
	         { printf "void %s(void) { none(\"%s\"); }\n", $$1, $$1 }' > $@

test: main.o lib.o lbm.a stubs.c
	$(CC) -m32 -O1 -c stubs.c -o stubs.o
	$(CC) -m32 main.o lib.o stubs.o lbm.a $(LIBS) -o $@

# The same built as the boards in PKG_PPM_TRIM_BOARDS are (HW_NO_PKG_PPM)
lib_trim.o: $(LIBSRC) stub/decls.h $(wildcard stub/*.h)
	$(CC) $(CFLAGS) -DHW_NO_PKG_PPM -Wall -c $(LIBSRC) -o lib_trim.tmp.o
	objcopy --globalize-symbol=cif lib_trim.tmp.o $@
	rm -f lib_trim.tmp.o

main_trim.o: main.c stub/decls.h $(wildcard stub/*.h)
	$(CC) $(CFLAGS) -DHW_NO_PKG_PPM -Wall -Wextra -Wno-unused-parameter -c main.c -o $@

test_trim: main_trim.o lib_trim.o lbm.a stubs.c
	$(CC) -m32 -O1 -c stubs.c -o stubs.o
	$(CC) -m32 main_trim.o lib_trim.o stubs.o lbm.a $(LIBS) -o $@

run: test test_trim
	timeout 60 ./test
	timeout 60 ./test_trim

clean:
	rm -f lbm_obj/*.o
	rm -f test test_trim *.o lbm.a stubs.c stub/decls.h
