Make (software)

Software build automation tool


title: "Make (software)" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["articles-with-example-code", "build-automation", "compiling-tools", "gnu-project-software", "unix-programming-tools", "unix-sus2008-utilities"] description: "Software build automation tool" topic_path: "technology/operating-systems" source: "https://en.wikipedia.org/wiki/Make_(software)" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

::summary Software build automation tool ::

::data[format=table title="Infobox programming language"]

FieldValue
logo
screenshot
paradigmmacro, declarative
designerStuart Feldman
developer
released
latest release date
latest preview date
programming languageC
operating systemUnix-like, Inferno
file formatMakefile
implementationsBSD, GNU, nmake
dialectsBSD make, GNU make, Microsoft nmake
influencedAnt, Rake, MSBuild, and others
::

| name = | logo =
| logo caption = | screenshot = | screenshot caption = | paradigm = macro, declarative | family = | designer = Stuart Feldman | developer =
| released =
| latest release version = | latest release date = | latest preview version = | latest preview date = | typing = | scope = | programming language = C | discontinued = | platform = | operating system = Unix-like, Inferno | license = | file ext = | file format = Makefile | implementations = BSD, GNU, nmake | dialects = BSD make, GNU make, Microsoft nmake | influenced by = | influenced = Ant, Rake, MSBuild, and others

In software development, Make is a command-line interface software tool that performs actions ordered by configured dependencies as defined in a configuration file called a makefile. It is commonly used for build automation to build executable code (such as a program or library) from source code. Make is also not limited to building and can perform any operation available via the operating system shell.

Make is widely used, especially in Unix and Unix-like operating systems, even though many competing technologies and tools are available, including similar tools that perform actions based on dependencies, some compilers and interactively via an integrated development environment.

In addition to referring to the original Unix tool, Make is also a technology since multiple tools have been implemented with roughly the same functionality including similar makefile syntax and semantics.

Origin

Stuart Feldman created Make while at Bell Labs. An early version was completed in April 1976.

Feldman describes the inspiration to write Make as arising from a coworker's frustration with the available tooling of the time:

Before Make, building on Unix mostly consisted of shell scripts written for each program's codebase. Make's dependency ordering and out-of-date checking makes the build process more robust and more efficient. The makefile allowed for better organization of build logic and often fewer build files.

Make is widely used in part due to its early inclusion in Unix, starting with PWB/UNIX 1.0, which featured a variety of software development tools.

Variants

Derivatives

Make has been implemented numerous times, generally using the same makefile format and providing the same features, but some providing enhancements from the original. Examples:

  • Sun DevPro Make appeared in 1986 with SunOS-3.2. With SunOS-3.2. It was delivered as an optional program; with SunOS-4.0, SunPro Make was made the default Make program. In December 2006, Sun DevPro Make was made open source as part of the efforts to open-source Solaris.
  • dmake or Distributed Make that came with Sun Solaris Studio as its default Make, but not the default one on the Solaris Operating System (SunOS). It was originally required to build OpenOffice, but in 2009 the build system was rewritten to use GNU Make. While Apache OpenOffice still contains a mixture of both build systems, the much more actively developed LibreOffice only uses the modernized "gbuild" now.
  • BSD Make (pmake, bmake or fmake), which is derived from Adam de Boor's work on a version of Make capable of building targets in parallel, and survives with varying degrees of modification in FreeBSD, NetBSD and OpenBSD. Distinctively, 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 Make (short gmake) is the standard implementation of Make for Linux and macOS. It provides several extensions over the original Make, such as conditionals. It also provides many built-in functions which can be used to eliminate the need for shell-scripting in the makefile rules as well as to manipulate the variables set and used in the makefile. For example, the foreach function can be used to iterate over a list of values, such as the names of files in a given directory. GNU Make is required for building many software systems, including GNU Compiler Collection (GCC) (since version 3.4), the Linux kernel, Apache OpenOffice, LibreOffice, and Mozilla Firefox.
  • Rocky Bernstein's Remake is a fork of GNU Make and provides several extensions over GNU Make, such as better location and error-location reporting, execution tracing, execution profiling, and it contains a debugger.
  • Glenn Fowler's nmake (unrelated to the same-named Microsoft variant) is incompatible with the UNIX variant, but provides features which, according to some, reduce the size of makefiles by a factor of 10.
  • Microsoft nmake is normally installed with Visual Studio. It supports preprocessor directives such as includes and conditional expressions which use variables set on the command-line or within the makefiles. Inference rules differ from Make; for example they can include search paths.
  • Embarcadero make has a command-line option that "Causes MAKE to mimic Microsoft's NMAKE.".
  • Qt Project's Jom tool is a clone of nmake.
  • Mk replaced Make in Research Unix, starting from version 9. A redesign of the original tool by Bell Labs programmer Andrew G. Hume, it features a different syntax. Mk became the standard build tool in Plan 9, Bell Labs' intended successor to Unix.
  • Kati is Google's replacement of GNU Make, as of 2020 used in Android OS builds. It translates the makefile into ninja for faster incremental builds (similar to the cmake metatool).
  • Snakemake is a Python-driven implementation for compiling and running bioinformatics workflows.

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

