1 / 34

15 Make

15 Make. Mauro Jaskelioff (Originally by Gail Hopkins). 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.

xander
Download Presentation

15 Make

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 15 Make Mauro Jaskelioff (Originally by Gail Hopkins)

  2. 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

  3. 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?

  4. 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)

  5. 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”

  6. 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

  7. 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

  8. 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

  9. 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 T3 A1 A2

  10. Example Makefile Targets javac calls a UNIX Java compiler. • java runs a UNIX Java program. • jar archives a set of Java classes in one JAR file. main1.class: main1.java javac main1.java Action

  11. Example Makefile main1.class: mysubroutines.jar main1.java javac main1.java mysubroutines.class: mysubroutines.java javac mysubroutines.java mysubroutines.jar: mysubroutines.class jar cvf mysubroutines.jar mysubroutines.class

  12. 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.

  13. 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). • Typically, actions create the target file from the dependency files. • GOTCHA: Watch out for actions that do not actually create their target.

  14. More on Actions • Actions do not have to invoke a compiler • Targets do not have to be files • Targets, like clean and all which do not create files are called phony targets

  15. Example Makefile all: main1.class main1.class: mysubroutines.jar main1.java javac main1.java mysubroutines.class: mysubroutines.java javac mysubroutines.java mysubroutines.jar: mysubroutines.class jar cvf mysubroutines.jar mysubroutines.class

  16. NOTE: Remember this will remove files! clean: rm *.class rm *.jar

  17. More on Phony Targets • Phony targets are useful for deployment • If I type make install, make checks to see if the file program in the current directory is newer than the one in my bin directory. If it is, it copies (or installs) the new version. install: /home/domain/zlizmj/bin/program /home/domain/zlizmj/bin/program: program cp program /home/domain/zlizmj/bin/program

  18. Another Makefile all: main1.class main1.class: mysubroutines.jar main1.java javac main1.java install: /home/domain/zlizmj/bin/main1.class /home/domain/zlizmj/bin/main1.class: main1.class cp main1.class /home/domain/zlizmj/bin/main1.class

  19. 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)/bin install: $(INSTALLDIR)/program $(INSTALLDIR)/program: program cp program $(HOME)/bin/program

  20. Another Makefile INSTALLDIR = $(HOME)/bin all: main1.class main1.class: mysubroutines.jar main1.java javac main1.java install: $(INSTALLDIR)/main1.class $(INSTALLDIR)/main1.class: main1.class cp main1.class $(INSTALLDIR)/main1.class

  21. 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

  22. Macro Substitution • Make performs strict textual replacement to work out what variables are, so the following two rules are equivalent: program: output.o g++ output.o -o program FOO = o pr$(FOO)gram: $(FOO)utput.$(FOO) g++ $(FOO)utput.$(FOO) -$(FOO) pr$(FOO)gram

  23. Increased Abstraction • Macros increase the level of abstraction in a Makefile mysubroutines.jar: mysubroutines.class main2.class jar cvf mysubroutines.class main2.class • is equivalent to JARFILE = mysubroutines.jar OBJECTS = mysubroutines.class main2.class $(JARFILE): $(OBJECTS) jar cvf $(OBJECTS)

  24. 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.

  25. 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

  26. Example of Automatic Variable Use mysubroutines.jar: mysubroutines.class jar mysubroutines.jar mysubroutines.class mysubroutines.jar: mysubroutines.class jar $@ $< mysubs.jar: mysubroutines.class myutil.class jar mysubs.jar mysubroutines.class myutil.class mysubs.jar: mysubroutines.class myutil.class jar $@ $ˆ

  27. 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

  28. Pattern Matching (2) exercise1.class: exercise1.java javac exercise1.java exercise2.class: exercise2.java javac exercise2.java • Or you could use a pattern: %.class: %.java javac $<

  29. Example Makefile INSTALLDIR = $(HOME)/bin all: main1.class install: $(INSTALLDIR)/main1.class $(INSTALLDIR)/main1.class: main1.class cp main1.class $(INSTALLDIR)/main1.class

  30. %.class: %.java javac $< clean: rm *.class rm *.jar

  31. 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.

  32. 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.

  33. Example of a Recursive Makefile all: cd interface; make cd program1; make cd program2; make clean cd interface; make clean cd program1; make clean cd program2; make clean

  34. 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.

More Related