makefile-style: make for named checkpoints, from the madmode-blog page

From pages/2026/makefile-style.md in madmode-blog, plus the CONFIRM=
guard on destructive targets ("no yolo! makefiles plz", the iPad
restore, 2026-11-25) and the Nix result mtime trap (coffeebox,
2026-11-26).

Generated-by: Claude Opus 5.5
This commit is contained in:
dckc committed 2026-11-26 10:48:53 -06:00
1 parent a633aa44da
commit 391d9383d1
2 files changed
+65

No files matched your search

+62
View File
@@ -0,0 +1,62 @@
---
name: makefile-style
description: >-
Makefile style: make for named checkpoints, short targets, scripts for logic, an accurate `make help` from `##` comments, file targets instead of phony ones, and a CONFIRM= guard on destructive targets. Use when writing or reviewing a Makefile or `.mk` file; when about to run a sequence of device or admin commands by hand ("no yolo! makefiles plz"); when a recipe grows loops, conditionals, or traps; when adding a `help` target. Not for build systems other than make.
---
# Makefile style
## make names the checkpoints
Device and admin work (flash a card, restore an iPad, install a service) goes through make targets, not commands typed ad hoc. A target is a named step the operator can read before running, rerun, and cite later: `make flash DEV=/dev/sdd CONFIRM=erase` says what it does and what it touches.
## Short targets; logic goes in a script
A recipe is a few commands. Loops, conditionals, traps, and multi-line `test ... || { ...; }` chains go in a script beside the Makefile, which the target runs. AWK is fine for light text transforms; a non-trivial one gets its own `.awk` file.
Why: make's quoting (`$$`, one shell per line, `\` continuations) makes logic hard to read and easy to break; a script can be run, tested, and read on its own.
## `make help` from `##` comments
Put `## description` on the line before each target the operator runs; a small awk program pairs each `##` comment with the next `target:` line. Keep `help` accurate and current, and make it the first target, so plain `make` shows it.
```makefile
## Show available targets
help:
@awk 'BEGIN {d=""} /^## / {d=substr($$0,4)} \
/^[a-zA-Z0-9_.-]+:/ {if(d) {split($$0,t,":"); \
printf " %-12s %s\n", t[1], d; d=""}}' \
$(MAKEFILE_LIST)
```
Where a repo already has a shared `make-help.awk`, use it.
## Target the real file
Name the file a step produces as its target, so make skips the step when the file is there: no idempotence check, no phony alias. Factor a long path into a variable so its two uses are plainly the same. Prefer architecture-independent paths.
```makefile
AYATANA_PKG = gir1.2-ayatanaappindicator3-0.1
AYATANA_DOC = /usr/share/doc/$(AYATANA_PKG)/copyright
all: $(AYATANA_DOC) $(TARGETS)
systemctl --user daemon-reload
$(AYATANA_DOC):
sudo apt-get install -y $(AYATANA_PKG)
```
Except Nix's `result`: it links into the store, where every mtime is 1970, so as a target it always looks stale, and as a prerequisite it always forces a rebuild. Let a target check `test -e result` instead.
## Paths and installs
- `$(CURDIR)` for the working directory (`make -C dir` sets it); no `$(makeFileDir)` dance. `$(HOME)` for the target's home.
- `ln -sf` for durable installs from the source tree, so updates flow without a copy step.
## Destructive targets need CONFIRM=
A target that erases or overwrites (a disk, a device, a restore) refuses unless the operator says so on the command line, with a word that names the harm: `make restore CONFIRM=erase`. Before writing, it shows what it's about to touch (`lsblk` of the device) and checks it's the kind of thing expected (removable, on USB, a plausible size); that check is a script, per above.
## Sources
- [Makefile conventions](https://github.com/dckc/madmode-blog/blob/master/pages/2026/makefile-style.md), madmode-blog, 2026: checkpoints, short targets, the `help` awk, `$(CURDIR)`, `ln -sf`, file targets.