RISCV_PREFIX ?= riscv64-unknown-elf-

CC := $(RISCV_PREFIX)gcc
OBJCOPY := $(RISCV_PREFIX)objcopy
OBJDUMP := $(RISCV_PREFIX)objdump
SIZE := $(RISCV_PREFIX)size
HOST_CC ?= cc

PROFILE ?= observe
ifeq ($(PROFILE),observe)
  OPTFLAGS := -O0 -fno-omit-frame-pointer -fno-inline -fno-optimize-sibling-calls
  DBGFLAGS := -g3 -ggdb
else ifeq ($(PROFILE),debug)
  OPTFLAGS := -Og -fno-omit-frame-pointer -fno-optimize-sibling-calls
  DBGFLAGS := -g3 -ggdb
else ifeq ($(PROFILE),release)
  OPTFLAGS := -O2
  DBGFLAGS :=
else
  $(error PROFILE must be observe, debug or release)
endif

RV_ENV_ROOT ?= /opt/rv-env
SUPPORT_ROOT ?= $(RV_ENV_ROOT)/vendor/Hazard3
H3_COMMON ?= $(SUPPORT_ROOT)/test/sim/common
H3_INIT ?= $(H3_COMMON)/init.S
LDSCRIPT ?= $(H3_COMMON)/link_hazard3.ld
LAB_RUNTIME_DIR ?= $(RV_ENV_ROOT)/vendor/lab-runtime
MEMOPS ?= $(LAB_RUNTIME_DIR)/memops.c

LOCAL_FREERTOS := $(abspath ../lab-rv32i-freertos-task-stack-vector/vendor/FreeRTOS-Kernel)
FREERTOS_KERNEL_PATH ?= $(if $(wildcard /opt/FreeRTOS-Kernel/include/FreeRTOS.h),/opt/FreeRTOS-Kernel,$(LOCAL_FREERTOS))
FREERTOS_PORT := $(FREERTOS_KERNEL_PATH)/portable/GCC/RISC-V
HEAP4_SOURCE := $(FREERTOS_KERNEL_PATH)/portable/MemMang/heap_4.c

BUILD_ROOT ?= build
HOST_BUILD_ROOT ?= host-build
TASKS := \
	task01_first_fit_split \
	task02_address_order_coalesce \
	task03_freertos_heap4

ARCH ?= rv32i_zicsr_zifencei
ABI ?= ilp32
COMMON_FLAGS := -std=c11 -march=$(ARCH) -mabi=$(ABI) -nostdlib -nostartfiles \
	-ffreestanding -fno-builtin -fno-stack-protector -ffunction-sections \
	-fdata-sections $(OPTFLAGS) $(DBGFLAGS) -Wall -Wextra -Wpedantic
MODEL_CPPFLAGS := -Isrc/tasks
KERNEL_CPPFLAGS := -Iinclude/freestanding -Iinclude -Isrc/tasks \
	-I$(FREERTOS_KERNEL_PATH)/include -I$(FREERTOS_PORT)
LINK_FLAGS := -Wl,--no-relax -Wl,--gc-sections -T $(LDSCRIPT)
LIBS := -lgcc
HOST_FLAGS := -std=c11 $(OPTFLAGS) $(DBGFLAGS) -Wall -Wextra -Wpedantic \
	-DHOST_PRINTF=1 -Isrc/tasks
HOST_HEAP4_FLAGS := $(HOST_FLAGS) -DHEAP4_HOST_ADAPTER=1 -Iinclude/host-shim

.PHONY: all tasks host task1 task2 task3 t1 t2 t3 \
	host-task1 host-task2 host-task3 ht1 ht2 ht3 clean check-support check-kernel \
	$(TASKS)

all: tasks
tasks: $(TASKS)
host: $(addprefix $(HOST_BUILD_ROOT)/,$(addsuffix /prog,$(TASKS)))

task1 t1: $(word 1,$(TASKS))
task2 t2: $(word 2,$(TASKS))
task3 t3: $(word 3,$(TASKS))

host-task1 ht1: $(HOST_BUILD_ROOT)/$(word 1,$(TASKS))/prog
host-task2 ht2: $(HOST_BUILD_ROOT)/$(word 2,$(TASKS))/prog
host-task3 ht3: $(HOST_BUILD_ROOT)/$(word 3,$(TASKS))/prog

