# -*- makefile -*-

ARCH = test
KEYMAP = test

ARCH_TOKEN = _Arch_$(ARCH)

TEST_DIR = ../test
UNITY_DIR = $(TEST_DIR)/unity
BUILD_DIR = ../build
UNITY_SCRIPTS = $(UNITY_DIR)/auto
ARCH_DIR = ./Arch
KEYMAPS_DIR = ./Keymaps

CC = gcc
VERSION = 
RELEASE=

CLEAN_FILES =
CLEANALL_FILES =

CFLAGS=-std=c99
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -Wpointer-arith
CFLAGS += -Wcast-align
CFLAGS += -Wwrite-strings
CFLAGS += -Wswitch-default
CFLAGS += -Wunreachable-code
CFLAGS += -Winit-self
CFLAGS += -Wmissing-field-initializers
CFLAGS += -Wno-unknown-pragmas
CFLAGS += -Wstrict-prototypes
CFLAGS += -Wundef
CFLAGS += -Wold-style-definition

SRC_FILES = main.c asdf.c asdf_actions.c asdf_modifiers.c asdf_repeat.c asdf_keymaps.c asdf_buffer.c asdf_arch.c
OBJ_FILES = $(SRC_FILES:.c=.o)


ARCH_FILES = asdf_arch.c asdf_arch.h
CLEAN_FILES += ARCH_FILES

TESTS = repeat modifiers keymaps interface keyscan virtual

TEST1 = asdf_repeat
TEST1_SRC = $(TEST_DIR)/test_$(TEST1).c
TEST1_DEPS = ./$(TEST1).c $(UNITY_DIR)/unity.c
TEST1_BUILD = $(BUILD_DIR)/test_$(TEST1)

TEST2 = asdf_modifiers
TEST2_SRC = $(TEST_DIR)/test_$(TEST2).c ./asdf_arch.c ./asdf_virtual.c ./asdf_physical.c
TEST2_DEPS = ./$(TEST2).c $(UNITY_DIR)/unity.c
TEST2_BUILD = $(BUILD_DIR)/test_$(TEST2)

TEST3 = asdf_keymaps
TEST3_SRC = $(TEST_DIR)/test_$(TEST3).c
TEST3_DEPS = ./$(TEST3).c $(UNITY_DIR)/unity.c ./asdf_physical.c \
             $(TEST_DIR)/test_asdf_lib.c ./asdf_virtual.c \
             ./asdf_hook.c ./asdf_modifiers.c ./asdf_arch.c
TEST3_BUILD = $(BUILD_DIR)/test_$(TEST3)

TEST4 = asdf_buffer
TEST4_SRC = $(TEST_DIR)/test_$(TEST4).c
TEST4_DEPS = ./$(TEST4).c $(UNITY_DIR)/unity.c
TEST4_BUILD = $(BUILD_DIR)/test_$(TEST4)

TEST5 = asdf_virtual
TEST5_SRC = $(TEST_DIR)/test_$(TEST5).c
TEST5_DEPS = ./$(TEST5).c $(UNITY_DIR)/unity.c ./asdf_physical.c ./asdf_modifiers.c \
             ./asdf_hook.c ./asdf_arch.c ./asdf_keymaps.c $(TEST_DIR)/test_asdf_lib.c
TEST5_BUILD = $(BUILD_DIR)/test_$(TEST5)

TEST7 = asdf_hook
TEST7_SRC = $(TEST_DIR)/test_$(TEST7).c ./asdf_arch.c  $(TEST_DIR)/test_asdf_lib.c
TEST7_DEPS = ./$(TEST7).c $(UNITY_DIR)/unity.c ./asdf_keymaps.c ./asdf.c \
             ./asdf_modifiers.c ./asdf_buffer.c \
             ./asdf_virtual.c ./asdf_physical.c ./asdf_repeat.c
TEST7_BUILD = $(BUILD_DIR)/test_$(TEST7)

.SUFFIXES:
.SUFFIXES: .c .o .bin .hex

%.o: %.c
	$(TARGET_CC) -c $(TARGET_CPPFLAGS) $(TARGET_CFLAGS) $<

all: test

asdf_keymap_defs.h: $(KEYMAPS_DIR)/asdf_all_keymap_defs_$(KEYMAP).h $(KEYMAPDEFS_H_TOKEN)
	cp $< $@
GENERATED_FILES += asdf_keymap_defs.h


