DOC PREVIEW
UW CSE 303 - Lecture Notes

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

'&$%CSE 303:Concepts and Tools for Software DevelopmentDan GrossmanSpring 2005Lecture 21— Recompilation, build-scripting (make)Dan Grossman CSE303 Spring 2005, Lecture 21 1'&$%Where are WeWe are working on tools and concepts for writing larger (multiperson,multifile, multiplatform, ...) programs.Homework 6 will require you to use make (for compilationmanagement) and cvs (for version control).Class will be more about the concepts.make has tons of fancy features, but only two basic ideas:1. Scripts for executing comm ands2. Dependencies for avoiding unnecessary workDan Grossman CSE303 Spring 2005, Lecture 21 2'&$%Build scriptingProgrammers spend a lot of time “building” (creating programs fromsource code)• Programs they write• Programs other people writeProgrammers automate repetitive tasks. Trivial example:gcc -Wall -g -o myprog foo.c bar.c baz.cIf you:• Retype this every time: “shame, shame”• Use up-arrow or history: “shame” (retype after logout)• Have a csh script: “good-thinkin”• Have a Make file: you’re ahead of usDan Grossman CSE303 Spring 2005, Lecture 21 3'&$%“Real” build processesOn larger projects, you can’t or don’t want to have one bigcomplicated call to the compiler (be it gcc, javac, etc.)1. If gcc didn’t combine steps behind your back, you could need topreprocess and compile e ach file, then c all the linker.2. If another program (e.g., sed) created some C file s, you wouldneed an “earlier” step.3. If you have other outputs for the same source files (e.g.,javadoc), it’s unpleasant to type the source files multiple times.4. If you want to distribute source code t o be built by other users.5. If you have 105to 107lines of source code, you don’t want torecompile them all every time you change something.A simple script handles 1–4 (use a variable for the filenames for 3), but5 is trickier.Dan Grossman CSE303 Spring 2005, Lecture 21 4'&$%Recompilation managementThe “theory” behind avoiding unnecessary compilation is a“dependency dag”:• To create a target t, you need sources s1,s2,...,snand acommand c (that directly or indirectly use s the sources)• If t is newer than every source (file-modification times), assumethere is no reason to rebuild it.• Recursive building: If some source siis itself a target for someother sources, see if it needs to be rebuilt. Etc.• Cycles “make no sense”Dan Grossman CSE303 Spring 2005, Lecture 21 5'&$%An algorithmWhat would a program (e.g., a shell script) that did this for you looklike? It would take:• a bunch of triples: target, sources, command(s)• a “current target to build”It would compute w hat commands needed to be ex ecute d, in whatorder, and do it. (It would detect cycles and give an error.)This is exactly what programs like make, ant, and things integratedinto IDEs do!For specific ity (and what I use), we’ll use make. A “simple utility forfriends” that accidentally took over.Dan Grossman CSE303 Spring 2005, Lecture 21 6'&$%make basicsThe “triples” are typed into a “makefile” like this:target: sourcescommandExample:foo.o: foo.c foo.h bar.hgcc -Wall -o foo.o -c foo.cSyntax gotchas:• The colon after the target is required.• Command lines must start with a TAB NOT SPACES• You can actually have multiple commands (executed in order); ifone command spans lines you m ust e nd the previous line with \.• Which shell-language interprets the commands? (Typically bash,to be sure set the SHELL variable in your makefile.)Dan Grossman CSE303 Spring 2005, Lecture 21 7'&$%Using makeAt the prompt:prompt% make -f nameOfMakefile aTargetDefaults:• If no -f spec ified, use a file named Makefile.• If not target specified, use the first one in the file.Together: I can download a tarball, extract it, type make (fourcharacters) and everything should work.Actually, there’s typically a “configure” step too, for finding things like“where is the compiler” that generates the Makefile (but we won’tget into that).Dan Grossman CSE303 Spring 2005, Lecture 21 8'&$%Basics SummarySo far, enough for homework 6 and basic use.• A tool that com bines sc ripting with dependency analysis to avoidunnecessary recom pilation.• Not language or tool-specific: just based on file-modification timesand shell-commands.But there’s so much more you want to do so that your Makefiles are:• Short and modular• Easy to reuse (with different flags, platforms, etc.)• Useful for m any tasks• Automatically maintained with respect to dependencies.Also, reading others’ makefiles can be tough bec ause of all thefeatures: see info make (also 269–275 of Nutshell?? )Dan Grossman CSE303 Spring 2005, Lecture 21 9'&$%make variablesYou can define variables in a Makefile. Example:CC = gccCFLAGS = -Wallfoo.o: foo.c foo.h bar.h$(CC) $(CFLAGS) -c foo.c -o foo.oWhy do this?• Easy to change things once and affect many commands.• Can change variables on the comm and-line (overrrides definitionsin file). (For example make CFLAGS=-g.)• Easy to reuse a Makefile from one “homework” to the next.• Can use conditionals to set variables (using inherited environmentvariables):Dan Grossman CSE303 Spring 2005, Lecture 21 10'&$%make conditionalsEXE=ifdef WINDIR # assume we are on a Windows machineEXE=.exeendifmyprog$(EXE): foo.o bar.o$(CC) $(CFLAGS) -o myprog$(EXE) foo.o bar.oOther forms of conditionals exist (e.g., are two strings equal)Dan Grossman CSE303 Spring 2005, Lecture 21 11'&$%more variablesIt’s also common to use variables to hold list of filenames:MYOBJFILES = foo.o bar.o baz.omyprog: $(MYOBJFILES)gcc -o myprog $(MYOBJFILES)clean:rm $(MYOBJFILES) myprogclean is a convention: remove any generated files, to “start over” andhave just the source.It’s “funny” because the target does n’t exist and there are no sources,but that’s okay:• If target doesn’t exist, it must be “remade” so run the commands• These “phony” targets have several uses, another is an “all”target:Dan Grossman CSE303 Spring 2005, Lecture 21 12'&$%“all” exampleall: prog B.class someLib.a # notice no commands this timeprog1: foo.o bar.o main.ogcc -o prog1 foo.o bar.o main.oB.class: B.javajavac B.javasomeLib.a: foo.o baz.oar r foo.o baz.oDan Grossman CSE303 Spring 2005, Lecture 21 13'&$%Revenge of funny charactersUNIX hackers just can’t get enough of funny metacharacters can they?In commands:• $@ for target• $^ for all sources• $< for left-mos t target•


View Full Document

UW CSE 303 - Lecture Notes

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

22 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?