Makefile 8.22 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
# enables c++17 on CAEN or 281 autograder
paoletti's avatar
paoletti committed
21
22
23
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
24

25
26
27
# 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.
Marcus M. Darden's avatar
Marcus M. Darden committed
28
29
# REMOTE_BASEDIR := w18/eecs281    # /home/mmdarden/w18/eecs281/project0
REMOTE_BASEDIR := eecs281
30

Marcus M. Darden's avatar
Marcus M. Darden committed
31
32
# TODO
# Change EXECUTABLE to match the command name given in the project spec.
Marcus M. Darden's avatar
Marcus M. Darden committed
33
EXECUTABLE  = executable
Marcus M. Darden's avatar
Marcus M. Darden committed
34
35
36
DEBUG       = $(EXECUTABLE)_debug

# designate which compiler to use
Marcus M. Darden's avatar
Marcus M. Darden committed
37
CXX         = g++
Marcus M. Darden's avatar
Marcus M. Darden committed
38
39
40
41
42
43
44

# 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
45
SOURCES     = $(wildcard *.cpp)
Marcus M. Darden's avatar
Marcus M. Darden committed
46
47
SOURCES     := $(filter-out $(TESTSOURCES), $(SOURCES))
# list of objects used in project
Marcus M. Darden's avatar
Marcus M. Darden committed
48
OBJECTS     = $(SOURCES:%.cpp=%.o)
Marcus M. Darden's avatar
Marcus M. Darden committed
49
50
51

# TODO
# If main() is in a file named project*.cpp, use the following line
52
PROJECTFILE = $(or $(wildcard project*.cpp), $(EXECUTABLE).cpp)
Marcus M. Darden's avatar
Marcus M. Darden committed
53
# TODO
Marcus M. Darden's avatar
Marcus M. Darden committed
54
# If main() is in another file delete line above, edit and uncomment below
Marcus M. Darden's avatar
Marcus M. Darden committed
55
56
#PROJECTFILE = mymainfile.cpp

57
# name of the tarball created for submission
Marcus M. Darden's avatar
Marcus M. Darden committed
58
59
60
PARTIAL_SUBMITFILE = partialsubmit.tar.gz
FULL_SUBMITFILE = fullsubmit.tar.gz

61
62
63
# 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
64
#Default Flags (we prefer -std=c++17 but Mac/Xcode/Clang doesn't support)
65
CXXFLAGS = -std=c++1z -Wconversion -Wall -Werror -Wextra -pedantic 
Marcus M. Darden's avatar
Marcus M. Darden committed
66
67

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

# make debug - will compile "all" with $(CXXFLAGS) and the -g flag
Marcus M. Darden's avatar
Marcus M. Darden committed
73
#              also defines DEBUG, so "#ifdef DEBUG /*...*/ #endif" works
Marcus M. Darden's avatar
Marcus M. Darden committed
74
75
debug: EXECUTABLE := $(DEBUG)
debug: CXXFLAGS += -g3 -DDEBUG
76
debug: clean $(EXECUTABLE)
Marcus M. Darden's avatar
Marcus M. Darden committed
77
78
79

# make profile - will compile "all" with $(CXXFLAGS) and the -pg flag
profile: CXXFLAGS += -pg
80
profile: clean $(EXECUTABLE)
Marcus M. Darden's avatar
Marcus M. Darden committed
81

82
83
84
# make static - will perform static analysis in the matter currently used
#               on the autograder
static:
Marcus M. Darden's avatar
Marcus M. Darden committed
85
	cppcheck --enable=all --suppress=missingIncludeSystem \
86
      $(SOURCES) *.h *.hpp
87

88
89
# Build both release and debug executables
all: clean
90
91
	$(MAKE) -Rr release
	$(MAKE) debug
Marcus M. Darden's avatar
Marcus M. Darden committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

$(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
110
    HDRS = $$(wildcard *.h *.hpp)
Marcus M. Darden's avatar
Marcus M. Darden committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    $(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))))

alltests: clean $(TESTS)

# 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
125
	rm -f $(OBJECTS) $(EXECUTABLE) $(DEBUG) $(TESTS) \
126
      $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE) $(PERF_FILE)
Marcus M. Darden's avatar
Marcus M. Darden committed
127
128
	rm -Rf *.dSYM

Marcus M. Darden's avatar
Marcus M. Darden committed
129
130
131
# make partialsubmit.tar.gz - cleans, runs dos2unix, creates tarball
# omitting test cases
PARTIAL_SUBMITFILES=$(filter-out $(TESTSOURCES), \
132
                      $(wildcard Makefile *.h *.hpp *.cpp))
