Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu...
Transcript of Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu...
![Page 1: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/1.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Introduction to Make
Kurt Schmidt
Dept. of Computer Science, Drexel University
November 17, 2018
![Page 2: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/2.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Intro
![Page 3: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/3.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
make
Automates certain tasksUsually simple command-line stuffCompiling multi-file programsArchiving/extractingSoftware installation
Often used to manage buildsCompiles only as necessary
Uses file modification times to decide when it isnecessary
![Page 4: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/4.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Basic Make
![Page 5: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/5.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Make Rules
A basic makefile consists of rulestarget : dependencies<tab>command1<tab>[command2]...
The tab character precedes the ruleThe target is (usually) a file to be createdEach command is executed in its own shell1
1We’ll look at multi-line commands in a bit
![Page 6: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/6.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Make Example
E.g.
program : main.cgcc main.c -oprogram
main.c should already existOr, there’s another target that creates it
main.c will only be compiled if:1 program doesn’t exist, or2 main.c is newer than program
![Page 7: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/7.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Dependency Recursion
Dependencies are checked recursively down the tree:
program : main.ogcc main.o -oprogram
main.o : main.cgcc -c main.c
Nothing happens if program is newer than main.o, andmain.o is newer than main.c
If main.o doesn’t exist, or is older than main.c, it will berebuilt, then program will be rebuiltIf program doesn’t exist, or is older than main.o, it willbe rebuilt
![Page 8: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/8.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Slightly More Involved Example
program : main.o service.ogcc main.o service.o -oprogram
service.o : service.c service.hgcc -c service.c
main.o : main.c service.hgcc -c main.c
If main.c is updated, then main.o and program arerebuiltIf service.c is updated, then service.o and programare rebuiltIf service.h is updated, everybody is updated
![Page 9: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/9.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Recipe Without Commands
A target may simply depend on other targets:
makefile
all : this that other
this : this.cgcc this.c -o this
that : that.cgcc that.c -o that
other : other.cgcc other.c -o other
![Page 10: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/10.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Continuing Lines
![Page 11: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/11.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Continuing Lines
Use \ to continue a dependency list or a commandprogram are rebuilt
program : main.o curses.o utils.o keyboard.o \deck.o suits.o
gcc -oprogram main.o curses.o utils.o keyboard.o \deck.o suits.o
...
![Page 12: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/12.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Multi-Line Commands
So now you can pass more than one line to the shellBeware, the shell won’t get any newlines (youescaaped them)So, use the shell’s separator (most shells use ; )
input :f=‘mktemp‘ ;\i=1 ;\while [ $$i -le 10000 ] ;\
do echo $$i >> "$$f" ;\i=‘expr $$i + 1‘ ;\
done ;\shuf "$$f" >> input ;\rm "$$f"
Note, make uses Bourne (or, a minimal,Bourne-compliant) shell by default
![Page 13: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/13.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Command Prefixes
![Page 14: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/14.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Command Prefixes
make echos recipe commands to stdoutTurn off make’s echo by preceding line with a @
blah :@echo "Don’t say this line twice"
If any command returns an unsuccessful status, makereports the error and exitsPrecede a line with a - to have make ignore the statusNote, each of those rm statements happens in aseparate shell
clean :-rm program # fails if program doesn’t exist-rm *.o # We want this to happen, regardless
![Page 15: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/15.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Invocation
![Page 16: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/16.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Specifying Input File
Specify a makefile using the -f option to make:
$ make -f someMakeFile
If not specified, GNU make looks in the current directoryfor:
1 GNUmakefile1
2 makefile3 Makefile
1Only use this if file has Gnu make extensions
![Page 17: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/17.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Specifying a Target
Make allows you to specify target(s)make [options] [target]
If no target is specified, make builds the first target itfinds-n (dry run) is another handy option
Just print commands that would execute, w/outexecuting them
![Page 18: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/18.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Example – Invoking Make
Given the makefile in example 1:
$ make
or
$ make all
will make all
$ make that
will attempt that
If the file were called silly, then use
$ make -f silly other
to make other
![Page 19: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/19.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Phony Targets (Gnu only)
Some targets exist for convenienceWe don’t actually want to produce a fileCommands won’t run if a file of the same name existsWe can declare targets as phony:
.PHONY : clean
clean :-rm program # fails if program doesn’t exist-rm *.o # We want this to happen, regardless
No times are compared, commands run every time
![Page 20: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/20.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Macros
![Page 21: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/21.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Defining Macros in a Makefile
Macros can be defined in a makefile:
OBJS = main.o curses.o utils.o keyboard.o \deck.o suits.o
cc = gccCFLAGS =
program : $(OBJS)$(cc) $(CFLAGS) $(OBJS) -o program
main.o : main.c$(cc) -c $(CFLAGS) main.c
$(OBJS) : sysdefs.h
...
![Page 22: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/22.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Macro Substitution
Evaluates the macro, after some substitutions.
SOURCE = main.c curses.c utils.c keyboard.c \deck.c suits.c
OBJS = ${SOURCE:.c=.o}
cc = gccCFLAGS =
...
![Page 23: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/23.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Defined Macros
$@ Name of current target$< Name of first prerequisite$^ All prerequisites$? All prerequisites newer than target
program : main.c service.h$(cc) $(CFLAGS) $< -o $@
...
![Page 24: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/24.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Choosing a Different Shell
If you want to use a different shell, say, bash, tointerpret the commandsSet the SHELL variable at the top to modify allcommands:
SHELL := /bin/bash
...
You can do this for individual targets:
program : SHELL:=/bin/bashprogram : main.c service.h
$(cc) $(CFLAGS) $< -o $@...
![Page 25: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/25.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Suffix Rules
Some rules easy enough to be generalisedIf target has the same name as a dependency, butdifferent suffixE.g., compile C files into object code
big.o : big.c this.h that.h other.h
%.o : %.c$(cc) -c $(CFLAGS) $<
Other dependencies can be namedCan also be specified this way:
.c.o :$(cc) -c $(CFLAGS) $<
![Page 26: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/26.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Recap
![Page 27: Introduction to Makekschmidt/CS265/Lectures/Make/make.pdf3 Makefile 1Only use this if file has Gnu make extensions. Introduction to Make Kurt Schmidt Intro Basic Make Dependencies](https://reader033.fdocuments.in/reader033/viewer/2022060913/60a79f314c129202710e5836/html5/thumbnails/27.jpg)
Introduction toMake
Kurt Schmidt
Intro
Basic MakeDependencies
ContinuingLines
CommandPrefixes
Invocation
Macros
Recap
Recap
Make files can do anything you do at the command lineCare has to be taken to make them portableWe’ve looked at fairly simply makefiles
Still wildly usefulMakefile might call other makefilesMacros can be defined in a separate file, used byseveral makefiles