Introduction
A Makefile holds the rules for the make program, the Unix build tool
first written in 1976. Each rule says how to make a file, such as a program or an
object file, from the files it depends on. make compares the files’
modification times and runs only the commands whose results are out of date.
The main file usually has no extension at all: make looks for a file
named GNUmakefile, makefile or Makefile in the
current folder. Makefiles that are included by others, or kept in a library of shared
rules, are often named .mk. Tools such as
CMake and Autotools write Makefiles for a project
rather than having people write them by hand.
Structure
A Makefile is plain text. A rule is a line naming a target, a colon and its
prerequisites, followed by the recipe: commands on lines that must start with a tab
character, not spaces. Variables are set with = or := and used
as $(NAME). Automatic variables stand for parts of the rule:
$@ is the target, $< the first prerequisite and
$^ all of them. Pattern rules with % describe how to make any
file of one kind from another, and .PHONY marks targets such as
clean that are not files.
CC = cc
CFLAGS = -O2 -Wall
app: main.o util.o
$(CC) -o $@ $^
%.o: %.c
$(CC) $(CFLAGS) -c $<
.PHONY: clean
clean:
rm -f app *.o
GNU make, BSD make and Microsoft’s NMAKE share this core but differ in their extensions, such as conditionals and functions, so a Makefile is often written for one of them.
Not described
Known, not yet covered
- Reference: this overview is the only page. It gives the shape of a rule and a few variables. These are not described: how make reads a Makefile in two phases, the flavours of variable assignment (
=,:=,?=,+=),include, conditionals (ifeq,ifdef), the text and file functions ($(wildcard),$(foreach),$(call),$(shell)),defineblocks, implicit and pattern rules, the built-in variables, order-only prerequisites, double-colon rules and the special targets other than.PHONY(.SUFFIXES,.ONESHELL,.DEFAULT_GOAL). The GNU Make manual describes them. - Reference: the differences between GNU make, BSD make and NMAKE are named but not listed: NMAKE’s
!IFdirectives and inline files, BSD make’s.ifconditionals and.include, and POSIX make’s own minimal rules. The make command line (-j,-n,-f) is not described. - Viewer: both words of
else ifdef DEBUGare directives, and a nested reference is one colour up to its matching bracket ($(call f,$(g $(h))), within 256 characters on the line). NMAKE directives that start with!(!IF,!INCLUDE) and BSD make’s.iflines are not directives. - Viewer: a reference is a single colour, so the function name inside it (
wildcard,shell,call) is not told apart from a variable name and a reference inside it is not coloured separately.target: VAR = x(a target-specific variable) is coloured as a target. - Viewer: a recipe line is only split into variables and quoted strings: a
$(…)inside quotes is part of the string colour, and the shell commands are not read as shell. The file is not parsed or run, so there is no target list, no dependency graph and no list of problems, and only the first 1 MiB is read and coloured.
Links
Software
Reference
- GNU Make manual – the full description of GNU Make’s Makefile syntax