Slide 1

Slide 1 text

Introduction to Makefile Ray Song February 25, 2012 Ray Song Introduction to Makefile

Slide 2

Slide 2 text

Motivation Small programs -> single file -> manual compilation “not so small” programs many files multiple components more than one programmers Ray Song Introduction to Makefile

Slide 3

Slide 3 text

Motivation Small programs -> single file -> manual compilation “not so small” programs many files multiple components more than one programmers Ray Song Introduction to Makefile

Slide 4

Slide 4 text

Motivation Small programs -> single file -> manual compilation “not so small” programs many files multiple components more than one programmers Ray Song Introduction to Makefile

Slide 5

Slide 5 text

Motivation Small programs -> single file -> manual compilation “not so small” programs many files multiple components more than one programmers Ray Song Introduction to Makefile

Slide 6

Slide 6 text

Motivation Small programs -> single file -> manual compilation “not so small” programs many files multiple components more than one programmers Ray Song Introduction to Makefile

Slide 7

Slide 7 text

Motivation - cont. Problems harder to manage every change require long compilation division to components is desired Ray Song Introduction to Makefile

Slide 8

Slide 8 text

Motivation - cont. Problems harder to manage every change require long compilation division to components is desired Ray Song Introduction to Makefile

Slide 9

Slide 9 text

Motivation - cont. Problems harder to manage every change require long compilation division to components is desired Ray Song Introduction to Makefile

Slide 10

Slide 10 text

Motivation - cont. Problems harder to manage every change require long compilation division to components is desired Ray Song Introduction to Makefile

Slide 11

Slide 11 text

Motivation - cont. Solution - Makefile Ray Song Introduction to Makefile

Slide 12

Slide 12 text

Makefile Makefile describes project structure instructions for files creation A makefile consists of many rules. Ray Song Introduction to Makefile

Slide 13

Slide 13 text

Makefile Makefile describes project structure instructions for files creation A makefile consists of many rules. Ray Song Introduction to Makefile

Slide 14

Slide 14 text

Makefile Makefile describes project structure instructions for files creation A makefile consists of many rules. Ray Song Introduction to Makefile

Slide 15

Slide 15 text

Makefile Makefile describes project structure instructions for files creation A makefile consists of many rules. Ray Song Introduction to Makefile

Slide 16

Slide 16 text

Rule syntax TARGETS: PREREQUISITES RECIPE In short, each rule describe instructions (RECIPE) to create files (TARGETS) with PREREQUISITES. PREREQUISITES are targets must be created prior to TARGETS. A target is considered old if its modification timestamp is smaller than one of its dependencies’s. Ray Song Introduction to Makefile

Slide 17

Slide 17 text

Rule syntax TARGETS: PREREQUISITES RECIPE In short, each rule describe instructions (RECIPE) to create files (TARGETS) with PREREQUISITES. PREREQUISITES are targets must be created prior to TARGETS. A target is considered old if its modification timestamp is smaller than one of its dependencies’s. Ray Song Introduction to Makefile

Slide 18

Slide 18 text

Rule syntax TARGETS: PREREQUISITES RECIPE In short, each rule describe instructions (RECIPE) to create files (TARGETS) with PREREQUISITES. PREREQUISITES are targets must be created prior to TARGETS. A target is considered old if its modification timestamp is smaller than one of its dependencies’s. Ray Song Introduction to Makefile

Slide 19

Slide 19 text

Makefile – cont. TARGETS and PREREQUISITES are file names separated by spaces. Usually there is only one target per rule. TARGETS and PREREQUISITES may contain wildcards, e.g. %.c. Each line of RECIPE starts with a TAB. The first rule indicates the default target (not counting targets that contain wildcards). Ray Song Introduction to Makefile

Slide 20

Slide 20 text

Makefile – cont. TARGETS and PREREQUISITES are file names separated by spaces. Usually there is only one target per rule. TARGETS and PREREQUISITES may contain wildcards, e.g. %.c. Each line of RECIPE starts with a TAB. The first rule indicates the default target (not counting targets that contain wildcards). Ray Song Introduction to Makefile

Slide 21

Slide 21 text

Makefile – cont. TARGETS and PREREQUISITES are file names separated by spaces. Usually there is only one target per rule. TARGETS and PREREQUISITES may contain wildcards, e.g. %.c. Each line of RECIPE starts with a TAB. The first rule indicates the default target (not counting targets that contain wildcards). Ray Song Introduction to Makefile

Slide 22

Slide 22 text

