CC ?= cc
CPPFLAGS += -Iinclude
CFLAGS ?= -std=c99 -O2 -Wall -Wextra -Wpedantic -Werror
SOURCES = src/coffee_controller.c src/coffee_buttons.c src/coffee_protocol.c src/coffee_app.c
BUILD_DIR ?= build

.PHONY: all test diag-test link-test sanitize clean
all: test

$(BUILD_DIR)/test_controller: $(SOURCES) tests/test_controller.c include/coffee_controller.h include/coffee_buttons.h include/coffee_protocol.h include/coffee_app.h
	mkdir -p $(BUILD_DIR)
	$(CC) $(CPPFLAGS) $(CFLAGS) $(SOURCES) tests/test_controller.c -o $@

$(BUILD_DIR)/test_diag: src/coffee_diag.c tests/test_diag.c include/coffee_diag.h
	mkdir -p $(BUILD_DIR)
	$(CC) $(CPPFLAGS) $(CFLAGS) src/coffee_diag.c tests/test_diag.c -o $@

diag-test: $(BUILD_DIR)/test_diag
	"$(abspath $(BUILD_DIR))/test_diag"

$(BUILD_DIR)/test_link: $(SOURCES) src/coffee_diag.c tests/test_link.c targets/esp32c3/main/ui_protocol.c targets/esp32c3/main/ui_protocol.h
	mkdir -p $(BUILD_DIR)
	$(CC) $(CPPFLAGS) -Itargets/esp32c3/main $(CFLAGS) $(SOURCES) src/coffee_diag.c targets/esp32c3/main/ui_protocol.c tests/test_link.c -o $@

link-test: $(BUILD_DIR)/test_link
	"$(abspath $(BUILD_DIR))/test_link"

test: $(BUILD_DIR)/test_controller diag-test link-test
	"$(abspath $(BUILD_DIR))/test_controller"

sanitize:
	$(MAKE) test BUILD_DIR=build-sanitize CFLAGS='-std=c99 -O1 -g -Wall -Wextra -Wpedantic -Werror -fsanitize=address,undefined -fno-omit-frame-pointer'

clean:
	rm -rf build build-sanitize