Marcus M. Darden's avatar
Marcus M. Darden committed
133
134
135
$(PARTIAL_SUBMITFILE): $(PARTIAL_SUBMITFILES)
	rm -f $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE)
	-dos2unix $(PARTIAL_SUBMITFILES)
Marcus M. Darden's avatar
Marcus M. Darden committed
136
	COPYFILE_DISABLE=true tar -vczf $(PARTIAL_SUBMITFILE) \
137
      $(PARTIAL_SUBMITFILES)
Marcus M. Darden's avatar
Marcus M. Darden committed
138
139
	@echo !!! WARNING: No test cases included. Use 'make fullsubmit' to include test cases. !!!

Marcus M. Darden's avatar
Marcus M. Darden committed
140
141
142
# make fullsubmit.tar.gz - cleans, runs dos2unix, creates tarball
# including test cases
FULL_SUBMITFILES=$(filter-out $(TESTSOURCES), \
143
                   $(wildcard Makefile *.h *.hpp *.cpp test*.txt))
Marcus M. Darden's avatar
Marcus M. Darden committed
144
145
146
147
148
149
150
151
152
153
$(FULL_SUBMITFILE): $(FULL_SUBMITFILES)
	rm -f $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE)
	-dos2unix $(FULL_SUBMITFILES)
	COPYFILE_DISABLE=true tar -vczf $(FULL_SUBMITFILE) $(FULL_SUBMITFILES)
	@echo !!! Final submission prepared, test cases included... READY FOR GRADING !!!

# shortcut for make submit tarballs
partialsubmit: $(PARTIAL_SUBMITFILE)
fullsubmit: $(FULL_SUBMITFILE)

154
sync2caen: REMOTE_PATH := ${REMOTE_BASEDIR}/$(notdir $(shell pwd))
155
sync2caen:
156
157
	# Synchronize local files into target directory on CAEN
	rsync \
158
159
      -av \
      --delete \
160
      --exclude '.git*' \
161
162
      --exclude '.vs*' \  
	  --filter=":- .gitignore" \
163
      ./ \
164
      "login.engin.umich.edu:'${REMOTE_PATH}'/"
165

Marcus M. Darden's avatar
Marcus M. Darden committed
166
167
168
169
170
171
172
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
173
       a. Set EXECUTABLE equal to the name from the project specification.
Marcus M. Darden's avatar
Marcus M. Darden committed
174
175
176
177
178
       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
179
    A) To build 'partialsubmit.tar.gz', a tarball without tests used to
Marcus M. Darden's avatar
Marcus M. Darden committed
180
       find buggy solutions in the autograder.
Marcus M. Darden's avatar
Marcus M. Darden committed
181
182
183
184
185
186
187
188
189
190
191
192
193

           *** 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
       cases.

           *** 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
194
195
196
197
198
199
200
201
202
           $$ 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
203
204
    C) If test drivers need special dependencies, they must be added
       manually.
205
206
    D) IMPORTANT: NO SOURCE FILES WITH NAMES THAT BEGIN WITH test WILL BE
       ADDED TO ANY SUBMISSION TARBALLS.
207
208
209
210
211

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

213
* Sync to CAEN support
214
215
    A) Requires an .ssh/config file with a login.engin.umich.edu host
       defined, SSH Multiplexing enabled, and an open SSH connection.
216
217
    B) Edit the REMOTE_BASEDIR variable if default is not preferred.
    C) Usage:
218
           $$ make sync2caen
Marcus M. Darden's avatar
Marcus M. Darden committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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
238
239
# SOME EXAMPLES
#
Marcus M. Darden's avatar
Marcus M. Darden committed
240
241
242
243
#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
244
245
246
247
248
249
#
# THE COMPILER CAN GENERATE DEPENDENCIES FROM SOURCE CODE
#
# % g++ -MM *.cpp
#
# ADD YOUR OWN DEPENDENCIES HERE
Marcus M. Darden's avatar
Marcus M. Darden committed
250
251
252
253
254
255

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

# these targets do not create any files
Marcus M. Darden's avatar
Marcus M. Darden committed
256
.PHONY: all release debug profile static clean alltests partialsubmit \
257
        fullsubmit sync2caen help
Marcus M. Darden's avatar
Marcus M. Darden committed
258
259
# disable built-in rules
.SUFFIXES: