lecture 14
Computer Science 303 with Perkins at University of Washington - Seattle Campus
About this note
By: Maziar Rezakhani
Created: 2009-03-05
File Size: 22 page(s)
Views: 1
Created: 2009-03-05
File Size: 22 page(s)
Views: 1
About StudyBlue
STUDYBLUE makes things that make you better at school.
Things like online flashcards with photos and audio.
Things like personalized quizzes and friendly reminders about when (and what) to study next.
Think of it as a digital backpack™: access to all of your study materials online and on your phone.
STUDYBLUE exists to make studying efficient and effective for every student, for free. Join us.
“I have used this website for three exams, and I see a huge difference in my test results.”
Naj
Naj
Sign up (free) to study this.
a39 a38 a36 a37 CSE 30 3: Co nc epts and T o ols fo r S oft w a re Devel o p ment Hal P e rkins Winte r 2009 Lec tu re 14? Mak efile s & Compila tion Mana ge me n t CSE 3 03 W in ter 20 09, Lectur e 14 1 a39 a38 a36 a37 Where a re W e ? Onto to ols. . . ? Ba sic s of m a k e, pa rtic u la r the conce p ts ? Some fancier m a k e featur e s (rev enge of fun ky cha racte rs) Besides the slid e s a nd on line U nix do cs, th e Sta nfo rd CSLi b note s on Un ix P rogra m ming T o ols h as a nice ove rv ie w of m a k e and oth e r to ols: ? http: //c slib ra ry .sta nfo rd .e d u/107/U nixProgr amm in gT o ols .p d f CSE 3 03 W in ter 20 09, Lectur e 14 2 a39 a38 a36 a37 On to to ols Th e la ngua ge -implem entation (p re p ro ce ss o r, compiler, link er, stand a rd -lib ra ry) is h a rd ly the only useful th ing fo r deve lop ing soft w a re. Th e rest of the c ou rse : ? T o ols (re compila tion m a nagers, ve rsion control, d e b ugge rs, p rofilers) ? Sof tw a re -e n ginee ring is sues ? A taste of C+ + ? Concur renc y ? So c ietal implication s CSE 3 03 W in ter 20 09, Lectur e 14 3 a39 a38 a36 a37 mak e make is a c la ssic p rogra m fo r controllin g wha t gets (r e )com p ile d a nd ho w. Many other su c h p rograms exis t (e.g., ant , maven , ?p roj e cts ? in IDEs , . . . ) make ha s tons of fa ncy fe a tu res , b ut on ly tw o ba sic ideas: 1. Scrip ts fo r exe cuting com mand s 2. De p e n dencie s fo r a voiding un nec es sa ry w o rk T o a void ?ju st te a c h ing m a k e featur e s? (b o rin g a nd n a rro w), let ?s fo c u s m o re on the conce p ts. . . CSE 3 03 W in ter 20 09, Lectur e 14 4 a39 a38 a36 a37 Bui ld scriptin g Progr amm ers sp e n d a lot of time ?b uildi ng? (c re a ting p rogra m s from source co de) ? Progr ams they wri te ? Progr ams other p eople w rite Progr amm ers automate re p e titive ta sks. T rivia l example: gcc -Wall -g -o myprog foo.c bar.c baz.c If you: ? Ret yp e this e very tim e: ?sha m e, shame ? ? Use u p-a rr o w o r h ist o ry : ?sha m e? (re typ e af te r logou t) ? Have a n alia s o r ba sh script: ?go o d -th inkin ? ? Have a Ma k e file: y ou ?re a head of us CSE 3 03 W in ter 20 09, Lectur e 14 5 a39 a38 a36 a37 ?Real? bu ild p ro cess es On la rge r p roje cts, you can? t o r don ?t w an t to have one bi g (set of ) com mand (s) th at re d o es e verythin g eve ry time you c h ange anythi ng. 1. If gcc did n?t c ombin e ste ps b ehin d y ou r back, you c oul d ne ed to p rep ro c es s an d c ompile each file, th e n call the link e r. 2. If a nother p rogr am (e.g., sed ) c re a te d som e C files, you w ould nee d a n ?e a rlier? step. 3. If you ha ve other outpu ts fo r the same sour c e files (e.g., javadoc ), it?s unp le a sa nt to typ e th e source files multiple tim es . 4. If you w a nt to d is tr ibute sou rc e c o d e to b e b uilt b y oth e r users. 5. If you ha ve 10 5 to 10 7 lin e s of sou rc e c o d e , you don ?t w ant to rec ompil e them all eve ry time y ou cha nge som ethin g. A sim p le sc ript ha ndles 1?4 (u se a va ria ble fo r the fi le na m es fo r 3), b ut 5 is trickier. CSE 3 03 W in ter 20 09, Lectur e 14 6 a39 a38 a36 a37 Rec o mp ilation m a n a geme nt Th e ?theo ry? b ehind a voiding un nec es sa ry com p ilation is a ?dep end e n c y dag?: ? T o create a ta rget t , you ne ed source s s 1 ,s 2 ,. . . ,s n an d a com mand c (tha t d irec tly o r indir e ctly u ses th e sou rce s) ? If t is new er tha n e very sou rc e (file -mo dification time s), assum e there is no reason to re b uild it. ? Rec u rsive bui lding: If some source s i is itself a ta rget fo r some other sou rce s, see if it needs to b e re b uilt. Etc . ? Cycle s ?m a k e no sense ? CSE 3 03 W in ter 20 09, Lectur e 14 7 a39 a38 a36 a37 Theo ry a p pli ed to C An othe r whol e le ctur e on lin king is in our fu ture (w ell, a c tu ally , w e did som e of it ea rli e r) , b ut here is w ha t you need to kn o w to da y fo r C: ? Compilin g a .c cre a te s a .o an d dep ends on all in c lu de d files (recursive ly /tr ansitively). ? Creatin g a n e xec u tabl e (?linki ng?) dep ends on .o files . ? So if on e .c file cha nge s, ju st nee d to rec re a te one .o file a nd relink . ? If a h e a der-file c h an ge s, m a y nee d to rebui ld m o re. ? Of course , th is is on ly th e sim p le st situa tion. CSE 3 03 W in ter 20 09, Lectur e 14 8 a39 a38 a36 a37 An a lg o rith m What w ou ld a p rogr am (e.g., a shell scrip t) th at did this fo r you lo ok lik e? It w ou ld tak e : ? a bu nch of tripl e s: ta rget , sour c es , com man d(s) ? a ?curr e n t ta rge t to b uild? It w ou ld c ompute w h at c omm a nds ne eded to b e exe cuted, in what o rder, a nd do it. (It w oul d de tec t c ycles an d give an err o r.) Th is is e xa c tly w h at p rogra m s lik e make , ant , an d things integrated into IDEs do! CSE 3 03 W in ter 20 09, Lectur e 14 9 a39 a38 a36 a37 ma ke basi cs Th e ?trip le s? a re typ e d in to a ?mak efi le ? lik e this: target: sources command Example: foo.o: foo.c foo.h bar.h gcc -Wall -o foo.o -c foo.c Syn tax got cha s: ? Th e colon af ter the ta rge t is re q uired. ? Comm a nd lin e s must sta rt with a T AB NO T SP A CE S ? Y ou c a n actuall y ha ve multiple c omm a nds (e xec u te d in o rd e r); if one c omm a nd span s lin e s y ou must end the p rev ious lin e with \ . ? Which sh e ll-la ngua ge interp re ts the c omm a nds? (T yp ic a lly b ash, to b e sur e se t the S H ELL va riab le in your mak e file.) CSE 3 03 W in ter 20 09, Lectur e 14 1 0 a39 a38 a36 a37 Usin g ma ke A t the p rom pt: prompt% make -f nameOfMakefile aTarget De fau lts : ? If n o -f sp ec ified, use a file named Makefile . ? If n ot ta rge t sp ec ified, use the fi rst one in th e file. T oge th e r: Y ou c a n do wnload a ta rba ll, e xtr act it, typ e make (f our cha ra c ters) and eve rything should w o rk. Actua lly , there?s typically a ?configure? step to o, fo r findi ng th ings lik e ?where is th e com pi le r? tha t gene rates the Makefile (bu t w e w on ?t get in to tha t). CSE 3 03 W in ter 20 09, Lectur e 14 1 1 a39 a38 a36 a37 Basics Sum ma ry So fa r, enough fo r h om ew o rk 4 an d basic u se. ? A to ol tha t com b ines sc ri pting with d e p e n dency a nalysis to avoid un nec es sa ry recom pi lation. ? Not la ngua ge o r to ol-s p ec ific: ju st ba sed on file -mo dification time s an d sh e ll -com man ds. Bu t there?s so m u c h mo re you w a nt to do so th at y ou r Mak e files a re : ? Sh o rt a nd mo du la r ? Easy to reuse (with differe n t fla gs , pl atfo rms, e tc.) ? Useful fo r man y ta sks ? Au tom a tic a lly main tain e d with res p e ct to dep end e n c ies . Also, read ing others? mak efile s c a n b e tou gh b ec a use of a ll th e featu res : se e info make o r e n tire b o oks. CSE 3 03 W in ter 20 09, Lectur e 14 1 2 a39 a38 a36 a37 Pr e cise review A Mak efile ha s a b unch of thes e: target: source_1 ... source_n shell_command Run nin g make target do e s th is : ? F o r each sou rce , if it is a ta rge t in the Ma k e file, p ro ces s it rec u rsive ly ? Th e n : ? If some sou rce do es n ot exis t, erro r. ? If some sou rce is n e w e r tha n the ta rge t (o r ta rget d o e s not ex ist), ru n shell_command (p re sumabl y up da te s target , bu t tha t is up to y ou ). CSE 3 03 W in ter 20 09, Lectur e 14 1 3 a39 a38 a36 a37 mak e v a riable s Y ou c a n defi n e va ri ables in a M a k e file. E xa m p le : CC = gcc CFLAGS = -Wall foo.o: foo.c foo.h bar.h $(CC) $(CFLAGS) -c foo.c -o foo.o Why d o th is ? ? Easy to chan ge thin gs once an d affe ct man y com man ds. ? Ca n c ha nge va riab le s on the com man d-line (overr ides definitions in file). (F o r e xa m p le make CFLAGS=-g .) ? Easy to reuse mos t of a Ma k efile from one ?home w o rk? to the next. ? Ca n use c on ditiona ls to se t va ria bles (using in he rite d envir onm ent va ri ables). . . . CSE 3 03 W in ter 20 09, Lectur e 14 1 4 a39 a38 a36 a37 mak e co n ditionals EXE= ifdef WINDIR # assume we are on a Windows machine EXE=.exe endif myprog$(EXE): foo.o bar.o $(CC) $(CFLAGS) -o myprog$(EXE) foo.o bar.o Other fo rms of condi tionals e xist (e .g. , a re tw o strings e q ual) CSE 3 03 W in ter 20 09, Lectur e 14 1 5 a39 a38 a36 a37 mo re va ri a b les It?s also c omm on to use va ri ables to hold list of file n ame s: OBJFILES = foo.o bar.o baz.o myprog: $(OBJFILES) gcc -o myprog $(OBJFILES) clean: rm $(OBJFILES) myprog clean is a c on v ention : re move a ny genera te d files , to ?s ta rt ov er? an d ha ve ju st the sour c e. It?s ?fun ny? b ec a use the ta rge t do e sn?t e xist and th e re a re n o sour c es, bu t tha t?s ok a y: ? If ta rge t do esn?t e xist, it must b e ?r e made? so ru n the com man ds ? Th e se ?p hony? ta rgets ha ve se veral uses , a nother is a n ?all? ta rget. . . . CSE 3 03 W in ter 20 09, Lectur e 14 1 6 a39 a38 a36 a37 ?a ll? exampl e all: prog B.class someLib.a # notice no commands this time prog: foo.o bar.o main.o gcc -o prog foo.o bar.o main.o B.class: B.java javac B.java someLib.a: foo.o baz.o ar r foo.o baz.o foo.o: foo.c foo.h header1.h header2.h gcc -c -Wall foo.c ... (similar targets for bar.o, main.o, baz.o) ... CSE 3 03 W in ter 20 09, Lectur e 14 1 7 a39 a38 a36 a37 Reven g e o f fu nny c ha racters UNIX ha c k ers just c a n?t ge t e nou gh of funn y me tacha racte rs c a n th e y? In com man ds: ? $@ fo r ta rge t ? $^ fo r a ll sou rc es ? $< fo r left-m ost sou rce ? . . . Examples : myprog$(EXE): foo.o bar.o $(CC) $(CFLAGS) -o $@ $^ foo.o: foo.c foo.h bar.h $(CC) $(CFLAGS) -c $< CSE 3 03 W in ter 20 09, Lectur e 14 1 8 a39 a38 a36 a37 Mo re fanc y stuff ? Th e re a re a lot of ?built-in? ru le s. E.g., make ju st ?kno ws ? to cre a te foo.o b y c a lling $(CC) $(CFLAGS) on foo.c . (Opin ion: mo re c on fu sin g th an helpf ul. YMMV ) ? Th e re a re ?s u ffix? rules an d ?pattern? ru le s. Example: %.class: %.java javac $< # Note we need $< here ? Rem em b e r you can pu t a ny sh e ll com man d on th e com mand -line, ev en whole scripts ? Y ou c a n rep eat ta rget n ame s to ad d m o re de p endencie s (use ful with automatic de p endency generation ). Often this stuff is mo re useful fo r re a ding mak e files th an writing your o w n (until som e d a y. . . ) CSE 3 03 W in ter 20 09, Lectur e 14 1 9 a39 a38 a36 a37 Dep end ency g en era tion So fa r, w e a re still listing dep endencies m a nu ally , e.g.: foo.o: foo.c foo.h bar.h If you fo rge t, sa y, bar.h , you c a n intro d uce subtle b ugs in y ou r p rogr am (o r if y ou ?re lucky , ge t confu sin g err o rs). Th is is not m a k e?s p roblem: It ha s no un derstand ing of differe n t p rogr amm in g langu ages , com man ds, etc ., just file-m o d times . Bu t it do es see m to o erro r-p ro n e an d busy-w o rk to have to rem emb er to up d ate d e p e n denc ie s, so there a re often lan gua ge -s p ec ific to ol s tha t do it fo r you. . . CSE 3 03 W in ter 20 09, Lectur e 14 2 0 a39 a38 a36 a37 Dep end ency- g en era to r exa m ple gcc -M ? Actua lly lots of use ful va ria nts, includin g -MM an d -MG . See man gcc ? Au tom a tic a lly creates a ru le fo r you. ? Th e n include the re sultin g file in y ou r Mak efile . ? T ypically ru n v ia a phon y depend ta rget, e .g.: depend: $(PROGRAM_C_FILES) gcc -M $^ ? Th e p rogr am makedepend com bi ne s m a ny of thes e steps; a gain it is C -sp ecific but som e oth e r la ngua ge s have th e ir o wn. CSE 3 03 W in ter 20 09, Lectur e 14 2 1 a39 a38 a36 a37 Bui ld- script summ a ry Alw a ys script c ompli c a ted tasks. Alw a ys a utomate ?wha t needs rebu ildin g? via d e p e n dency a nalysis. make is a text- b ase d p rogra m with lots of b e lls and whistles fo r d oing this. It is not lan gua ge -s p ec ific. U se it. With langu age- sp e cific to ols, y ou can a utomate dep e nd e n c y gene ration . make files ha ve this w a y of sta rting sim p le an d e nd ing up u nread able. It is w o rth k e eping them cle a n. Th e re a re c on v ention s lik e make all an d make clean com mon w h e n distrib uting source co de. CSE 3 03 W in ter 20 09, Lectur e 14 2 2
Back
Next
About this note
By: Maziar Rezakhani
Created: 2009-03-05
File Size: 22 page(s)
Views: 1
Created: 2009-03-05
File Size: 22 page(s)
Views: 1
About StudyBlue
STUDYBLUE makes things that make you better at school.
Things like online flashcards with photos and audio.
Things like personalized quizzes and friendly reminders about when (and what) to study next.
Think of it as a digital backpack™: access to all of your study materials online and on your phone.
STUDYBLUE exists to make studying efficient and effective for every student, for free. Join us.
“I have used this website for three exams, and I see a huge difference in my test results.”
Naj
Naj