# vexpress: the Express firmware's script and command layers as a Linux
# program. See README.md.
#
#   make               all four programs, below
#   make vexpress      just the LispBM one on an S3, which is what smoke needs
#   make smoke         the quick check tests/check.sh runs
#   make flood         the CAN RX queue under a flood (flood.sh)
#   make bounce        CAN replies not relayed back to their sender (bounce.sh)
#   make stored        the stored settings beside the running ones (stored.sh)
#   make busoff        CAN transmits and receives again after a bus-off (busoff.sh)
#   make pause         a pause that gives up does not leave LispBM paused (pause.sh)
#   make can2          a C6's second CAN bus decoded on its own (can2.sh)
#   make clean
#
# Four programs because the firmware is four here: a board runs LispBM or Lua,
# chosen at build time (SCRIPT_ENGINE in main/CMakeLists.txt), and commands.c
# and terminal.c differ between them; and an S3 and a P4 compile different
# extensions (a P4 has no native WiFi radio, so esp-now and the
# channel calls are not there, and it has the MIPI panels the others lack).
#
#   vexpress         LispBM, ESP32-S3     vexpress-p4      LispBM, ESP32-P4
#   vexpress-lua     Lua,    ESP32-S3     vexpress-lua-p4  Lua,    ESP32-P4
#
# All from the same firmware sources and the same platform layer.
#
# The firmware sources are compiled unchanged, as in tests/script_tcp. What
# is different is underneath them: platform/include holds the ESP-IDF and
# FreeRTOS headers they include, and platform/*.c implements that subset on
# POSIX -- threads, time, sockets, files. That is the layer an Android build
# replaces, or rather mostly keeps, since bionic is POSIX too.

MAIN    = ../../main
LBM     = $(MAIN)/lispBM
LUA_DIR = $(MAIN)/lua
BUILD   = build
GIT_BRANCH := $(shell git -C $(MAIN) rev-parse --abbrev-ref HEAD 2>/dev/null || echo unknown)
GIT_HASH   := $(shell git -C $(MAIN) rev-parse --short HEAD 2>/dev/null || echo unknown)

# gcc, not cc: the firmware uses nested functions, a GCC extension, and the
# dev shell exports CC=clang.
CC      = gcc
OPT    ?= -O1
SAN    ?=
WARN    = -Wall -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable \
          -Wno-missing-field-initializers -Wno-format-truncation -Wno-stringop-truncation \
          -Wno-address-of-packed-member -Wno-sign-compare -Wno-pointer-sign \
          -Wno-unused-parameter -Wno-format-security -Wno-discarded-qualifiers \
          -Wno-incompatible-pointer-types -Wno-int-conversion -Wno-array-bounds \
          -Wno-stringop-overflow -Wno-maybe-uninitialized -Wno-unused-label \
          -Wno-unused-const-variable -Wno-dangling-pointer -Wno-attributes

# What the firmware's CMakeLists adds for every board, and for the S3 and P4
# (Clay, the full runtime library). Both with PSRAM, which on a P4 is the
# largest LispBM heap the firmware sizes for. A package written for a C3
# board (a Dash16) runs on them as on a bigger board.
DEFS = -DCONFIG_SPIRAM=1 \
       -DHW_USE_CLAY -DFULL_RTS_LIB -DUSE_TINYGFX_FONT_ALL \
       -DLBM_USE_DYN_FUNS -DLBM_USE_DYN_MACROS -DLBM_USE_DYN_LOOPS \
       -DLBM_USE_DYN_ARRAYS -DLBM_USE_TIME_QUOTA -DLBM_USE_ERROR_LINENO \
       -DLBM_USE_MACRO_REST_ARGS -DLBM64 \
       -DHW_HEADER='"hw_vexpress.h"' -DHW_SOURCE='"hw_vexpress.c"' \
       -DVEXPRESS=1 -D_GNU_SOURCE -DVEXP_LUAPACK='"$(abspath ../../tools/luapack.py)"' -include vexp_prelude.h \
       -DGIT_BRANCH_NAME='"$(GIT_BRANCH)"' -DGIT_COMMIT_HASH='"$(GIT_HASH)"'

INCS = -Iplatform/include -Iplatform -Iboards \
       -I$(MAIN) -I$(MAIN)/config -I$(MAIN)/lua -I$(MAIN)/script \
       -I$(LBM) -I$(LBM)/include -I$(LBM)/src/extensions -I$(LBM)/platform/freertos/include \
       -I$(LBM)/utils -I$(MAIN)/display -I$(MAIN)/display/clay -I$(MAIN)/wifi \
       -I$(MAIN)/ble -I$(MAIN)/rgbled -I$(MAIN)/touch -I$(MAIN)/drivers \
       -I$(MAIN)/drivers/bme280 -I$(MAIN)/drivers/imu -I$(MAIN)/lowzip -I$(MAIN)/hwconf -I$(MAIN)/modem

CFLAGS_ALL = $(OPT) -g -std=gnu11 -fno-strict-aliasing -pthread $(WARN) $(SAN) $(DEFS) $(INCS)
LDLIBS = -pthread -lm -lz -lmbedtls -lmbedx509 -lmbedcrypto
# The VFS: paths under /sdcard reach the --sd directory (platform/vfs_host.c)
WRAP = $(addprefix -Wl$(comma)--wrap=,fopen opendir mkdir rmdir unlink remove rename stat open)
comma := ,

# --- firmware sources, compiled as they are ---------------------------------

