The Fandom Coders Encyclopædia

Makefile

Text file format for scripting automation

A Makefile is a kind of text file which is intended to automate scripting tasks, especially the build process of a program or other file. Makefiles are typically executed via a command line program called make, for example External : GNU Make. Although the format of Makefiles itself comprises a kind of rudimentary programming language (including variables which can be modified and functions which can be called), Makefiles also typically consist of a large number of commands written in a shell scripting language, typically for the Bourne shell.

§ Overview

A Makefile consists of a number of rules, each of which has the following basic form :—

target […]: [prerequisite …]
	[command]
	[…]

—: that is, one or more targets, a colon, zero or more prerequisites, and then, on the following lines, zero or more commands, each prefixed by a U+0009 HORIZONTAL TABULATION character. The targets and prerequisites may be paths to actual files; if they are, the rule will only be executed if the last‐modified date of the target is older than one of the prerequisites. This prevents unneccessary rebuilding of files whose prerequisites have not changed. If the target does not point to an actual file, it is “phony” and the rule will always be executed. all is a common phony target for building everything, while clean is commonly used to delete temporary build files.

The commands of a Makefile are shell commands, typically for the Bourne shell. When make executes a rule, it first executes any rules corresponding to its prerequisites (if necessary and applicable), and then simply runs each command in the rule in order. If the target of the rule isn’t phony, executing the rule should build the target (assuming a well‐written Makefile). Then, since the last‐modification date of the target is now newer than that of its prerequisites, running make again will do nothing (until the next time a prerequisite is updated).

Makefiles can be considerably more complex than this simple description allows, although the exact features will depend on the version of make you are using. See the External : GNU Make Manual for a comprehensive account of the list of features in GNU Make.

This article was written by kibigo!.