Makefile – cont. TARGETS and PREREQUISITES are file names separated by spaces. Usually there is only one target per rule. TARGETS and PREREQUISITES may contain wildcards, e.g. %.c. Each line of RECIPE starts with a TAB. The first rule indicates the default target (not counting targets that contain wildcards). Ray Song Introduction to Makefile

Slide 23

Slide 23 text

Makefile – cont. TARGETS and PREREQUISITES are file names separated by spaces. Usually there is only one target per rule. TARGETS and PREREQUISITES may contain wildcards, e.g. %.c. Each line of RECIPE starts with a TAB. The first rule indicates the default target (not counting targets that contain wildcards). Ray Song Introduction to Makefile

Slide 24

Slide 24 text

make’s mechanism make command reads a makefile and records these rules into its data base. GNU Make defaults to search GNUmakefile, makefile, Makefile in order, use the first of these which exists. The first goal (terminology used to refer to the list of targets you specified on the command line) should be created. Prerequisites which appeared in the target must be processed first. This is a recursive process (depth first search). After updating the dependencies , make decides whether it is necessary to recreated the target. This is the case when it is older than one of its dependencies. In the case we recreate the target, execute the associated recipe. Ray Song Introduction to Makefile

Slide 25

Slide 25 text

make’s mechanism make command reads a makefile and records these rules into its data base. GNU Make defaults to search GNUmakefile, makefile, Makefile in order, use the first of these which exists. The first goal (terminology used to refer to the list of targets you specified on the command line) should be created. Prerequisites which appeared in the target must be processed first. This is a recursive process (depth first search). After updating the dependencies , make decides whether it is necessary to recreated the target. This is the case when it is older than one of its dependencies. In the case we recreate the target, execute the associated recipe. Ray Song Introduction to Makefile

Slide 26

Slide 26 text

make’s mechanism make command reads a makefile and records these rules into its data base. GNU Make defaults to search GNUmakefile, makefile, Makefile in order, use the first of these which exists. The first goal (terminology used to refer to the list of targets you specified on the command line) should be created. Prerequisites which appeared in the target must be processed first. This is a recursive process (depth first search). After updating the dependencies , make decides whether it is necessary to recreated the target. This is the case when it is older than one of its dependencies. In the case we recreate the target, execute the associated recipe. Ray Song Introduction to Makefile

Slide 27

Slide 27 text

make’s mechanism make command reads a makefile and records these rules into its data base. GNU Make defaults to search GNUmakefile, makefile, Makefile in order, use the first of these which exists. The first goal (terminology used to refer to the list of targets you specified on the command line) should be created. Prerequisites which appeared in the target must be processed first. This is a recursive process (depth first search). After updating the dependencies , make decides whether it is necessary to recreated the target. This is the case when it is older than one of its dependencies. In the case we recreate the target, execute the associated recipe. Ray Song Introduction to Makefile

Slide 28

Slide 28 text

make’s mechanism make command reads a makefile and records these rules into its data base. GNU Make defaults to search GNUmakefile, makefile, Makefile in order, use the first of these which exists. The first goal (terminology used to refer to the list of targets you specified on the command line) should be created. Prerequisites which appeared in the target must be processed first. This is a recursive process (depth first search). After updating the dependencies , make decides whether it is necessary to recreated the target. This is the case when it is older than one of its dependencies. In the case we recreate the target, execute the associated recipe. Ray Song Introduction to Makefile

Slide 29

Slide 29 text

make’s mechanism make command reads a makefile and records these rules into its data base. GNU Make defaults to search GNUmakefile, makefile, Makefile in order, use the first of these which exists. The first goal (terminology used to refer to the list of targets you specified on the command line) should be created. Prerequisites which appeared in the target must be processed first. This is a recursive process (depth first search). After updating the dependencies , make decides whether it is necessary to recreated the target. This is the case when it is older than one of its dependencies. In the case we recreate the target, execute the associated recipe. Ray Song Introduction to Makefile

Slide 30

Slide 30 text

make’s mechanism - cont. make virtually construct a dependency DAG (directed acyclic graph). make ensures minimum compilation as long as the project structure is written properly. Do not write something like: prog: main.c sum1.c sum2.c gcc –o prog main.c sum1.c sum2.c which requires compilation of all project when something is changed Ray Song Introduction to Makefile

Slide 31

Slide 31 text

make’s mechanism - cont. make virtually construct a dependency DAG (directed acyclic graph). make ensures minimum compilation as long as the project structure is written properly. Do not write something like: prog: main.c sum1.c sum2.c gcc –o prog main.c sum1.c sum2.c which requires compilation of all project when something is changed Ray Song Introduction to Makefile

Slide 32

Slide 32 text

