1 # Copyright (C) 2012-2013 W. Trevor King
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.
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.
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/>.
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 phys305-hw4-2.tar.gz
26 RELEASE = $(COURSE)-$(PACKAGE)-$(VERSION)
29 C_PROGRAMS = one_gaussian_bump error_analysis
31 PROGRAMS = $(C_PROGRAMS) $(CXX_PROGRAMS)
33 # Define the source files that will be distributed in the tarball
34 SOURCE = *.c *.h COPYING Makefile README $(SCRIPTS)
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))
43 # You can override a paricular case here with something like:
44 #hello_world_OBJECTS = hello_world.o utility_code.o
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 = -lm))
51 # Define useful programs (this makes it easy to swap in alternates)
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
62 # target: all - the default target
63 # The target of the first rule is the default goal. See:
64 # http://www.gnu.org/software/make/manual/html_node/Rules.html
67 # Dependency declarations
68 one_gaussian_bump_OBJECTS = one_gaussian_bump.o safe-strto.o
69 one_gaussian_bump.o safe-strto.o: config.h safe-strto.h
71 # target: help - display callable targets
72 # Use `grep` to search this file for target comments
74 $(GREP) '^# target:' [Mm]akefile
76 # target: clean - remove automatically generated files
78 $(RM) -rf $(PROGRAMS) *.o *.data *.png $(RELEASE)*
80 # target: dist - generate a tarball packaging the source
81 # Here, we move the source into a temporary release directory, tar the
82 # release directory, and remove the release directory.
85 $(CP) -r $(SOURCE) $(RELEASE)
86 $(TAR) -czf $(RELEASE).tar.gz $(RELEASE)
89 # target: hello_world - compile the hello_world program
90 # Use GCC to link the program from object files.
91 # For an explanation of the static pattern rule syntax:
92 # targets ...: target-pattern: prereq-patterns ...
94 # http://www.gnu.org/software/make/manual/html_node/Static-Usage.html
95 # For an explanation of $@, $^, and other special variables, see
96 # http://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html
97 # For an explanation of .SECONDEXPANSION, see
98 # http://www.gnu.org/software/make/manual/html_node/Secondary-Expansion.html
99 # `$$` escapes make-variable expansion for the first pass through the
101 # http://www.gnu.org/software/make/manual/html_node/Variables-in-Recipes.html
103 # What's going on here? During the read-in phase, Make expands the
105 # hello_world goodbye_world: % : $($(@)_OBJECTS)
106 # Because we're using .SECONDEXPANSION, Make expands the prerequisites
107 # again during the target-update phase. If we're building
108 # hello_world, $(@) will expand to hello_world, and we'll have
109 # hello_world goodbye_world: hello_world : $(hello_world_OBJECTS)
110 # as the variable expansion continues, we end up with
111 # hello_world goodbye_world: hello_world : hello_world.o
112 # which is the final rule used to determine the prerequisites.
114 # The recipe expands to
115 # cc -o hello_world hello_world.o $(hello_world_LIBS)
116 # which expands to (if hello_world_LIBS was set to `-lm`)
117 # cc -o hello_world hello_world.o -lm
119 # Striking the right balance between "everything handled
120 # automatically" (i.e. "complicated") and "everything handled
121 # manually" (i.e. tedious) is difficult, and maybe this rule crosses
122 # the line. The simpler alternative would be to define your own
123 # prerequisites for each program you wish to compile, and you're
124 # certainly allowed to go that route if you wish.
126 $(C_PROGRAMS): % : $$($$(@)_OBJECTS)
127 $(CC) $(LDFLAGS) -o "$@" $^ $($(@)_LIBS)
130 $(CXX_PROGRAMS): % : $$($$(*)_OBJECTS)
131 $(CXX) $(LDFLAGS) -o "$@" $^ $($(@)_LIBS)
133 # target: run - use the program for its intended purpose
134 # Here we just execute RUN_PROGRAM, but you could also use something
136 # run: $(RUN_PROGRAM) plot.gp
137 # ./$(RUN_PROGRAM) > data
139 # where plot.gp was a gnuplot script for plotting data generated by
141 run: error.png phase.png energy.png
144 error-%.data: $(PROGRAMS)
145 echo -e "# dt\terror" > "$@"
146 for dt in 0.00004 0.0002 0.001 0.005 0.025; do \
147 echo -en "$$dt\t" >> "$@"; \
148 ./one_gaussian_bump --time-step "$$dt" --$(@:error-%.data=%) | \
149 ./error_analysis >> "$@"; \
152 error.png: error.gp error-euler.data error-midpoint.data error-rk4.data
157 for E in 0.1 0.5 1 1.5 2 2.5 3 3.5 4 4.5 4.9 5 5.1 5.5; do \
158 ./one_gaussian_bump --vx0-energy "$$E" >> "$@"; \
163 phase.png: phase.gp phase.data
166 energy.png: energy.gp phase.data
169 # Pattern rule for compiling object files from C++ source
170 # There is an implicit rule for this in GNU make
171 # http://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html
172 # but I redefine it here for clarity. For an explanation of the
175 # http://www.gnu.org/software/make/manual/html_node/Pattern-Intro.html
177 # CXX and CXXFLAGS have defaults defined by make
178 # http://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html
179 # but they can also be come from the environment
180 # http://www.gnu.org/software/make/manual/html_node/Environment.html
182 # $ make CXX=/usr/local/bin/g++ CXXFLAGS=-Wall
184 $(CXX) $(CXXFLAGS) -c "$<"
186 # Pattern rule for compiling object files from C source
187 # The comments from the C++ rule above also apply here
189 $(CC) $(CFLAGS) -c "$<"
191 # target: print-% - display a variable value (e.g. print-PROGRAMS)
192 # Take some of the mystery out of variable manipulation. For example,
193 # $ make print-hello_world_OBJECTS
194 # The `@` suppresses recipe echoing. See
195 # http://www.gnu.org/software/make/manual/html_node/Echoing.html
196 # The `info` function acts like the shell `echo` command. See
197 # http://www.gnu.org/software/make/manual/html_node/Make-Control-Functions.html
198 # For an explanation of $* and other special variables, see
199 # http://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html
203 # target: printvars - display all non-default variables
204 # .VARIBALES holds a list of all global variables. See
205 # http://www.gnu.org/software/make/manual/html_node/Special-Variables.html
206 # The functions---`sort`, `if`, `filter-out`, `origin`, `warning`, and
207 # `value`--- are discussed, respectively, here:
208 # http://www.gnu.org/software/make/manual/html_node/Text-Functions.html#index-sort-580
209 # http://www.gnu.org/software/make/manual/html_node/Conditional-Functions.html
210 # http://www.gnu.org/software/make/manual/html_node/Text-Functions.html#index-filter_002dout-577
211 # http://www.gnu.org/software/make/manual/html_node/Origin-Function.html
212 # http://www.gnu.org/software/make/manual/html_node/Value-Function.html
215 $(sort $(.VARIABLES)), \
217 $(filter-out environment default automatic, \
219 $(info $V=$($V) (origin: $(origin $V), value: $(value $V))) \