Makefile 10.3 KB
Newer Older
Marcus M. Darden's avatar
Marcus M. Darden committed
1
2
3
4
5
6
7
8
9
10
11
12
## EECS 281 Advanced Makefile

# How to use this Makefile...
###################
###################
##               ##
##  $ make help  ##
##               ##
###################
###################

# IMPORTANT NOTES:
Marcus M. Darden's avatar
Marcus M. Darden committed
13
#   1. Set EXECUTABLE to the command name from the project specification.
Marcus M. Darden's avatar
Marcus M. Darden committed
14
#   2. To enable automatic creation of unit test rules, your program logic
Marcus M. Darden's avatar
Marcus M. Darden committed
15
16
#      (where main() is) should be in a file named project*.cpp or
#      specified in the PROJECTFILE variable.
Marcus M. Darden's avatar
Marcus M. Darden committed
17
18
19
#   3. Files you want to include in your final submission cannot match the
#      test*.cpp pattern.

20
21
22
#######################
# TODO (begin) #
#######################
23
24
25
# Change UNIQNAME to match your UM uniqname.
UNIQNAME    = youruniqname

26
# Change IDENTIFIER to match the project identifier given in the project spec.
Marcus M. Darden's avatar
Marcus M. Darden committed
27
IDENTIFIER  = EEC50281EEC50281EEC50281EEC50281EEC50281
28

29
# Change 'executable' to match the command name given in the project spec.
30
31
EXECUTABLE  = executable

Marcus M. Darden's avatar
Marcus M. Darden committed
32
# The following line looks for a project's main() in files named project*.cpp,
33
# executable.cpp (substituted from EXECUTABLE above), or main.cpp
Marcus M. Darden's avatar
Marcus M. Darden committed
34
PROJECTFILE = $(or $(wildcard project*.cpp $(EXECUTABLE).cpp), main.cpp)
35
36
37
38
39
40
# If main() is in another file delete line above, edit and uncomment below
#PROJECTFILE = mymainfile.cpp
#######################
# TODO (end) #
#######################

41
# enables c++17 on CAEN or 281 autograder
paoletti's avatar
paoletti committed
42
43
44
PATH := /usr/um/gcc-6.2.0/bin:$(PATH)
LD_LIBRARY_PATH := /usr/um/gcc-6.2.0/lib64
LD_RUN_PATH := /usr/um/gcc-6.2.0/lib64
Marcus M. Darden's avatar
Marcus M. Darden committed
45

46
47
48
# This is the path from the CAEN home folder to where projects will be
# uploaded. (eg. /home/mmdarden/eecs281/project1)
# Change this if you prefer a different path.
49
50
# REMOTE_PATH := eecs281_$(EXECUTABLE)_sync  # /home/mmdarden/eecs281_proj0_sync
REMOTE_PATH := eecs281_$(EXECUTABLE)_sync
51

Marcus M. Darden's avatar
Marcus M. Darden committed
52
# designate which compiler to use
Marcus M. Darden's avatar
Marcus M. Darden committed
53
CXX         = g++
Marcus M. Darden's avatar
Marcus M. Darden committed
54
55
56
57
58
59
60

# list of test drivers (with main()) for development
TESTSOURCES = $(wildcard test*.cpp)
# names of test executables
TESTS       = $(TESTSOURCES:%.cpp=%)

# list of sources used in project
Marcus M. Darden's avatar
Marcus M. Darden committed
61
SOURCES     = $(wildcard *.cpp)
Marcus M. Darden's avatar
Marcus M. Darden committed
62
63
SOURCES     := $(filter-out $(TESTSOURCES), $(SOURCES))
# list of objects used in project
Marcus M. Darden's avatar
Marcus M. Darden committed
64
OBJECTS     = $(SOURCES:%.cpp=%.o)
Marcus M. Darden's avatar
Marcus M. Darden committed
65

66
# name of the tarball created for submission
Marcus M. Darden's avatar
Marcus M. Darden committed
67
FULL_SUBMITFILE = fullsubmit.tar.gz
Marcus M. Darden's avatar
Marcus M. Darden committed
68
69
PARTIAL_SUBMITFILE = partialsubmit.tar.gz
UNGRADED_SUBMITFILE = ungraded.tar.gz
Marcus M. Darden's avatar
Marcus M. Darden committed
70

71
72
73
# name of the perf data file, only used by the clean target
PERF_FILE = perf.data*

Marcus M. Darden's avatar
Marcus M. Darden committed
74
#Default Flags (we prefer -std=c++17 but Mac/Xcode/Clang doesn't support)
75
CXXFLAGS = -std=c++1z -Wconversion -Wall -Werror -Wextra -pedantic 
Marcus M. Darden's avatar
Marcus M. Darden committed
76
77

