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