FW_COMMON = buffer.c packet.c pkt_dec.c can_rxbuf.c crc.c commands.c log_ring.c \
	conf_custom.c terminal.c mempools.c utils.c rb.c bms.c log_comm.c \
	digital_filter.c nmea.c comm_can.c can_bridge_wire.c can_bridge.c flash_helper.c log.c \
	wifi/script_tcp.c config/confsrc.c hwconf/hw.c lowzip/lowzip.c \
	script/pkg_table.c script/script_pack.c display/color_core.c \
	display/disp_backend.c display/ttf_font.c \
	$(LBM_UTILS)

LBM_UTILS = lispBM/utils/tinygfx.c lispBM/utils/tjpgd.c

LBM_SRCS = 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/display_extensions.c \
	extensions/mutex_extensions.c extensions/runtime_extensions.c \
	extensions/lbm_dyn_lib.c extensions/ttf_extensions.c extensions/schrift.c \
	extensions/ttf_backend.c

FW_LISP = $(FW_COMMON) lispif.c lispif_events.c lbm_vesc_utils.c \
	lispif_vesc_extensions.c lbm_color_extensions.c \
	wifi/lispif_wifi_extensions.c \
	touch/lispif_touch_extensions.c display/lispif_disp_extensions.c \
	display/clay_layout.c script/lbm_pkgs.c \
	$(addprefix lispBM/src/,$(LBM_SRCS)) \
	lispBM/platform/freertos/src/platform_mutex.c \
	lispBM/platform/freertos/src/platform_timestamp.c

FW_LUA = $(FW_COMMON) script/script_lua.c script/lua_vesc_ext.c script/lua_vesc_io.c \
	script/lua_vesc_disp.c script/lua_vesc_disp_load.c script/lua_vesc_touch.c \
	script/lua_vesc_bms.c script/lua_vesc_wifi.c script/lua_vesc_tcp.c \
	script/lua_vesc_conf.c script/lua_vesc_color.c script/lua_vesc_font.c \
	script/luaif.c lispBM/src/extensions/ttf_backend.c

# --- the platform layer -----------------------------------------------------

PLATFORM = platform/freertos_posix.c platform/esp_host.c platform/nvs_host.c \
	platform/partition_host.c platform/twai_host.c platform/vbus.c \
	platform/vbus_nodes.c platform/ride.c platform/gnss_host.c \
	platform/panel_host.c platform/png_write.c platform/hw_stubs.c \
	platform/comm_tcp.c platform/vexp_main.c platform/vexp_pkg.c \
	platform/touch_host.c platform/vfs_host.c boards/boards.c

LUA_SRCS = $(wildcard $(LUA_DIR)/*.c)

HDRS = $(wildcard platform/include/*.h platform/include/*/*.h platform/*.h boards/*.h)

# One build per program: $(1) the program, $(2) LISP or LUA, $(3) S3 or P4
define VARIANT
$(1)_DIR   := $(BUILD)/$(1)
$(1)_FLAGS := -DSCRIPT_ENGINE_$(2)=1 -DCONFIG_IDF_TARGET_ESP32$(3)=1
$(1)_OBJS  := $$(addprefix $$($(1)_DIR)/fw/,$$(FW_$(2):.c=.o)) \
	$$(addprefix $$($(1)_DIR)/pl/,$$(PLATFORM:.c=.o) platform/engine_$(2).o) \
	$(if $(filter LUA,$(2)),$(BUILD)/lua_objs.a)

$(1): $$($(1)_OBJS)
	$$(CC) $$(SAN) $$(WRAP) -o $$@ $$^ $$(LDLIBS)

$$($(1)_DIR)/fw/%.o: $$(MAIN)/%.c $$(HDRS)
	@mkdir -p $$(dir $$@)
	$$(CC) $$(CFLAGS_ALL) $$($(1)_FLAGS) -c $$< -o $$@

$$($(1)_DIR)/pl/%.o: %.c $$(HDRS)
	@mkdir -p $$(dir $$@)
	$$(CC) $$(CFLAGS_ALL) -Wextra -Wno-unused-parameter -Wno-sign-compare $$($(1)_FLAGS) -c $$< -o $$@
endef

PROGRAMS = vexpress vexpress-lua vexpress-p4 vexpress-lua-p4

.PHONY: all clean smoke flood bounce stored busoff nomem logtask pause can2
all: $(PROGRAMS)

$(eval $(call VARIANT,vexpress,LISP,S3))
$(eval $(call VARIANT,vexpress-lua,LUA,S3))
$(eval $(call VARIANT,vexpress-p4,LISP,P4))
$(eval $(call VARIANT,vexpress-lua-p4,LUA,P4))
# The S3 program as a C6, for the second CAN bus a C6 has (can2.sh)
$(eval $(call VARIANT,vexpress-c6,LISP,C6))

# Vendored Lua, without our warning flags, as main/script/test builds it.
$(BUILD)/lua_objs.a: $(LUA_SRCS) $(wildcard $(LUA_DIR)/*.h)
	@mkdir -p $(BUILD)/luaobj
	@for f in $(LUA_SRCS); do \
		$(CC) $(OPT) -g -std=c11 -I$(LUA_DIR) $(SAN) -c $$f \
			-o $(BUILD)/luaobj/`basename $$f .c`.o || exit 1; \
	done
	@$(AR) rcs $@ $(BUILD)/luaobj/*.o

smoke: vexpress vexpress-p4
	./smoke.sh

flood: vexpress
	./flood.sh

bounce: vexpress
	./bounce.sh

can2: vexpress-c6
	./can2.sh

stored: vexpress
	./stored.sh

busoff: vexpress
	./busoff.sh

nomem: vexpress
	./nomem.sh

pause: vexpress
	./pause.sh

logtask: vexpress
	./logtask.sh

clean:
	rm -rf $(BUILD) $(PROGRAMS) vexpress-c6