# make release - will compile "all" with $(CXXFLAGS) and the -O3 flag
Marcus M. Darden's avatar
Marcus M. Darden committed
78
#                also defines NDEBUG so that asserts will not check
Marcus M. Darden's avatar
Marcus M. Darden committed
79
release: CXXFLAGS += -O3 -DNDEBUG
80
release: $(EXECUTABLE)
Marcus M. Darden's avatar
Marcus M. Darden committed
81

Marcus M. Darden's avatar
Marcus M. Darden committed
82
# make debug - will compile sources with $(CXXFLAGS) and the -g3 flag
Marcus M. Darden's avatar
Marcus M. Darden committed
83
#              also defines DEBUG, so "#ifdef DEBUG /*...*/ #endif" works
Marcus M. Darden's avatar
Marcus M. Darden committed
84
debug: CXXFLAGS += -g3 -DDEBUG
Marcus M. Darden's avatar
Marcus M. Darden committed
85
86
debug:
	$(CXX) $(CXXFLAGS) $(SOURCES) -o $(EXECUTABLE)_debug
Marcus M. Darden's avatar
Marcus M. Darden committed
87

88
89
# make profile - will compile "all" with $(CXXFLAGS) and the -g3 and -O3 flags
profile: CXXFLAGS += -g3 -O3
Marcus M. Darden's avatar
Marcus M. Darden committed
90
91
profile:
	$(CXX) $(CXXFLAGS) $(SOURCES) -o $(EXECUTABLE)_profile
92
93
94
95
96
	
# make gprof - will compile "all" with $(CXXFLAGS) and the -pg (for gprof)
gprof: CXXFLAGS += -pg
gprof:
	$(CXX) $(CXXFLAGS) $(SOURCES) -o $(EXECUTABLE)_profile
Marcus M. Darden's avatar
Marcus M. Darden committed
97

98
99
100
# make static - will perform static analysis in the matter currently used
#               on the autograder
static:
Marcus M. Darden's avatar
Marcus M. Darden committed
101
	cppcheck --enable=all --suppress=missingIncludeSystem \
102
      $(SOURCES) *.h *.hpp
103

104
105
106
# make identifier - will check to ensure that all source code and header files
#                   include the project identifier; skip subdirectories;
#                   also removes old submit tarballs, they are outdated
107
identifier:
Marcus M. Darden's avatar
Marcus M. Darden committed
108
	@if [ $$(grep --include=*.{h,hpp,c,cpp} --exclude=xcode_redirect.hpp --directories=skip -L $(IDENTIFIER) * | wc -l) -ne 0 ]; then \
109
		printf "Missing project identifier in file(s): "; \
Marcus M. Darden's avatar
Marcus M. Darden committed
110
111
112
113
		echo `grep --include=*.{h,hpp,c,cpp} --directories=skip -L $(IDENTIFIER) *`; \
		rm -f $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE); \
		exit 1; \
	fi
114

Marcus M. Darden's avatar
Marcus M. Darden committed
115
116
# Build all executables
all: release debug profile
Marcus M. Darden's avatar
Marcus M. Darden committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

$(EXECUTABLE): $(OBJECTS)
ifeq ($(EXECUTABLE), executable)
	@echo Edit EXECUTABLE variable in Makefile.
	@echo Using default a.out.
	$(CXX) $(CXXFLAGS) $(OBJECTS)
else
	$(CXX) $(CXXFLAGS) $(OBJECTS) -o $(EXECUTABLE)
endif

# Automatically generate any build rules for test*.cpp files
define make_tests
    ifeq ($$(PROJECTFILE),)
	    @echo Edit PROJECTFILE variable to .cpp file with main\(\)
	    @exit 1
    endif
    SRCS = $$(filter-out $$(PROJECTFILE), $$(SOURCES))
    OBJS = $$(SRCS:%.cpp=%.o)
Marcus M. Darden's avatar
Marcus M. Darden committed
135
    HDRS = $$(wildcard *.h *.hpp)
Marcus M. Darden's avatar
Marcus M. Darden committed
136
137
138
139
140
141
    $(1): CXXFLAGS += -g3 -DDEBUG
    $(1): $$(OBJS) $$(HDRS) $(1).cpp
	$$(CXX) $$(CXXFLAGS) $$(OBJS) $(1).cpp -o $(1)
endef
$(foreach test, $(TESTS), $(eval $(call make_tests, $(test))))