make’s mechanism - cont. make virtually construct a dependency DAG (directed acyclic graph). make ensures minimum compilation as long as the project structure is written properly. Do not write something like: prog: main.c sum1.c sum2.c gcc –o prog main.c sum1.c sum2.c which requires compilation of all project when something is changed Ray Song Introduction to Makefile

Slide 33

Slide 33 text

make’s mechanism - cont. make virtually construct a dependency DAG (directed acyclic graph). make ensures minimum compilation as long as the project structure is written properly. Do not write something like: prog: main.c sum1.c sum2.c gcc –o prog main.c sum1.c sum2.c which requires compilation of all project when something is changed Ray Song Introduction to Makefile

Slide 34

Slide 34 text

make’s mechanism - cont. make virtually construct a dependency DAG (directed acyclic graph). make ensures minimum compilation as long as the project structure is written properly. Do not write something like: prog: main.c sum1.c sum2.c gcc –o prog main.c sum1.c sum2.c which requires compilation of all project when something is changed Ray Song Introduction to Makefile

Slide 35

Slide 35 text

make’s mechanism - cont. make virtually construct a dependency DAG (directed acyclic graph). make ensures minimum compilation as long as the project structure is written properly. Do not write something like: prog: main.c sum1.c sum2.c gcc –o prog main.c sum1.c sum2.c which requires compilation of all project when something is changed Ray Song Introduction to Makefile

Slide 36

Slide 36 text

Automatic variables $@ $ˆ $< others including $, $?, $+, $|, $%, $(%D), %(F), . . . Ray Song Introduction to Makefile

Slide 37

Slide 37 text

Automatic variables $@ $ˆ $< others including $, $?, $+, $|, $%, $(%D), %(F), . . . Ray Song Introduction to Makefile

Slide 38

Slide 38 text

Automatic variables $@ $ˆ $< others including $, $?, $+, $|, $%, $(%D), %(F), . . . Ray Song Introduction to Makefile

Slide 39

Slide 39 text

Automatic variables $@ $ˆ $< others including $, $?, $+, $|, $%, $(%D), %(F), . . . Ray Song Introduction to Makefile

Slide 40

Slide 40 text

Example convert: cmdline.o convert.o g++ $ˆ -o $@ convert.o: convert.cpp convert.h g++ -c $< cmdline.o: cmdline.cpp cmdline.h convert.h g++ -c $< Ray Song Introduction to Makefile

Slide 41

Slide 41 text

Equivalent (implicit rules) make can dedude appropriate recipes according to suffixes convert: cmdline.o convert.o convert.o: convert.cpp convert.h cmdline.o: cmdline.cpp cmdline.h convert.h Ray Song Introduction to Makefile

Slide 42

Slide 42 text

Equivalent (implicit rules) - cont. convert: cmdline.o convert.o: convert.h cmdline.o: cmdline.h convert.h Ray Song Introduction to Makefile

Slide 43

Slide 43 text

Another example %.o: %.c gcc -c $< Ray Song Introduction to Makefile

Slide 44

Slide 44 text

Passing parameters test: FORCE echo $(VAR) FORCE: make VAR=hello make VAR=world Ray Song Introduction to Makefile

Slide 45

Slide 45 text

Passing parameters test: FORCE echo $(VAR) FORCE: make VAR=hello make VAR=world Ray Song Introduction to Makefile

Slide 46

Slide 46 text

Force targets Targets without recipes or prerequisites Ray Song Introduction to Makefile

Slide 47

Slide 47 text

Phony targets They do not correspond to real file. Provide some utility, e.g. cleaning intermediate files, archiving the whole project, creating TAGS file for some editors Forced to run its recipe upon executing. Ray Song Introduction to Makefile

Slide 48

Slide 48 text

Phony targets They do not correspond to real file. Provide some utility, e.g. cleaning intermediate files, archiving the whole project, creating TAGS file for some editors Forced to run its recipe upon executing. Ray Song Introduction to Makefile

Slide 49

Slide 49 text

Phony targets They do not correspond to real file. Provide some utility, e.g. cleaning intermediate files, archiving the whole project, creating TAGS file for some editors Forced to run its recipe upon executing. Ray Song Introduction to Makefile

Slide 50

Slide 50 text

Example all : prog1 prog2 prog3 .PHONY : all prog1 : prog1.o utils.o cc -o prog1 prog1.o utils.o prog2 : prog2.o cc -o prog2 prog2.o prog3 : prog3.o sort.o utils.o cc -o prog3 prog3.o sort.o utils.o Ray Song Introduction to Makefile

Slide 51

Slide 51 text

Practical options of make -n, print the commands to be run but do not execute them -t, touch files instead of running the recipes -B, unconditionally make all targets (overide timestamps) -W file, pretend file has been just modified -p, print the data base that results from reading the makefiles -d, debug mode others, RTFM Ray Song Introduction to Makefile

Slide 52

Slide 52 text

Practical options of make -n, print the commands to be run but do not execute them -t, touch files instead of running the recipes -B, unconditionally make all targets (overide timestamps) -W file, pretend file has been just modified -p, print the data base that results from reading the makefiles -d, debug mode others, RTFM Ray Song Introduction to Makefile

Slide 53

Slide 53 text

Practical options of make -n, print the commands to be run but do not execute them -t, touch files instead of running the recipes -B, unconditionally make all targets (overide timestamps) -W file, pretend file has been just modified -p, print the data base that results from reading the makefiles -d, debug mode others, RTFM Ray Song Introduction to Makefile

Slide 54

Slide 54 text

Practical options of make -n, print the commands to be run but do not execute them -t, touch files instead of running the recipes -B, unconditionally make all targets (overide timestamps) -W file, pretend file has been just modified -p, print the data base that results from reading the makefiles -d, debug mode others, RTFM Ray Song Introduction to Makefile

Slide 55

Slide 55 text

Practical options of make -n, print the commands to be run but do not execute them -t, touch files instead of running the recipes -B, unconditionally make all targets (overide timestamps) -W file, pretend file has been just modified -p, print the data base that results from reading the makefiles -d, debug mode others, RTFM Ray Song Introduction to Makefile

Slide 56

Slide 56 text

Practical options of make -n, print the commands to be run but do not execute them -t, touch files instead of running the recipes -B, unconditionally make all targets (overide timestamps) -W file, pretend file has been just modified -p, print the data base that results from reading the makefiles -d, debug mode others, RTFM Ray Song Introduction to Makefile

Slide 57

Slide 57 text

Practical options of make -n, print the commands to be run but do not execute them -t, touch files instead of running the recipes -B, unconditionally make all targets (overide timestamps) -W file, pretend file has been just modified -p, print the data base that results from reading the makefiles -d, debug mode others, RTFM Ray Song Introduction to Makefile

Slide 58

Slide 58 text

Other usage Makefile’s mechanism is not limited to programs LaTeX sources website deployment describe any task where some files need updating as a result of changes of other files Ray Song Introduction to Makefile

Slide 59

Slide 59 text

Other usage Makefile’s mechanism is not limited to programs LaTeX sources website deployment describe any task where some files need updating as a result of changes of other files Ray Song Introduction to Makefile

Slide 60

Slide 60 text

Other usage Makefile’s mechanism is not limited to programs LaTeX sources website deployment describe any task where some files need updating as a result of changes of other files Ray Song Introduction to Makefile

Slide 61

Slide 61 text

Other usage Makefile’s mechanism is not limited to programs LaTeX sources website deployment describe any task where some files need updating as a result of changes of other files Ray Song Introduction to Makefile

Slide 62

Slide 62 text

Additional features Variables (two flavors: simple and recursive, the latter is also called macros) Functions including string substitution, file name manipulation, foreach, even the the root of evil – eval VPATH Ability to manipulate archives(.a) Including other makefiles Conditionals Secondary expansion Order-only prerequisites Static patterns Double-colon rules Target/pattern-specific variable values Ray Song Introduction to Makefile

Slide 63

Slide 63 text

Additional features Variables (two flavors: simple and recursive, the latter is also called macros) Functions including string substitution, file name manipulation, foreach, even the the root of evil – eval VPATH Ability to manipulate archives(.a) Including other makefiles Conditionals Secondary expansion Order-only prerequisites Static patterns Double-colon rules Target/pattern-specific variable values Ray Song Introduction to Makefile

Slide 64

Slide 64 text

Additional features Variables (two flavors: simple and recursive, the latter is also called macros) Functions including string substitution, file name manipulation, foreach, even the the root of evil – eval VPATH Ability to manipulate archives(.a) Including other makefiles Conditionals Secondary expansion Order-only prerequisites Static patterns Double-colon rules Target/pattern-specific variable values Ray Song Introduction to Makefile

Slide 65

Slide 65 text

Additional features Variables (two flavors: simple and recursive, the latter is also called macros) Functions including string substitution, file name manipulation, foreach, even the the root of evil – eval VPATH Ability to manipulate archives(.a) Including other makefiles Conditionals Secondary expansion Order-only prerequisites Static patterns Double-colon rules Target/pattern-specific variable values Ray Song Introduction to Makefile

Slide 66

Slide 66 text

