AAmit’s LibraryLEARN · PRACTISE · REFLECT

Computers · Programming Atlas

Makefile Foundations

Build-language basics: targets, prerequisite graphs, timestamps, phony actions, variables, automatic variables, pattern rules, order-only prerequisites, failure and observable tests. Not a general-purpose language or a replacement shell tutorial.

Audience: Learners comfortable saving plain-text files and using a terminal.

Practice: 4 to 6 hours.

Runtime and boundaries: GNU Make 3.81 is the installed older implementation. Save each example and its companions in a separate NEW practice directory. Run /usr/bin/make -rR --no-print-directory -f "Example.mk" (replace the filename). Recipe lines begin with a literal tab. Outputs are fixed toy filenames inside that directory. Built-in rules/variables are disabled; /bin/sh runs recipes. Do not run untrusted makefiles, pass untrusted variable overrides or point at existing documents. make -n is NOT a sandbox: makefile evaluation, included files and recursive commands can execute code. No .ONESHELL, grouped targets, downloads, recursive builds or destructive clean targets are used.

Lessons

A build language and its executor

Makefile describes targets, prerequisites and recipes. GNU Make is the build tool executing that description; recipes use a shell. This pack tests GNU Make 3.81, not every Make dialect. Start each example in a separate new directory with only its supplied files.

Graphs and freshness

A normal target is rebuilt when missing or when a normal prerequisite has a newer modification time. Make compares timestamps, not file contents. Changing content while preserving an old timestamp may be missed. Each direct input belongs in the graph; a successful build does not prove complete dependencies.

Rules and phony actions

The first ordinary target is the default goal here. Write target: prerequisites, followed by tab-prefixed recipe lines. .PHONY marks actions that should run even if a same-named file exists. Do not make a phony target a normal prerequisite of a real file unless rebuilding each time is intentional.

Variables and expansion

With =, references expand when the variable is used; with :=, the right-hand side expands at assignment. Command-line assignments can override ordinary assignments. Expansion is not shell quoting: even quoted interpolated values can become dangerous when untrusted data contains shell syntax. All values here are fixed authored text.

Recipes and process boundaries

Each recipe line runs in a separate shell in this version. Join commands deliberately when state must persist. $$ passes a dollar sign through Make to the shell. A nonzero recipe exit stops that target and normally the build. Do not hide errors with a leading minus just to make the output appear successful.

Automatic and pattern variables

In a recipe, $@ names the target and $< names the first prerequisite. A %.out: %.in rule matches stems. These examples use fixed simple filenames without spaces; arbitrary path quoting and multi-output generator correctness require a separate design.

Order-only and failure semantics

Prerequisites after | must be available before a target is built, but their newer timestamp alone does not make the target stale. .DELETE_ON_ERROR requests deletion of a changed target after recipe failure; it is discussed, not used here. Our expected failure example writes no partial target.

Testing the graph

The harness runs each successful default target, checks outputs, repeats selected builds and changes controlled timestamps. A separate check expects a recipe failure and confirms dependents do not run. Invalid parser fixtures have no executable recipes. Neither -n nor a temporary folder is a security sandbox; only this authored material was exercised.

Reference cards

Rule: target: prerequisites

Names a graph edge and its recipe.

Recipe: TAB command

A real tab starts the command line.

Default: first ordinary target

Used when no goal is supplied in these examples.

Action: .PHONY: all test

Always considers these action targets out of date.

Expansion: NAME = $(OTHER)

Expand references later.

Snapshot: NAME := $(OTHER)

Expand the right side now.

Override: make NAME=value

Command-line assignments can override ordinary ones.

Target: $@

Current target name inside a recipe.

Input: $<

First prerequisite name in a recipe.

Pattern: %.out: %.in

Match a common filename stem.

Order only: target: input | stamp

Wait for stamp without using its age for freshness.

Dollar: $$name

Pass a dollar reference to the recipe shell.

Echo: @command

Suppress echo of this recipe, not its output.

Failure: nonzero exit status

Stop this target; dependents cannot succeed.

Inspect: make -n

Show most recipes; NOT safe for untrusted makefiles.

Pinned run: make -rR -f file.mk

Disable built-ins and choose this authored file.

