# S1D13700 behind a TCA9539: panel core, simulator, DRM driver, overlay.
#   make test        panel core tests (no hardware, no libraries)
#   make asan        the same under ASan and UBSan
#   make dtbo        device tree overlay(s) from dts/*.dts
#   make module      DRM driver, against KDIR (default: /usr/src/linux)

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
DTBOS   := $(patsubst dts/%.dts,$(BUILD)/%.dtbo,$(wildcard dts/*.dts))

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

$(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
	$<

dtbo: $(DTBOS)

$(BUILD)/%.dtbo: dts/%.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 $(wildcard $(BUILD)/*.d)
