# Radiance build file.

# Core standard library modules.
STD := -pkg std $(patsubst %,-mod %,$(shell cat std.lib))

# Full standard library with test modules.
STD_TEST := $(STD) $(patsubst %,-mod %,$(shell cat std.lib.test))

# Source files.
STD_LIB := $(shell find lib -name '*.rad')
BIN_DIR := bin
RAD_BIN := $(BIN_DIR)/radiance.rv64.dev

# Emulator command used to invoke the self-hosted compiler.
EMU       := $(or $(RAD_EMULATOR),emulator)
EMU_FLAGS := -memory-size=385024 \
			 -data-size=348160 \
			 -stack-size=512 \
			 -count-instructions
RADIANCE  := $(EMU) $(EMU_FLAGS) -run $(RAD_BIN)

# Verify the emulator binary exists.
EMU_PATH := $(shell command -v $(EMU) 2>/dev/null)

default: emulator $(RAD_BIN)
test: emulator std-test bin-test

# Emulator command check

emulator:
ifeq ($(EMU_PATH),)
	$(error Emulator not found. Install it or set RAD_EMULATOR to its path)
endif

# Compiler build

SEED      := seed/radiance.rv64
SEED_OPTS := $(STD) -pkg radiance -mod compiler/radiance.rad -entry radiance

$(RAD_BIN): $(STD_LIB) compiler/radiance.rad | $(BIN_DIR)
	@echo "radiance $(SEED) => $@"
	@$(EMU) $(EMU_FLAGS) -run $(SEED) $(SEED_OPTS) -o $@

$(BIN_DIR):
	@mkdir -p $@

# Standard Library Tests

STD_LIB_TEST := lib/std.test.rv64

std-test: $(STD_LIB_TEST)
	@echo
	@$(EMU) $(EMU_FLAGS) -run $(STD_LIB_TEST)

$(STD_LIB_TEST): $(STD_LIB) $(RAD_BIN)
	@echo "radiance -test $(STD_TEST) -entry std -o $@"
	@$(RADIANCE) -test $(STD_TEST) -entry std -o $@

clean-std-test:
	@rm -f lib/std.test.rv64 \
		lib/std.test.rv64.debug \
		lib/std.test.rv64.s \
		lib/std.test.rv64.o \
		lib/std.test.rv64.rw.data \
		lib/std.test.rv64.ro.data

# Binary Tests

BIN_TEST_DIR := test/tests
# Only tests with `//! returns:` are compiled to binaries and executed.
BIN_TEST_EXE_SRC := $(shell grep -rl '^//! returns:' $(BIN_TEST_DIR))
BIN_TEST_RAD_EXE_SRC := $(filter %.rad,$(BIN_TEST_EXE_SRC))
BIN_TEST_RAS_EXE_SRC := $(filter %.ras,$(BIN_TEST_EXE_SRC))
BIN_TEST_RAD_ASM_SRC := $(wildcard $(BIN_TEST_RAD_EXE_SRC:.rad=.ras))
BIN_TEST_EXE_BIN := $(patsubst %.rad,%.rv64,$(BIN_TEST_RAD_EXE_SRC)) \
	$(patsubst %.ras,%.rv64,$(BIN_TEST_RAS_EXE_SRC))
BIN_RUNNER   := test/runner.rv64
BIN_TEST_RUN := test/run

bin-test: $(BIN_RUNNER) $(BIN_TEST_EXE_BIN)
	@echo
	@$(BIN_TEST_RUN)

# Runner binary: the lowering IL checker.
$(BIN_RUNNER): test/runner.rad $(STD_LIB) $(RAD_BIN)
	@echo "radiance test/runner.rad => $@"
	@$(RADIANCE) $(STD) -pkg runner -mod test/runner.rad -entry runner -o $@

# A `.rad` executable test can have a same-basename `.ras` module.
$(patsubst %.ras,%.rv64,$(BIN_TEST_RAD_ASM_SRC)): %.rv64: %.ras

# Compile each executable test to a binary.
$(BIN_TEST_DIR)/%.rv64: $(BIN_TEST_DIR)/%.rad $(RAD_BIN)
	@echo "radiance $< => $@"
	@$(RADIANCE) -pkg test -mod $< $(patsubst %,-mod %,$(wildcard $(@:.rv64=.ras))) -o $@

$(BIN_TEST_DIR)/%.rv64: $(BIN_TEST_DIR)/%.ras $(BIN_RUNNER)
	@echo "asm $< => $@"
	@$(EMU) $(EMU_FLAGS) -run $(BIN_RUNNER) -- assemble $< $@

clean-bin-test:
	@rm -f $(BIN_RUNNER) \
		$(BIN_RUNNER:.rv64=.rv64.debug) \
		$(BIN_RUNNER:.rv64=.rv64.s) \
		$(BIN_RUNNER:.rv64=.rv64.o) \
		$(BIN_RUNNER:.rv64=.rv64.rw.data) \
		$(BIN_RUNNER:.rv64=.rv64.ro.data) \
		$(BIN_TEST_EXE_BIN) \
		$(wildcard $(BIN_TEST_DIR)/*.rv64.debug) \
		$(wildcard $(BIN_TEST_DIR)/*.rv64.s) \
		$(wildcard $(BIN_TEST_DIR)/*.rv64.rw.data) \
		$(wildcard $(BIN_TEST_DIR)/*.rv64.ro.data)

seed:
	seed/update

clean-rad:
	rm -rf bin/*
	rm -f seed/radiance.rv64.s[0-9]*

clean: clean-std-test clean-bin-test clean-rad

t: test
c: clean

.PHONY: test clean default std-test bin-test seed \
	clean-std-test clean-bin-test clean-rad emulator
.SUFFIXES:
.DELETE_ON_ERROR:
.SILENT:
