[Software Development] Makefiles. Davide Balzarotti. Eurecom Sophia Antipolis, France

Similar documents
GNU make. Michal Koutný. Software development and monitoring tools (NSWI126)

The make utility. Alark Joshi COMPSCI 253

Makefiles SE 2XA3. Term I, 2018/19

We d like to hear your suggestions for improving our indexes. Send to

GNU make... Martin Ohlerich, Parallel Programming of High Performance Systems

Software Building (Sestavování aplikací)

GDB and Makefile. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Intermediate Programming, Spring 2017*

CSE 333 Interlude - make and build tools

COSC345 Software Engineering. Make

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

Kurt Schmidt. May 23, 2018

Compilation & linkage. Compilation & linkage. Make. Compilation & linkage. Explicit rules. What makefile contains

Make: a build automation tool

CSE 374 Programming Concepts & Tools

HPC User Environment

The make utility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them.

CMPT 300. Operating Systems. Brief Intro to UNIX and C

GNU Make. A Program for Directing Recompilation GNU make Version 3.79 April Richard M. Stallman and Roland McGrath

GNU Make 1. 1 material adapted from GNU Make by Richard Stallman

Make: a build automation tool 1/23

CS11 Advanced C++ Fall Lecture 4

Multiple file project management & Makefile

Introduction to System Programming : makefile

Using GNU make C HAPTER 4

independent compilation and Make

Makefile Brief Reference

Basic Compilation Control with Make

CS Basics 15) Compiling a C prog.

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

2 Compiling a C program

GNU Make. A Program for Directing Recompilation GNU make Version 3.82 July Richard M. Stallman, Roland McGrath, Paul D.

Make! CSC230: C and Software Tools. N.C. State Department of Computer Science. Some examples adapted from

DAY 4. CS3600, Northeastern University. Alan Mislove

Makefile Tutorial. Eric S. Missimer. December 6, 2013

@shorttitlepage GNU Make Copyright (C) 1988, '89, '90, '91, '92, '93 Free Software Foundation, Inc.

The Makefile utility. (Extract from the slides by Terrance E. Boult

Make. Dependency Graphs

CS11 Intro C++ Spring 2018 Lecture 4

Reviewing gcc, make, gdb, and Linux Editors 1

GNU Make. A Program for Directing Recompilation. Edition 0.51, for make Version 3.75 Beta. May Richard M. Stallman and Roland McGrath

Build automation. CSE260, Computer Science B: Honors Stony Brook University

The makeutility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them.

Lab 2.2. Out: 9 February 2005

Maemo Diablo GNU Make and makefiles Training Material

The Linux Programming Environment. Computer Science Department Texas State University

Unix Power Use. Martti Louhivuori. Spring School in Computational Chemistry 2013 PRACE Advanced Training CSC

Introduction to Supercomputing

UNIX Makefile. C Project Library Distribution and Installation.

Program Development Tools

CSCi 4061: Intro to Operating Systems Spring 2017 Instructor: Jon Weissman Assignment 1: Simple Make Due: Feb. 15, 11:55 pm

Tutorial: Compiling, Makefile, Parallel jobs

CSE 333 Midterm Exam 7/27/15 Sample Solution

Maemo Diablo Reference Manual for maemo 4.1. GNU Build System

Chapter 2: An Introduction to Makeles 5 2 An Introduction to Makeles You need a le called a makele to tell make what to do. Most often, the makele tel

Make was originally a Unix tool from 1976, but it has been re-implemented several times, notably as GNU Make.

Exercise 1: Basic Tools

CAAM 420 Daily Note. Scriber: Qijia Jiang. Date: Oct.16. Project 3 Due Wed 23.Oct. Two parts: debug code and library exercise.

CS 261 Recitation 1 Compiling C on UNIX

LSF Make. Platform Computing Corporation

BMS: Build Management System

Data and File Structures Laboratory

CSE 15L Winter Midterm :) Review

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017

CS2141 Software Development using C/C++ Compiling a C++ Program

Projects and Make Files

Using the Unix system. UNIX Introduction

Index. Numbers and Symbols. backward incompatibilities, for GNU make 3.82, backslash (\) as continuation character, 124 converting slash to, 144

Systems Programming. laboratory. Compilation automation. make ceedling

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Introduction to Linux

February 15, 2017 CSC Assignment #3. Decomposition Outline Due: February 8, :59pm Due: February 15, :59pm

/visualc/vcug/_asug_overview.3a_.nmake_reference.htm

CSE 390 Lecture 8. Large Program Management: Make; Ant

Concurrent Variants, Isolation, and Feature Models in Software Deployment

The make Utility in Unix SEEM