Use

In general, based on a makefile, Make updates target files from source files if any source file has a newer timestamp than the target file or the target file does not exist. For example, this could include compiling C files () into object files, then linking the object files into an executable program. Or this could include compiling TypeScript files () to JavaScript for use in a browser. Other examples include: convert a source image file to another format, copy a file to a content management system, and send e-mail about build status.

A makefile defines targets where each is either a file to generate or is a user-defined concept, called a phony target.

Make updates the targets passed as arguments:

::code[lang=bash] make [-f makefile] [options] [targets] ::

If no target is specified, Make updates the first target in the makefile which is often a phony target to perform the most commonly used action.

Make skips build actions if the target file timestamp is after that of the source files. Doing so optimizes the build process by skipping actions when the target file is up-to-date, but sometimes updates are skipped erroneously due to file timestamp issues including restoring an older version of a source file, or when a network filesystem is a source of files and its clock or time zone is not synchronized with the machine running Make. Also, if a source file's timestamp is in the future, make repeatedly triggers unnecessary actions, causing longer build time.

When Make starts, it uses the makefile specified on the command-line or if not specified, then uses the one found by via specific search rules. Generally, Make defaults to using the file in the working directory named . GNU Make searches for the first file matching: , , or .

Make processes the options of the command-line based on the loaded makefile.

Makefile

makefile |name=Makefile |uniform type=public.make-source

The makefile language is partially declarative programming where end conditions are described but the order in which actions are to be taken is not. This type of programming can be confusing to programmers used to imperative programming.

Makefiles can contain the following constructs:

  • Explicit rule: defines when and how to update a target, listing prerequisites (dependent targets) and commands that define the update action, called the recipe
  • Implicit rule: defines when and how to remake a class of files based on their names, including how a target depends on a file with a name similar to the target and an update recipe
  • Variable definition: associates a text value with a name that can be substituted into later text
  • Directive: instruction to do something special such as include another makefile
  • Comment: line starting with

Rules

Each rule begins with a dependency line which consists of the rule's target name followed by a colon (:), and optionally a list of targets (also known as prerequisites) on which the rule's target depends.

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

Usually a rule has a single target, rather than multiple.

A dependency line may be followed by a recipe: a series of TAB indented command lines that define how to generate the target from the components (i.e. source files). If any prerequisite has a more recent timestamp than the target file or the target does not exist as a file, the recipe is performed.

The first command may appear on the same line after the prerequisites, separated by a semicolon, ::code[lang=make] targets: prerequisites ; command ::

for example, ::code[lang=make] hello: ; @echo "hello" ::

Each command line must begin with a tab character. Even though a space is also whitespace, Make requires tab. Since this often leads to confusion and mistakes, this aspect of makefile syntax is subject to criticism. Eric S. Raymond describes it as "one of the worst design botches in the history of Unix" and The Unix-Haters Handbook said "using tabs as part of the syntax is like one of those pungee [sic] stick traps in The Green Berets". Feldman explains the choice as caused by a workaround for an early implementation difficulty, and preserved by a desire for backward compatibility with the very first users:

GNU Make since version 3.82 allows the choice of any symbol (one character) as the recipe prefix using the .RECIPEPREFIX special variable: ::code[lang=make] .RECIPEPREFIX := : all: :@echo "recipe prefix symbol is set to '$(.RECIPEPREFIX)'" ::

