Restore C++ example, and expad Makefile to handle multiple programs.
[assignment-template.git] / Makefile
1 # Copyright (C) 2012  W. Trevor King
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 # Declare variables describing package information.
17 #   http://www.gnu.org/software/make/manual/html_node/Setting.html
18 # The $(...) syntax references a variable's value.
19 #   http://www.gnu.org/software/make/manual/html_node/Reference.html
20 # You can override variables from the command line.  For example
21 #   $ make VERSION=2 dist
22 # will generate phys405-hw0-2.tar.gz
23 COURSE = phys405
24 PACKAGE = hw0
25 VERSION = 1
26 RELEASE = $(COURSE)-$(PACKAGE)-$(VERSION)
27 RUN_PROGRAM = hello_world
28 C_PROGRAMS = hello_world
29 CXX_PROGRAMS = goodbye_world
30 PROGRAMS = $(C_PROGRAMS) $(CXX_PROGRAMS)
31
32 # Define the source files that will be distributed in the tarball
33 SOURCE = *.c COPYING Makefile README
34
35 # Define a list of object files needed to link PROGRAM.  We're setting
36 # things up here so that a program % depends on the object file %.o.
37 # For details on `foreach` and `eval`, see
38 #   http://www.gnu.org/software/make/manual/html_node/Foreach-Function.html
39 #   http://www.gnu.org/software/make/manual/html_node/Eval-Function.html
40 $(foreach PROGRAM, $(PROGRAMS), $(eval $(PROGRAM)_OBJECTS = $(PROGRAM).o))
41
42 # You can override a paricular case here with something like:
43 #hello_world_OBJECTS = hello_world.o utility_code.o
44
45 # You may want to link agains external libraries.  For example, to
46 # link against the system math library, use.
47 #   hello_world_LIBS = -lm
48 $(foreach PROGRAM, $(PROGRAMS), $(eval $(PROGRAM)_LIBS =))
49
50 # Define useful programs (this makes it easy to swap in alternates)
51 CP = cp
52 GREP = grep
53 MKDIR = mkdir
54 RM = rm
55 TAR = tar
56
57 # Declare targets that do not generate files of the same name.
58 #   http://www.gnu.org/software/make/manual/html_node/Phony-Targets.html
59 .PHONY: all help clean dist run print- printvars
60
61 # target: all - the default target
62 all: $(PROGRAMS)
63
64 # target: help - display callable targets
65 # Use `grep` to search this file for target comments
66 help:
67         $(GREP) '^# target:' [Mm]akefile
68
69 # target: clean - remove automatically generated files
70 clean:
71         $(RM) -rf $(PROGRAMS) *.o $(RELEASE)*
72
73 # target: dist - generate a tarball packaging the source
74 # Here, we move the source into a temporary release directory, tar the
75 # release directory, and remove the release directory.
76 dist:
77         $(MKDIR) $(RELEASE)
78         $(CP) -r $(SOURCE) $(RELEASE)
79         $(TAR) -czf $(RELEASE).tar.gz $(RELEASE)
80         $(RM) -rf $(RELEASE)
81
82 # target: hello_world - compile the hello_world program
83 # Use GCC to link the program from object files.
84 # For an explanation of the
85 #   targets ...: target-pattern: prereq-patterns ...
86 # syntax, see
87 #   http://www.gnu.org/software/make/manual/html_node/Static-Usage.html
88 # For an explanation of $@, $^ and other special variables, see
89 #   http://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html
90 # For an explanation of .SECONDEXPANSION, see
91 #   http://www.gnu.org/software/make/manual/html_node/Secondary-Expansion.html
92 # `$$` escapes make-variable expansion for the first pass through the
93 # recipe.  See
94 #   http://www.gnu.org/software/make/manual/html_node/Variables-in-Recipes.html
95 #
96 # What's going on here?  During the read-in phase, Make expands the
97 # rule to
98 #   hello_world goodbye_world: % : $($(@)_OBJECTS)
99 # Because we're using .SECONDEXPANSION, Make expands the prerequisites
100 # again during the target-update phase.  If we're building
101 # hello_world, $(@) will expand to hello_world, and we'll have
102 #   hello_world goodbye_world: hello_world : $(hello_world_OBJECTS)
103 # as the variable expansion continues, we end up with
104 #   hello_world goodbye_world: hello_world : hello_world.o
105 # which is the final rule used to determine the prerequisites.
106 #
107 # The recipe expands to
108 #   gcc  -o hello_world hello_world.o $(hello_world_LIBS)
109 # which expands to (if hello_world_LIBS was set to `-lm`)
110 #   gcc  -o hello_world hello_world.o -lm
111 #
112 # Striking the right balance between "everything handled
113 # automatically" (i.e. "complicated") and "everything handled
114 # manually" (i.e. tedious) is difficult, and maybe this rule crosses
115 # the line.  The simpler alternative would be to define your own
116 # prerequisites for each program you wish to compile, and you're
117 # certainly allowed to go that route if you wish.
118 .SECONDEXPANSION:
119 $(C_PROGRAMS): % : $$($$(@)_OBJECTS)
120         $(CC) $(LDFLAGS) -o $@ $^ $($(@)_LIBS)
121
122 .SECONDEXPANSION:
123 $(CXX_PROGRAMS): % : $$($$(*)_OBJECTS)
124         $(CXX) $(LDFLAGS) -o $@ $^ $($(@)_LIBS)
125
126 # target: run - use the program for its intended purpose
127 # Here we just execute RUN_PROGRAM, but you could also use something
128 # like
129 #   run: $(RUN_PROGRAM) plot.gp
130 #     ./$(RUN_PROGRAM) > data
131 #     gnuplot plot.gp
132 # where plot.gp was a gnuplot script for plotting data generated by
133 # RUN_PROGRAM.
134 run: $(PROGRAMS)
135         ./$(RUN_PROGRAM)
136
137 # Matching rule for compiling object files from C++ source
138 # There is an implicit rule for this in GNU make
139 #   http://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html
140 # but I redefine it here for clarity.
141 #
142 # CXX and CXXFLAGS have defaults defined by make
143 #   http://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html
144 # but they can also be come from the environment
145 #   http://www.gnu.org/software/make/manual/html_node/Environment.html
146 # for example, try
147 #    $ make CXX=/usr/local/bin/g++ CXXFLAGS=-Wall
148 %.o: %.cpp
149         $(CXX) $(CXXFLAGS) -c $<
150
151 # Matching rule for compiling object files from C source
152 # The comments from the C++ rule above also apply here
153 %.o: %.c
154         $(CC) $(CFLAGS) -c $<
155
156 # target: print-% - display a variable value (e.g. print-PROGRAMS)
157 # Take some of the mystery out of variable manipulation.  For example,
158 #   $ make print-hello_world_OBJECTS
159 # The `@` suppresses recipe echoing.  See
160 #   http://www.gnu.org/software/make/manual/html_node/Echoing.html
161 # The `info` function acts like the shell `echo` command.  See
162 #   http://www.gnu.org/software/make/manual/html_node/Make-Control-Functions.html
163 # For an explanation of $* and other special variables, see
164 #   http://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html
165 print-%:
166         @$(info $* = $($*))
167
168 # target: printvars - display all non-default variables
169 # .VARIBALES holds a list of all global variables.  See
170 #   http://www.gnu.org/software/make/manual/html_node/Special-Variables.html
171 # The functions---`sort`, `if`, `filter-out`, `origin`, `warning`, and
172 # `value`--- are discussed, respectively, here:
173 #   http://www.gnu.org/software/make/manual/html_node/Text-Functions.html#index-sort-580
174 #   http://www.gnu.org/software/make/manual/html_node/Conditional-Functions.html
175 #   http://www.gnu.org/software/make/manual/html_node/Text-Functions.html#index-filter_002dout-577
176 #   http://www.gnu.org/software/make/manual/html_node/Origin-Function.html
177 #   http://www.gnu.org/software/make/manual/html_node/Value-Function.html
178 printvars:
179         @$(foreach V, \
180                 $(sort $(.VARIABLES)), \
181                 $(if \
182                         $(filter-out environment default automatic, \
183                                 $(origin $V)), \
184                         $(info $V=$($V) (origin: $(origin $V), value: $(value $V))) \
185                         ) \
186                 )