Skip to content

Rebuild outputs when their commands change - #8

Merged
anemitz merged 2 commits into
masterfrom
rebuild-on-command-change
Nov 18, 2026
Merged

anemitz merged 2 commits into
masterfrom
rebuild-on-command-change

Conversation

@anemitz

@anemitz anemitz commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

This PR rebuilds an object, archive, or link output when its command changes, not only when a prerequisite file is newer. Changed flags, tools, and source lists used to leave stale outputs; for example, a linker flag added in a project's Build.mk never relinked existing shared libraries.

  • Each output records the command of its last successful build. Objects append the record to the dependency file the compiler rewrites on each compile; link and archive outputs keep one record file each under build/.
  • Two shared rules compare every output's command during secondary expansion and add a phony prerequisite on a mismatch, so make -n, -q, and -B stay accurate. Templates now declare only each command and its file prerequisites.
  • Dropping a source from a static library now rebuilds the archive without the stale member; the archive's rm -f now appears in its echoed command.
  • A variable inherited from another target's target-specific assignment is not compared, and it cannot cause a rebuild on every run.
  • Existing build trees have no records, so the first build after upgrading rebuilds everything once. Whirlwind picks this up in a separate vendoring PR after this one merges.

Validated with make -C tests host (GNU Make 3.81) and make -C tests docker (Ubuntu arm64 and amd64, GNU Make 4.3); reverting any of the six new mechanisms fails the suite. In Whirlwind a no-op build goes from 0.29 s to 0.34 s, and a linker flag added in Build.mk relinks all 17 shared libraries without compiling; on a synthetic component of similar size, peak Make memory rises from 22 MB to 49 MB.

anemitz and others added 2 commits November 18, 2026 00:08
LazyMake rebuilt an output only when a prerequisite file was newer, so a
changed compiler or linker flag, a changed tool, or a source dropped from
a target left stale objects, archives, and links. Each output now records
the command of its last successful build, and an output whose command no
longer matches gains a phony prerequisite.

The comparison runs during secondary expansion, so make -n, -q, and -B
keep their meaning. That expansion has no $<, so the compile command names
its source. It also cannot see a variable inherited from another target's
target-specific assignment. The record holds the text the expansion saw,
so such a variable goes uncompared instead of rebuilding on every run.

Existing build trees have no records, so the first build after upgrading
rebuilds every output once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Templates now declare only each output's command and file prerequisites.
Two rules supply the comparison and recipe for every object and every
link or archive output, so a new output kind cannot miss either piece.
Objects append their record to the dependency file the compiler rewrites
on each compile, which drops an included makefile per object and most of
the no-op cost it added. The record write is now the only escaping step,
and binaries and tests share one link command. The shared recipe makes
the archive's rm -f part of its echoed command.

The header-edit check now restores the default command first, so only
the header edit can rebuild the binary it checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anemitz
anemitz marked this pull request as ready for review November 18, 2026 14:56
@anemitz
anemitz merged commit 4785cc9 into master Nov 18, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant