introduction
DESCRIPTION
Introduction. Use of makefiles to manage the build process Declarative, imperative and relational rules Environment variables, phony targets, automatic variables, macros and pattern matching in makefiles Creating recursive makefiles. Build Management. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/1.jpg)
Introduction
• Use of makefiles to manage the build process
• Declarative, imperative and relational rules
• Environment variables, phony targets, automatic variables, macros and pattern matching in makefiles
• Creating recursive makefiles
![Page 2: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/2.jpg)
Build Management
• During the implementation phase, the process for constructing a system should be engineered– What are the steps needed to build the
system?– Who is authorised to build a system?
• Individual programmers, build/configuration managers
– When are system builds performed?
![Page 3: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/3.jpg)
Build Management and Tools
• Most modern programming environments have build management capabilities built into them– E.g. a Java development environment
typically has the notion of a “project” and it can compile all project files in the correct order (and it only compiles files dependent on a change)
![Page 4: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/4.jpg)
UNIX Build Management
• In UNIX environments, a common management tool is “make”
• Make uses three specification styles– Declarative– Imperative– Relational
• These styles are combined into “the makefile”
![Page 5: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/5.jpg)
Specification Styles
• Operational or Imperative– Describes the actions to be taken
• Descriptive or Declarative– Describes the desired properties
• Structural or Relational– Describes relationships between things
![Page 6: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/6.jpg)
The Make Specification Language
• Dependencies between things (modules, files, etc.) are relational
• Rules for creating new things are declarative
• The Actions needed to carry out the rules are imperative
![Page 7: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/7.jpg)
Make Command Line$ make
• Make will look for a file called makefile or Makefile
• Make looks inside the file for a target– A target can be a file to be generated (but not
necessarily!)
• Different targets can be specified - frequently all and clean - and called as make all. If no target specified (i.e. just make is called) then it picks the first in the file
![Page 8: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/8.jpg)
A Typical Make Rule
T1 is the target T2 and T3 are dependencies - T1 depends
on T2 and T3. These are other targets in the file
A1 and A2 are actions written as a tab followed by a list of UNIX (shell) commands
T1: T2 T3A1A2
![Page 9: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/9.jpg)
Example Makefile
javac calls a UNIX Java compiler.• java runs a UNIX Java program.• jar archives a set of Java classes in
one JAR file.
Targets
Actionmain1.class: main1.java
javac main1.java
![Page 10: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/10.jpg)
Example Makefile
main1.class: mysubroutines.jar main1.javajavac main1.java
mysubroutines.class: mysubroutines.javajavac mysubroutines.java
mysubroutines.jar: mysubroutines.classjar cvf mysubroutines.jar mysubroutines.class
![Page 11: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/11.jpg)
More on Rules
target: dependencies actions
• Target and dependencies are generally files.
• If any dependency is modified more recently than its target then make performs the associated actions.
![Page 12: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/12.jpg)
More on Rules (2)
• An action can be any shell command, one per line. Each action must begin with a tab. All variables used in actions must have ( ) brackets round them – e.g. $(PATH). Different to SHELL programming.
• Typically, actions create the target file from the dependency files.
• GOTCHA: Watch out for actions that do not actually create their target.
![Page 13: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/13.jpg)
More on Actions
• Actions do not have to invoke a compiler
• Targets do not have to be files• Targets, like clean which do not
create files are called phony targets
![Page 14: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/14.jpg)
Example Makefile
all: main1.class
main1.class: mysubroutines.jar main1.javajavac main1.java
mysubroutines.class: mysubroutines.javajavac mysubroutines.java
mysubroutines.jar: mysubroutines.classjar cvf mysubroutines.jar mysubroutines.class
![Page 15: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/15.jpg)
• NOTE: Remember this will remove files!
clean:rm *.classrm *.jar
![Page 16: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/16.jpg)
Using Environment Variables
• Installing in bin isn’t much use if you don’t know where it is!
• Make has variable-like objects known as macros.
• Environment variables, like $HOME and $PATH can be detected by make.
INSTALLDIR = $(HOME)/bininstall: $(INSTALLDIR)/program
$(INSTALLDIR)/program: programcp program $(HOME)/bin/program
![Page 17: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/17.jpg)
Another Makefile
INSTALLDIR = $(HOME)/binall: main1.classmain1.class: mysubroutines.jar main1.java
javac main1.java
install: $(INSTALLDIR)/main1.class
$(INSTALLDIR)/main1.class: main1.classcp main1.class $(INSTALLDIR)/main1.class
![Page 18: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/18.jpg)
Make Macros
• A Macro holds a string value• This string is defined using an equal
sign and preceded by a dollar sign.• Without the brackets make assumes
that the name is just one letter long: $INSTALLDIR is interpreted as $(I)NSTALLDIR
![Page 19: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/19.jpg)
Macro Substitution
• Make performs strict textual replacement to work out what variables are, so the following two rules are equivalent:
program: output.og++ output.o -o program
FOO = opr$(FOO)gram: $(FOO)utput.$(FOO)
g++ $(FOO)utput.$(FOO) -$(FOO) pr$(FOO)gram
![Page 20: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/20.jpg)
Increased Abstraction• Macros increase the level of abstraction
in a Makefilemysubroutines.jar: mysubroutines.class main2.classjar cvf mysubroutines.class main2.class
• is equivalent to
JARFILE = mysubroutines.jarOBJECTS = mysubroutines.class main2.class$(JARFILE): $(OBJECTS)jar cvf $(OBJECTS)
![Page 21: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/21.jpg)
Automatic Variables
• Make has a special feature called automatic variables
• Automatic variables can only be used within the actions of a make rule – its value depends on the target and dependencies of the rule.
![Page 22: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/22.jpg)
Automatic Variables (2)
• $@ – The target of the rule
• $< – The first dependency
• $ˆ – All the dependencies
• $? – All of the dependencies that are newer than the
target.
• $* – The stem of a pattern matching rule
![Page 23: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/23.jpg)
Example of Automatic Variable Use
mysubroutines.jar: mysubroutines.classjar mysubroutines.jar mysubroutines.class
mysubroutines.jar: mysubroutines.classjar $@ $<
mysubs.jar: mysubroutines.class myutil.classjar mysubs.jar mysubroutines.class myutil.class
mysubs.jar: mysubroutines.class myutil.classjar $@ $ˆ
![Page 24: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/24.jpg)
Pattern Matching
• NOTE: These are not present in all versions of Make – but are on unnc-cslinux and most Linux based versions.
• Suppose you have a lot of classes in a directory and you want to compile them individually.
• You could have a rule for each class
![Page 25: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/25.jpg)
Pattern Matching (2)
• Or you could use a pattern:
exercise1.class: exercise1.javajavac exercise1.java
exercise2.class: exercise2.javajavac exercise2.java
%.class: %.javajavac $<
![Page 26: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/26.jpg)
Example Makefile
INSTALLDIR = $(HOME)/bin
all: main1.class
install: $(INSTALLDIR)/main1.class
$(INSTALLDIR)/main1.class: main1.classcp main1.class $(INSTALLDIR)/main1.class
![Page 27: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/27.jpg)
%.class: %.javajavac $<
clean:rm *.classrm *.jar
![Page 28: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/28.jpg)
Benefits of Pattern Matching
• Scalability – The same rule can apply to thousands
(or more) files.• Compactness
– Small compact specifications are easier to understand and debug.
• These are similar to the benefits of using wild-cards and regular expressions.
![Page 29: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/29.jpg)
Managing More Complex Projects with Make
• Many large projects will contain a lot of subprograms, the code will be spread over several directories and all will have to be compiled to get the final system to work.– You can manage this using several
Makefiles if you chose.– Use cd in the Actions to enter a
subdirectory and then you can call a makefile there.
![Page 30: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/30.jpg)
Example of a Recursive Makefile
all:
cd interface; makecd program1; makecd program2; make
clean
cd interface; make cleancd program1; make cleancd program2; make clean
![Page 31: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/31.jpg)
Summary
• Using a tool, like make, for installation can make your life easier. It saves remembering dependencies, and typing compiler flags etc.
• It will also make life easier for anyone else who wants to use your code. They only have to type make.
• Typically all distributions contain a README (or possibly install.txt) which should tell you what to do, in particular if the Makefile needs to be edited.
![Page 32: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/32.jpg)
Dag
• Can we get circular dependencies?
• Think about building blocks
• Can one block depend on more than one block.
• Can many blocks depend on only one block.
![Page 33: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/33.jpg)
• make will only re-build things that need to be re-built (object or executables that depend on files that have been modified since the last time the objects or executables were built).
• target... : dependencies ...• command• ...• ...• Please note: Each command line must begin
with a tab character.
![Page 34: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/34.jpg)
myMaker2
• EXAMPLE
• HelloMum.class: HelloMum.java
• javac HelloMum.java
• $ make -f myMaker2
• javac HelloMum.java
• $ make -f myMaker2
• make: `HelloMum.class' is up to date.
![Page 35: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/35.jpg)
• HelloMum.class: HelloMum.java
• echo compiling HelloMum
• javac HelloMum.java
• echo finished compiling HelloMum
• clean:
• rm *.class
![Page 36: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/36.jpg)
Touch (to illustrate)
• make -f myMaker2• make: `HelloMum.class' is up to date.• $ touch HelloMum.java• $ make -f myMaker2• echo compiling HelloMum• compiling HelloMum• javac HelloMum.java• echo finished compiling HelloMum• finished compiling HelloMum
![Page 37: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/37.jpg)
macros
• A macro is just text replacement
• $(variable), not ${variable} as in shells
![Page 38: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/38.jpg)
• javac people.java
• people.java:4: cannot find symbol
• symbol : variable person
• location: class people
• person = new person();
• ^
• 1 error
![Page 39: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/39.jpg)
CIRCULAR REFERENCE
• HelloMum.java: HelloMum.java
• javac HelloMum.java
• make -f myMaker2
• make: Circular HelloMum.java <- HelloMum.java dependency dropped.
• make: `HelloMum.java' is up to date.
![Page 40: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/40.jpg)
Question
• Do we need make files for scripts?
![Page 41: Introduction](https://reader033.vdocuments.site/reader033/viewer/2022051820/56813b7c550346895da48ed3/html5/thumbnails/41.jpg)
Online tutorial
• http://www.eng.hawaii.edu/Tutor/Make/index.html