Make
Encyclopedia
In software development
Software development
Software development is the development of a software product...

, Make is a utility
Utility software
Utility software is system software designed to help analyze, configure, optimize or maintain a computer. A single piece of utility software is usually called a utility or tool....

 that automatically builds
Build Automation
Build automation is the act of scripting or automating a wide variety of tasks that software developers do in their day-to-day activities including things like:* compiling computer source code into binary code* packaging binary code* running tests...

 executable programs and libraries from source code
Source code
In computer science, source code is text written using the format and syntax of the programming language that it is being written in. Such a language is specially designed to facilitate the work of computer programmers, who specify the actions to be performed by a computer mostly by writing source...

 by reading files called makefiles which specify how to derive the target program. Make can decide where to start through topological sorting
Topological sorting
In computer science, a topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that, for every edge uv, u comes before v in the ordering...

. Though integrated development environment
Integrated development environment
An integrated development environment is a software application that provides comprehensive facilities to computer programmers for software development...

s and language-specific compiler features can also be used to manage the build process in modern systems, Make remains widely used, especially in Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

.

Origin

There are now a number of dependency-tracking build utilities, but Make is one of the most widespread, primarily due to its inclusion in Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

, starting with the PWB/UNIX
PWB/UNIX
The Programmer's Workbench was an early version of the Unix operating system created in the Bell Labs Computer Science Research Group of AT&T....

 1.0, which featured a variety of tools targeting software development tasks. It was originally created by Stuart Feldman
Stuart Feldman
Stuart Feldman received an A.B. in astrophysical sciences from Princeton University and a Ph.D in applied mathematics from the Massachusetts Institute of Technology. He is best known as the creator of the computer software program make for UNIX systems...

 in 1977 at Bell Labs
Bell Labs
Bell Laboratories is the research and development subsidiary of the French-owned Alcatel-Lucent and previously of the American Telephone & Telegraph Company , half-owned through its Western Electric manufacturing subsidiary.Bell Laboratories operates its...

. In 2003 Dr. Feldman received the ACM
Association for Computing Machinery
The Association for Computing Machinery is a learned society for computing. It was founded in 1947 as the world's first scientific and educational computing society. Its membership is more than 92,000 as of 2009...

 Software System Award for the authoring of this widespread tool.

Before Make's introduction, the Unix build system most commonly consisted of operating system
Operating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...

 dependent "make" and "install" shell scripts accompanying their program's source. Being able to combine the commands for the different targets into a single file and being able to abstract out dependency tracking and archive handling was an important step in the direction of modern build environments.

Modern versions

Make has gone through a number of rewrites
Rewrite (programming)
A rewrite in computer programming is the act or result of re-implementing a large portion of existing functionality without re-use of its source code. When the rewrite is not using existing code at all, it is common to speak of a rewrite from scratch...