Each command is executed in a separate shell. Since operating systems use different shells, this can lead to unportable makefiles. For example, GNU Make (all POSIX Makes) executes commands with /bin/sh by default, where Unix commands like cp are normally used. In contrast, Microsoft's nmake executes commands with cmd.exe where batch commands like copy are available but not necessarily cp.

Since a recipe is optional, the dependency line can consist solely of components that refer to other targets: ::code[lang=make] realclean: clean distclean ::

The following example rule is evaluated when Make updates target file.txt via . If file.html is newer than file.txt or file.txt does not exist, then the command is run to generate file.txt from file.html.

::code[lang=make] file.txt: file.html lynx -dump file.html > file.txt ::

A command can have one or more of the following prefixes (after the tab):

  • minus (-) specifies to ignore an error from the command
  • at (@) specifies to not output the command before it is executed
  • plus (+) specifies to execute the command even if Make is invoked in "do not execute" mode

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

Microsoft's NMAKE has predefined rules that can be omitted from these makefiles, e.g. .

Macros

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

For example, the macro is frequently used in makefiles to refer to the location of a C compiler. If used consistently throughout the makefile, then the compiler used can be changed by changing the value of the macro rather than changing each rule command that invokes the compiler.

Macros are commonly named in all-caps:

::code[lang=make] MACRO = definition ::

A macro value can consist of other macro values. The value of macro is expanded on each use lazily.

A macro is used by expanding either via $NAME or $(NAME). The latter is safer since omitting the parentheses leads to Make interpreting the next letter after the as the entire variable name. An equivalent form uses curly braces rather than parentheses, i.e. , which is the style used in BSD.

::code[lang=make] NEW_MACRO = $(MACRO)-$(MACRO2) ::

Macros can be composed of shell commands by using the command substitution operator !=.

::code[lang=make] YYYYMMDD != date ::

The command-line syntax for overriding a macro is:

::code[lang=bash] make MACRO="value" [MACRO="value" ...] TARGET [TARGET ...] ::

Makefiles can access predefined internal macros, with and being common.

::code[lang=make] target: component1 component2 # echo components YOUNGER than TARGET echo $? # echo TARGET name echo $@ ::

A common syntax when defining macros, which works on BSD and GNU Make, is to use , , and instead of the equal sign ().

Suffix rules

