DOC PREVIEW
UW CSE 303 - Makefiles & Compilation Management

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 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 22 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 22 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 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

'&$%CSE 303:Concepts and Tools for Software DevelopmentHal PerkinsWinter 2009Lecture 14— Makefile s & Compilation ManagementCSE 303 Winter 2009, Lecture 14 1'&$%Where are We• Onto tools. . .• Basics of make, particular the concepts• Some fancier make features (rev enge of funky characters)Besides the slides and online Unix docs, the Stanford CSLib notes onUnix Programming Tools has a nice overview of make and other tools:• http://cslibrary.stanford.edu/107/UnixProgrammingTools.pdfCSE 303 Winter 2009, Lecture 14 2'&$%Onto toolsThe language-implementation (preprocessor, compiler, linker,standard-library) is hardly the only useful thing for developing software.The rest of the course:• Tools (recompilation managers, version control, debuggers,profilers)• Software-engineering issues• A taste of C++• Concurrency• Societal implicationsCSE 303 Winter 2009, Lecture 14 3'&$%makemake is a classic program for controlling what gets (re)compiled andhow. Many other such programs exis t (e.g., ant, maven, “projects” inIDEs, . . . )make has tons of fancy features, but only two basic ideas:1. Scripts for executing com mands2. Dependencies for avoiding unnecessary workTo avoid “just teaching make features” (boring and narrow), let’sfocus more on the concepts. . .CSE 303 Winter 2009, Lecture 14 4'&$%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 an alias or bash script: “good-thinkin”• Have a Makefile: you’re ahead of usCSE 303 Winter 2009, Lecture 14 5'&$%“Real” build processesOn larger projects, you can’t or don’t want to have one big (set of)command(s) that redoes everything every time you change anything.1. If gcc didn’t combine steps behind your back, you could need topreprocess and compile each file, then call the linker.2. If another program (e.g., sed) created some C files, 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 to 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.CSE 303 Winter 2009, Lecture 14 6'&$%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 uses 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”CSE 303 Winter 2009, Lecture 14 7'&$%Theory applied to CAnother whole lecture on linking is in our future (well, actually, we didsome of it earlier), but here is what you need to know today for C:• Compiling a .c creates a .o and depends on all included files(recursively/transitively).• Creating an executable (“linking”) depends on .o files.• So if one .c file changes, just need to recreate one .o file andrelink.• If a header-file changes, may nee d to rebuild more.• Of course, this is only the simplest situation.CSE 303 Winter 2009, Lecture 14 8'&$%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 comm ands needed to be executed, in whatorder, and do it. (It would detect c ycles and give an error.)This is exactly what programs like make, ant, and things integratedinto IDEs do!CSE 303 Winter 2009, Lecture 14 9'&$%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 must end the previous line with \.• Which shell-language interprets the c omm ands? (Typically bash,to be sure set the SHELL variable in your makefile.)CSE 303 Winter 2009, Lecture 14 10'&$%Using makeAt the prompt:prompt% make -f nameOfMakefile aTargetDefaults:• If no -f specified, use a file named Makefile.• If not target specified, use the first one in the file.Together: You c an 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).CSE 303 Winter 2009, Lecture 14 11'&$%Basics SummarySo far, enough for homework 4 and basic use.• A tool that combines scripting 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 many tasks• Automatically maintained with respect to dependencies.Also, reading others’ makefiles c an be tough because of all thefeatures: see info make or entire books.CSE 303 Winter 2009, Lecture 14 12'&$%Precise reviewA Makefile has a bunch of these:target: source_1 ... source_nshell_commandRunning make target does this:• For each source, if it is a target in the Makefile, process itrecursively• Then:– If some source does not exist, error.– If some source is newer than the target (or target does notexist), run shell_command (presumably updates target, butthat is up to you).CSE 303 Winter 2009, Lecture 14 13'&$%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 command-line (overrides definitionsin file). (For example make


View Full Document

UW CSE 303 - Makefiles & Compilation Management

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

22 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
Download Makefiles & Compilation Management
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 Makefiles & Compilation Management 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 Makefiles & Compilation Management 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?