, including a number of from-scratch variants which used the same file format and basic algorithmic principles and also provided a number of their own non-standard enhancements. Some of them are:
  • BSD Make, which is derived from Adam de Boor's work on a version of Make capable of building targets in parallel
    Parallel computing
    Parallel computing is a form of computation in which many calculations are carried out simultaneously, operating on the principle that large problems can often be divided into smaller ones, which are then solved concurrently . There are several different forms of parallel computing: bit-level,...

    , and survives with varying degrees of modification in FreeBSD
    FreeBSD
    FreeBSD is a free Unix-like operating system descended from AT&T UNIX via BSD UNIX. Although for legal reasons FreeBSD cannot be called “UNIX”, as the direct descendant of BSD UNIX , FreeBSD’s internals and system APIs are UNIX-compliant...

    , NetBSD
    NetBSD
    NetBSD is a freely available open source version of the Berkeley Software Distribution Unix operating system. It was the second open source BSD descendant to be formally released, after 386BSD, and continues to be actively developed. The NetBSD project is primarily focused on high quality design,...

     and OpenBSD
    OpenBSD
    OpenBSD is a Unix-like computer operating system descended from Berkeley Software Distribution , a Unix derivative developed at the University of California, Berkeley. It was forked from NetBSD by project leader Theo de Raadt in late 1995...

    . Most notably, it has conditionals and iterative loops which are applied at the parsing stage and may be used to conditionally and programmatically construct the makefile, including generation of targets at runtime.
  • GNU
    GNU
    GNU is a Unix-like computer operating system developed by the GNU project, ultimately aiming to be a "complete Unix-compatible software system"...

     Make is frequently used in conjunction with the GNU build system
    GNU build system
    The GNU build system, also known as the Autotools, is a suite of programming tools designed to assist in making source-code packages portable to many Unix-like systems....

    . Its departures from traditional Make are most noticeable in pattern-matching in dependency graph
    Dependency graph
    In mathematics, computer science and digital electronics, a dependency graph is a directed graph representing dependencies of several objects towards each other...

    s and build targets, as well as a number of functions which may be invoked allowing functionality like listing the files in the current directory. It is also included in Apple’s Xcode
    Xcode
    Xcode is a suite of tools, developed by Apple, for developing software for Mac OS X and iOS. Xcode 4.2, the latest major version, is available on the Mac App Store for free for Mac OS X 10.7 , and on the Apple Developer Connection website for free to registered developers Xcode is a suite of tools,...

     development suite for the Mac OS
    Mac OS
    Mac OS is a series of graphical user interface-based operating systems developed by Apple Inc. for their Macintosh line of computer systems. The Macintosh user experience is credited with popularizing the graphical user interface...

    .
  • Microsoft
    Microsoft
    Microsoft Corporation is an American public multinational corporation headquartered in Redmond, Washington, USA that develops, manufactures, licenses, and supports a wide range of products and services predominantly related to computing through its various product divisions...

     nmake, commonly available on Windows. It is fairly basic in that it offers only a subset of the features of the two versions of Make mentioned above. Microsoft's nmake is not to be confused with nmake from AT&T
    AT&T
    AT&T Inc. is an American multinational telecommunications corporation headquartered in Whitacre Tower, Dallas, Texas, United States. It is the largest provider of mobile telephony and fixed telephony in the United States, and is also a provider of broadband and subscription television services...

     and Bell Labs
    Bell Labs
    Bell Laboratories is the research and development subsidiary of the French-owned Alcatel-Lucent and previously of the American Telephone & Telegraph Company , half-owned through its Western Electric manufacturing subsidiary.Bell Laboratories operates its...

     for Unix.


POSIX
POSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...

 includes standardization of the basic features and operation of the Make utility, and is implemented with varying degrees of completeness in Unix-based versions of Make. In general, simple makefiles may be used between various versions of Make with reasonable success. GNU Make and BSD Make can be configured to look first for files named "GNUmakefile" and "BSDmakefile" respectively, which allows one to put makefiles which use implementation-defined behavior in separate locations.

Behaviour

Make is typically used to build
Software build
In the field of computer software, the term software build refers either to the process of converting source code files into standalone software artifact that can be run on a computer, or the result of doing so...

 executable programs and libraries from source code. Generally though, any process that involves transforming a source file to a target result (by executing arbitrary commands) is applicable to Make. For example, Make could be used to detect a change made to an image file (the source) and the transformation actions might be to convert the file to some specific format, copy the result into a content management system, and then send e-mail to a predefined set of users that the above actions were performed.

Make is invoked with a list of target file names to build as command-line arguments:


make TARGET [TARGET ...]


Without arguments, Make builds the first target that appears in its makefile, which is traditionally a symbolic "phony" target named all.

Make decides whether a target needs to be regenerated by comparing file modification times. This solves the problem of avoiding the building of files which are already up to date, but it fails when a file changes but its modification time stays in the past. Such changes could be caused by restoring an older version of a source file, or when a network filesystem is a source of files and its clock or timezone is not synchronized with the machine running Make. The user must handle this situation by forcing a complete build. Conversely, if a source file's modification time is in the future, it triggers unnecessary rebuilding, which may inconvenience users.

Makefiles

Make searches the file to run from current directory. E.g. the GNU Make searches files in order GNUmakefile, makefile, Makefile.

The makefile language is similar to declarative programming
Declarative programming
In computer science, declarative programming is a programming paradigm that expresses the logic of a computation without describing its control flow. Many languages applying this style attempt to minimize or eliminate side effects by describing what the program should accomplish, rather than...

. This class of language, in which necessary end conditions are described but the order in which actions are to be taken is not important, is sometimes confusing to programmers used to imperative programming
Imperative programming
In computer science, imperative programming is a programming paradigm that describes computation in terms of statements that change a program state...

.

One problem in build automation
Build Automation
Build automation is the act of scripting or automating a wide variety of tasks that software developers do in their day-to-day activities including things like:* compiling computer source code into binary code* packaging binary code* running tests...

 is the tailoring of a build process to a given platform
