-
Notifications
You must be signed in to change notification settings - Fork 20
/
Makefile
306 lines (249 loc) · 9.78 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
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
# Makefile
#
# A flexible Makefile for C++ programs, including testing, debugging, leak
# checking, profiling, and coverage.
#
# Andrew DeOrio <[email protected]>
#
# For documentaiton, see https://github.com/awdeorio/supermakefilecxx
# Top level executable (should correspond to a cpp file with the same name)
EXECUTABLE := \
csvstream_test \
example1 \
example2 \
example3 \
example4
# Default target, this is what happens when you just type "make"
all : $(EXECUTABLE)
# Compiler
# You can override variables set with "?=" by using an environment variable
CXX ?= g++
# Linker
LD := $(CXX)
# Compiler flags
# -std=c++11 C++ 11 standard
# -pedantic Strict ISO C++
# -Wall Enable many warning flags
# -Wextra Enable more warning flags
# -Wconversion Warn for implicit conversions that may alter a value
# -Wsign-conversion Warn for implicit conversions that may change the sign of
# an integer value
# -Werror Make all warnings into errors
# -O3 Optimization
# -DNDEBUG Disable assert statements (like #define NDEBUG)
# -c Don't run linker
CXXFLAGS := -std=c++11 -pedantic -Wall -Wextra -Werror
CXXFLAGS += -Wconversion -Wsign-conversion
CXXFLAGS += -O3 -DNDEBUG
CXXFLAGS += -c
# Linker flags
# Include libraries here, if needed. For example, -lm
LDFLAGS :=
# Other tools
GCOV ?= gcov --relative-only
GPROF ?= gprof
VALGRIND ?= valgrind --leak-check=full
# Your tests
UNIT_TEST_SOURCES := $(wildcard *test*.cpp)
SYSTEM_TEST_INPUT_FILES := $(wildcard *.in)
CUSTOM_TEST_SCRIPT_FILES := $(wildcard *test*.sh)
################################################################################
# Regression test
# List of custom test script and output files
CUSTOM_TEST_OUTPUT_FILES := $(CUSTOM_TEST_SCRIPT_FILES:%.sh=%.out)
CUSTOM_TEST_PASSED_FILES := $(CUSTOM_TEST_SCRIPT_FILES:%.sh=%.passed)
CUSTOM_TEST := $(CUSTOM_TEST_PASSED_FILES)
# List of unit test .cpp files, output files and executables
UNIT_TEST_OUTPUT_FILES := $(UNIT_TEST_SOURCES:%.cpp=%.out)
UNIT_TEST_EXECUTABLES := $(UNIT_TEST_SOURCES:%.cpp=%)
UNIT_TEST_PASSED_FILES := $(UNIT_TEST_SOURCES:%.cpp=%.passed)
UNIT_TEST_VALGRIND_FILES := $(UNIT_TEST_SOURCES:%.cpp=%.valgrind.out)
UNIT_TEST_NOLEAK_FILES := $(UNIT_TEST_SOURCES:%.cpp=%.noleak)
UNIT_TEST := $(UNIT_TEST_PASSED_FILES)
# List of system test input and output files
SYSTEM_TEST_OUTPUT_FILES := $(SYSTEM_TEST_INPUT_FILES:%.in=%.out)
SYSTEM_TEST_PASSED_FILES := $(SYSTEM_TEST_INPUT_FILES:%.in=%.passed)
SYSTEM_TEST_VALGRIND_FILES := $(SYSTEM_TEST_INPUT_FILES:%.in=%.valgrind.out)
SYSTEM_TEST_NOLEAK_FILES := $(SYSTEM_TEST_INPUT_FILES:%.in=%.noleak)
SYSTEM_TEST := $(SYSTEM_TEST_PASSED_FILES)
# System test passed files depend on system test output files
$(SYSTEM_TEST_PASSED_FILES) : $(SYSTEM_TEST_OUTPUT_FILES)
# Run unit tests
# NOTE: We need to call make again because the UNIT_TEST variable
# may have changed. For example, this occurs with "make valgrind unittest". If
# the UNIT_TEST variable *did not* change, we could do it this way:
# unittest : CXXFLAGS := $(filter-out -DNDEBUG, $(CXXFLAGS))
# unittest : $(UNIT_TEST)
# unittest : $(filter-out unittest, $(MAKECMDGOALS))
#
# NOTE: We need to filter out -DNDEBUG to avoid asserts being disabled
unittest :
$(MAKE) CXXFLAGS="$(filter-out -DNDEBUG, $(CXXFLAGS))" $(filter-out test unittest, $(MAKECMDGOALS)) $(UNIT_TEST)
# Run system tests
systemtest :
$(MAKE) CXXFLAGS="$(filter-out -DNDEBUG, $(CXXFLAGS))" $(filter-out test systemtest, $(MAKECMDGOALS)) $(SYSTEM_TEST)
# Run custom tests
customtest : CXXFLAGS := $(filter-out -DNDEBUG, $(CXXFLAGS))
customtest : $(CUSTOM_TEST)
# Run regression test
test :
$(MAKE) CXXFLAGS="$(filter-out -DNDEBUG, $(CXXFLAGS))" \
$(filter-out test unittest systemtest customtest, $(MAKECMDGOALS)) \
$(UNIT_TEST) $(SYSTEM_TEST) $(CUSTOM_TEST)
# Run one custom test
%.out %.passed : %.sh $(EXECUTABLE)
sh $< > $*.out 2>&1
touch $*.passed
# Run one unit test
%.out %.passed : %
./$< > $*.out 2>&1
touch $*.passed
# Run one unit test with valgrind
# NOTE: "2>&1" means "redirect stderr to stdout"
%.valgrind.out : %
$(VALGRIND) ./$< > $*.valgrind.out 2>&1
# Run one system test and save output
%.out : %.in $(EXECUTABLE)
./$(EXECUTABLE) < $*.in > $*.out 2>&1
# Run one system test with valgrind and save output
%.valgrind.out : %.in $(EXECUTABLE)
$(VALGRIND) ./$(EXECUTABLE) < $*.in > $*.valgrind.out 2>&1
# Compare the output of one system test
%.passed %.diff.txt : %.out %.out.correct
@echo "sdiff $*.out $*.out.correct" > $*.diff.txt
sdiff $*.out $*.out.correct >> $*.diff.txt 2>&1
touch $*.passed
# Helpful error message for system tests
%.passed :
@echo "Error running system test $*. Did you forget to write $*.in or $*.out.correct ?"
@false
################################################################################
# Debugging
# This code does three things:
# 1. Filter out production flags
# 2. Add debug flags
# 3. Continue doing whatever make was doing
debug : CXXFLAGS := $(filter-out -DNDEBUG -O1 -O2 -O3, $(CXXFLAGS))
debug : CXXFLAGS += -O0 -ggdb3 -DDEBUG
debug : $(filter-out debug, $(MAKECMDGOALS))
################################################################################
# Leak detection
valgrind : debug
valgrind : SYSTEM_TEST += $(SYSTEM_TEST_NOLEAK_FILES)
valgrind : UNIT_TEST += $(UNIT_TEST_NOLEAK_FILES)
valgrind : $(filter-out valgrind, $(MAKECMDGOALS))
# Check valgrind output for leaks
# NOTE: see the Regression Test for targets that run unit and system tests
%.noleak : %.valgrind.out
grep -q 'ERROR SUMMARY: 0 errors' $*.valgrind.out
touch $*.noleak
################################################################################
# Profiling
# Set profiling flags and build executable
profile : CXXFLAGS += -pg
profile : LDFLAGS += -pg
profile : $(filter-out profile, $(MAKECMDGOALS))
profile : analysis.txt
# Run profiling analysis
analysis.txt :
@[ -f gmon.out ] || echo "Error: can't find gmon.out. Did you run a test execution? For example, $ make profile test00.out"
@[ -f gmon.out ] || false
$(GPROF) $(EXECUTABLE) gmon.out > analysis.txt
################################################################################
# Coverage
# This code does four things
# 1. Add coverage flags for compiler
# 2. Add coverage flags for linker
# 3. Continue executing the other make targets
# 4. Run gcov
# NOTE: this *must* be called as a recursive call to Make because step 3
# will generate new and unknown .gcda (dynamic coverage) files. Thus,
# the GCDA_FILES variable will be out of date by the time we need it.
#
# NOTE about file formats:
# .gcno: static information about a .cpp file
# .gcda: dynamic information about an execution of a compiled .cpp file
# Running an executable several times will append to this file
# .cpp.gcno: coverage report generated by gcov using .gcno and .gcda files
coverage : CXXFLAGS += --coverage
coverage : LDFLAGS += --coverage
coverage : $(filter-out coverage, $(MAKECMDGOALS))
coverage :
$(MAKE) run_gcov
# Run gcov on all files that have dynamic analysis files
GCDA_FILES := $(wildcard *.gcda)
GCOV_FILES := $(GCDA_FILES:%.gcda=%.cpp.gcov)
run_gcov : $(GCOV_FILES)
# Run gcov on one .cpp file
%.cpp.gcov : %.cpp %.gcno %.gcda
$(GCOV) $<
# Helpful error message
%.gcov :
@echo "Error running gcov. Be sure to specify a target, like main.out"
@echo "You could also check files .out and .cpp ."
@false
################################################################################
# Compiling and linking
# Dependencies for compiling each object
#
# Best solution: Generate dependencies with "g++ -MM *.cpp", and copy-paste here
# Binary_tree_test.o: Binary_tree_test.cpp Binary_tree.h
# Set_test.o: Set_test.cpp Set.h Binary_tree.h
# main.o: main.cpp
#
# HACK: we'll use a rule with more dependencies than we necessary. All
# objects depend on all header files
ALL_SOURCES := $(wildcard *.cpp)
ALL_OBJECTS := $(ALL_SOURCES:%.cpp=%.o)
ALL_HEADERS := $(wildcard *.h)
$(ALL_OBJECTS) : $(ALL_HEADERS)
# Dependencies for linking each executable
#
# Best solution: write one rule for every .cpp file containing a main()
# function. This is usually a top-level program, and several unit tests. For
# example:
# Binary_tree_test: Binary_tree_test.o
# Set_test: Set_test.o
# main: main.o
#
# HACK: we'll use a rule with more dependencies than we necessary. All
# executables depend on all support objects. Support objects are .o files
# compiled from .cpp files that are not top level executables or unit tests.
# For example, a util.cpp file.
ALL_EXECUTABLES := $(EXECUTABLE) $(UNIT_TEST_EXECUTABLES)
SUPPORT_SOURCES := $(filter-out $(UNIT_TEST_SOURCES) $(EXECUTABLE:%=%.cpp), $(wildcard *.cpp))
SUPPORT_OBJECTS := $(SUPPORT_SOURCES:%.cpp=%.o)
$(ALL_EXECUTABLES) : $(SUPPORT_OBJECTS)
# Link one executable
# NOTE: order sometimes matters for the position of the linker flags!
% : %.o
$(LD) $^ $(LDFLAGS) -o $@
# Compile one source file
%.o : %.cpp
$(CXX) $(CXXFLAGS) $< -o $@
################################################################################
# Housekeeping
# Build distribution tarball
DIST_INPUT_FILES := $(wildcard Makefile *.h *.cpp)
DIST_OUTPUT_FILE := submit.tar.gz
dist : $(DIST_INPUT_FILES)
rm -vf $(DIST_OUTPUT_FILE)
-dos2unix $^
tar -czvf $(DIST_OUTPUT_FILE) $(DIST_INPUT_FILES)
# Remove created files
clean :
rm -vf $(EXECUTABLE) $(UNIT_TEST_EXECUTABLES) \
*.o *~ *.out *.gch *.passed *.gcov *.gcno *.gcda *.diff.txt analysis.txt *.noleak
rm -vfr *.dSYM
# Clean up configuration and distribution files
distclean : clean
rm -vf $(DIST_OUTPUT_FILE)
# These targets do not create any files
.PHONY : all debug profile clean distclean test depends dist run_gcov \
unittest systemtest customtest
# Preserve intermediate files
.SECONDARY:
# Disable built-in rules and variables
MAKEFLAGS += --no-builtin-rules
MAKEFLAGS += --no-builtin-variables
.SUFFIXES :