include ../warnings.mk

CORE_SRC_DIR	:= ../src/core
TEST_SRC_DIR	:= .
UNITY_SRC_DIR	:= ../unity/src
CC				= gcc
# One directory per compiler: `make CC=clang` builds into ../build/clang.
BUILD_DIR		:= ../build/$(notdir $(CC))

CORE_SRCS		:= $(wildcard $(CORE_SRC_DIR)/*.c)
CORE_HDRS		:= $(wildcard $(CORE_SRC_DIR)/*.h)
TEST_SRCS		:= $(filter-out $(TEST_SRC_DIR)/fuzz_%.c,$(wildcard $(TEST_SRC_DIR)/*.c))
TEST_HDRS		:= $(wildcard $(TEST_SRC_DIR)/*.h)
UNITY_SRCS		:= $(UNITY_SRC_DIR)/unity.c
ALL_SRCS		:= $(CORE_SRCS) $(TEST_SRCS) $(UNITY_SRCS)
ALL_HDRS		:= $(CORE_HDRS) $(TEST_HDRS)
OBJS			:= $(patsubst %.c,$(BUILD_DIR)/%.o,$(notdir $(ALL_SRCS)))
DEPS			:= $(OBJS:.o=.d)
TARGET			:= $(BUILD_DIR)/test_runner

HOST_WARNINGS	= $(WARNINGS) $(if $(findstring clang,$(CC)),,$(GCC_WARNINGS))
# AddressSanitizer and UndefinedBehaviorSanitizer; the first error ends the
# run. tools/check.sh also runs MemorySanitizer, through this variable.
SANITIZE		:= -fsanitize=address,undefined -fno-sanitize-recover=all
CPPFLAGS		:= -I$(CORE_SRC_DIR) -I$(TEST_SRC_DIR) -I$(UNITY_SRC_DIR)
CFLAGS			= -std=c11 -O0 -g -MMD -MP $(HOST_WARNINGS) $(SANITIZE)
CFLAGS			+= $(EXTRA_CFLAGS)
LD				= $(CC)
LDFLAGS			= $(SANITIZE)
MKDIR			:= mkdir
MKDIR_FLAGS		:= -p


.PHONY: all clean run coverage fuzz fuzz-build

all: run

run: $(TARGET)
	./$(TARGET)

$(BUILD_DIR):
	$(MKDIR) $(MKDIR_FLAGS) $@

$(TARGET): $(OBJS) | $(BUILD_DIR)
	$(LD) $(LDFLAGS) $^ -o $@

$(BUILD_DIR)/%.o: $(CORE_SRC_DIR)/%.c $(ALL_HDRS) | $(BUILD_DIR)
	$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

$(BUILD_DIR)/%.o: $(TEST_SRC_DIR)/%.c $(ALL_HDRS) | $(BUILD_DIR)
	$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

$(BUILD_DIR)/%.o: $(UNITY_SRC_DIR)/%.c | $(BUILD_DIR)
	$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@


# Line and branch coverage of src/core/ by the unit tests (gcc + gcov), per
# file, then every line and branch not covered. Reports only: some of what
# isn't covered can't be reached (the parser's default case, for one). The
# annotated sources end up in ../build/coverage/*.gcov.
COV_DIR			:= ../build/coverage

coverage:
	$(RM) -r $(COV_DIR) && $(MKDIR) $(MKDIR_FLAGS) $(COV_DIR)
	for f in $(CORE_SRCS); do \
	  gcc $(CPPFLAGS) -std=c11 -O0 -g --coverage -c $$f -o $(COV_DIR)/$$(basename $$f .c).o || exit 1; done
	gcc $(CPPFLAGS) -std=c11 -O0 -g --coverage $(COV_DIR)/*.o $(TEST_SRCS) $(UNITY_SRCS) -o $(COV_DIR)/test_runner
	$(COV_DIR)/test_runner > /dev/null
	gcov -b -o $(COV_DIR) $(CORE_SRCS) 2>/dev/null | grep -A4 "^File '.*core/"
	mv *.gcov $(COV_DIR)/
	@awk '{ f = FILENAME; sub(/.*\//, "", f); sub(/\.gcov$$/, "", f) } \
	  /^ +[^ ]+: +[0-9]+:/ { split($$0, a, ":"); ln = a[2] + 0; if (a[1] ~ /#####/) { print f ":" ln ": not executed"; n++ } } \
	  /^branch/ && /taken 0%|never executed/ { print f ":" ln ": " $$0; n++ } \
	  END { if (!n) print "everything covered" }' $(COV_DIR)/*.c.gcov


# Coverage-guided fuzzing (clang + libFuzzer, with ASan and UBSan): every
# fuzz_*.c, FUZZ_TIME seconds each. Corpora are kept in ../build/fuzz/; a
# crash leaves its input there as fuzz_*-crash-*.
#   fuzz_keyboard  sequences of key states into keyboard_update(): the report
#                  and the keyboard's state must stay consistent with the keys
#                  that are down
FUZZ_CC			?= clang
FUZZ_TIME		?= 60
FUZZ_DIR		:= ../build/fuzz
FUZZERS			:= $(patsubst $(TEST_SRC_DIR)/%.c,%,$(wildcard $(TEST_SRC_DIR)/fuzz_*.c))
FUZZ_FLAGS		= -std=c11 -g -O1 -fsanitize=fuzzer,address,undefined -fno-sanitize-recover=all \
				  $(WARNINGS) $(CPPFLAGS) $(EXTRA_CFLAGS)

fuzz-build: $(FUZZERS:%=$(FUZZ_DIR)/%)

$(FUZZ_DIR)/fuzz_%: $(TEST_SRC_DIR)/fuzz_%.c $(CORE_SRCS) $(ALL_HDRS)
	@$(MKDIR) $(MKDIR_FLAGS) $(FUZZ_DIR)
	$(FUZZ_CC) $(FUZZ_FLAGS) $(CORE_SRCS) $< -o $@

fuzz: fuzz-build
	@for f in $(FUZZERS); do \
	  $(MKDIR) $(MKDIR_FLAGS) $(FUZZ_DIR)/corpus-$$f; \
	  echo "== $$f, $(FUZZ_TIME) s"; \
	  $(FUZZ_DIR)/$$f -max_total_time=$(FUZZ_TIME) -max_len=1024 -close_fd_mask=1 -print_final_stats=1 \
	    -artifact_prefix=$(FUZZ_DIR)/$$f- $(FUZZ_DIR)/corpus-$$f 2>&1 \
	    | grep -E 'stat::number_of_executed_units|ERROR|SUMMARY|crash|Done' || true; \
	  failed=$$(find $(FUZZ_DIR) -maxdepth 1 -name "$$f-crash-*" -o -name "$$f-leak-*" -o -name "$$f-timeout-*"); \
	  [ -z "$$failed" ] || { echo "$$failed"; echo "$$f: failing input saved"; exit 1; }; \
	done


clean:
	- $(RM) -r $(BUILD_DIR)/

-include $(DEPS)