Suffix rules have "targets" with names in the form and are used to launch actions based on file extension. In the command lines of suffix rules, POSIX specifies that the internal macro is part of the command line, whereas {{code|$

::code[lang=make] .SUFFIXES: .txt .html

From .html to .txt

.html.txt: lynx -dump $< > $@ ::

When called from the command line, the example above expands:

::code[lang=console] $ make -n file.txt lynx -dump file.html > file.txt ::

Pattern rules

Suffix rules cannot have any prerequisites of their own. If they have any, they are treated as normal files with unusual names, not as suffix rules. GNU Make supports suffix rules for compatibility with old makefiles but otherwise encourages usage of pattern rules.

A pattern rule looks like an ordinary rule, except that its target contains exactly one character within the string. The target is considered a pattern for matching file names: the can match any substring of zero or more characters, while other characters match only themselves. The prerequisites likewise use to show how their names relate to the target name.

The example above of a suffix rule would look like the following pattern rule: ::code[lang=make]

From %.html to %.txt

%.txt : %.html lynx -dump $< > $@ ::

Comment

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

Directive

A directive specifies special behavior such as including another makefile.

Line continuation

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

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

Examples

The following commands are in the context of the makefile that follows.

::code[lang=bash] make # updates first target, 'all' make help # updates target 'help' to list targets make dist # updates target 'dist' to build for distribution ::

::code[lang=make] PACKAGE = package VERSION = date "+%Y.%m%d%" RELEASE_DIR = .. RELEASE_FILE = $(PACKAGE)-$(VERSION)

Default target

note: variable LOGNAME comes from the environment

all: echo "Hello $(LOGNAME), nothing to do by default" echo "Try 'make help'"

Display targets by searching this file

help: egrep "^# target:" [Mm]akefile

Make a release

dist: tar -cf $(RELEASE_DIR)/$(RELEASE_FILE) &&
gzip -9 $(RELEASE_DIR)/$(RELEASE_FILE).tar ::

Below is a simple makefile that by default (the "all" rule is listed first) compiles a source file called "helloworld.c" using the system's C compiler and also provides a "clean" target to remove the generated files if the user desires to start over. The and {{code|$

::code[lang=make] CFLAGS ?= -g LDLIBS += -lm

all: Out.txt

Out.txt: helloworld ./$< > $@

helloworld: helloworld.o $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

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

clean: $(RM) Out.txt helloworld helloworld.o ::

Many systems at least open group base specification compliant systems, see above ref -- come with predefined Make rules and macros to specify common tasks such as compilation based on file suffix. This lets users omit the actual (often unportable) instructions of how to generate the target from the source(s). On such a system the makefile above could be modified as follows:

::code[lang=make] all: helloworld

helloworld: helloworld.o $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^

clean: $(RM) helloworld helloworld.o

suffix rule

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

.SUFFIXES: .c ::

it appears that the open group base specification says .SUFFIXES and simple suffix rules must work, so there, gnu make.

That "helloworld.o" depends on "helloworld.c" is now automatically handled by Make. In such a simple example as the one illustrated here this hardly matters, but the real power of suffix rules becomes evident when the number of source files in a software project starts to grow. One only has to write a rule for the linking step and declare the object files as prerequisites. Make will then implicitly determine how to make all the object files and look for changes in all the source files.

Simple suffix rules work well as long as the source files do not depend on each other and on other files such as header files. Another route to simplify the build process is to use so-called pattern matching rules that can be combined with compiler-assisted dependency generation. As a final example requiring the gcc compiler and GNU Make, here is a generic makefile that compiles all C files in a folder to the corresponding object files and then links them to the final executable. Before compilation takes place, dependencies are gathered in makefile-friendly format into a hidden file ".depend" that is then included to the makefile. Portable programs ought to avoid constructs used below.

::code[lang=make]

Generic GNUMakefile

snippet to fail if not GNU

ifneq (,) This makefile requires GNU Make. endif

PROGRAM = foo C_FILES := $(wildcard *.c) OBJS := $(patsubst %.c, %.o, $(C_FILES)) CC = cc CFLAGS = -Wall -pedantic LDFLAGS = LDLIBS = -lm

all: $(PROGRAM)

$(PROGRAM): .depend $(OBJS) $(CC) $(CFLAGS) $(OBJS) $(LDFLAGS) -o $(PROGRAM) $(LDLIBS)

depend: .depend

.depend: cmd = gcc -MM -MF depend $(var); cat depend >> .depend; .depend: @echo "Generating dependencies..." @$(foreach var, $(C_FILES), $(cmd)) @rm -f depend

-include .depend

These are the pattern matching rules. In addition to the automatic

variables used here, the variable $* that matches whatever % stands for

can be useful in special cases.

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

%: %.o $(CC) $(CFLAGS) -o $@ $<

clean: rm -f .depend $(OBJS)

.PHONY: clean depend

::

Dependency tracking

Makefile consist of dependencies and a forgotten or an extra one may not be immediately obvious to the user and may result in subtle bugs in the generated software that are hard to catch. Various approaches may be used to avoid this problem and keep dependencies in source and makefiles in sync. One approach is using the compiler to keep track of dependencies changes. GCC can statically analyze the source code and produce rules for the given file automatically by using the switch. The other approach would be makefiles or third-party tools that would generate makefiles with dependencies (e.g. Automake toolchain by the GNU Project, can do so automatically).

Another approach is to use meta-build tools like CMake, Meson etc.

References

References

  1. (1 September 2013). "V7/usr/src/cmd/make/ident.c".
  2. (April 1979). "Make --- A Program for Maintaining Computer Programs". Software: Practice and Experience.
  3. Matthew Doar. (2005). "Practical Development Environments". [[O'Reilly Media]].
  4. (November 1980). "Designer's Workbench: Providing a Production Environment". Bell System Technical Journal.
  5. "Google Groups".
  6. (12 December 2013). "OpenSolaris at Two (Jim Grisanzio)".
  7. Grisanzio, Jim. [http://www.guug.de/veranstaltungen/osdevcon2007/slides/grisanzio-opensolaris-story-guug.pdf The OpenSolaris Story]{{Dead link. (December 2024)
  8. "Apache OpenOffice Building Guide - Apache OpenOffice Wiki".
  9. "Development/Gbuild - The Document Foundation Wiki".
  10. (1993). "FreeBSD 2.0.5 Make Source Code".
  11. "Bmake(1)".
  12. "fmake(1) General Commands Manual".
  13. "make".
  14. "make(1) - OpenBSD manual pages".
  15. "make".
  16. Arnold Robbins. (2005). "Unix in a Nutshell, Fourth Edition". O'Reilly.
  17. (2013). "8. Functions for Transforming Text". Free Software Foundation.
  18. (2013). "8.5 The foreach Function". Free Software Foundation.
  19. (2006). "GCC 3.4 Release Series Changes, New Features, and Fixes". Free Software Foundation.
  20. Javier Martinez Canillas. (December 26, 2012). "Kbuild: the Linux Kernel Build System".
  21. Greg Kroah-Hartman. (2006). "Linux Kernel in a Nutshell". O'Reilly.
  22. "Build Instructions".
  23. Rocky Bernstein. "Remake – GNU Make with comprehensible tracing and a debugger".
  24. Glenn Fowler. (January 4, 2012). "nmake Overview". Information and Software Systems Research, AT&T Labs Research.
  25. (2015). "NMAKE Reference Visual Studio 2015". Microsoft.
  26. (2014). "Makefile Preprocessing Directives".
  27. (2014). "Makefile Preprocessing Operators". Microsoft.
  28. (2014). "Search Paths in Rules". Microsoft.
  29. (2008). "MAKE". CodeGear(TM).
  30. (2021). "Jom - Qt Wiki". Qt Project.
  31. McIlroy, M. D.. (1987). "A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986".
  32. (2002). "Maintaining files on Plan 9 with Mk". AT&T Bell Laboratories.
  33. (30 November 2020). "google/kati: An experimental GNU make clone".
  34. (2021-04-19). "Sustainable data analysis with Snakemake". F1000Research.
  35. "GNU 'make'". Free Software Foundation.
  36. "Makepp".
  37. "Free BSD make".
  38. [http://alvinalexander.com/linux/unix-linux-ls-command-file-sort-sorting How to sort Linux ls command file output] {{webarchive. link. (September 13, 2016)
  39. "makefile". [[Apple Inc]].
  40. Adams, P. and Solomon, M., 1993, An overview of the CAPITL software development environment. In International Workshop on Software Configuration Management (pp. 1-34). Berlin, Heidelberg: Springer Berlin Heidelberg.
  41. link. (October 23, 2007 , 2007/02/27, phoenix wiki)
  42. [http://lists.w3.org/Archives/Public/www-ws-arch/2002Aug/0105.html Re: Choreography and REST] {{webarchive. link. (September 12, 2016 , from Christopher B Ferris on 2002-08-09)
  43. [http://www.robots.ox.ac.uk/~tgtjr/makefiles.html Target Junior Makefiles] {{webarchive. link. (January 7, 2010 , Andrew W. Fitzgibbon and William A. Hoffman)
  44. [https://www.gnu.org/software/make/manual/html_node/Makefile-Contents.html 3.1 What Makefiles Contain] {{Webarchive. link. (2021-05-05 , ''GNU make'', [[Free Software Foundation]])
  45. "Prerequisite Types (GNU make)". [[GNU Project]].
  46. "Chapter 15. Tools: make: Automating Your Recipes", ''The Art of Unix Programming'', [[Eric S. Raymond]] 2003
  47. {{man. 1. make. SUS
  48. "Make".
  49. {{man. 1. make. FreeBSD
  50. "GNU make manual: suffix rules". Free Software Foundation.
  51. "GNU make manual: pattern rules". Free Software Foundation.
  52. [http://www.lehman.cuny.edu/cgi-bin/man-cgi?make+1 See section ''Pattern Matching Rules'' in the SunPro man page] {{webarchive. link. (May 29, 2014)
  53. [https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html#Automatic-Variables Automatic Variables] {{webarchive. link. (April 25, 2016 GNU `make')
  54. "make".

::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::

articles-with-example-codebuild-automationcompiling-toolsgnu-project-softwareunix-programming-toolsunix-sus2008-utilities