Introduction Use of makefiles to manage the build process Declarative, imperative and relational rules Environment variables, phony targets, automatic.

Slides:



Advertisements
Similar presentations
Separate compilation Large programs are generally separated into multiple files, e.g. tuples.h, ray.h, ray.c, tuples.c main.c With several files, we can.
Advertisements

ANT: Another Nice Tool Ali Beyad October 1, 2003.
Declarative Languages. Declarative languages In an imperative programming language, a program specifies how to solve a problem In a declarative language,
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
COMP205 Comparative Programming Languages Part 1: Introduction to programming languages Lecture 3: Managing and reducing complexity, program processing.
Introduction to a Programming Environment
Guide To UNIX Using Linux Third Edition
Guide to Linux Installation and Administration, 2e1 Chapter 6 Using the Shell and Text Files.
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
1 Building. 2 Goals of this Lecture Help you learn about: The build process for multi-file programs Partial builds of multi-file programs make, a popular.
Lecture 8  make. Overview: Development process  Creation of source files (.c,.h,.cpp)  Compilation (e.g. *.c  *.o) and linking  Running and testing.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
1 Lecture 2 Working with Files and Directories COP 3344 Introduction to UNIX.
Introduction to Shell Script Programming
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Unix Makefiles COP 3330 Lecture Notes Dr. David A. Gaitros.
July 29, 2003Serguei Mokhov, 1 Makefile Brief Reference COMP 229, 346, 444, 5201 Revision 1.2 Date: July 18, 2004.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Apache Ant Java-Based Build Tool. Making your builds boring… Building projects should be easy and standardized. You should not be spending a substantial.
An Introduction to Unix Shell Scripting
Jump to first page (C) 1998, Arun Lakhotia 1 Software Configuration Management: Build Control Arun Lakhotia University of Southwestern Louisiana Po Box.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Appendix E The EZJava.
The Java Programming Language
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Introduction to programming in the Java programming language.
GNU Make Computer Organization II 1 © McQuain What is make ? make is a system utility for managing the build process (compilation/linking/etc).
Lecture 24CS311 – Operating Systems 1 1 CS311 – Lecture 24 Outline Final Exam Study Guide Note: These lecture notes are not intended replace your notes.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
Context-free grammars. Roadmap Last time – Regex == DFA – JLex for generating Lexers This time – CFGs, the underlying abstraction for Parsers.
Chapter Six Introduction to Shell Script Programming.
CSCI 330 UNIX and Network Programming Unit IV Shell, Part 2.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
Building Packages BCIS 3680 Enterprise Programming.
More Unix Naomi Altman. Directories Directory = folder mkdir - makes a new directory rmdir - removes an empty directory cd mydirectory - moves you into.
Lecture 8  make. Using make for compilation  With medium to large software projects containing many files, it’s difficult to: Type commands to compile.
Chapter Linux Basics. Acknowledgements This presentation was prepared by – Banyat Settapanich – Bahran Madaen This presentation will be updated later.
Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects can become huge My research consists of 1600.
CSc 352 An Introduction to make Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
GNU Make Computer Organization II 1 © McQuain What is make ? make is a system utility for managing the build process (compilation/linking/etc).
Linux Administration Working with the BASH Shell.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Makefiles Manolis Koubarakis Data Structures and Programming Techniques 1.
Multiple file project management & Makefile
The make utility (original presentation courtesy of Alark Joshi)
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
Brief Intro to Make CST494/ Gannod.
Text by: Lambert and Osborne
Makefiles Caryl Rahn.
Context-free grammars (CFGs)
Introduction to computing
Makefile Tutorial CIS5027 Prof: Dr. Shu-Ching Chen
What is make? make is a system utility for managing the build process (compilation/linking/etc). There are various versions of make; these notes discuss.
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Makefiles and Notes on Programming Assignment PA2
CS 3304 Comparative Languages
Linux Shell Script Programming
Build Tools (make) CSE 333 Autumn 2018
CSE 303 Concepts and Tools for Software Development
CSc 352: Elementary “make”
CSc 352 An Introduction to make
Compiler vs linker The compiler translates one .c file into a .o file
SPL – PS1 Introduction to C++.
What is make? make is a system utility for managing the build process (compilation/linking/etc). There are various versions of make; these notes discuss.
The make utility (original presentation courtesy of Alark Joshi)
Presentation transcript:

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

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)

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”

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

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

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

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

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 Action main1.class: main1.java javac main1.java

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

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.

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.

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

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

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

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

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

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

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

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)

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.

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

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 $ˆ

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

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

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

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

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.

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.

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

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.

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.

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.

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.

HelloMum.class: HelloMum.java echo compiling HelloMum javac HelloMum.java echo finished compiling HelloMum clean: rm *.class

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

macros A macro is just text replacement $(variable), not ${variable} as in shells

javac people.java people.java:4: cannot find symbol symbol : variable person location: class people person = new person(); ^ 1 error

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.

Question Do we need make files for scripts?

Online tutorial ex.html