Platform (computing)
A computing platform includes some sort of hardware architecture and a software framework , where the combination allows software, particularly application software, to run...

. For instance, the compiler used on one platform might not accept the same options as the one used on another. This is not well handled by Make. This problem is typically handled by generating platform specific build instructions, which in turn are processed by Make. Common tools for this process are Autoconf
Autoconf
GNU Autoconf is a tool for producing configure scripts for building, installing and packaging software on computer systems where a Bourne shell is available....

 and CMake
CMake
CMake is a cross-platform, open-source system for managing the build process of software using a compiler-independent method. It is designed to support directory hierarchies and applications that depend on multiple libraries, and for use in conjunction with native build environments such as Make,...

.

Rules

A makefile consists of rules. Each rule begins with a textual dependency line which defines a target followed by a colon (:) and optionally an enumeration of components (files or other targets) on which the target depends. The dependency line is arranged so that the target (left hand of the colon) depends on components (right hand of the colon).

An example is a specific object file
Object file
An object file is a file containing relocatable format machine code that is usually not directly executable. Object files are produced by an assembler, compiler, or other language translator, and used as input to the linker....

 target which depends on a C source file and header file
Header file
Some programming languages use header files. These files allow programmers to separate certain elements of a program's source code into reusable files. Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers...

s. Because Make itself does not understand, recognize or distinguish different kinds of files, this opens up a possibility for human error. A forgotten or an extra dependency may not be immediately obvious and may result in subtle bugs in the generated software. It is possible to write makefiles which generate these dependencies by calling third-party tools, and some makefile generators, such as the Automake
Automake
GNU Automake is a programming tool that produces portable makefiles for use by the make program, used in compiling software. It is made by the Free Software Foundation as one of GNU programs, and is part of the GNU build system. The makefiles produced follow the GNU Coding Standards.It is written...

 toolchain provided by the GNU Project
GNU Project
The GNU Project is a free software, mass collaboration project, announced on September 27, 1983, by Richard Stallman at MIT. It initiated GNU operating system development in January, 1984...

, can do so automatically.

After each dependency line, a series of command lines may follow which define how to transform the components (usually source files) into the target (usually the "output"). If any of the
components have been modified, the command lines are run.

Each command line must begin with a tab character to be recognized as a command. The tab is a whitespace
Whitespace (computer science)
In computer science, whitespace is any single character or series of characters that represents horizontal or vertical space in typography. When rendered, a whitespace character does not correspond to a visual mark, but typically does occupy an area on a page...

 character, but the space character does not have the same special meaning. This is problematic, since there may be no visual difference between a tab and a series of space characters. This aspect of the syntax of makefiles is often subject to criticism. When using makefile generators or text editors with explicit makefile support, this issue is likely less important.

Each command is executed by a separate shell
Unix shell
A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems...

 or command-line interpreter instance. Since operating systems use different command-line interpreters this can lead to unportable makefiles. For instance, GNU Make by default executes commands with /bin/sh, where Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 commands like cp
Cp (Unix)
cp is a UNIX command used to copy a file. Files can be copied either to the same directory or to a completely different directory, possibly on a different file system or hard disk drive. If the file is copied to the same directory, the new file must have a different name to the original; in all...

 are normally used. In contrast to that Microsoft's nmake executes commands with cmd.exe, where batch
Batch file
In DOS, OS/2, and Microsoft Windows, batch file is the name given to a type of script file, a text file containing a series of commands to be executed by the command interpreter....

 commands like copy
Copy (command)
In computing, copy is a command in RT-11, RSX-11, OpenVMS, DOS, OS/2 and Microsoft Windows operating systems. The command copies computer files from one directory to another. The destination defaults to the current working directory. If more than one source file is indicated, the destination must...

 will be used.


target [target ...]: [component ...]
[command 1]
.
.
.
[command n]


Usually each rule has a single unique target, rather than multiple targets.

A rule may have no command lines defined. The dependency line can consist solely of components that refer targets like:


realclean: clean distclean


The command lines of a rule are usually arranged so that they generate the target. An example: if "file.html" is newer, it is converted to text. The contents of the makefile:


file.txt: file.html
lynx -dump file.html > file.txt


The above rule would be triggered when Make updates "file.txt". In the following invocation, Make would typically use this rule to update the "file.txt" target if necessary.


make file.txt


