-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefile
99 lines (77 loc) · 3.12 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
BINARY_NAME = stupid_ecs
AUTHOR = Nicholas Warfield
SOURCE_DIR = src
OBJECT_DIR = src/obj
INCLUDE_DIR = include
LIBRARY_DIR = lib
RESOURCE_DIR = resources
BUILD_DIR = build
TEST_DIR = tests
CC = ccache clang++
OPT = O2
CFLAGS = -std=c++17 -Wall -Wextra -Wno-missing-braces -DDEBUG
LDFLAGS =
CFLAGS += -I$(INCLUDE_DIR) -I$(LIBRARY_DIR)
HEADERS = $(wildcard *, $(INCLUDE_DIR)/*.hpp)
SOURCE = $(wildcard *, $(SOURCE_DIR)/*.cpp)
OBJECTS := $(patsubst %.cpp,%.o, $(notdir $(SOURCE)))
OBJECTS := $(addprefix $(OBJECT_DIR)/, $(OBJECTS))
TEST_SOURCE = $(wildcard *, $(TEST_DIR)/*.cpp)
TEST_OBJECTS := $(patsubst %.cpp,%.o, $(notdir $(TEST_SOURCE)))
TEST_OBJECTS := $(addprefix $(TEST_DIR)/obj/, $(TEST_OBJECTS))
TEST_OBJECTS += $(OBJECTS)
TEST_OBJECTS := $(filter-out $(OBJECT_DIR)/main.o, $(TEST_OBJECTS))
RUN_ARGS =
ifeq (run,$(firstword $(MAKECMDGOALS)))
RUN_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
$(eval $(RUN_ARGS):;@:)
else ifeq (test,$(firstword $(MAKECMDGOALS)))
RUN_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
$(eval $(RUN_ARGS):;@:)
else ifeq (bench,$(firstword $(MAKECMDGOALS)))
RUN_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
$(eval $(RUN_ARGS):;@:)
endif
$(OBJECT_DIR)/%.o: $(SOURCE_DIR)/%.cpp $(HEADERS)
$(CC) -c -o $@ $< $(CFLAGS) -$(OPT)
$(BUILD_DIR)/$(BINARY_NAME): init-build $(OBJECTS)
$(CC) -o $@ $(OBJECTS) $(CFLAGS) -$(OPT) $(LDFLAGS)
$(TEST_DIR)/obj/benchmark_%.o: $(TEST_DIR)/benchmark_%.cpp $(HEADERS)
$(CC) -c -o $@ $< $(CFLAGS) -O2
$(TEST_DIR)/obj/test_%.o: $(TEST_DIR)/test_%.cpp $(HEADERS)
$(CC) -c -o $@ $< $(CFLAGS) -O0 -g --coverage
$(TEST_DIR)/.test: init-tests $(TEST_OBJECTS)
$(CC) -o $@ $(TEST_OBJECTS) $(CFLAGS) -O0 -g --coverage $(LDFLAGS)
.PHONY: run clean test coverage bench init-tests init-build all
all: $(BUILD_DIR)/$(BINARY_NAME) $(TEST_DIR)/.test
run: $(BUILD_DIR)/$(BINARY_NAME)
@./$(BUILD_DIR)/$(BINARY_NAME)
clean:
@[ ! -d $(OBJECT_DIR) ] || trash $(OBJECT_DIR)
@[ ! -d $(BUILD_DIR) ] || trash $(BUILD_DIR)
@[ ! -d $(TEST_DIR)/obj ] || trash $(TEST_DIR)/obj
@[ ! -e $(TEST_DIR)/.test ] || trash $(TEST_DIR)/.test
bench: $(TEST_DIR)/.test
@unbuffer ./$(TEST_DIR)/.test '[benchmark]' $(RUN_ARGS) --benchmark-no-analysis \
| tee tests/reports/benchmark.txt
test: $(TEST_DIR)/.test
@unbuffer ./$(TEST_DIR)/.test ~'[benchmark]' $(RUN_ARGS) \
| tee tests/reports/report.txt
coverage: $(TEST_DIR)/.test
@$(MAKE) clean
@$(MAKE) test
@llvm-cov gcov -o tests/obj tests/test_*.cpp -bcfp > /dev/null
@unbuffer gcovr -g -f src -f include \
--html-details tests/reports/html/coverage.html \
--json tests/reports/coverage.json \
--txt | tee -a tests/reports/report.txt
init-tests:
@[ -d $(TEST_DIR)/obj ] || mkdir -p $(TEST_DIR)/obj
@[ -d $(TEST_DIR)/reports ] || mkdir -p $(TEST_DIR)/reports
@[ -d $(TEST_DIR)/reports/html ] || mkdir -p $(TEST_DIR)/reports/html
init-build:
@[ -d $(OBJECT_DIR) ] || mkdir -p $(OBJECT_DIR)
@[ -d $(BUILD_DIR) ] || mkdir -p $(BUILD_DIR)
@[[ !(! -d $(BUILD_DIR)/$(RESOURCE_DIR) && -d $(RESOURCE_DIR)) ]] \
|| (ln -s "$(realpath $(RESOURCE_DIR))" $(BUILD_DIR); \
echo "$(RESOURCE_DIR) linked to $(BUILD_DIR)")