Supporting configuration: Run instructions.txt
GNU Make 3.81 is the installed older implementation. Save each example and its companions in a separate NEW practice directory. Run /usr/bin/make -rR --no-print-directory -f "Example.mk" (replace the filename). Recipe lines begin with a literal tab. Outputs are fixed toy filenames inside that directory. Built-in rules/variables are disabled; /bin/sh runs recipes. Do not run untrusted makefiles, pass untrusted variable overrides or point at existing documents. make -n is NOT a sandbox: makefile evaluation, included files and recursive commands can execute code. No .ONESHELL, grouped targets, downloads, recursive builds or destructive clean targets are used.

Examples and practice

Code below is reading material, not an execution console. Download the edition for the complete example files and companion modules.

Make 01 - Default and phony

The first ordinary target is the default. .PHONY ensures the action still runs if a file named all exists.

.PHONY: all
all:
	@printf 'Goal: all\n'

Expected:

Goal: all

Intentionally invalid Makefile: expected Make parser diagnostic containing missing separator. Expected rejection; not a successful program. No runtime execution follows rejection.

all:
    printf bad
Make 02 - Dependency graph

A missing report is built from the supplied input. The phony all action has no recipe and does not itself force report.txt to rebuild.

.PHONY: all
all: report.txt
report.txt: input.txt
	@cp "$<" "$@"
	@printf 'Built report\n'

Expected:

Built report

Companion file: input.txt

minutes: 30
Make 03 - Expansion time

The recursively expanded variable sees the later LABEL; the simple variable captured the earlier text. Values are trusted literals.

LABEL = first
LATER = $(LABEL)
NOW := $(LABEL)
LABEL = second
.PHONY: all
all:
	@printf '%s / %s\n' '$(LATER)' '$(NOW)'

Expected:

second / first

Intentionally invalid Makefile: expected Make parser diagnostic containing unterminated variable reference. Expected rejection; not a successful program. No runtime execution follows rejection.

