Back to Explore
GuideVibeFix editorialUpdated Nov 24, 2026

Makefile Automation in Practice: One Word to Run Build, Test, and Clean

A README full of commands pasted by hand will go wrong one day. With 5 notes: 15 lines merged into a 17-line page, checks passed 3/3, a second make prints only Nothing to be done, and spaces instead of a Tab fail with missing separator. GNU Make 4.3.

Real photo: a developer's hands typing on a laptop, with two monitors showing code behind it

Halfway through a vibe project, the scene is usually this: a string of commands pasted in the README, builds done by copy and paste, checks run only when someone remembers, cleanup done by deleting files by hand. Once the commands pile up, it falls apart: you build before you check, you check a stale build, you delete the wrong thing while cleaning up. A Makefile is what tidies this up. It does not teach you new commands; it turns the commands you already type into rules — “to get this file, you first need those files, then run this command” — and afterwards you only say one word, like make or make test, and it works out the rest itself. This guide demos with a fictional small project: 5 reading notes, merged into one site/index.html, plus 3 checks. The demo script is rerun_en_demo248_makefile.sh, run on GNU Make 4.3 and Bash 5.2.21 with zero third-party packages; the Chinese version is demo248_makefile_zh.sh, and the numbers and outputs are identical in both.

Here are the demo’s 8 steps and their results up front, and the rest of the guide explains each one. The demo builds a small project in /tmp/make248_demo_en: 5 notes of 3 lines each, plus build.py, check.py, and a Makefile. Step 1, make, the first build, prints two lines: python3 build.py, then Built site/index.html: 5 notes, 15 lines. Step 2, changing nothing and running make again, prints a single line: make: Nothing to be done for 'all'. Step 3 touches only the timestamp of notes/note-03.md and runs make again, and the build script runs once more, still 5 notes and 15 lines. Step 4, make test, prints python3 check.py and Checks passed: 3/3. Step 5, make help, prints Available targets: all build test clean help. Step 6 runs make clean and then make -n build, which prints only python3 build.py without actually running it. Step 7, make build, builds for real. Step 8 uses a Makefile whose recipe line is wrong, and the error is a single line: Makefile.bad:2: *** missing separator. Stop. Finally, site/index.html has 17 lines. Eight steps, none of them guessed — every line is real output from a real run.

One rule, three parts

The basic unit of a Makefile is the rule, and a rule has only three parts — the demo Makefile is the best example. The first part is the target, the thing you want to end up with, such as site/index.html. The second part is the prerequisites, the files the target depends on; the demo writes $(SITE): $(NOTES) build.py, meaning this page depends on the 5 notes and the build script. The third part is the recipe, the commands to run to produce the target; the demo has a single line, python3 build.py. Read together it is one sentence: “to get site/index.html, first have the 5 notes and build.py, then run python3 build.py.” The manual says the same thing in its own words: make automatically determines which pieces of a large program need to be recompiled, and issues the commands to recompile them, and you prepare a file called the makefile that describes the relationships among files in your program and provides commands for updating each file.

