-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
199 lines (171 loc) · 8.02 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Inspired by c-project-template (https://github.com/tiborsimon/c-project-template)
CONFIG_FILE := Makefile.conf
-include $(CONFIG_FILE)
UNAME := $(shell uname -s)
IS_MAC = $(filter Darwin, $(UNAME))
IS_LNX = $(filter Linux, $(UNAME))
IS_WIN = $(filter Windows_NT, $(UNAME))
ifeq ($(strip $(IS_MAC)$(IS_WIN)$(IS_LNX)),)
$(error Unrecognized platform!)
endif
#==============================================================================
# FUNCTIONS
#==============================================================================
define error_handling
@if [ -f _error_flag ]; then \
echo "$(1)$(RED)$(BOLD)Error$(RESET)"; \
cat temp_error_file; \
rm -f temp_error_file; \
rm -f _error_flag; \
rm -f *.o; \
false; \
else \
if [ -s temp_error_file ]; then \
echo "$(1)$(YELLOW)$(BOLD)Warning$(RESET)"; \
cat temp_error_file; \
else \
echo "$(1)$(GREEN)$(BOLD)Success$(RESET)"; \
fi \
fi
@rm -f temp_error_file
endef
#==============================================================================
# TARGETS
#==============================================================================
.PHONY: test_compile test clean files help compile run benchmark_compile benchmark static dynamic
help:
@echo "---------------------------------------------------------------------"
@echo " $(BOLD)$(YELLOW)$(PROJECT_NAME)$(RESET) $(BOLD)$(VERSION)$(RESET)"
@echo "---------------------------------------------------------------------"
@echo " $(BOLD)make [help]$(RESET) - Prints out this help message."
@echo " $(BOLD)make compile$(RESET) - Compiles the project."
@echo " $(BOLD)make run$(RESET) - Compiles and runs the project."
@echo " $(BOLD)make test$(RESET) - Compiles the whole test suite and runs it."
@echo " $(BOLD)make benchmark$(RESET) - Compiles the whole benchmark suite and runs it."
@echo " $(BOLD)make files$(RESET) - Prints out the files registered by make."
@echo " $(BOLD)make clean$(RESET) - Cleans up the build directory."
#==============================================================================
# COMPILE TARGETS
#==============================================================================
# Compile the whole project
compile: $(OBJ_DIR) $(BIN_DIR) $(ENTRY_OBJECT)
@cc -o $(BIN_DIR)/$(BINARY) $(ENTRY_OBJECT) $(SOURCE_OBJECTS) $(CFLAGS) $(CDEFINE) $(LDLIBS) \
2> temp_error_file; if [ $$? -ne 0 ]; then touch _error_flag; fi; true
$(call error_handling, "Compiling project... ")
# Compiling the object files
$(ENTRY_OBJECT) $(SOURCE_OBJECTS): $(ENTRY_FILE) $(SOURCE_FILES) $(HEADER_FILES)
@echo "Compiling production code.. "
@$(CC) $(CFLAGS) $(LDLIBS) $(CDEFINE) -c $^ \
2> temp_error_file; if [ $$? -ne 0 ]; then touch _error_flag; fi; true
$(call error_handling, "Compiling source code... ")
@rm -f $(INC_DIR)/*.gch
@mv $(notdir $(SOURCE_OBJECTS) $(ENTRY_OBJECT)) $(OBJ_DIR)
#==============================================================================
# LIBRARY TARGETS
#==============================================================================
# Compiling the dynamic library
dynamic: CFLAGS = $(DYN_CFLAGS)
dynamic: LDLIBS = $(DYN_LDLIBS)
dynamic: CDEFINE = $(DYN_CDEFINE)
dynamic: $(OBJ_DIR) $(BIN_DIR) $(SOURCE_OBJECTS)
@$(CC) -shared -o $(BIN_DIR)/$(DYN_BINARY) $(SOURCE_OBJECTS) $(DYN_CFLAGS) $(DYN_LDLIBS) $(DYN_CDEFINE)
# Compiling the static library
static: CFLAGS = $(STAT_CFLAGS)
static: LDLIBS = $(STAT_LDLIBS)
static: CDEFINE = $(STAT_CDEFINE)
static: $(OBJ_DIR) $(BIN_DIR) $(SOURCE_OBJECTS)
@ar rcs $(BIN_DIR)/$(STAT_BINARY) $(SOURCE_OBJECTS)
#==============================================================================
# TEST TARGETS
#==============================================================================
# Compiling the object files
$(TEST_ENTRY_OBJECT) $(TEST_SOURCE_OBJECTS): $(TEST_ENTRY_FILE) $(TEST_SOURCE_FILES)
@echo "Compiling test code.. "
@$(CC) $(TEST_CFLAGS) $(TEST_LDLIBS) $(TEST_CDEFINE) -c $^ \
2> temp_error_file; if [ $$? -ne 0 ]; then touch _error_flag; fi; true
@$(call error_handling, "Compiling test code... ")
@mv $(notdir $(TEST_SOURCE_OBJECTS) $(TEST_ENTRY_OBJECT)) $(OBJ_DIR)
test_compile: CFLAGS = $(TEST_CFLAGS)
test_compile: LDLIBS = $(TEST_LDLIBS)
test_compile: CDEFINE = $(TEST_CDEFINE)
test_compile: compile $(TEST_ENTRY_OBJECT)
@$(CC) $(TEST_ENTRY_FILE) $(SOURCE_OBJECTS) $(TEST_SOURCE_OBJECTS) -o $(BIN_DIR)/$(TEST_BINARY) $(TEST_CFLAGS) $(TEST_LDLIBS) $(TEST_CDEFINE) \
2> temp_error_file; if [ $$? -ne 0 ]; then touch _error_flag; fi; true
@$(call error_handling, "Compiling test binary... ")
test: test_compile
@echo "Testing... $(YELLOW)$(BOLD)Start$(RESET)"
@./$(BIN_DIR)/$(TEST_BINARY) \
2> temp_error_file; if [ $$? -ne 0 ]; then touch _error_flag; fi; true
$(call error_handling, "Testing... ")
#==============================================================================
# BENCHMARK TARGETS
#==============================================================================
# Compiling the benchmark files
$(BENCH_ENTRY_OBJECT) $(BENCH_SOURCE_OBJECTS): $(BENCH_ENTRY_FILE) $(BENCH_SOURCE_FILES)
@echo "Compiling benchmark code.. "
@$(CC) $(BENCH_CFLAGS) $(BENCH_LDLIBS) $(BENCH_CDEFINE) -c $^ \
2> temp_error_file; if [ $$? -ne 0 ]; then touch _error_flag; fi; true
@$(call error_handling, "Compiling benchmark code... ")
@mv $(notdir $(BENCH_SOURCE_OBJECTS) $(BENCH_ENTRY_OBJECT)) $(OBJ_DIR)
benchmark_compile: CFLAGS = $(BENCH_CFLAGS)
benchmark_compile: LDLIBS = $(BENCH_LDLIBS)
benchmark_compile: CDEFINE = $(BENCH_CDEFINE)
benchmark_compile: compile $(BENCH_ENTRY_OBJECT)
@$(CC) $(BENCH_ENTRY_FILE) $(SOURCE_OBJECTS) $(BENCH_SOURCE_OBJECTS) -o $(BIN_DIR)/$(BENCH_BINARY) $(BENCH_CFLAGS) $(BENCH_LDLIBS) $(BENCH_CDEFINE) \
2> temp_error_file; if [ $$? -ne 0 ]; then touch _error_flag; fi; true
@$(call error_handling, "Compiling benchmark binary... ")
benchmark: benchmark_compile
@echo "Benchmarking... $(YELLOW)$(BOLD)Start$(RESET)"
@./$(BIN_DIR)/$(BENCH_BINARY) \
2> temp_error_file; if [ $$? -ne 0 ]; then touch _error_flag; fi; true
$(call error_handling, "Benchmarking... ")
#==============================================================================
# UTILITY TARGETS
#==============================================================================
# Creating the build directory
$(OBJ_DIR):
@mkdir -p $@
# Creating the bin directory
$(BIN_DIR):
@mkdir -p $@
# Running the project
run: compile
@echo "Running $(BOLD)$(PROJECT_NAME)$(RESET) $(BOLD)$(VERSION)$(RESET)"
@echo "---------------------------------------------------------------------"
@$(BIN_DIR)/$(BINARY)
# Cleaning up the build directory
clean:
@echo "Cleaning up.. "
@rm -rf $(OBJ_DIR)
@rm -rf $(BIN_DIR)
@echo "$(GREEN)$(BOLD)Done$(RESET)"
# File and target listing
files:
@echo ' $(YELLOW)$(BOLD)Production files$(RESET)'
@echo ' $(BOLD)Entry$(RESET): $(ENTRY_FILE)'
@echo ' $(BOLD)Headers$(RESET): $(HEADER_FILES)'
@echo ' $(BOLD)Sources$(RESET): $(SOURCE_FILES)'
@echo ' $(YELLOW)$(BOLD)Test files$(RESET)'
@echo ' $(BOLD)Entry$(RESET): $(TEST_ENTRY_FILE)'
@echo ' $(BOLD)Sources$(RESET): $(TEST_SOURCE_FILES)'
@echo ' $(YELLOW)$(BOLD)Benchmark files$(RESET)'
@echo ' $(BOLD)Entry$(RESET): $(BENCH_ENTRY_FILE)'
@echo ' $(BOLD)Sources$(RESET): $(BENCH_SOURCE_FILES)'
@echo ' '
@echo ' $(GREEN)$(BOLD)Production objects$(RESET)'
@echo ' $(BOLD)Entry$(RESET): $(ENTRY_OBJECT)'
@echo ' $(BOLD)Objects$(RESET): $(SOURCE_OBJECTS)'
@echo ' $(GREEN)$(BOLD)Test objects$(RESET)'
@echo ' $(BOLD)Entry$(RESET): $(TEST_ENTRY_OBJECT)'
@echo ' $(BOLD)Objects$(RESET): $(TEST_SOURCE_OBJECTS)'
@echo ' $(GREEN)$(BOLD)Benchmark objects$(RESET)'
@echo ' $(BOLD)Entry$(RESET): $(BENCH_ENTRY_OBJECT)'
@echo ' $(BOLD)Objects$(RESET): $(BENCH_SOURCE_OBJECTS)'
@echo ' '
@echo ' $(BLUE)$(BOLD)Production binary$(RESET)'
@echo ' $(BOLD)Binary$(RESET): $(BIN_DIR)/$(BINARY)'
@echo ' $(BLUE)$(BOLD)Test binary$(RESET)'
@echo ' $(BOLD)Binary$(RESET): $(BIN_DIR)/$(TEST_BINARY)'
@echo ' $(BLUE)$(BOLD)Benchmark binary$(RESET)'
@echo ' $(BOLD)Binary$(RESET): $(BIN_DIR)/$(BENCH_BINARY)'
@echo ' '