Autotools Tutorial. Mengke HU. ASPITRG Group Meeting. ECE Department Drexel University

COMP 2400 UNIX Tools

Task Automation. Anthony Scemama Labratoire de Chimie et Physique Quantiques IRSAMC (Toulouse)

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

Section 1: Tools. Kaifei Chen, Luca Zuccarini. January 23, Make Motivation How... 2

CMPSC 311- Introduction to Systems Programming Module: Build Processing

Workshop Agenda Feb 25 th 2015

Final C Details, Build Tools

Chris' Makefile Tutorial

CAAM 420 Fall 2012 Lecture 15. Roman Schutski

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2017

A Fast Review of C Essentials Part II

$Id: asg4-shell-tree.mm,v :36: $

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

CSC 2500: Unix Lab Fall 2016

Using Platform LSF Make

CS240: Programming in C

Software Development With Emacs: The Edit-Compile-Debug Cycle

Pattern Matching, set-up. Pattern Matching, example. Pattern Matching, set-up, cont. Lecture 9: Make Pattern Matching & Conceptual Integrity

CS631 - Advanced Programming in the UNIX Environment. UNIX development tools

Tcl Extension Architecture Developer s Guide DRAFT

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CMPSC 311- Introduction to Systems Programming Module: Build Processing

Transcription:

[Software Development] Makefiles Davide Balzarotti Eurecom Sophia Antipolis, France 1

Software Development Tools 1. Configuring and Building the program GCC Makefiles Autotools 2. Writing and managing code 3. Packaging and Distributing the application 4. Debugging and Profiling 2

Why Makefiles Programs usually contains multiple source files Written by different people Often organized in subdirectories With complicated dependency (to compile X you first have to compile Y, that requires to compile the library Z..) Problems: Recompiling the entire project for each small change it is very inefficient and may requires long compilation time Maintaining complex projects quickly becomes a nightmare 3

Make and Makefiles Makefiles date back to 1977 but are still the most widely used tool to manage the build process Different versions exist: e.g., GNU make, bsd make, Microsoft nmake A makefile is a script that describes: The project structure: the files and the dependency between them How to derive the target program from each of its dependencies Instructions to compile each file The instructions inside a makefile are interpreted and executed by the make program By checking the dependencies, make only recompiles what needs to be recompiled 4

Structure of a Make File A makefile is a text files containing one or more rules target: dependency1 dependency2... action1 action2... TABs (mandatory) If the dependencies are satisfied, the target is built by executing the actions Makefiles can also contains variable (macro) assignments: Definition: CC = gcc Use: echo ${CC} or echo $(CC) By convention, all variables in a makefile are uppercase 5

A Simple Makefile all: func.o main.o gcc func.o main.o -o main func.o: func.c func.h gcc -g -c func.c main.o: main.c func.h gcc -g -c main.c clean: rm -f *.o rm main The target "all" depends on the targets func.o and main.o The target "func.o" depends on func.c Actually, any.o depends by default on the corresponding.c file (so func.c could be omissed) The target "clean" does not have any dependency 6

How It Works The make command first look for a file named makefile or Makefile in the current directory Then, it parses the file content and construct the dependency tree Then it checks if any of the target prerequisites does not exist or it is newer than the target itself (by checking file timestamps) If so, it first executes the prerequisite targets (which, of course, can have other dependencies and so on..) If the target is not a filename, make cannot determine its timestamp and therefore it always executes it For each action in a target, make prints the action and then execute it (in a separate subshell for each action command) 7

A Simple Makefile # This is a comment all: func.o main.o gcc func.o main.o -o main func.o: func.c func.h gcc -g -I. -c func.c main.o: main.c func.h gcc -g -I. -c main.c clean: rm -f *.o rm main make target : executes the action associated with target (and the required dependences) make : executes the first target specified in the makefile > make gcc -g -I. -c func.c gcc -g -I. -c main.c gcc func.o main.o -o main > touch func.c > make gcc -g -I. -c func.c gcc func.o main.o -o main > make clean rm -f *.o rm main 8

More on Variable Assignment There are four way to assign a variable: := (simple assignment) = (recursive assignment) the right side of the assignment expression is not evaluated immediately but when the variable is used Example: COMPILE = ${CC} ${CFLAGS}?= (conditional assignment) The assignment is performed only if the variable did not have a value yet += (append) Append text to an existing variable 9

Patterns Defining patterns The percent sign, %, can be used to perform wildcard matching to write more general targets When a corresponding % appears in the dependencies list, it is replaced by the same string of text that assumed in the target Example: %.o : %.c gcc -c???? 10

Patterns Defining patterns The percent sign, %, can be used to perform wildcard matching to write more general targets When a corresponding % appears in the dependencies list, it is replaced by the same string of text that assumed in the target Example: %.o : %.c gcc -c???? PROBLEM: How can I reference the filename in the action? 11

Special Variables Special variables $@ full target name of the current target $? evaluates to the list of prerequisites that are newer than the current target $* evaluates to the current target name with its suffix deleted (in practice, the value of the % wildcard in the target) $< The name of the first dependency $^ The names of all the dependencies, with spaces between them main.o: main.c operation.h gcc -c $< -o $@ 12

Using Shell Wildcards in Makefiles Suppose you want to express that the foo target is made (i.e., depends) of all the object files in the current directory objects = *.o foo : $(objects) cc -o foo $(CFLAGS) $(objects) Each existing '.o' file becomes a dependency of foo and will be recompiled if necessary 13

Using Shell Wildcards in Makefiles Suppose you want to express that the foo target is made (i.e., depends) of all the object files in the current directory objects = *.o foo : $(objects) cc -o foo $(CFLAGS) $(objects)? Each existing '.o' file becomes a dependency of foo and will be recompiled if necessary But what happen if there are no.o files? Since the wildcard does not match any file, it is left as it is Therefore foo will depend on a file named '*.o' Since '*.o' does not exists, make will raise an error because it does not know how to build it 14

Functions Functions allow text processing inside a makefile Usually used to compute a file list to operate on compute the commands to use Functions are invoked with the following syntax $(function_name arg1, arg2, argn) function_name must be one of the pre-defined functions Check Makefile documentation for the list of functions It is not possible to define new functions 15

Useful Functions $(subst from,to,text) Substitute <from> with <to> in <text> $(patsubst pattern,replacement,text) Finds whitespace-separated words in <text> that match <pattern> and replaces them with <replacement> $(patsubst %.c,%.o,foo.c bar.c) foo.o bar.o $(filter pattern...,text) Removes all whitespace-separated words in text that do not match any of the pattern words, returning only matching words sources := foo.c bar.c baz.s ugh.h foo: $(sources) cc $(filter %.c %.s,$(sources)) -o foo 16

Useful Functions $(shell command) Takes as argument a shell command and returns the output of the command contents := $(shell cat foo) $(wildcard pattern) Return a space-separated list of names of existing files that match the pattern $(dir names...) $(notdir names...) Extract and return the directories (or the filenames) from a list of file names $(dir dir1/foo.c dir2/bar.h) dir1 dir2 $(notdir dir1/foo.c dir2/bar.h) foo.c bar.h 17

Useful Functions $(addprefix prefix,names...) Prepend <prefix> at the beginning of each file name $(addprefix src/,foo bar) src/foo src/bar $(basename names...) Extracts all but the extension of each file name $(basename src/foo.c bar.x new) src/foo bar new $(addsuffix suffix,names...) Append a suffix at the end of each file name $(addsuffix.c,foo bar) foo.c bar.c 18

Action Modifiers Each action can be modified by pre-pending one of the following prefixes - (dash) any error found while executing the command is ignored By default, make stops if one of the action returns an exit status different than zero With - make prints out a message with the status code the command exited with, and says that the error has been ignored Example: -rm file # delete the file but does not stop if it does not exist @ (at) the command is not written to standard output before it is executed Example: @echo "hello" # print hello, without printing the echo command 19

CC = gcc CC_OPT = -O2 -g -Wall INCLUDES = -I. CFLAGS = ${CC_OPT} OBJS = func.o main.o # compilator to use # compilation parameters # path to the.h files # list of object file to build all: ${OBJS} ${CC} ${CFLAGS} ${OBJS} -o main ${INCLUDES} %.o: %.c ${CC} -c ${CFLAGS} $*.c ${INCLUDES} clean: @echo "Cleaning..." rm -f *.o rm main 20

Phony Targets A target does not necessarily need to be a filename But if there is a file in the current directory with the same name of the target, make gets confused: > make clean rm -f *.o rm main > touch clean > make clean make: 'clean' is up to date. A way to explicitly specify that a target is NOT a filename is to declare it as a phony target.phony:clean clean: rm -f *.o rm main 21

Nested Makefiles subsystem: cd subdir && $(MAKE) Useful for large systems containing many directory, each with its own makefile and you would like the containing directory's makefile to run make on the subdirectory Note the use of the $(MAKE) variable instead of calling the make program It ensures that make is always executed (also if the user is just simulating the makefile with -t or -n options) 22

Useful stuff If you want to know the commands that will be executed to generate a file, run make with -n If you want to change Makefile variables from the command line, specify them after the target, i.e. make test.o CC=gcc CFLAGS= -g -O0 23