# Linux port: run from this directory.
#   make test        panel core tests (no hardware, no libraries)
#   make lib         panel core library for the Python UI (./radio-ui)
#   make pytest      UI tests: MPD client, rendering, simulated panel
#   make dtbo        device tree overlay
#   make module      DRM driver, against KDIR (default: running kernel source)

BUILD   := build
CC      ?= gcc
CFLAGS  ?= -O2 -g
CFLAGS  += -std=gnu11 -Wall -Wextra -MMD -MP
CFLAGS  += -Idriver -Isim
KDIR    ?= /usr/src/linux

CORE     := driver/s1d13700_core.c
SIM      := sim/s1d13700_sim.c
PYTHON   ?= python3
PANEL_LIB := $(BUILD)/libradiopanel.so

.PHONY: all test asan lib pytest dtbo module clean
all: test lib

# ---- tests -----------------------------------------------------------------
$(BUILD)/test_panel: tests/test_panel.c $(CORE) $(SIM)
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -o $@ $^

$(BUILD)/test_panel_asan: tests/test_panel.c $(CORE) $(SIM)
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -fsanitize=address,undefined -fno-sanitize-recover=all -o $@ $^

test: $(BUILD)/test_panel
	$<

asan: $(BUILD)/test_panel_asan
	$<

# ---- Python UI -------------------------------------------------------------
lib: $(PANEL_LIB)

$(PANEL_LIB): radio/libpanel.c $(CORE) $(SIM)
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -fPIC -shared -o $@ $^

pytest: $(PANEL_LIB)
	$(PYTHON) -W 'ignore::DeprecationWarning:gi.events' -m unittest discover -s tests -p 'test_*.py' -v

# ---- device tree and kernel module -----------------------------------------
dtbo: $(BUILD)/radio-panel.dtbo

$(BUILD)/radio-panel.dtbo: dts/radio-panel.dts
	@mkdir -p $(dir $@)
	dtc -@ -I dts -O dtb -o $@ $<

module:
	$(MAKE) -C $(KDIR) M=$(CURDIR)/driver modules

clean:
	rm -rf $(BUILD)
	-$(MAKE) -C $(KDIR) M=$(CURDIR)/driver clean

-include $(shell find $(BUILD) -name '*.d' 2>/dev/null)