asdf_arch.c: $(ARCH_DIR)/asdf_arch_$(ARCH).c _Arch_$(ARCH)
	cp $< $@
GENERATED_FILES += asdf_arch.c


asdf_arch.h: $(ARCH_DIR)/asdf_arch_$(ARCH).h _Arch_$(ARCH)
	cp $< $@
GENERATED_FILES += asdf_arch.h

$(ARCH_TOKEN):
	touch $(ARCH_TOKEN)

asdf_keymaps.c: asdf.h asdf_ascii.h asdf_modifiers.h asdf_arch.h asdf_keymaps.h asdf_keymap_defs.h
asdf_modifiers.c: asdf_modifiers.h asdf_arch.h asdf_keymap_defs.h
asdf_arch.h: asdf_arch.h
asdf_virtual.c: asdf_virtual.h asdf_arch.h asdf_keymap_defs.h
asdf_hook.c: asdf_hook.h asdf_keymap_defs.h asdf_arch.h

tags:   $(SRC_FILES)
	etags $(SRC_FILES)

.PHONY: test
test: test1 test2 test3 test4 test6 test5 test7

.PHONY: test1
test1: $(TEST1_BUILD)
	$(TEST1_BUILD)

.PHONY: test2
test2: $(TEST2_BUILD)
	$(TEST2_BUILD)

.PHONY: test3
test3: $(TEST3_BUILD)
	$(TEST3_BUILD)

.PHONY: test4
test4: $(TEST4_BUILD)
	$(TEST4_BUILD)

.PHONY: test5
test5: $(TEST5_BUILD)
	$(TEST5_BUILD)

.PHONY: test6
test6: $(TEST6_BUILD)
	$(TEST6_BUILD)

.PHONY: test7
test7: $(TEST7_BUILD)
	$(TEST7_BUILD)


$(TEST1_BUILD): $(TEST1_SRC) $(TEST1_DEPS)
	$(CC) -o $@ -I. -I$(TEST_DIR) -I$(UNITY_DIR) $(CFLAGS) $(TEST1_SRC) $(TEST1_DEPS)

$(TEST2_BUILD): $(TEST2_SRC) $(TEST2_DEPS)
	$(CC) -o $@ -I. -I$(TEST_DIR) -I$(UNITY_DIR) $(CFLAGS) $(TEST2_SRC) $(TEST2_DEPS)

$(TEST3_BUILD): $(TEST3_SRC) $(TEST3_DEPS)
	$(CC) -o $@ -I. -I$(TEST_DIR) -I$(UNITY_DIR) $(CFLAGS) $(TEST3_SRC) $(TEST3_DEPS)

$(TEST4_BUILD): $(TEST4_SRC) $(TEST4_DEPS)
	$(CC) -o $@ -I. -I$(TEST_DIR) -I$(UNITY_DIR) $(CFLAGS) $(TEST4_SRC) $(TEST4_DEPS)

$(TEST5_BUILD): $(TEST5_SRC) $(TEST5_DEPS)
	$(CC) -o $@ -I. -I$(TEST_DIR) -I$(UNITY_DIR) $(CFLAGS) $(TEST5_SRC) $(TEST5_DEPS)

$(TEST6_BUILD): $(TEST6_SRC) $(TEST6_DEPS)
	$(CC) -o $@ -I. -I$(TEST_DIR) -I$(UNITY_DIR) $(CFLAGS) $(TEST6_SRC) $(TEST6_DEPS)

$(TEST7_BUILD): $(TEST7_SRC) $(TEST7_DEPS)
	$(CC) -o $@ -I. -I$(TEST_DIR) -I$(UNITY_DIR) $(CFLAGS) $(TEST7_SRC) $(TEST7_DEPS)

TEST_BUILD_FILES += $(TEST1_BUILD) $(TEST2_BUILD) $(TEST3_BUILD) $(TEST4_BUILD) $(TEST5_BUILD) $(TEST6_BUILD) $(TEST7_BUILD)

CLEAN_FILES += $(TEST_BUILD_FILES) _Arch_* *.o
CLEAN_FILES += ~* *\#

CLEANALL_FILES += $(GENERATED_FILES) $(TARGET_BUILD_FILES) $(TEST_BUILD_FILES)

.PHONY: clean
clean:
	rm -f $(CLEAN_FILES)

.PHONY: cleanall
cleanall:
	rm -f $(CLEAN_FILES) $(CLEANALL_FILES)


include Makefile.deps