Marcus M. Darden's avatar
Marcus M. Darden committed
142
alltests: $(TESTS)
Marcus M. Darden's avatar
Marcus M. Darden committed
143
144
145
146
147
148
149

# rule for creating objects
%.o: %.cpp
	$(CXX) $(CXXFLAGS) -c $*.cpp

# make clean - remove .o files, executables, tarball
clean:
Marcus M. Darden's avatar
Marcus M. Darden committed
150
	rm -f $(OBJECTS) $(EXECUTABLE) $(EXECUTABLE)_debug $(EXECUTABLE)_profile \
Marcus M. Darden's avatar
Marcus M. Darden committed
151
152
      $(TESTS) $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE) $(PERF_FILE) \
      $(UNGRADED_SUBMITFILE)
Marcus M. Darden's avatar
Marcus M. Darden committed
153
154
	rm -Rf *.dSYM

Marcus M. Darden's avatar
Marcus M. Darden committed
155
156
157
158
159
160
161
162
163
164
165
166
167

# get a list of all files that might be included in a submit
# different submit types can do additional filtering to remove unwanted files
FULL_SUBMITFILES=$(filter-out $(TESTSOURCES), \
                   $(wildcard Makefile *.h *.hpp *.cpp test*.txt))

# make fullsubmit.tar.gz - cleans, runs dos2unix, creates tarball
# including test files
$(FULL_SUBMITFILE): $(FULL_SUBMITFILES)
	rm -f $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE) $(UNGRADED_SUBMITFILE)
	COPYFILE_DISABLE=true tar -vczf $(FULL_SUBMITFILE) $(FULL_SUBMITFILES)
	@echo !!! Final submission prepared, test files included... READY FOR GRADING !!!

168
# make partialsubmit.tar.gz - cleans, creates tarball
169
# omitting test files
Marcus M. Darden's avatar
Marcus M. Darden committed
170
PARTIAL_SUBMITFILES=$(filter-out $(wildcard test*.txt), $(FULL_SUBMITFILES))
Marcus M. Darden's avatar
Marcus M. Darden committed
171
$(PARTIAL_SUBMITFILE): $(PARTIAL_SUBMITFILES)
Marcus M. Darden's avatar
Marcus M. Darden committed
172
	rm -f $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE) $(UNGRADED_SUBMITFILE)
Marcus M. Darden's avatar
Marcus M. Darden committed
173
	COPYFILE_DISABLE=true tar -vczf $(PARTIAL_SUBMITFILE) \
174
      $(PARTIAL_SUBMITFILES)
175
	@echo !!! WARNING: No test files included. Use 'make fullsubmit' to include test files. !!!
Marcus M. Darden's avatar
Marcus M. Darden committed
176

Marcus M. Darden's avatar
Marcus M. Darden committed
177
178
179
180
181
182
183
184
185
# make ungraded.tar.gz - cleans, creates tarball omitting test files, Makefile
UNGRADED_SUBMITFILES=$(filter-out Makefile, $(PARTIAL_SUBMITFILES))
$(UNGRADED_SUBMITFILE): $(UNGRADED_SUBMITFILES)
	rm -f $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE) $(UNGRADED_SUBMITFILE)
	@touch __ungraded
	COPYFILE_DISABLE=true tar -vczf $(UNGRADED_SUBMITFILE) \
      $(UNGRADED_SUBMITFILES) __ungraded
	@rm -f __ungraded
	@echo !!! WARNING: This submission will not be graded. !!!
Marcus M. Darden's avatar
Marcus M. Darden committed
186
187

# shortcut for make submit tarballs
188
fullsubmit: identifier $(FULL_SUBMITFILE)
Marcus M. Darden's avatar
Marcus M. Darden committed
189
190
partialsubmit: identifier $(PARTIAL_SUBMITFILE)
ungraded: identifier $(UNGRADED_SUBMITFILE)
Marcus M. Darden's avatar
Marcus M. Darden committed
191

192
# REMOTE_PATH has default definition above
193
sync2caen:
194
195
	# Synchronize local files into target directory on CAEN
	rsync \
196
      -av \
197
198
199
200
201
      --delete \
      --exclude '*.o' \
      --exclude '$(EXECUTABLE)' \
      --exclude '$(EXECUTABLE)_debug' \
      --exclude '$(EXECUTABLE)_profile' \
202
      --exclude '.git*' \
203
      --exclude '.vs*' \
204
      --exclude '*.code-workspace' \
205
      --filter=":- .gitignore" \
206
      "."/ \
207
      "$(UNIQNAME)@login.engin.umich.edu:'$(REMOTE_PATH)/'"
208
	echo "Files synced to CAEN at ~/$(REMOTE_PATH)/"
209

