# Host test harness Makefile — puzzle_binding parser
UNITY_DIR ?= $(HOME)/esp/esp-idf/components/unity/unity/src
CJSON_DIR ?= $(HOME)/esp/esp-idf/components/json/cJSON
CC        ?= cc

COMPONENT_DIR := ../..
TEST_DIR      := ..
BUILD_DIR     := build

TEST_BIN := $(BUILD_DIR)/test_runner

SRCS := \
	$(COMPONENT_DIR)/puzzle_binding.c \
	$(CJSON_DIR)/cJSON.c \
	$(UNITY_DIR)/unity.c \
	../../../local_puzzles/test/host/host_runner.c

TEST_SRCS := \
	$(TEST_DIR)/test_puzzle_binding.c

ALL_SRCS := $(SRCS) $(TEST_SRCS)

INCS := \
	-I$(COMPONENT_DIR)/include \
	-I$(COMPONENT_DIR) \
	-Iesp_err_shim \
	-I$(TEST_DIR)/host \
	-I../../../local_puzzles/test/host \
	-I$(CJSON_DIR) \
	-I$(UNITY_DIR)

CFLAGS := -std=c11 -Wall -Wextra -Werror $(INCS) -DUNITY_INCLUDE_CONFIG_H

TEST_CFLAGS := $(CFLAGS) -include ../../../local_puzzles/test/host/unity_test_case.h

.PHONY: all test clean

all: $(TEST_BIN)

$(BUILD_DIR):
	mkdir -p $(BUILD_DIR)
	$(if $(wildcard $(UNITY_DIR)/unity.c),,$(error UNITY_DIR not found: $(UNITY_DIR) — set UNITY_DIR=...))
	$(if $(wildcard $(CJSON_DIR)/cJSON.c),,$(error CJSON_DIR not found: $(CJSON_DIR) — set CJSON_DIR=...))

$(TEST_BIN): $(ALL_SRCS) | $(BUILD_DIR)
	$(if $(wildcard $(UNITY_DIR)/unity.c),,$(error UNITY_DIR not found: $(UNITY_DIR) — set UNITY_DIR=...))
	$(if $(wildcard $(CJSON_DIR)/cJSON.c),,$(error CJSON_DIR not found: $(CJSON_DIR) — set CJSON_DIR=...))
	$(CC) $(TEST_CFLAGS) -c -o $(BUILD_DIR)/test_puzzle_binding.o $(TEST_DIR)/test_puzzle_binding.c
	$(CC) $(CFLAGS) -o $@ $(SRCS) $(BUILD_DIR)/test_puzzle_binding.o

test: $(TEST_BIN)
	./$(TEST_BIN)

clean:
	rm -rf $(BUILD_DIR)

.PRECIOUS: $(TEST_BIN)