Additional features Variables (two flavors: simple and recursive, the latter is also called macros) Functions including string substitution, file name manipulation, foreach, even the the root of evil – eval VPATH Ability to manipulate archives(.a) Including other makefiles Conditionals Secondary expansion Order-only prerequisites Static patterns Double-colon rules Target/pattern-specific variable values Ray Song Introduction to Makefile

Slide 67

Slide 67 text

Additional features Variables (two flavors: simple and recursive, the latter is also called macros) Functions including string substitution, file name manipulation, foreach, even the the root of evil – eval VPATH Ability to manipulate archives(.a) Including other makefiles Conditionals Secondary expansion Order-only prerequisites Static patterns Double-colon rules Target/pattern-specific variable values Ray Song Introduction to Makefile

Slide 68

Slide 68 text

Additional features Variables (two flavors: simple and recursive, the latter is also called macros) Functions including string substitution, file name manipulation, foreach, even the the root of evil – eval VPATH Ability to manipulate archives(.a) Including other makefiles Conditionals Secondary expansion Order-only prerequisites Static patterns Double-colon rules Target/pattern-specific variable values Ray Song Introduction to Makefile

Slide 69

Slide 69 text

Additional features Variables (two flavors: simple and recursive, the latter is also called macros) Functions including string substitution, file name manipulation, foreach, even the the root of evil – eval VPATH Ability to manipulate archives(.a) Including other makefiles Conditionals Secondary expansion Order-only prerequisites Static patterns Double-colon rules Target/pattern-specific variable values Ray Song Introduction to Makefile

Slide 70

Slide 70 text

Additional features Variables (two flavors: simple and recursive, the latter is also called macros) Functions including string substitution, file name manipulation, foreach, even the the root of evil – eval VPATH Ability to manipulate archives(.a) Including other makefiles Conditionals Secondary expansion Order-only prerequisites Static patterns Double-colon rules Target/pattern-specific variable values Ray Song Introduction to Makefile

Slide 71

Slide 71 text

Additional features Variables (two flavors: simple and recursive, the latter is also called macros) Functions including string substitution, file name manipulation, foreach, even the the root of evil – eval VPATH Ability to manipulate archives(.a) Including other makefiles Conditionals Secondary expansion Order-only prerequisites Static patterns Double-colon rules Target/pattern-specific variable values Ray Song Introduction to Makefile

Slide 72

Slide 72 text

Additional features Variables (two flavors: simple and recursive, the latter is also called macros) Functions including string substitution, file name manipulation, foreach, even the the root of evil – eval VPATH Ability to manipulate archives(.a) Including other makefiles Conditionals Secondary expansion Order-only prerequisites Static patterns Double-colon rules Target/pattern-specific variable values Ray Song Introduction to Makefile

Slide 73

Slide 73 text

Acknowledgements This slide incorporate some stuff from Roded Sharan’s http://www.cs.tau.ac.il/~roded/courses/softp-b06/ Makefile.ppt Markdown http://daringfireball.net/projects/markdown/ in which this slide source is written. Pandoc http://johnmacfarlane.net/pandoc/ by which this slide is generated. Haskell http://www.haskell.org/ in which Pandoc is implemented. Ray Song Introduction to Makefile

Slide 74

Slide 74 text

Acknowledgements This slide incorporate some stuff from Roded Sharan’s http://www.cs.tau.ac.il/~roded/courses/softp-b06/ Makefile.ppt Markdown http://daringfireball.net/projects/markdown/ in which this slide source is written. Pandoc http://johnmacfarlane.net/pandoc/ by which this slide is generated. Haskell http://www.haskell.org/ in which Pandoc is implemented. Ray Song Introduction to Makefile

Slide 75

Slide 75 text

Acknowledgements This slide incorporate some stuff from Roded Sharan’s http://www.cs.tau.ac.il/~roded/courses/softp-b06/ Makefile.ppt Markdown http://daringfireball.net/projects/markdown/ in which this slide source is written. Pandoc http://johnmacfarlane.net/pandoc/ by which this slide is generated. Haskell http://www.haskell.org/ in which Pandoc is implemented. Ray Song Introduction to Makefile

Slide 76

Slide 76 text

Acknowledgements This slide incorporate some stuff from Roded Sharan’s http://www.cs.tau.ac.il/~roded/courses/softp-b06/ Makefile.ppt Markdown http://daringfireball.net/projects/markdown/ in which this slide source is written. Pandoc http://johnmacfarlane.net/pandoc/ by which this slide is generated. Haskell http://www.haskell.org/ in which Pandoc is implemented. Ray Song Introduction to Makefile

Slide 77

Slide 77 text

References GNU Make Manual http://www.gnu.org/software/make/manual/ Ray Song Introduction to Makefile