Marcus M. Darden's avatar
Marcus M. Darden committed
210
211
212
213
214
215
216
define MAKEFILE_HELP
EECS281 Advanced Makefile Help
* This Makefile uses advanced techniques, for more information:
    $$ man make

* General usage
    1. Follow directions at each "TODO" in this file.
Marcus M. Darden's avatar
Marcus M. Darden committed
217
       a. Set EXECUTABLE equal to the name from the project specification.
Marcus M. Darden's avatar
Marcus M. Darden committed
218
219
220
221
222
       b. Set PROJECTFILE equal to the name of the source file with main()
       c. Add any dependency rules specific to your files.
    2. Build, test, submit... repeat as necessary.

* Preparing submissions
Marcus M. Darden's avatar
Marcus M. Darden committed
223
    A) To build 'partialsubmit.tar.gz', a tarball without tests used to
Marcus M. Darden's avatar
Marcus M. Darden committed
224
       find buggy solutions in the autograder.
Marcus M. Darden's avatar
Marcus M. Darden committed
225
226
227
228
229
230
231

           *** USE THIS ONLY FOR TESTING YOUR SOLUTION! ***

       This is useful for faster autograder runs during development and
       free submissions if the project does not build.
           $$ make partialsubmit
    B) Build 'fullsubmit.tar.gz' a tarball complete with autograder test
232
       files.
Marcus M. Darden's avatar
Marcus M. Darden committed
233
234
235
236
237

           *** ALWAYS USE THIS FOR FINAL GRADING! ***

       It is also useful when trying to find buggy solutions in the
       autograder.
Marcus M. Darden's avatar
Marcus M. Darden committed
238
239
240
241
242
243
244
245
246
           $$ make fullsubmit

* Unit testing support
    A) Source files for unit testing should be named test*.cpp.  Examples
       include test_input.cpp or test3.cpp.
    B) Automatic build rules are generated to support the following:
           $$ make test_input
           $$ make test3
           $$ make alltests        (this builds all test drivers)
Marcus M. Darden's avatar
Marcus M. Darden committed
247
248
    C) If test drivers need special dependencies, they must be added
       manually.
249
250
    D) IMPORTANT: NO SOURCE FILES WITH NAMES THAT BEGIN WITH test WILL BE
       ADDED TO ANY SUBMISSION TARBALLS.
251
252
253
254
255

* Static Analysis support
    A) Matches current autograder style grading tests
    B) Usage:
           $$ make static
256

257
* Sync to CAEN support
258
259
    A) Requires an .ssh/config file with a login.engin.umich.edu host
       defined, SSH Multiplexing enabled, and an open SSH connection.
260
261
    B) Edit the REMOTE_BASEDIR variable if default is not preferred.
    C) Usage:
262
           $$ make sync2caen
Marcus M. Darden's avatar
Marcus M. Darden committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
endef
export MAKEFILE_HELP

help:
	@echo "$$MAKEFILE_HELP"

#######################
# TODO (begin) #
#######################
# individual dependencies for objects
# Examples:
# "Add a header file dependency"
# project2.o: project2.cpp project2.h
#
# "Add multiple headers and a separate class"
# HEADERS = some.h special.h header.h files.h
# myclass.o: myclass.cpp myclass.h $(HEADERS)
# project5.o: project5.cpp myclass.o $(HEADERS)
#
Marcus M. Darden's avatar
Marcus M. Darden committed
282
283
# SOME EXAMPLES
#
Marcus M. Darden's avatar
Marcus M. Darden committed
284
285
286
287
#test_thing: test_thing.cpp class.o functions.o
#class.o: class.cpp class.h
#functions.o: functions.cpp functions.h
#project0.o: project0.cpp class.h functions.h
Marcus M. Darden's avatar
Marcus M. Darden committed
288
289
290
#
# THE COMPILER CAN GENERATE DEPENDENCIES FROM SOURCE CODE
#
291
# % g++ -std=c++1z -MM *.cpp
Marcus M. Darden's avatar
Marcus M. Darden committed
292
293
#
# ADD YOUR OWN DEPENDENCIES HERE
Marcus M. Darden's avatar
Marcus M. Darden committed
294
295
296
297
298
299

######################
# TODO (end) #
######################

# these targets do not create any files
Marcus M. Darden's avatar
Marcus M. Darden committed
300
.PHONY: all release debug profile static clean alltests partialsubmit \
Marcus M. Darden's avatar
Marcus M. Darden committed
301
        fullsubmit ungraded sync2caen help identifier
Marcus M. Darden's avatar
Marcus M. Darden committed
302
303
# disable built-in rules
.SUFFIXES: