Professional Documents
Culture Documents
file:///C:/home/swc/build/www/make.html
Automated Builds
prev Draft Version 348 (Tue Aug 23 11:52:49 2005) next
1 of 8
8/23/2005 11:55 AM
file:///C:/home/swc/build/www/make.html
Note: examples below are written in terms of Java, but Make doesn't care what language you use
Course material includes examples in C and Fortran Comment on this slide
Hello, Make
Put program in hello.java Put the following into a file called hello.mk:
# $Id: hello.mk 111 2005-06-10 21:13:26Z gvwilson $ hello.class : hello.java javac hello.java Note: that indentation must be a tab character, not eight spaces, or a mix of spaces and tabs Yeah, it's a wart, but we're stuck with it
Some terminology:
Figure 5.1: Structure of a Make Rule hello.class is the target of the rule hello.java is its prerequisite We'll see in a moment that a target may have many prerequisites The compilation command is the rule's action A rule may also have many actions
Multiple Targets
2 of 8
8/23/2005 11:55 AM
file:///C:/home/swc/build/www/make.html
Phony Targets
Obviously don't want to have to run Make separately for each possible target
That would hardly count as automation
3 of 8
8/23/2005 11:55 AM
file:///C:/home/swc/build/www/make.html
Dependencies
If Overlap depends on Rect, then Overlap.java must be recompiled if:
It's newer than Overlap.class, or It's newer than Rect.class Which must be re-created if it's older than Rect.java # $Id: depend.mk 111 2005-06-10 21:13:26Z gvwilson $ Overlap.class : Overlap.java Rect.class javac Overlap.java Rect.class : Rect.java javac Rect.java
Figure 5.2: Visualizing Dependencies Mark all the files that are out of date, and follow arrows to find out what will happen Note: Make can execute actions in any order it wants to, as long as it doesn't violate dependency ordering
Avoiding Redundancy
Often want to set options when compiling
"-d" to specify output directory "-source 1.4" to specify Java language version
4 of 8
8/23/2005 11:55 AM
file:///C:/home/swc/build/www/make.html
Rule #3: anything repeated in two or more places will eventually be wrong in at least one
Automatic Variables
Make defines automatic variables to represent parts of rules
These variables are automatically redefined for each rule Names are unfortunately very cryptic Keep a reference card handy
"$@" "$<" "$?" "$^"
The rule's target The rule's first prerequisite All of the rule's out-of-date prerequisites All prerequisites
Table 5.1: Automatic Variables in Make
Example
# $Id: auto.mk 111 2005-06-10 21:13:26Z gvwilson $ JC = javac -source 1.4 Test.class : Test.java Overlap.class @echo "Building" $@ ${JC} $<
5 of 8
8/23/2005 11:55 AM
file:///C:/home/swc/build/www/make.html
Overlap.class : Overlap.java Rect.class @echo "Building" $@ ${JC} $< Rect.class : Rect.java @echo "Building" $@ ${JC} $< Note: by default, Make echoes actions before executing them Putting "@" at the start of the action line prevents this Comment on this slide
Pattern Rules
Most files of similar type (Java, C++, etc.) are compiled the same way
Rule #3 again: write a pattern rule specifying what to do in the general case
Use the wildcard "%" to represent the stem of the file's name in the target and prerequisites
Must use automatic variables in the actions This is why they were invented # $Id: rule.mk 111 2005-06-10 21:13:26Z gvwilson $ JC = javac -source 1.4 all : Left.class Right.class %.class : %.java ${JC} $< Comment on this slide
Example:
# $Id: accumulate.mk 111 2005-06-10 21:13:26Z gvwilson $ JC = javac -source 1.4 Test.class : Overlap.class Overlap.class : Rect.class Rect.class : Rect.java javac -source 1.4 -nowarn $<
6 of 8
8/23/2005 11:55 AM
file:///C:/home/swc/build/www/make.html
%.class : %.java ${JC} $< Tell Make that Test depends on Overlap, and Overlap depends on Rect Turn off warnings when compiling Rect.java, but use one pattern for everything else Comment on this slide
Passing In Data
Sometimes useful to pass values into Make when invoking it
E.g., change the directory that the output is put in
Rather than rewriting the Makefile each time, you can specify name=value pairs on the command line
Define a macro with the default value Override it when you want to
Example:
VAL = original echo : @echo "VAL is" ${VAL} $ make -f env.mk echo VAL is original $ make -f env.mk VAR=changed echo VAL is changed
Analysis
Pro
Simple things are simple to do and not too difficult to read especially compared to the alternatives
Con
The syntax is unpleasant Complex things are difficult to read and even more difficult to debug
7 of 8
8/23/2005 11:55 AM
file:///C:/home/swc/build/www/make.html
Best you can do is use echo to print things as Make executes Not really very portable Hands commands to the shell for execution But commands use different flags on different operating systems Do you use del or rm to delete files?
Alternatives
Ant primarily for Java Less platform-dependent but just as hard to read and debug Integrated development environments Most are proprietary and platform-specific SCons Give users a library to manage dependencies and actions Makefile is actually a small program More powerful, and debuggable, but steeper learning curve Comment on this slide
prev
next
8 of 8
8/23/2005 11:55 AM