-
Notifications
You must be signed in to change notification settings - Fork 5
/
runtime.mk
372 lines (292 loc) · 11.7 KB
/
runtime.mk
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
ADDRESS_SANITIZER?=0 # Turn on/off ASAN
DEBUG = 0 # GDB support
DUMP_STATS = 1 # Detailed stat-dump to file
DO_PROFILE = 0 # Profiling using dedos statistics
OPTIM = 3 # -O level for gcc
# List of log levels to be printed to stderr
LOGS = \
INFO \
ERROR \
WARN \
CRITICAL \
CUSTOM \
# ADD CUSTOM LOG LEVELS HERE
# List of subdirectories under msus/ to compile
MSU_APPLICATIONS = webserver baremetal
SRC_DIR = src/
RNT_DIR = $(SRC_DIR)runtime/
MSU_DIR = $(SRC_DIR)msus/
COM_DIR = $(SRC_DIR)common/
LEG_DIR = $(SRC_DIR)legacy/
TST_DIR = test/
ITST_DIR = $(TST_DIR)/integration_tests/
MSU_DIRS = $(MSU_DIR) $(foreach APP, $(MSU_APPLICATIONS), $(MSU_DIR)$(APP)/)
SRC_DIRS = $(RNT_DIR) $(COM_DIR) $(MSU_DIRS)
TARGET=rt
MAIN=$(RNT_DIR)main.c
BLD_DIR = build/
DEP_DIR = $(BLD_DIR)depends/
OBJ_DIR = $(BLD_DIR)objs/
TST_BLD_DIR = $(BLD_DIR)test/
RES_DIR = $(TST_BLD_DIR)reults/
COV_DIR = $(TST_BLD_DIR)coverage/
LEG_BLD_DIR =$(BLD_DIR)legacy/
BLD_DIRS = $(BLD_DIR) $(DEP_DIR) $(OBJ_DIR) $(RES_DIR) $(LEG_BLD_DIR)
BLD_DIRS += $(patsubst $(SRC_DIR)%/, $(OBJ_DIR)%/, $(SRC_DIRS))
CLEANUP=rm -f
CLEAN_DIR=rm -rf
MKDIR=mkdir -p
CC:=gcc
CXX:=g++
COMPILE=$(CC) -c
COMPILE_PP=$(CXX) -c
DEPEND=$(CC) -MM -MF
DEPEND_PP=$(CXX) -MM -MF
LINK=ld -r
FINAL=$(CXX)
FINAL_TEST=$(CXX)
SELF=./runtime.mk
define upper
$(shell echo $1 | tr a-z A-Z)
endef
LOG_DEFINES=$(foreach logname, $(LOGS), -DLOG_$(logname)) \
$(foreach logname, $(NO_LOGS), -DNO_LOG_$(logname))
MSU_DEFINES=$(foreach MSU_APP, $(MSU_APPLICATIONS), -DCOMPILE_$(call upper, $(MSU_APP))_MSUS)
CFLAGS=-Wall -pthread -lssl -lrt -lcrypto -lm -O$(OPTIM) $(LOG_DEFINES) $(MSU_DEFINES)
CC_EXTRAFLAGS = --std=gnu99
# If your MSU application requires additional cflags, include them here
ifneq (,$(findstring webserver,$(MSU_APPLICATIONS)))
CFLAGS+=-lssl -lpcre
endif
ifneq (,$(findstring ndlog,$(MSU_APPLICATIONS)))
CFLAGS+=-lvdeplug
endif
ifneq (,$(findstring pico_tcp,$(MSU_APPLICATIONS)))
# Add folders to be compiled under src/legacy/ to the LEGACY_LIBS variable
LEGACY_LIBS+= picotcp
CFLAGS+=-lpcap
endif
ifeq ($(DEBUG), 1)
CFLAGS+=-ggdb
endif
ifeq ($(DATAPLANE_PROFILING),1)
CFLAGS+=-DDATAPLANE_PROFILING
endif
ifeq ($(ADDRESS_SANITIZER),1)
CFLAGS+=-fsanitize=address -fno-omit-frame-pointer
endif
ifeq ($(DUMP_STATS), 1)
CFLAGS+=-DDUMP_STATS=1
endif
ifeq ($(DO_PROFILE), 1)
CFLAGS+=-DDEDOS_PROFILER
endif
ifneq (,$(findstring ndlog,$(MSU_APPLICATIONS)))
CFLAGS+=-lgmp
endif
# Legacy code to be built
LEG_MAKE=$(foreach LEG_LIB, $(LEGACY_LIBS), $(LEG_DIR)$(LEG_LIB)/Makefile)
LEG_INC=$(foreach LEG_LIB, $(LEGACY_LIBS), $(LEG_DIR)$(LEG_LIB)/build/include)
LEG_SRC=$(foreach LEG_LIB, $(LEGACY_LIBS), $(wildcard $(LEG_DIR)$(LEG_LIB)/src/*))
LEG_OBJ=$(foreach LEG_LIB, $(LEGACY_LIBS), $(LEG_BLD_DIR)$(LEG_LIB).o)
# Resources to be copied from test/ to build/
RESOURCE_EXTS=.txt .json
# Directories for unit tests
UTST_DIRS = $(patsubst $(SRC_DIR)%/, $(TST_DIR)%/, $(SRC_DIRS))
# Test directories, including integration tests
TST_DIRS = $(UTST_DIRS) $(foreach APP, $(MSU_APPLICATIONS), $(ITST_DIR)$(APP)/)
# Unit tests, Tests, and associated resources and objects
UTSTS = $(foreach TST_D, $(UTST_DIRS), $(wildcard $(TST_D)*.c))
TSTS = $(foreach TST_D, $(TST_DIRS), $(wildcard $(TST_D)*.c))
TST_MKS = $(foreach TST_D, $(TST_DIRS), $(wildcard $(TST_D)*.mk))
TST_RSCS = $(foreach TST_D, $(TST_DIRS), $(foreach EXT, $(RESOURCE_EXTS), $(wildcard $(TST_D)*$(EXT))))
TST_OBJS = $(patsubst $(TST_DIR)%.c, $(TST_BLD_DIR)%.o, $(TSTS))
# C/C++ sources
SRCS = $(foreach src_dir, $(SRC_DIRS), $(wildcard $(src_dir)*.c))
SRCS_PP = $(foreach src_dir, $(SRC_DIRS), $(wildcard $(src_dir)*.cc))
# Test executables and coverage files
TST_BLDS = $(patsubst $(TST_DIR)%.c, $(TST_BLD_DIR)%.out, $(TSTS))
TST_COV = $(patsubst $(TST_DIR)%.c, $(TST_BLD_DIR)%.gcda, $(TSTS)) \
$(patsubst $(TST_DIR)%.c, $(TST_BLD_DIR)%.gcno, $(TSTS))
TST_COV_INFOS = $(patsubst $(TST_DIR)%.c, $(TST_BLD_DIR)%.gcno, $(TSTS))
# Coverage information
COV_DIRS = $(sort $(dir $(patsubst $(TST_DIR)%/, $(COV_DIR)%, $(TST_DIRS)) $(COV_DIR)))
COV_INFOS = $(patsubst $(TST_DIR)%/, $(COV_DIR)%.info, $(TST_DIRS))
COV_INIT_INFOS = $(patsubst $(TST_DIR)%/, $(COV_DIR)%.init_info, $(TST_DIRS))
COV_INDEX = $(COV_DIR)index.html
# Test output
RESULTS = $(patsubst $(TST_DIR)%.c, $(RES_DIR)%.txt, $(TSTS))
UNIT_RESULTS = $(patsubst $(TST_DIR)%.c, $(RES_DIR)%.txt, $(UTSTS))
MEM_RESULTS = $(patsubst $(TST_DIR)%.c, $(RES_DIR)%_memcheck.txt, $(TSTS))
TST_BLD_RSCS = $(patsubst $(TST_DIR)%, $(TST_BLD_DIR)%, $(TST_RSCS))
# Build dependency files for automatic change detection
DEP_DIRS = $(patsubst $(TST_DIR)%/, $(DEP_DIR)%/, $(TST_DIRS))
DEP_TST = $(patsubst $(TST_DIR)%.c, $(DEP_DIR)%.d, $(TSTS))
DEP_SRC = $(patsubst $(SRC_DIR)%.c, $(DEP_DIR)%.d, $(SRCS)) \
$(patsubst $(SRC_DIR)%.cc, $(DEP_DIR)%.d, $(SRCS_PP))
# Object files with/without the main executable
OBJECTS = $(patsubst $(SRC_DIR)%.c, $(OBJ_DIR)%.o, $(SRCS)) \
$(patsubst $(SRC_DIR)%.cc, $(OBJ_DIR)%.o, $(SRCS_PP))
OBJECTS_NOMAIN = $(patsubst $(SRC_DIR)%.c, $(OBJ_DIR)%.o, $(filter-out $(MAIN), $(SRCS))) \
$(patsubst $(SRC_DIR)%.cc, $(OBJ_DIR)%.o, $(SRCS_PP))
TST_BLD_DIRS = $(patsubst $(TST_DIR)%/, $(TST_BLD_DIR)%/, $(TST_DIRS))
RES_DIRS = $(patsubst $(TST_DIR)%/, $(RES_DIR)%/, $(TST_DIRS))
# Include folders (-I)
INCS=$(LEG_INC) $(RNT_DIR) $(COM_DIR) $(MSU_DIR)
CFLAGS+= $(foreach inc, $(INCS), -I$(inc))
# Gets the corresponding dependency variable for testing
define test_dep_name
$(notdir $(subst Test_,,$1))_DEPS
endef
# Filters out files included in the test from those included in the build
define test_filters
$(subst Test_,, $(patsubst $(TST_BLD_DIR)%, $(OBJ_DIR)%.o, $1)) \
$(foreach dep, $($(call test_dep_name, $1)), $(OBJ_DIR)$(dep:.c=.o))
endef
# Flags specifically for testing
TEST_CFLAGS= $(CFLAGS) $(CC_EXTRAFLAGS) -I$(TST_DIR) -O0 `pkg-config --libs check`
ifneq ($(MAKECMDGOALS),)
# If goals are present, and they're for coverage
ifeq ($(MAKECMDGOALS),$(filter $(MAKECMDGOALS),coverage init_cov cov-site cov))
CFLAGS+= -fprofile-arcs -ftest-coverage --coverage
OPTIM=0
INIT_COV=init_cov
TEST_CFLAGS+=-fprofile-arcs -ftest-coverage --coverage
endif
endif
CCFLAGS=$(CFLAGS) $(CC_EXTRAFLAGS)
CPPFLAGS=$(CFLAGS) $(CPP_EXTRAFLAGS)
# Build directories to create
DIRS = $(BLD_DIRS) $(OBJ_DIRS) $(DEP_DIRS) $(TST_BLD_DIRS) $(RES_DIRS) $(COV_DIRS)
all: dirs legacy ${TARGET}
# Makes the build directories
dirs: $(DIRS)
echo $(MAKECMDGOALS)
# Makes legacy code
legacy: $(LEG_OBJ)
# Builds the dependency files for change detection
depends: $(DEP_DIRS) ${DEP_SRC}
# Makes the coverage files without the html
coverage: $(DIRS) $(OBJECTS) $(COV_INIT_INFOS) test $(TST_COV) $(COV_INFOS)
# Makes coverage files including html
cov: coverage
genhtml --show-details -o $(COV_DIR) $(shell find $(COV_DIR) -name '*.info' ! -empty)
mv $(COV_DIR) coverage
# Makes coverage files, html, then starts up webserver
cov-site: coverage
genhtml --show-details -o $(COV_DIR) $(shell find $(COV_DIR) -name '*.info' ! -empty)
cd $(COV_DIR) && python2 -m SimpleHTTPServer 8081
# Coverage intermediate file
$(TST_BLD_DIR)%.gcda: $(TST_BLD_DIR)%.out $(DIRS)
init_cov: $(DIRS) $(OBJECTS) $(COV_INIT_INFOS)
lcov: $(DIRS) $(TST_COV) $(COV_INFOS)
$(COV_DIR)%.init_info: $(TST_BLD_DIR)%/
-lcov --no-recursion --directory $(patsubst $(TST_BLD_DIR)%,$(OBJ_DIR)%,$<) --zerocounters
-lcov --no-recursion --directory $(patsubst $(TST_BLD_DIR)%,$(OBJ_DIR)%,$<) --capture --initial --output-file $@ --test-name $(notdir $(subst .init_info,,$@));
$(COV_DIR)%.info: $(TST_BLD_DIR)%/ test-results
-lcov --external --directory $< --capture --no-recursion --output-file $(subst .info,.raw_info,$@) --test-name $(notdir $(subst .info,,$@))
-@if [ -e $(subst .info,.init_info,$@) ]; then \
lcov --external -a $(subst .info,.init_info,$@) -a $(subst .info,.raw_info,$@) -o $(subst .info,.all_info,$@) --test-name $(notdir $(subst .info,,$@)); \
else \
lcov --external -a $(subst .info,.raw_info,$@) -o $(subst .info,.all_info,$@) --test-name $(notdir $(subst .info,,$@)); \
fi
-lcov --remove $(subst .info,.all_info,$@) '/test/*' '/usr/*' 'src/legacy/*' -o $@ --test-name $(notdir $(subst .info,,$@))
# Builds a single legacy object for each legacy directory
$(LEG_OBJ): $(LEG_BLD_DIR)
@echo ___________ $< ___________
@filename=$$(basename "$@"); filename="$${filename%.*}"; echo $$filename; cd $(LEG_DIR)/$$filename && make;
$(LINK) -o $@ src/legacy/$(patsubst %.o,%,$(notdir $(LEG_OBJ)))/build/*.o
# Final build of the runtime target
$(TARGET): ${OBJECTS} ${LEG_OBJ}
$(FINAL) -o $@ $^ $(CFLAGS)
# Run only unit tests
unit: all ${INIT_COV} test-blds utest-results
# Run all tests
test: all ${INIT_COV} test-blds test-results
# Run all tests, check for memory leaks (VERY SLOW!)
memcheck: test $(MEM_RESULTS)
@export CK_DEFAULT_TIMEOUT=30; \
for FILE in $(filter-out test, $^); do \
echo ___ $$FILE ___ ; \
if grep -q "definitely lost: [^0]" "$$FILE"; then \
grep --color=never '\(\*\*\*\*\*.*\*\*\*\*\)\|\(definitely lost: [^0]\)' "$$FILE" | sed s/==/`printf "\\033[032m"==`/g ;\
tput sgr0; \
else \
echo "Nothing definitely lost!"; \
fi \
done;
test-blds: $(TST_OBJS) $(TST_BLDS) $(TST_BLD_RSCS)
define show_results
@echo "-----------------------\nTEST OUTPUT:\n-----------------------"
@for FILE in $1; do \
if grep -q ":[FES]:" "$$FILE"; then \
echo ___ $$FILE ___ ; \
cat $$FILE; \
elif grep -q "Aborted" "$$FILE"; then \
echo ___ $$FILE ___ ; \
cat $$FILE; \
else \
echo ___ $$FILE ___ ; \
tail -1 $$FILE; \
fi \
done
@echo "-----------------------\nFAILURES:\n-----------------------"
@-grep -s ":F:" $^; echo "";
@echo "-----------------------\nERRORS:\n-----------------------"
@-grep -s ":E:" $^; echo "";
@echo "\nDONE"
@if grep -q ":[FE]:" $1; then \
false;\
fi
endef
utest-results: all test-blds $(UNIT_RESULTS)
$(call show_results, $(filter-out all test-blds, $^))
test-results: all test-blds $(RESULTS)
$(call show_results, $(filter-out all test-blds, $^))
$(RES_DIR)%_memcheck.txt: $(TST_BLD_DIR)%.out
-valgrind --track-origins=yes --leak-check=full $< > $@ 2>&1
# Output the results of the tests by executing each of the builds
# of the tests. Output STDOUT and STDERR to the name of the rule
$(RES_DIR)%.txt: $(TST_BLD_DIR)%.out FORCE
@killall $(notdir $<) 2> /dev/null || true
-./$< > $@ 2>&1
$(TST_BLD_DIR)%.o:: $(TST_DIR)%.c $(SELF)
$(COMPILE) $(TEST_CFLAGS) $< -o $@
# creates the test executables by linking the test objects with the build objects excluding
# the specific source under test
$(TST_BLD_DIR)%.out:: $(TST_BLD_DIR)%.o $(OBJECTS_NOMAIN) $(LEG_OBJ)
$(FINAL_TEST) -o $@ $(filter-out $(call test_filters, $(@:.out=)), $^) $(TEST_CFLAGS)
# $(FINAL) -o $@ $(filter-out $(subst Test_,, $(patsubst $(TST_BLD_DIR)%, $(OBJ_DIR)%.o, $@)), $^) $(TEST_CFLAGS)
$(TST_BLD_DIR)%: $(TST_DIR)% FORCE
-@cp $< $@
# Creates object files from the source file
$(OBJ_DIR)%.o:: $(SRC_DIR)%.c $(SELF)
$(COMPILE) $(CCFLAGS) $< -o $@
$(OBJ_DIR)%.o:: $(SRC_DIR)%.cc $(SELF)
$(COMPILE_PP) $(CPPFLAGS) $< -o $@
$(DEP_SRC): $(DEP_DIRS)
$(DEP_DIR)%.d:: $(SRC_DIR)%.cc $(LEG_OBJ)
@$(DEPEND) $@ -MT $(patsubst $(DEP_DIR)%.d, $(OBJ_DIR)%.o, $@) $(CPPFLAGS) $<
$(DEP_DIR)%.d:: $(SRC_DIR)%.c $(LEG_OBJ)
@$(DEPEND) $@ -MT $(patsubst $(DEP_DIR)%.d, $(OBJ_DIR)%.o, $@) $(TEST_CFLAGS) $<
$(DEP_DIR)%.d:: $(TST_DIR)%.c $(LEG_OBJ)
@$(DEPEND) $@ -MT $(patsubst $(DEP_DIR)%.d, $(TST_BLD_DIR)%.o, $@) $(TEST_CFLAGS) $<
$(DIRS)::
@$(MKDIR) $@
clean:
$(CLEAN_DIR) $(BLD_DIR)
$(foreach leg_lib, $(LEG_LIBS), $(CLEAN_DIR) $(PATH_SRC_LEG)$(leg_lib)/build)
$(CLEANUP) $(TARGET)
ifneq ($(MAKECMDGOALS), clean)
-include $(DEP_TST)
-include $(DEP_SRC)
-include $(TST_MKS)
endif
.PHONY: all
.PHONY: clean
.PHONY: test
.PHONY: legacy
.PHONY: depends
.PHONY: coverage
.PRECIOUS: $(DEP_DIR)%.d $(RES_DIR)%.txt $(OBJ_DIR)%.o $(TST_BLD_DIR)%.out
FORCE:;