Services

Resources

Company

Makefile

A Makefile is a file of rules read by make, a build tool that runs commands to produce targets from their prerequisites. Each rule names a target, the files or other targets it depends on, and the shell commands, called the recipe, that build it. Many teams also use a Makefile as a short, documented list of project commands, such as make build, make test and make run.

How it works

When you ask for a target, make builds its prerequisites first, then runs its recipe. For file targets it compares modification times and runs the recipe only if a prerequisite is newer than the target, so an unchanged binary is not rebuilt. Every recipe line must start with a tab character, not spaces, or GNU make stops with missing separator. Targets that name commands rather than files are listed under .PHONY so they always run.

The SRE bootcamp asks for targets that start the database, run migrations, build the API image and run the API, where running the API first starts the database and applies migrations. A rule run: start-db migrate does that: make run runs start-db (docker compose up -d db), then migrate, then its own recipe, docker compose up -d api. For a file target such as bin/api: main.go, a second make bin/api with no edits prints 'bin/api' is up to date and compiles nothing.

Why it matters in production

A Makefile gives every repository the same entry points, so developers and the CI/CD pipeline run identical commands: CI calls make test instead of copying shell steps into pipeline YAML. Keep recipes short and move longer logic into scripts, because each recipe line runs in its own shell. With make -j, prerequisites can run in parallel, so if migrate needs the database, declare migrate: start-db as well rather than relying on left-to-right order.

See also

On the One2N blog

References

  1. GNU make manual (GNU project)

Four-panel comic about Makefiles: Asha follows a five-step README to start the API and skips the migrations; a rule shows a target, its prerequisites and a tab-indented recipe; make run starts the database, runs migrations, then starts the API, and a second make bin/api says it is up to date; Asha and the CI bot run the same make test.