X = $(missing
all: $(X)
Make 04 - Automatic variables

Automatic variables are defined in the recipe context. Filenames are fixed simple toy names.

.PHONY: all
all: copied.txt
copied.txt: study.txt
	@cp "$<" "$@"
	@printf 'Target %s from %s\n' "$@" "$<"

Expected:

Target copied.txt from study.txt

Companion file: study.txt

study
Make 05 - Pattern rule

One pattern rule serves two stems. Output order here assumes the default serial invocation; parallel builds are outside this evidence.

.PHONY: all
all: first.out second.out
%.out: %.in
	@cp "$<" "$@"
	@printf 'Built %s\n' "$@"

Expected:

Built first.out
Built second.out

Companion file: first.in

one

Companion file: second.in

two
Make 06 - Order-only prerequisite

The stamp must exist first. Its newer timestamp alone does not cause result.txt to rebuild.

.PHONY: all
all: result.txt
result.txt: readiness.txt | ready.stamp
	@cp "$<" "$@"
	@printf 'Built result\n'
ready.stamp:
	@printf 'ready\n' > "$@"

Expected:

Built result

Companion file: readiness.txt

ready input
Make 07 - Recipe shell boundary

Make turns $$ into $. Each separate recipe line starts another shell, so the first line variable is absent from the second.

.PHONY: all
all:
	@word=study; printf 'Same shell: %s\n' "$$word"
	@printf 'Next shell: %s\n' "$${word-unset}"

Expected:

Same shell: study
Next shell: unset

Intentionally invalid Makefile: expected Make parser diagnostic containing missing `endef'. Expected rejection; not a successful program. No runtime execution follows rejection.

define NOTE
unfinished
Make 08 - Failure propagation

The default target succeeds. A separate harness invocation of after intentionally fails; the dependent recipe must not run. That failure is expected test evidence, not a successful build.

.PHONY: all check fail after
all: check
check:
	@printf 'Normal check passed\n'
after: fail
	@printf 'should not exist\n' > after.txt
fail:
	@printf 'Expected toy failure\n'
	@exit 7

Expected:

Normal check passed
Make 09 - Trusted configuration

The harness overrides LEVEL with one trusted literal. This recipe is not safe for arbitrary user-controlled override values; Make expansion precedes shell parsing.

LEVEL = basic
.PHONY: all
all:
	@printf 'Level: %s\n' '$(LEVEL)'

Expected:

Level: basic

Intentionally invalid Makefile: expected Make parser diagnostic containing missing `endif'. Expected rejection; not a successful program. No runtime execution follows rejection.

ifeq (one,one)
all:
Make exercise A - Build then test

The test depends on the output. cmp verifies its bytes; the build alone does not establish this assertion.

.PHONY: all test
all: summary.txt
summary.txt: lesson.txt
	@cp "$<" "$@"
	@printf 'Built summary\n'
test: summary.txt
	@cmp lesson.txt summary.txt
	@printf 'Test passed\n'

Expected:

Built summary

Companion file: lesson.txt

practice: 20

Practice: Write a real file target and a separate phony test target that checks it.

Make exercise B - All direct inputs

Both input files are prerequisites. A changed second input must trigger a rebuild. Recipe filenames are explicit authored literals.

.PHONY: all
all: combined.txt
combined.txt: first.txt second.txt
	@cat first.txt second.txt > "$@"
	@printf 'Built combined\n'

Expected:

Built combined

Companion file: first.txt

first

Companion file: second.txt

second

Practice: Combine two toy inputs and declare both as normal prerequisites.

Make exercise C - Observable assertions

Four shell test commands execute under Make. A failing check stops the target before the success message. The authored fresh directory makes absent.txt meaningful.

.PHONY: all test
all: test
toy.txt:
	@printf 'study\n' > "$@"
test: toy.txt
	@test -f toy.txt
	@test -s toy.txt
	@test "$$(cat toy.txt)" = study
	@test ! -e absent.txt
	@printf 'Assertions: 4 passed\n'

Expected:

Assertions: 4 passed

Practice: Make a test target that checks existence, nonempty output, content and an absent toy path.

Prompt templates

These are editable planning prompts in the downloadable workbench; they do not contact an AI service here.

Build a bounded example
For {language}, help with {goal}. Runtime: {runtime}. Constraints: {constraints}. Provide complete source, exact commands, expected output and supported tool versions. Do not claim checks ran without evidence.
Debug a reproducible failure
For {language}, help with {goal}. Runtime: {runtime}. Constraints: {constraints}. Separate parser or compiler diagnostics, process failures and wrong results; reduce the reproduction. Do not claim checks ran without evidence.
Test observable behavior
For {language}, help with {goal}. Runtime: {runtime}. Constraints: {constraints}. Cover empty, boundary and failure cases; distinguish proposed tests from executed tests. Do not claim checks ran without evidence.
Refactor with evidence
For {language}, help with {goal}. Runtime: {runtime}. Constraints: {constraints}. Preserve outputs and behavior; explain regression checks and tool-specific assumptions. Do not claim checks ran without evidence.
Document the contract
For {language}, help with {goal}. Runtime: {runtime}. Constraints: {constraints}. State input types, side effects, error policy, portability limits and actual test evidence. Do not claim checks ran without evidence.
Explain with practice
For {language}, help with {goal}. Runtime: {runtime}. Constraints: {constraints}. Use an original example, predicted output, an exercise and a separate solution. Do not claim checks ran without evidence.

Further reading

Download the offline edition

Complete Atlas 0.11.0 ZIP (6,079,970 bytes) · 411-page guide and catalogue PDF · Versioned source JSON

Unzip the edition and open Programming and Scripting Atlas.html, Web Foundations.html, Python Foundations.html, SQL Foundations.html, Shell Foundations.html, TypeScript Foundations.html Data Formats Foundations.html, C Foundations.html C++ Foundations.html, Ruby Foundations.html, Perl Foundations.html, Swift Foundations.html, Makefile Foundations.html, awk Foundations.html, sed Foundations.html, jq Foundations.html, XSLT 1.0 Foundations.html, Tcl Foundations.html, Expect Foundations.html or Objective-C Foundations.html locally. The original interactive workbenches retain their offline prompt-building, local presets and export controls. Read the included README before running examples yourself. The workbench does not execute pasted code or call an AI service; prompt text is for you to inspect and use separately.

Version 0.11.0 is pinned here. The other 257 registry entries remain planned; this edition does not claim complete language coverage.

Evidence and limits

Objective-C adds 12 accepted native programs, four separate compiler or warning-policy rejections, 120 root checks and 147 independent native checks. Two rejection fixtures are warnings promoted by -Werror; they are not intrinsic syntax failures. No rejected fixture was executed. Tests dated 26 October 2026 used Apple Clang 21.0.0, ARC, C17 base mode, macOS SDK 27.0 headers and system Foundation 6.9 (5027.0.69) on macOS 27.0.1 arm64. This is introductory macOS/Foundation coverage, not iOS, GNUstep, Objective-C++, UI, concurrency or cross-platform coverage. No sanitizers, leak detector, race analysis, allocation/disk-full/permission fault injection or asynchronous timing tests were run. Trusted authored fixtures and process bounds do not establish an arbitrary-code sandbox.

The pinned Atlas 0.11.0 evidence records 607 browser checks, 55 Python harness checks plus four unittest methods, 74 SQL checks, 515 shell checks across 71 runs, 99 TypeScript checks, 51 Data Formats checks, 119 C checks, 116 C++ checks, 97 Ruby checks, 97 Perl checks, 105 Swift harness checks and 97 Makefile harness checks, 145 awk checks, 145 sed checks, 161 jq checks, 157 XSLT checks, 150 Tcl checks and 161 Expect checks. These are separate measures. Tcl and Expect tests are dated 26 October 2026 on installed Tcl 8.5.9 / Expect 5.45 with Tcl 8.5.9 on macOS arm64. These old versions are recorded teaching evidence, not deployment recommendations. Tcl has 12 accepted programs and Expect has 12 accepted controllers; each has four expected evaluation failures, not static compilation passes. Tcl has nine scenarios, five expected nonzero; Expect has seven scenarios, four expected nonzero. Expect default controllers invoke 15 toy children, including two expected child failures; one scenario also expects a failed child. Controller success does not mean every child succeeds. The authoring release records 100 owned-runner regression checks and 109 independent runner checks separately. Only authored fixtures and retained native child handles are covered. Cooperative cleanup requires event-loop progress; outer-timeout child cleanup remains uncertain without complete wait receipts. The retired PID-inspection branch was not retried. No sign-ins, secrets, network or external application control are taught or tested by this pack. awk and sed are text-processing languages; their examples use fixed authored ASCII input and the C locale with installed awk 20200816 and macOS sed on one platform. jq and XSLT each have 12 accepted examples and four separately rejected programs. jq has 21 scenarios, including five contract errors and one no-result status; XSLT has 20 scenarios, including five terminating messages and one CLI parameter rejection. Expected nonzero outcomes are not successful programs. Tests dated 26 October 2026 used jq 1.7.1-apple and XSLT 1.0 with libxslt 1.1.35/libxml2 2.9.13, fixed trusted programs and tiny authored inputs in the C locale on macOS arm64. These old installed XML libraries are recorded learning environments, not recommendations for untrusted-document processing. No imports, external documents, DTDs, custom/external entities, extension functions or generated source are covered. Command flags and bounds are not a complete sandbox. Standalone XPath remains planned; supporting XPath 1.0 expressions are limited to these XSLT examples. These tests do not establish GNU-tool equivalence, Unicode behavior, arbitrary-script safety or cross-platform portability. Swift has 12 accepted programs and four expected static rejections; Makefile has 12 accepted default targets, four expected parser rejections, eight additional scenarios and one expected recipe failure. Makefile is build scripting, not a general-purpose language. The Swift and Makefile test execution dates remain 25 October 2026. awk and sed tests are dated 26 October 2026. Delivery verification does not replace or refresh test execution dates. TypeScript includes 12 accepted programs executed as JavaScript and nine intentionally invalid cases correctly rejected. Each C/C++ pack has 12 accepted programs run normally and with AddressSanitizer/UndefinedBehaviorSanitizer, plus four expected compiler rejections that are never executed. Ruby and Perl each have 12 syntax-accepted programs executed and four expected syntax rejections; rejected cases are not successful programs. Syntax checks may execute compile-time code and are not arbitrary-code sandboxes. Sanitizer results cover executed paths only; leak detection and allocation-failure injection were not tested and memory safety is not proved.

Tested environments are Chrome 155, CPython 3.9.6, SQLite CLI 3.54.0, macOS sh/Bash 3.2.57/Zsh 5.9, TypeScript 6.0.3, Node 24.18.0 Apple Clang 21.0.0, Ruby 2.6.10p210 and Perl 5.34.1, Apple Swift 6.4 in Swift 6 mode and GNU Make 3.81 on one arm64 macOS host. C uses a C17 subset; C++ uses a C++20 subset with libc++ 220106. Python 3.9.6 is unsupported upstream. Ruby 2.6 is end-of-life upstream; the recorded installed interpreter is not a recommended modern runtime. Data Formats uses installed Tomli 1.0.3 through a private fallback; the Python tomllib branch was not run. No new-installation recommendation, complete language/format conformance, schema certification, hardened untrusted XML parsing or general-purpose execution sandbox is established. Other platforms, browsers and screen readers remain unverified. The 145 official references belong to the saved Atlas review, not fresh website-time verification.