-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
106 lines (82 loc) · 2.93 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# **************************************************************************** #
# COMPILER #
# **************************************************************************** #
IS_CPP ?= false
RM = rm -rf
MKDIR = mkdir -p
AS = nasm
ASFLAGS = -f macho64
CC ?= gcc
CXX ?= g++
CFLAGS ?= -Wall -Wextra -Werror -Wpedantic -Wshadow
CXXFLAGS ?= -Wall -Wextra -Werror -Wpedantic -Wshadow
COMMON_FLAGS += -I./ -I./$(RELATIVE_PATH) $(addprefix -I./, $(RELATIVE_PATH)/$(INCLUDES))
COMMON_FLAGS += -DRELATIVE_PATH="\"$(RELATIVE_PATH)\""
CFLAGS += $(COMMON_FLAGS)
CXXFLAGS += $(COMMON_FLAGS)
# **************************************************************************** #
# DIRS #
# **************************************************************************** #
OBJ_DIR = obj
TEST_DIR = tests
# **************************************************************************** #
# SOURCES #
# **************************************************************************** #
OBJ := $(addprefix $(OBJ_DIR)/, $(patsubst %.s,%.o,$(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SRC)))))
TEST_OBJ := $(addprefix $(OBJ_DIR)/, $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(TEST_SRC))))
TEST_BIN := $(addprefix $(TEST_DIR)/, $(patsubst %_test.c,%.test,$(patsubst %_test.cpp,%.test,$(TEST_SRC))))
# **************************************************************************** #
# RULES #
# **************************************************************************** #
SILENT := all
PHONY := all
all: check
SILENT += check
PHONY += check
check: $(TEST_BIN)
total=0; passs=0; fail=0; \
echo; \
for test in $^; do \
./$$test; \
exit_code=$$?; \
if [ $$exit_code -eq 0 ]; then \
pass=$$((pass+1)); \
else \
fail=$$((fail+1)); \
fi; \
total=$$((total+1)); \
done; \
echo; \
echo "=================================================="; \
echo "test summary"; \
echo "=================================================="; \
echo "# TOTAL: $$total"; \
echo "# PASS: $$pass"; \
echo "# FAIL: $$fail"; \
echo "=================================================="; \
echo;
$(TEST_DIR)/%.test: $(OBJ_DIR)/%_test.o $(filter-out %_test.o, $(OBJ))
@$(MKDIR) $(dir $@)
ifeq ($(IS_CPP), true)
$(CXX) -o $@ $^ $(CXXFLAGS) $(LDFLAGS)
else
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
endif
$(OBJ_DIR)/%.o: $(RELATIVE_PATH)/%.s
@$(MKDIR) $(dir $@)
$(AS) $(ASFLAGS) -o $@ $<
$(OBJ_DIR)/%.o: $(RELATIVE_PATH)/%.c
@$(MKDIR) $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
$(OBJ_DIR)/%.o: $(RELATIVE_PATH)/%.cpp
@$(MKDIR) $(dir $@)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
PHONY += clean
clean:
$(RM) $(OBJ_DIR)
PHONY += fclean
fclean: clean
$(RM) $(TEST_DIR)
.SECONDARY: $(OBJ) $(TEST_OBJ)
.SILENT: $(SILENT)
.PHONY: $(PHONY)