check-support:
	@test -f "$(H3_INIT)" || { echo "Missing $(H3_INIT). Run inside hazard3-sim." >&2; exit 1; }
	@test -f "$(LDSCRIPT)" || { echo "Missing $(LDSCRIPT)." >&2; exit 1; }
	@test -f "$(MEMOPS)" || { echo "Missing $(MEMOPS)." >&2; exit 1; }

check-kernel:
	@test -f "$(HEAP4_SOURCE)" || { echo "Missing pinned FreeRTOS heap_4.c at $(HEAP4_SOURCE)." >&2; exit 1; }
	@grep -F 'FreeRTOS Kernel V11.3.0' "$(FREERTOS_KERNEL_PATH)/include/FreeRTOS.h" >/dev/null || { echo "Expected FreeRTOS Kernel V11.3.0." >&2; exit 1; }

define model_task_rules
$(1): $(BUILD_ROOT)/$(1)/prog.bin $(BUILD_ROOT)/$(1)/prog.elf $(BUILD_ROOT)/$(1)/prog.lst

$(BUILD_ROOT)/$(1)/prog.elf: crt0.S src/tasks/$(1).c src/tasks/heap4_model.h $(H3_INIT) $(LDSCRIPT) $(MEMOPS) | check-support
	mkdir -p $$(@D)
	$(CC) $(COMMON_FLAGS) $(MODEL_CPPFLAGS) $(LINK_FLAGS) -Wl,-Map,$(BUILD_ROOT)/$(1)/prog.map -o $$@ $(H3_INIT) crt0.S $(MEMOPS) src/tasks/$(1).c $(LIBS)
	$(SIZE) -A -x $$@

$(BUILD_ROOT)/$(1)/prog.bin: $(BUILD_ROOT)/$(1)/prog.elf
	$(OBJCOPY) -O binary $$< $$@

$(BUILD_ROOT)/$(1)/prog.lst: $(BUILD_ROOT)/$(1)/prog.elf
	$(OBJDUMP) -d -S -M no-aliases,numeric $$< > $$@

$(HOST_BUILD_ROOT)/$(1)/prog: src/tasks/$(1).c src/tasks/heap4_model.h
	mkdir -p $$(@D)
	$(HOST_CC) $(HOST_FLAGS) -o $$@ $$<
endef

$(eval $(call model_task_rules,$(word 1,$(TASKS))))
$(eval $(call model_task_rules,$(word 2,$(TASKS))))

TASK03 := $(word 3,$(TASKS))
TASK03_KERNEL_SOURCES := \
	$(FREERTOS_KERNEL_PATH)/tasks.c \
	$(FREERTOS_KERNEL_PATH)/list.c \
	$(HEAP4_SOURCE) \
	$(FREERTOS_PORT)/port.c \
	$(FREERTOS_PORT)/portASM.S

$(TASK03): $(BUILD_ROOT)/$(TASK03)/prog.bin $(BUILD_ROOT)/$(TASK03)/prog.elf $(BUILD_ROOT)/$(TASK03)/prog.lst

$(BUILD_ROOT)/$(TASK03)/prog.elf: crt0.S src/tasks/$(TASK03).c include/FreeRTOSConfig.h $(TASK03_KERNEL_SOURCES) $(H3_INIT) $(LDSCRIPT) $(MEMOPS) | check-support check-kernel
	mkdir -p $(@D)
	$(CC) $(COMMON_FLAGS) $(KERNEL_CPPFLAGS) $(LINK_FLAGS) -Wl,-Map,$(BUILD_ROOT)/$(TASK03)/prog.map -o $@ $(H3_INIT) crt0.S $(MEMOPS) $(TASK03_KERNEL_SOURCES) src/tasks/$(TASK03).c $(LIBS)
	$(SIZE) -A -x $@

$(BUILD_ROOT)/$(TASK03)/prog.bin: $(BUILD_ROOT)/$(TASK03)/prog.elf
	$(OBJCOPY) -O binary $< $@

$(BUILD_ROOT)/$(TASK03)/prog.lst: $(BUILD_ROOT)/$(TASK03)/prog.elf
	$(OBJDUMP) -d -S -M no-aliases,numeric $< > $@

$(HOST_BUILD_ROOT)/$(TASK03)/prog: src/tasks/$(TASK03).c include/host-shim/FreeRTOS.h include/host-shim/task.h $(HEAP4_SOURCE) | check-kernel
	mkdir -p $(@D)
	$(HOST_CC) $(HOST_HEAP4_FLAGS) -o $@ src/tasks/$(TASK03).c $(HEAP4_SOURCE)

clean:
	rm -rf $(BUILD_ROOT) $(HOST_BUILD_ROOT)