The demo Makefile is worth reading line by line, because at 15 lines it already uses the common idioms. The first two lines are variables: NOTES := $(wildcard notes/*.md) means “list every .md file under notes/”, and SITE := site/index.html gives the target a short name so the rest of the file can write $(SITE). Next comes .PHONY: all build test clean help, which gets its own section below. Then four targets: all depends on build and does nothing, it only gives make a default entry point; build depends on $(SITE), the real file target; test depends on build and runs python3 check.py; clean has no prerequisites and runs rm -rf site; help runs one @echo line that prints the available targets. Fifteen lines, not one of them spare, and your own project can start by copying this shape.

The colon and the dependencies are the key to understanding Makefiles. The target goes on the left of the colon, the prerequisites on the right, and the recipe on the next line. A target can depend on many files: if any one of them changes, the target has to be rebuilt. Dependencies can also form a chain: test depends on build, build depends on site/index.html, and site/index.html depends on the notes and the script, so make test walks the whole chain, rebuilding whichever link is out of date and skipping whichever is not. This “chained dependency” style is what makes a Makefile better than a string of shell commands: shell commands run top to bottom every time, while a Makefile first works out which steps are out of date, then runs only those.

Timestamps decide everything

make has exactly one way to judge “out of date”: comparing timestamps. If the target file is newer than all of its prerequisites, it counts as fresh and nothing runs; if any prerequisite is newer than the target, the target is out of date and the recipe runs again. Step 2 of the demo is direct evidence of this rule: step 1 had just built site/index.html, newer than the 5 notes and build.py, so running make again printed only make: Nothing to be done for 'all'., and the build script never ran. Step 3 only touched the timestamp of notes/note-03.md — note that this was a touch, not one word of content changed — and running make again immediately ran the build script once more, still printing Built site/index.html: 5 notes, 15 lines. The content did not change, but the timestamp did, and make treated the target as out of date. That is how it judges, and it is both the strength and the limitation, which the closing section returns to.

The benefit of the timestamp rule only shows once a project grows. Picture 50 notes and 3 build steps: change one note, and make rebuilds only the step that depends on it, skipping everything tied to the other 49; change nothing, and make runs nothing at all, just telling you there is nothing to do. The demo has only 5 notes and 1 build step, so the time saved is invisible, but the rule is the same. Turn it around: if your build step re-runs everything from scratch every time, changing one word means waiting for the whole thing, and the bigger the project gets, the longer you wait. That is why builds in many vibe projects keep getting slower: not because the machine got slower, but because nobody ever told the build tool “you do not need to redo this”.

One common misunderstanding is worth clearing up: make does not run commands in order, it decides what to run from the dependencies. In the demo, make test printed only python3 check.py and Checks passed: 3/3, without rebuilding, because test depends on build and the build target was already up to date, so the build step was skipped. If your Makefile rebuilds everything every time you run make test, the dependencies are probably written wrong: check whether test depends on the build script itself instead of the thing the build produces. When the product is fresh, the check just runs; when the product is out of date, the product is rebuilt first and the check runs after — the dependency graph guarantees that order, and you do not have to think about it.

Flow diagram: notes/*.md, 5 notes, merged by build.py into 15 lines to produce site/index.html, 17 lines total, then check.py passes 3/3; touching note-03.md rebuilds only index.html

The diagram above is the demo project’s dependency graph, with numbers matching the transcript exactly: 5 notes on the left, merged by build.py into 15 lines, producing site/index.html at 17 lines, then checked by check.py with 3/3 passed. The arrows are dependencies, not execution order: the right side rebuilds only when the left side changes. The orange note under the diagram is the point of this section: touch note-03.md, and only index.html is rebuilt, nothing else runs. Once you can read this picture, you understand Makefiles: they are not command lists, they are dependency graphs, and the commands are just recipes hung on the relationships.

Variables and wildcards: adding a file needs no Makefile edit

The first line of the demo Makefile, NOTES := $(wildcard notes/*.md), deserves its own section, because it solves a very practical problem: when the notes grow from 5 to 6, does the Makefile have to change? No. The wildcard $(wildcard notes/*.md) means “go and find every .md file under notes/ each time it runs”, so however many notes there are, that is how many the target depends on, and a new note is picked up automatically on the next make. The build script is written the same way — sorted(glob.glob("notes/*.md")) reads whatever notes exist at the time — so neither side needs editing, and adding a file just works. That is what “automation” should look like: write the rule once, and the data can grow freely.

The way variables are written matters too. := means “expand immediately”: the right-hand side is computed the moment the line is written, and later changes do not affect it; = means “expand recursively”: it is computed when the variable is used, so definitions written later can still affect it. The demo uses :=, because the note list should be settled at run time, not drift afterwards. The manual draws this distinction in detail, but for a beginner one rule is enough: when you want the value fixed at the point of definition, use :=; when you want it to follow later definitions, use =. Most of the time := matches intuition better and is less surprising, so the demo — and your first Makefile — should use it.

Variables have another benefit: “change it in one place, and it changes everywhere”. In the demo, the target file is called site/index.html, and that name appears exactly once in the Makefile, on the line SITE := site/index.html; everything else writes $(SITE). If you ever want the output to be dist/index.html, you change that one line, and there is nothing else to hunt for. A common flaw in AI-generated Makefiles is the same path hard-coded in five or six places, so one gets changed and another missed, and the build and the check end up using different files. Variables are not about saving keystrokes; they are about making sure “the same thing has exactly one source”, a rule that holds at any size of Makefile.

.PHONY: declare the targets that are not files

One line in the demo Makefile is the easiest to delete, and the trouble only shows up much later: .PHONY: all build test clean help. It means: these targets are not files, just names for jobs, so run them every time and do not check the disk for a file of the same name. Why bother? Because make treats a target as a filename by default: if a file called clean happens to exist on disk, and it is newer than clean’s prerequisites, make concludes “clean is already up to date” and does nothing. This is not hypothetical — it was verified live in the demo directory: with the .PHONY line removed, and an empty file called clean created in the directory, running make clean printed only make: 'clean' is up to date., the cleanup never ran, and the site directory sat there untouched.

What makes this trap nasty is that it fails silently: the build does not error, it just does no work, and you walk away believing the cleanup happened. And the trigger is everyday stuff: some tool writes a file called test, or you casually create a directory called build, and suddenly the matching target “stops working”, for a reason that has nothing to do with the Makefile itself, which makes it miserable to track down. So the rule is simple: any target that is not a real file goes into .PHONY. In the demo, all, build, test, clean, and help are all not files, so all five are on that line. The other way around, site/index.html is a real file, so it must not go into .PHONY, or make would rebuild it every time and the timestamp rule would stop working.

To decide whether a target belongs in .PHONY, ask one question: “after this runs, will a file of this name exist on disk?” If yes, leave it out; if no, put it in. Cleanup, testing, help, packaging, deploying — these are actions, not files, and they all need declaring. Generating a page, compiling a program, packing an archive — these are files, and none of them need it. Remember this rule and your Makefile will never produce the strange sight of “the recipe is written right there, but make says there is nothing to do”.

Look before you run: -n and clean

Step 6 of the demo is an option many people have never heard of, and never give up once they use it: make -n build. First make clean deletes the site directory, then make -n build prints a single line, python3 build.py, without actually running it, and the site directory does not come back. -n means “print the commands you would run, but do not run them” — a dry run, shown to you first. Its use is this: taking over someone else’s Makefile, or after changing the dependencies yourself, run -n first to see what make is planning, confirm there are no extra steps and nothing will be deleted by mistake, and only then run it for real. It is a particularly good habit when a recipe contains something irreversible like rm -rf: look first, then act.

clean itself is worth a sentence too: the demo’s clean has a single recipe line, rm -rf site, which deletes the build product, not the sources. The 5 notes, build.py, check.py, and the Makefile all stay; only the site directory goes. Step 6 is followed by a real make build, which prints Built site/index.html: 5 notes, 15 lines again, proving the cleanup damaged no source and the build can be redone at any time. The rule for designing clean is this: only delete things that can be regenerated, and before deleting anything, ask “if I delete this, can I rebuild it?” Things that cannot be rebuilt — notes, source code — must never appear in a clean recipe. The demo’s check.py also has 3 checks as a safety net: the file exists, all 5 note titles are present, and the total is 17 lines; only 3/3 counts as a pass, and after cleaning and rebuilding, the check is still 3/3.

Step 5’s make help is another good habit that is easy to overlook: a target list that speaks for itself. The demo’s help recipe is one line, @echo "Available targets: all build test clean help", and the leading @ means “do not print this command itself, only its output”, so all you see is Available targets: all build test clean help, with no extra echo ... line. Your project can do the same: as the targets grow, make help becomes a living manual, and a newcomer knows what can be done without reading the README. Many projects paste a list of commands into the README, and the moment a command changes, the README is out of date; a help target lives inside the Makefile, so it gets updated in the same breath as the rules, and it never goes stale.

Tabs and missing separator: the most common error

Step 8 of the demo is the error every first-time Makefile writer runs into, kept for last on purpose because it makes you doubt your sanity. The demo has a Makefile.bad with the same content as the real Makefile, except the recipe line starts with 4 spaces instead of a Tab. Running make -f Makefile.bad prints a single line: Makefile.bad:2: *** missing separator. Stop. Line 2 is the recipe line, and “missing separator” means “the separator is missing here” — and the separator it means is the Tab. The GNU Make rule has been the same for decades: a recipe line must start with a Tab; spaces do not count, no matter how similar they look.

This error is hard to track down because it is invisible in the editor: spaces and Tabs both show up as blank space, and staring at the screen will not tell them apart. The way to check is to make the whitespace visible: cat -A Makefile shows a Tab as ^I and a line end as $, so if there is no ^I before a recipe line, it was written with spaces. The way to prevent it is to change the editor settings: make the editor show Tabs inside Makefiles, or set it to never convert Tabs to spaces there — many editors have settings aimed at Makefiles precisely to stop this from happening. Copy and paste is another repeat offender: a Makefile copied from a web page or a chat message often has its Tabs pasted as spaces, so run cat -A first after pasting, and only then run make.

Incidentally, this error also explains why the demo scripts write the Makefile with printf '%b': inside a script there is no reliable way to guarantee a literal Tab survives, so writing it as the \t escape is the safe way. When you write a Makefile by hand you do not need this — just press the Tab key in the editor — the point is that what you press must be a Tab, not spaces. Remember this, and the next time you see missing separator, your first thought will not be “my Makefile is wrong” but “the Tab before the recipe line got replaced by spaces”, and it will take five minutes to fix.

Checklist and boundaries

Collect the method into a checklist: for each file you want, write a rule — target, prerequisites, recipe, three parts, with the recipe line starting with a Tab; write the dependencies as a chain, and make checks depend on the built product rather than the build script; put every target that is not a file into .PHONY, and keep real file targets out of it; use variables plus wildcards for file lists, so adding a file needs no Makefile edit, and let each path appear exactly once; after changing dependencies, dry-run with make -n first and only run it for real once you are sure; let clean delete only things that can be rebuilt; and add a help target so the Makefile speaks for itself. With this checklist, 15 lines are enough for your first Makefile — the demo’s file is the template.

Finally, what this guide does not cover, so you do not take Makefiles into fights they cannot win: it does not cover shell scripting itself — the site’s Bash basics guide covers writing scripts, and Bash runs top to bottom in order while Makefiles decide by timestamp which steps to run. The two work together rather than replacing each other: the demo’s build.py and check.py are ordinary scripts, and the Makefile only decides when to call them. It does not cover continuous integration — the site’s GitHub Actions CI guide covers pipelines, and a CI job can absolutely call make test, but CI means “run on every commit” while a Makefile means “decide locally what to run”, two different jobs. It does not cover containers — the site’s Docker production guide covers building images, and make can call docker build, but that is hanging a Docker command on a rule, not replacing it. And it does not cover scheduled jobs — the site’s cron operations guide covers jobs that die at 3 a.m. and how to notice, and a scheduled job can call make, but scheduling itself is not a Makefile’s business. One more boundary note: make reads timestamps, not contents — if a file’s content is unchanged but its timestamp is newer, it rebuilds; if the content changed but the timestamp stayed old (restoring an old file while keeping its old time, say), it calls it fresh. Keep that limitation in mind; at the critical moment, a manual make clean and rebuild is faster than wondering what went wrong.

Views 0Comments 0

Comments (0)

ME
0/1000
Loading comments...

Sources

Browse projectsPublish your project

Related articles