Command lines can have one or more of the following three prefixes:
  • a hyphen–minus (-), specifying that errors are ignored
  • an at sign
    At sign
    The at sign , also called the ampersat, apetail, arroba, atmark, at symbol, commercial at or monkey tail, is formally an abbreviation of the accounting and commercial invoice term "at the rate of"...

     (@), specifying that the command is not printed to standard output before it is executed
  • a plus sign (+), the command is executed even if Make is invoked in a "do not execute" mode


Ignoring errors and silencing echo can alternatively be obtained via the special targets ".IGNORE" and ".SILENT".

Macros

A makefile can contain definitions of macros. Macros are usually referred to as variables when they hold simple string definitions, like "CC=gcc". Macros in makefiles may be overridden in the command-line arguments passed to the Make utility. Environment variables are also available as macros.

Macros allow users to specify the programs invoked and other custom behavior during the build process. For example, the macro "CC" is frequently used in makefiles to refer to the location of a C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 compiler, and the user may wish to specify a particular compiler to use.

New macros (or simple "variables") are traditionally defined using capital letters:


MACRO = definition


A macro is used by expanding it. Traditionally this is done by enclosing its name inside $. Very rarely, though possible, inside ${}.


NEW_MACRO = $(MACRO)-$(MACRO2)


Macros can be composed of shell commands by using the command substitution
Command substitution
In computing, command substitution is a facility originally introduced in the Unix shells that allows a command to be run and its output to be pasted back on the command line as arguments to another command...

 operator, denoted by backticks (`).


YYYYMMDD = ` date `


The content of the definition is stored "as is". Lazy evaluation
Lazy evaluation
In programming language theory, lazy evaluation or call-by-need is an evaluation strategy which delays the evaluation of an expression until the value of this is actually required and which also avoids repeated evaluations...

 is used, meaning that macros are normally expanded only when their expansions are actually required, such as when used in the command lines of a rule. An extended example:


PACKAGE = package
VERSION = ` date +"%Y.%m%d" `
ARCHIVE = $(PACKAGE)-$(VERSION)

dist:
# Notice that only now macros are expanded for shell to interpret:
# tar -cf package-`date +"%Y%m%d"`.tar

tar -zcf $(ARCHIVE).tar .


The generic syntax for overriding macros on the command line is:


make MACRO="value" [MACRO="value" ...] TARGET [TARGET ...]


Makefiles can access predefined internal macros.


target: component1 component2
echo $? contains those components, that need attention, THAT ARE YOUNGER than current TARGET
echo $@ evaluates to current TARGET name to the left

Suffix rules

Suffix rules are in the form FROM.TO and they can be used to launch actions based on file extension. In the case of suffix rules, internal macro $< refers the component and $@ refers to the TARGET. An example to convert any HTML file into text; notice the shell redirection token > in the middle:


.SUFFIXES: .txt .html

# From .html to .txt
.html.txt:
lynx -dump $< > $@


When called from command line, the above example expands.


$ make -n file.txt
lynx -dump file.html > file.txt

Other elements

Single-line comments are started with the hash symbol (#).

Some directives in makefiles can include other makefiles.

Line continuation is indicated with a backslash \ character at the end of a line.


target: component \
component
command ; \
command | \
piped-command

Example makefiles

Makefiles are traditionally used for compiling code (*.c, *.cc, *.C, etc.), but they can also be used for providing commands to automate common tasks. One such makefile is called from the command line:


make # Without argument runs first TARGET
make help # Show available TARGETS
make dist # Make a release archive from current dir


The makefile:


PACKAGE = package
VERSION = ` date "+%Y.%m%d%" `
RELEASE_DIR = ..
RELEASE_FILE = $(PACKAGE)-$(VERSION)

# Notice that the variable LOGNAME comes from the environment in
# POSIX shells.
#
# target: all - Default target. Does nothing.
all:
echo "Hello $(LOGNAME), nothing to do by default"
# very rarely: echo "Hello ${LOGNAME}, nothing to do by default"
echo "Try 'make help'"

# target: help - Display callable targets.
help:
egrep "^# target:" [Mm]akefile

# target: list - List source files
list:
# Won't work. Each command is in separate shell
cd src
ls

# Correct, continuation of the same shell
cd src; \
ls

# target: dist - Make a release.
dist:
tar -cf $(RELEASE_DIR)/$(RELEASE_FILE) && \
gzip -9 $(RELEASE_DIR)/$(RELEASE_FILE).tar


Below is a very simple makefile that would compile a source called "helloworld.c" using gcc, a C compiler, and specifies a "clean" target to remove the generated files, for example to start over. The .PHONY tag is a technicality that tells Make that a particular target name does not produce an actual file. The $@ and $< are two of the so-called internal macros (also known as automatic variables) and stand for the target name and "implicit" source, respectively. There are a number of other internal macros.


CC = gcc
CFLAGS = -g

all: helloworld

helloworld: helloworld.o
# Commands start with TAB not spaces
$(CC) $(LDFLAGS) -o $@ $^

helloworld.o: helloworld.c
$(CC) $(CFLAGS) -c -o $@ $<

clean:
rm -f helloworld helloworld.o

# This is GNU makefile extension to notify that roughly means: 'clean' does
# not depend on any files in order to call it.
.PHONY: clean


Many systems come with a version of Make configured to handle common tasks like compiling based on file suffixes, which allows one to leave out the actual instructions from the target and source specification. Assuming such a system, the above example could be shortened as follows:


all: helloworld

helloworld: helloworld.o

helloworld.o: helloworld.c

clean:
rm -f helloworld helloworld.o

.PHONY: clean


Make allows for custom suffix rules to be defined, allowing for powerful abstraction of rules by separating the file build relationships from the construction rules.

An example makefile for GNU Make:


# Just a snippet to stop executing under other make(1) commands
# that won't understand these lines

ifneq
This makefile requires GNU Make.
endif

include makefile/environment.mk
include makefile/common.mk

# Define macros. In this case "simple variables that don't expand"
# Can be overridden on command line

CC = gcc
CFLAGS = -Wall -pedantic -g

all:
echo "Nothing to do, yet"

See also

  • Apache Ant
    Apache Ant
    Apache Ant is a software tool for automating software build processes. It is similar to Make but is implemented using the Java language, requires the Java platform, and is best suited to building Java projects....

  • CMake
    CMake
    CMake is a cross-platform, open-source system for managing the build process of software using a compiler-independent method. It is designed to support directory hierarchies and applications that depend on multiple libraries, and for use in conjunction with native build environments such as Make,...

  • Configure script
  • Fastmake
    Fastmake
    Fastmake is a program that is used mainly by software developers to maintain software build process.Fastmake is one of the make programs family. The most similar make program is GNU make...

  • Flowtracer
    Flowtracer
    FlowTracer, previously known as Flowtracer/EDA, is a commercial build management tool developed by Runtime Design Automation.FlowTracer, previously known as Flowtracer/EDA, is a commercial build management tool developed by Runtime Design Automation.FlowTracer, previously known as Flowtracer/EDA,...

  • GNU Automake
  • imake
    Imake
    imake is a build automation system implemented on top of the C preprocessor.imake generates makefiles from a template, a set of cpp macro functions, and a per-directory input file called an Imakefile...

  • Jam
    Perforce Jam
    Perforce Jam is an open-source build system developed by Christopher Seiwald of Perforce Software. It can be used as a replacement for make. Its primary feature is its ability to express build patterns in an imperative language which supports structured namespaces and simple lists...

  • mk
  • Premake
    Premake
    In software development, premake is a open source utility for automatically building configuration from source code.- Features :Some of the features of the system are:* It supports C, C++, and C# languages source code.* It has a simple syntax...

  • qmake
    Qmake
    qmake is a utility that automates the generation of Makefiles. Makefiles are used by the program make to build executable programs from source code; therefore qmake is a make-makefile tool, or makemake for short....

  • Rake
    Rake (software)
    Rake is a software task management tool. It allows you to specify tasks and describe dependencies as well as to group tasks in a namespace.It is similar to SCons and make, but has a number of differences. The tool is written in the Ruby programming language, and the Rakefiles use Ruby syntax...

  • SCons
    SCons
    SCons is a computer software construction tool that automatically analyzes source code file dependencies and operating system adaptation requirements from a software project description and generates final binary executables for installation on the target operating system platform...

  • Waf
    Waf
    Waf is a Build automation tool - a program that assists in the automatic compilation and installation of other programs or libraries.-General:* Portable to Unix and non-Unix systems* Lightweight...

  • Watcom C/C++ compiler
  • Xconfig
    Xconfig
    xconfig is short for the 'xconfig' target for the Linux Makefile. It is a graphical Linux compilation utility, which uses Qt. The xconfig utility is invoked by running 'make xconfig' in the base Linux source directory....


Reference, manuals, books


Tutorials


Debugging GNU Make


Other

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK