From 240b22b6420091eab0b7a83401aed10dba9e1b3a Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Sun, 18 Oct 2026 08:56:11 -0500 Subject: [PATCH 1/5] make: track ip65 blob's libs and cfg content (#309) The ip65 blob rule listed only ip65_stub.s and ip65.cfg, though ld65 also links ip65_tcp.lib and ip65_c64.lib. Both libs are now prerequisites (a missing one fails in make, naming it and 'make ip65-libs'), and stub, cfg, both libs and the toolchain id are content-stamped in ip65-build/ip65-c64.stamp at parse time (#305 shape; -n/-q/-t compare only; clean/ip65-libs skip). A stale stamp deletes the blob, its object and (for real goals) the link outputs. make ip65-blob does not force: docs corrected. Tests in test_build_flags_stamp.py, each mutation-checked. Co-Authored-By: Claude Sonnet 5.5 --- .gitignore | 1 + CLAUDE.md | 9 ++- Makefile | 50 +++++++++++- README.md | 3 +- docs/engineering-notes.md | 14 ++-- tools/test_build_flags_stamp.py | 131 ++++++++++++++++++++++++++++++++ 6 files changed, 198 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index a4b6730a..f30dcec6 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ dist/ ip65-build/*.o ip65-build/*.bin ip65-build/*.map +ip65-build/*.stamp .claude/* !.claude/settings.json tools/https_e2e/certs/* diff --git a/CLAUDE.md b/CLAUDE.md index ca5bd3f0..4d605554 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -27,8 +27,11 @@ Fresh clone (ip65 backend; UCI needs none of the ip65 steps): - `ip65-build/ip65-c64.bin` is a **gitignored artifact** (6,951 B, sha256 `cf1a5ff7…`, deterministic). Plain `make` builds it, but it - cannot build the ip65 `.lib` archives — skip `make ip65-libs` and the - link dies with `ld65: Input file '../ip65/ip65/ip65_tcp.lib' not found`. + cannot build the ip65 `.lib` archives — skip `make ip65-libs` and make + dies `missing ip65/ip65/ip65_tcp.lib -- run 'make ip65-libs'` (#309). The + blob tracks stub, `ip65.cfg` and both libs by content + (`ip65-build/ip65-c64.stamp`, #305 shape; `-n` writes nothing); `c64.lib` + is covered only via the toolchain id, not hashed. - **A nested git worktree needs `git submodule update --init --recursive` before it can link ip65** — a fresh worktree has no submodule working trees, so the link dies by name. It fails LOUDLY; that is the whole @@ -112,7 +115,7 @@ like "overflows". Targets: `make` (PRG + `build/labels.txt` + `build/c64-https.dbg`), `make clean` (removes `build/` only; the blob survives), `make run`, -`make ip65-libs`, `make ip65-blob` (force rebuild), `make package`, +`make ip65-libs`, `make ip65-blob` (build if stale; does not force), `make package`, `make package-verify`. Variables: diff --git a/Makefile b/Makefile index 80b3a01d..99736af4 100644 --- a/Makefile +++ b/Makefile @@ -1057,7 +1057,55 @@ ip65-libs: # and then reuses it — `clean` only removes build/, so it survives. ip65-blob: $(IP65_BIN) -$(IP65_BIN): $(IP65_BUILD)/ip65_stub.s $(IP65_BUILD)/ip65.cfg +# #309: the blob's inputs are its stub, its cfg and BOTH ip65 archives. They +# used to be only the first two, so a rebuilt lib or a same-second cfg edit +# left the blob (and, through .incbin, the PRG) stale at "Nothing to be done". +# - Missing archive: the empty rule below fails the build naming it, with +# the fix, instead of ld65's "Input file ... not found" from inside a cd. +# - Archive newer than the blob: an ordinary prerequisite relinks it. +# - Content (same-second edits, a pin move that keeps mtimes): the digest +# below, in the #305 shape -- compared at PARSE time, and a change deletes +# the blob, its object and the link outputs. Absence, not an mtime. +# c64.lib lives in the cc65 install and is deliberately NOT hashed: the digest +# carries TOOLCHAIN_ID (ca65/ld65 resolved binary + sha256), which moves +# whenever the install does; locating the file would need ld65's search path. +# BACKEND=ip65 only: a UCI build never reads or requires the ip65 tree. +# `clean` and `ip65-libs` skip it (#220: they must not mutate anything), the +# dry-run flags compare and warn only (#174). A maintenance-only goal set +# (`make ip65-blob`, possibly over a UCI tree) deletes just the blob and its +# object, never the link outputs (#220). ip65-c64.stamp is gitignored. +IP65_LIBS := $(IP65_DIR)/ip65/ip65_tcp.lib $(IP65_DIR)/drivers/ip65_c64.lib +IP65_BLOB_STAMP := $(IP65_BUILD)/ip65-c64.stamp +IP65_BLOB_OBJ := build/net/ip65/ip65_blob.o +IP65_BLOB_STAMP_BODY := { printf 'TOOLCHAIN=%s\n' '$(TOOLCHAIN_ID)'; \ + for f in $(IP65_BUILD)/ip65_stub.s $(IP65_BUILD)/ip65.cfg $(IP65_LIBS); do \ + (sha256sum $$f 2>/dev/null || shasum -a 256 $$f 2>/dev/null || echo "$$f MISSING"); \ + done; } +IP65_STAMP_SKIP := $(if $(MAKECMDGOALS),$(if $(filter-out clean ip65-libs,$(MAKECMDGOALS)),,skip)) + +ifeq ($(BACKEND),ip65) +ifeq ($(IP65_STAMP_SKIP),) +ifeq ($(MAKE_DRY_RUN),) +_ := $(shell $(IP65_BLOB_STAMP_BODY) > $(IP65_BLOB_STAMP).tmp; \ + if cmp -s $(IP65_BLOB_STAMP).tmp $(IP65_BLOB_STAMP); then \ + rm -f $(IP65_BLOB_STAMP).tmp; \ + else \ + mv $(IP65_BLOB_STAMP).tmp $(IP65_BLOB_STAMP); \ + rm -f $(IP65_BIN) $(IP65_BLOB_OBJ) $(if $(STAMP_SKIP),,$(LINK_OUTPUTS)); \ + fi) +else +ifneq ($(shell $(IP65_BLOB_STAMP_BODY) 2>/dev/null | cmp -s - $(IP65_BLOB_STAMP) >/dev/null 2>&1 || echo differs),) +$(warning DRY RUN (-$(MAKE_OPT_LETTERS)): ip65 blob inputs differ from $(IP65_BLOB_STAMP).) +$(warning A real build would delete $(IP65_BIN) and $(LINK_OUTPUTS). NOTHING was deleted.) +endif +endif +endif +endif + +$(IP65_LIBS): + @echo "make: missing $@ -- run 'make ip65-libs' first (once per clone)." >&2; false + +$(IP65_BIN): $(IP65_BUILD)/ip65_stub.s $(IP65_BUILD)/ip65.cfg $(IP65_LIBS) cd $(IP65_BUILD) && $(CA65) -I ../$(IP65_DIR) ip65_stub.s -o ip65_stub.o cd $(IP65_BUILD) && $(LD65) -C ip65.cfg -o ip65-c64.bin -m ip65-c64.map \ ip65_stub.o ../$(IP65_DIR)/ip65/ip65_tcp.lib \ diff --git a/README.md b/README.md index 7406bd7b..84f7bd04 100644 --- a/README.md +++ b/README.md @@ -542,7 +542,8 @@ with no intermediate step. What `make` cannot do for you is build the ip65 `.lib` archives the blob links against, so run `make ip65-libs` once per clone, as in "Before you -build or test" above; `make ip65-blob` exists only to force a rebuild. The +build or test" above; `make ip65-blob` builds it if stale (it does not force; flip a byte +of the blob to test provenance). The build is deterministic: 6,951 B, sha256 `cf1a5ff7809af4e4655e385b378b936054f41046ff2b7604828af3240c2d90dd`. `make clean` does not remove it, which is why the step is normally invisible. The UCI backend does not use the blob at all. diff --git a/docs/engineering-notes.md b/docs/engineering-notes.md index 3e8952ce..e0d8b487 100644 --- a/docs/engineering-notes.md +++ b/docs/engineering-notes.md @@ -71,13 +71,17 @@ error above. The fix changes no output: the ip65 PRG hashes What plain `make` still cannot do for you is build the ip65 `.lib` archives — the submodule ships sources, not binaries. Skip -`make ip65-libs` and the blob rule runs and dies at its link: +`make ip65-libs` and (since #309) make dies before the link, naming the lib: - ld65: Error: Input file '../ip65/ip65/ip65_tcp.lib' not found - make: *** [ip65-build/ip65-c64.bin] Error 1 + make: missing ip65/ip65/ip65_tcp.lib -- run 'make ip65-libs' first (once per clone). + make: *** [ip65/ip65/ip65_tcp.lib] Error 1 + +(Before #309 the libs were not prerequisites, and the blob rule died inside +its own ld65 line: `ld65: Error: Input file '../ip65/ip65/ip65_tcp.lib' not found`.) So: `make ip65-libs` once per clone, then plain `make`. `make ip65-blob` -exists only to force a rebuild. +builds the blob if stale (it does not force a rebuild; #309 made a rebuilt +lib or a content edit of stub/cfg count as stale). **Worktree trap — RETIRED by #116; the rule that survives it is different.** Recorded in full because this section asserted the old rule @@ -143,7 +147,7 @@ Targets: - `make ip65-blob` — rebuild `ip65-build/ip65-c64.bin` from those libraries. A plain `make` already builds the blob on demand and then reuses it, so this - target is only for forcing a rebuild. + target only builds when stale (it does not force). **`make clean` after a flag change — RETIRED by #159. The measurements below are why it existed, and are kept for that reason.** diff --git a/tools/test_build_flags_stamp.py b/tools/test_build_flags_stamp.py index 3f1e2c89..bbd3364a 100644 --- a/tools/test_build_flags_stamp.py +++ b/tools/test_build_flags_stamp.py @@ -863,6 +863,137 @@ def test_ip65_cfg_edit_changes_the_stamp_and_clears_outputs(): assert not farm.exists(o), f"{o} survived an ip65 cfg edit" +BLOB = "ip65-build/ip65-c64.bin" +BLOB_STAMP = "ip65-build/ip65-c64.stamp" +IP65_LIBS = ("ip65/ip65/ip65_tcp.lib", "ip65/drivers/ip65_c64.lib") +IP65 = ("BACKEND=ip65", "USE_NISTCURVES_ONCHIP=1") + + +class Ip65Farm(Farm): + """A Farm with PRIVATE copies of ip65/ and ip65-build/, so a test may + touch a lib or edit the blob's cfg without disturbing the checkout. + Libs come from the checkout when built there, else from `make ip65-libs` + run inside the copy.""" + + def __init__(self): + super().__init__() + for name in ("ip65", "ip65-build"): + os.unlink(self.dir / name) + shutil.copytree(REPO / name, self.dir / name, symlinks=True) + if not all(self.exists(r) for r in IP65_LIBS): + self.make("ip65-libs", check=False) + assert all(self.exists(r) for r in IP65_LIBS), "ip65 libs would not build" + + def blob_build(self, **kw): + return self.make("BACKEND=ip65", "ip65-blob", **kw) + + def blob_mtime(self): + return self.path(BLOB).stat().st_mtime_ns + + def edit_same_second(self, rel, text): + """Append to an input, then pin its mtime to the blob's: only a + CONTENT check can see the edit.""" + blob_ns = self.blob_mtime() + with open(self.path(rel), "a") as fh: + fh.write(text) + os.utime(self.path(rel), ns=(blob_ns, blob_ns)) + + +def test_ip65_blob_tracks_its_libs_cfg_and_stub(): + """#309: the blob's ld65 line links two archives and a cfg; none of them + was a prerequisite (or, for the cfg, tracked by anything but mtime).""" + _require_toolchain() + with Ip65Farm() as farm: + farm.blob_build() + good = farm.sha(BLOB) + assert farm.exists(BLOB_STAMP) + + # unchanged: nothing runs + old = farm.blob_mtime() + proc = farm.blob_build() + assert "ld65 " not in proc.stdout and farm.blob_mtime() == old, ( + "an unchanged blob relinked:\n" + proc.stdout) + + # each lib, touched (content unchanged): the mtime prerequisite + for lib in IP65_LIBS: + orig = farm.path(lib).stat().st_mtime_ns + newer = farm.blob_mtime() + 2_000_000_000 # unambiguously newer + os.utime(farm.path(lib), ns=(newer, newer)) + proc = farm.blob_build() + assert "ld65 " in proc.stdout, f"touching {lib} did not relink the blob" + assert farm.sha(BLOB) == good + # put it back in the past, or every later step relinks on the + # lib's future mtime and the cases below prove nothing + os.utime(farm.path(lib), ns=(orig, orig)) + + # same-second content edits of the cfg and of the stub + for rel, text in (("ip65-build/ip65.cfg", "\n# same-second edit\n"), + ("ip65-build/ip65_stub.s", "\n; same-second edit\n")): + farm.edit_same_second(rel, text) + proc = farm.blob_build() + assert "ld65 " in proc.stdout, ( + f"a same-second edit of {rel} did not relink the blob:\n" + + proc.stdout) + assert farm.sha(BLOB) == good, "a comment edit moved the blob" + + # a stale blob must take the PRG with it, with no clean + farm.make(*IP65) + prg = farm.sha() + out = {o: farm.path(o).stat().st_mtime_ns for o in LINK_OUTPUTS} + farm.edit_same_second("ip65-build/ip65.cfg", "\n# again\n") + proc = farm.make(*IP65) + assert "ld65 " in proc.stdout and farm.sha() == prg + assert all(farm.path(o).stat().st_mtime_ns != out[o] for o in LINK_OUTPUTS), ( + "a rebuilt blob left a stale link output") + + +def test_ip65_blob_dry_run_writes_nothing(): + """#174: -n/-q/-t on a stale blob compare and report, and mutate nothing.""" + _require_toolchain() + with Ip65Farm() as farm: + farm.blob_build() + farm.edit_same_second("ip65-build/ip65.cfg", "\n# stale\n") + + def snap(): + return {str(p.relative_to(farm.dir)): (p.stat().st_mtime_ns, + p.read_bytes()) + for p in farm.path("ip65-build").rglob("*") if p.is_file()} + before = snap() + for opt in ("-n", "-q", "-t"): + proc = farm.make("BACKEND=ip65", "ip65-blob", opts=(opt,), check=False) + assert snap() == before, f"`make {opt} ip65-blob` mutated ip65-build/" + proc = farm.make("BACKEND=ip65", "ip65-blob", opts=("-n",)) + assert "DRY RUN" in proc.stdout and BLOB_STAMP in proc.stdout, ( + "a dry run on a stale blob did not report it:\n" + proc.stdout) + + +def test_ip65_blob_missing_lib_fails_by_name(): + """A fresh clone without `make ip65-libs` fails loudly, naming the file + and the fix, before any link is attempted.""" + _require_toolchain() + with Ip65Farm() as farm: + for lib in IP65_LIBS: + farm.path(lib).rename(farm.path(lib + ".held")) + proc = farm.blob_build(check=False) + assert proc.returncode != 0, f"missing {lib} did not fail the build" + assert lib in proc.stdout and "make ip65-libs" in proc.stdout, ( + f"missing {lib}: failure does not name it and the fix:\n" + + proc.stdout) + assert "ld65" not in proc.stdout, "got as far as the link" + farm.path(lib + ".held").rename(farm.path(lib)) + + +def test_uci_build_does_not_need_the_ip65_tree(): + """BACKEND=uci neither reads nor requires ip65/ or ip65-build/.""" + _require_toolchain() + with Farm() as farm: + for name in ("ip65", "ip65-build"): + os.unlink(farm.dir / name) + farm.make(*UCI) + assert farm.exists(PRG) + assert not farm.exists("ip65-build") + + def test_unchanged_flags_rebuild_nothing(): """The inverse property: the stamp must not make every build a rebuild.""" _require_toolchain() From 2eb7a0b2042619975c36d5ac3f61569243b5b9d2 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Sun, 18 Oct 2026 09:47:14 -0500 Subject: [PATCH 2/5] make: ip65 blob review round 1 (#309): -t safety, blob byte stamp, tests - named-failure lib rule runs under -t (+ prefix): no empty lib files - blob's own sha256 stamped; a same-second byte flip deletes ip65_blob.o and link outputs - dry-run warning mirrors the real branch (names the object, no PRG for maintenance goals); LD65_LIB/LD65_OBJ/CC65_HOME in the digest - Farm tests get a private ip65-build (a symlink let them delete the real blob) - build_prgs.sh bootstraps ip65-libs when the libs are missing - tests for every previously unkilled mutant Co-Authored-By: Claude Sonnet 5.5 --- CLAUDE.md | 4 +- Makefile | 31 ++++- tools/package/build_prgs.sh | 10 +- tools/test_build_flags_stamp.py | 194 +++++++++++++++++++++++++++++++- 4 files changed, 225 insertions(+), 14 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 4d605554..55987cf9 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -31,7 +31,9 @@ Fresh clone (ip65 backend; UCI needs none of the ip65 steps): dies `missing ip65/ip65/ip65_tcp.lib -- run 'make ip65-libs'` (#309). The blob tracks stub, `ip65.cfg` and both libs by content (`ip65-build/ip65-c64.stamp`, #305 shape; `-n` writes nothing); `c64.lib` - is covered only via the toolchain id, not hashed. + is covered only via the toolchain id and LD65_LIB/OBJ/CC65_HOME, not + hashed. The blob's own bytes are stamped too (`ip65-c64-bin.stamp`), so a + same-second byte flip reaches the PRG. - **A nested git worktree needs `git submodule update --init --recursive` before it can link ip65** — a fresh worktree has no submodule working trees, so the link dies by name. It fails LOUDLY; that is the whole diff --git a/Makefile b/Makefile index 99736af4..0f0339d0 100644 --- a/Makefile +++ b/Makefile @@ -1077,10 +1077,17 @@ ip65-blob: $(IP65_BIN) IP65_LIBS := $(IP65_DIR)/ip65/ip65_tcp.lib $(IP65_DIR)/drivers/ip65_c64.lib IP65_BLOB_STAMP := $(IP65_BUILD)/ip65-c64.stamp IP65_BLOB_OBJ := build/net/ip65/ip65_blob.o +# ld65's own search-path environment (LD65_LIB, LD65_OBJ, CC65_HOME) can +# swap c64.lib without moving TOOLCHAIN_ID, so it rides in the digest too. IP65_BLOB_STAMP_BODY := { printf 'TOOLCHAIN=%s\n' '$(TOOLCHAIN_ID)'; \ + printf 'LD65_ENV=%s|%s|%s\n' "$$LD65_LIB" "$$LD65_OBJ" "$$CC65_HOME"; \ for f in $(IP65_BUILD)/ip65_stub.s $(IP65_BUILD)/ip65.cfg $(IP65_LIBS); do \ (sha256sum $$f 2>/dev/null || shasum -a 256 $$f 2>/dev/null || echo "$$f MISSING"); \ done; } +IP65_BIN_STAMP := $(IP65_BUILD)/ip65-c64-bin.stamp +IP65_BIN_SHA := (sha256sum $(IP65_BIN) 2>/dev/null || shasum -a 256 $(IP65_BIN)) +# Link outputs go only for a real goal; a maintenance-only goal (#220) keeps them. +IP65_RM_LINK = $(if $(STAMP_SKIP),,$(LINK_OUTPUTS)) IP65_STAMP_SKIP := $(if $(MAKECMDGOALS),$(if $(filter-out clean ip65-libs,$(MAKECMDGOALS)),,skip)) ifeq ($(BACKEND),ip65) @@ -1091,19 +1098,37 @@ _ := $(shell $(IP65_BLOB_STAMP_BODY) > $(IP65_BLOB_STAMP).tmp; \ rm -f $(IP65_BLOB_STAMP).tmp; \ else \ mv $(IP65_BLOB_STAMP).tmp $(IP65_BLOB_STAMP); \ - rm -f $(IP65_BIN) $(IP65_BLOB_OBJ) $(if $(STAMP_SKIP),,$(LINK_OUTPUTS)); \ + rm -f $(IP65_BIN) $(IP65_BIN_STAMP) $(IP65_BLOB_OBJ) $(IP65_RM_LINK); \ + fi) +# The blob's own bytes, by content, for a blob changed OUTSIDE this rule (a +# byte flip, the documented provenance test): blob -> ip65_blob.o is an mtime +# edge, and a same-second flip left the object, and the PRG, stale. A change +# deletes the object and link outputs. Absent blob or stamp = record only, so +# the rebuild after the block above does not trigger a second invalidation. +_ := $(shell if [ -f $(IP65_BIN) ]; then \ + $(IP65_BIN_SHA) > $(IP65_BIN_STAMP).tmp; \ + if [ -f $(IP65_BIN_STAMP) ] && ! cmp -s $(IP65_BIN_STAMP).tmp $(IP65_BIN_STAMP); then \ + rm -f $(IP65_BLOB_OBJ) $(IP65_RM_LINK); \ + fi; \ + mv $(IP65_BIN_STAMP).tmp $(IP65_BIN_STAMP); \ fi) else ifneq ($(shell $(IP65_BLOB_STAMP_BODY) 2>/dev/null | cmp -s - $(IP65_BLOB_STAMP) >/dev/null 2>&1 || echo differs),) $(warning DRY RUN (-$(MAKE_OPT_LETTERS)): ip65 blob inputs differ from $(IP65_BLOB_STAMP).) -$(warning A real build would delete $(IP65_BIN) and $(LINK_OUTPUTS). NOTHING was deleted.) +$(warning A real build would delete $(IP65_BIN) $(IP65_BLOB_OBJ) $(IP65_RM_LINK). NOTHING was deleted.) +endif +ifneq ($(shell if [ -f $(IP65_BIN) ] && [ -f $(IP65_BIN_STAMP) ]; then $(IP65_BIN_SHA) 2>/dev/null | cmp -s - $(IP65_BIN_STAMP) >/dev/null 2>&1 || echo differs; fi),) +$(warning DRY RUN (-$(MAKE_OPT_LETTERS)): $(IP65_BIN) differs from $(IP65_BIN_STAMP).) +$(warning A real build would delete $(IP65_BLOB_OBJ) $(IP65_RM_LINK). NOTHING was deleted.) endif endif endif endif +# `+`: runs under -t/-n/-q too, so `make -t` fails by name instead of +# touching an empty file into existence where a lib should be. $(IP65_LIBS): - @echo "make: missing $@ -- run 'make ip65-libs' first (once per clone)." >&2; false + +@echo "make: missing $@ -- run 'make ip65-libs' first (once per clone)." >&2; false $(IP65_BIN): $(IP65_BUILD)/ip65_stub.s $(IP65_BUILD)/ip65.cfg $(IP65_LIBS) cd $(IP65_BUILD) && $(CA65) -I ../$(IP65_DIR) ip65_stub.s -o ip65_stub.o diff --git a/tools/package/build_prgs.sh b/tools/package/build_prgs.sh index 38dd6244..45f8e2e7 100755 --- a/tools/package/build_prgs.sh +++ b/tools/package/build_prgs.sh @@ -58,9 +58,9 @@ # into every .o header — but ld65 does not propagate it, so the PRG is # deterministic and comparable. # -# Also bootstraps the ip65 blob (`ip65-build/ip65-c64.bin`), which is a -# gitignored artifact a plain `make` will NOT build: a fresh clone dies in the -# ca65 `.incbin` before any link. +# Also bootstraps the ip65 libs and blob (`ip65-build/ip65-c64.bin`), a +# gitignored artifact whose libs a plain `make` will NOT build: a fresh clone +# dies in make, naming the missing ip65 .lib (#309). # # Writes dist/build-info.txt (git HEAD, submodule pins, per-variant args/size/ # sha256). write_manifest.sh consumes it. @@ -95,8 +95,8 @@ git diff --quiet HEAD -- 2>/dev/null || GIT_DIRTY=" (working tree DIRTY)" # --- ip65 blob bootstrap ------------------------------------------------------ IP65_BIN="$PROJECT_ROOT/ip65-build/ip65-c64.bin" -if [ ! -f "$IP65_BIN" ]; then - echo "[package] ip65 blob absent — bootstrapping (make ip65-libs && make ip65-blob)" +if [ ! -f "$IP65_BIN" ] || [ ! -f "$PROJECT_ROOT/ip65/ip65/ip65_tcp.lib" ] || [ ! -f "$PROJECT_ROOT/ip65/drivers/ip65_c64.lib" ]; then + echo "[package] ip65 blob or libs absent — bootstrapping (make ip65-libs && make ip65-blob)" if [ ! -e "$PROJECT_ROOT/ip65/Makefile" ]; then echo "ERROR: ip65 submodule not checked out. Run:" >&2 echo " git submodule update --init --recursive" >&2 diff --git a/tools/test_build_flags_stamp.py b/tools/test_build_flags_stamp.py index bbd3364a..3a8d7841 100644 --- a/tools/test_build_flags_stamp.py +++ b/tools/test_build_flags_stamp.py @@ -236,7 +236,14 @@ def __init__(self): self.dir = Path(tempfile.mkdtemp(prefix="c64-flags-stamp-")) for name in FARM_LINKS: target = REPO / name - if target.exists(): + if not target.exists(): + continue + if name == "ip65-build": + # A PRIVATE copy: the ip65 rules write and delete the blob + # and its stamps here, and a symlink made a Farm test running + # `make BACKEND=ip65 ...` delete the developer's real blob. + shutil.copytree(target, self.dir / name, symlinks=True) + else: os.symlink(target, self.dir / name) def __enter__(self): @@ -877,9 +884,8 @@ class Ip65Farm(Farm): def __init__(self): super().__init__() - for name in ("ip65", "ip65-build"): - os.unlink(self.dir / name) - shutil.copytree(REPO / name, self.dir / name, symlinks=True) + os.unlink(self.dir / "ip65") + shutil.copytree(REPO / "ip65", self.dir / "ip65", symlinks=True) if not all(self.exists(r) for r in IP65_LIBS): self.make("ip65-libs", check=False) assert all(self.exists(r) for r in IP65_LIBS), "ip65 libs would not build" @@ -987,11 +993,172 @@ def test_uci_build_does_not_need_the_ip65_tree(): """BACKEND=uci neither reads nor requires ip65/ or ip65-build/.""" _require_toolchain() with Farm() as farm: - for name in ("ip65", "ip65-build"): + for name in ("ip65",): os.unlink(farm.dir / name) + shutil.rmtree(farm.dir / "ip65-build") farm.make(*UCI) assert farm.exists(PRG) assert not farm.exists("ip65-build") + # the ip65 block must not run for another backend at all: if it did, + # its stamp could never be written here and every build would + # invalidate the link outputs + proc = farm.make(*UCI) + assert "ld65 " not in proc.stdout, ( + "a second UCI build relinked: the ip65 blob block ran:\n" + proc.stdout) + + +def _write_text_keep_mtime(path, text): + ns = path.stat().st_mtime_ns + path.write_text(text) + os.utime(path, ns=(ns, ns)) + + +def test_ip65_blob_lib_content_change_with_restored_mtime_relinks(): + """The content digest must cover the libs, not just their mtimes.""" + _require_toolchain() + with Ip65Farm() as farm: + farm.blob_build() + lib = farm.path(IP65_LIBS[0]) + ns = lib.stat().st_mtime_ns + with open(lib, "ab") as fh: + fh.write(b"\0") + os.utime(lib, ns=(ns, ns)) + proc = farm.blob_build(check=False) + assert "ld65 " in proc.stdout, ( + "a lib's content changed (mtime restored) and the blob did not " + "relink:\n" + proc.stdout) + + +def test_ip65_blob_toolchain_change_relinks(): + """TOOLCHAIN_ID is part of the blob digest (ca65 is not a prerequisite).""" + _require_toolchain() + with Ip65Farm() as farm: + farm.blob_build() + other = farm.dir / "ca65-other" + shutil.copy(shutil.which("ca65"), other) + proc = farm.make("BACKEND=ip65", "ip65-blob", f"CA65={other}") + assert "ld65 " in proc.stdout, ( + "a different assembler left the blob alone:\n" + proc.stdout) + # ld65's search-path environment can swap c64.lib without moving + # TOOLCHAIN_ID; it rides in the digest + proc = farm.make("BACKEND=ip65", "ip65-blob", f"CA65={other}", + env={"LD65_LIB": str(farm.dir / "nolib")}) + assert "ld65 " in proc.stdout, ( + "LD65_LIB changed and the blob was left alone:\n" + proc.stdout) + + +def test_ip65_blob_content_change_moves_the_prg(): + """A changed stub must reach the PRG even when the object looks fresh. + + The object's mtime is pushed into the future, so only the parse-time + deletion of build/net/ip65/ip65_blob.o can make the PRG pick it up. + """ + _require_toolchain() + with Ip65Farm() as farm: + farm.make(*IP65) + prg = farm.sha() + obj = farm.path("build/net/ip65/ip65_blob.o") + fut = obj.stat().st_mtime_ns + 60_000_000_000 + os.utime(obj, ns=(fut, fut)) + stub = farm.path("ip65-build/ip65_stub.s") + orig = stub.read_text() + assert orig.count(" rts ; no standalone entry point") == 1 + _write_text_keep_mtime(stub, orig.replace( + " rts ; no standalone entry point", + " nop ; no standalone entry point")) + farm.make(*IP65) + assert farm.sha() != prg, "a changed blob did not reach the PRG" + fut = farm.path("build/net/ip65/ip65_blob.o").stat().st_mtime_ns + 60_000_000_000 + os.utime(farm.path("build/net/ip65/ip65_blob.o"), ns=(fut, fut)) + _write_text_keep_mtime(stub, orig) + farm.make(*IP65) + assert farm.sha() == prg, "restoring the stub did not restore the PRG" + + +def test_ip65_blob_byte_flip_reaches_the_prg(): + """The documented provenance test: flip one byte of the blob, same second + (mtime restored). blob -> ip65_blob.o is an mtime edge; the blob's own + content stamp must close it.""" + _require_toolchain() + with Ip65Farm() as farm: + farm.make(*IP65) + farm.make(*IP65) # a parse with the blob present records it + prg = farm.sha() + blob = farm.path(BLOB) + data = blob.read_bytes() + ns = blob.stat().st_mtime_ns + for new in (bytes([data[100] ^ 0xFF]), bytes([data[100]])): + blob.write_bytes(data[:100] + new + data[101:]) + os.utime(blob, ns=(ns, ns)) + farm.make(*IP65) + if new == bytes([data[100]]): + assert farm.sha() == prg, "restoring the byte did not restore the PRG" + else: + assert farm.sha() != prg, "a flipped blob byte left a stale PRG" + + +def test_ip65_maintenance_goal_keeps_the_prg(): + """#220 for ip65: `make BACKEND=ip65 ip65-blob` after a blob-input change + rebuilds the blob and deletes only the blob and its object.""" + _require_toolchain() + with Ip65Farm() as farm: + farm.make(*IP65) + farm.edit_same_second("ip65-build/ip65.cfg", "\n# maintenance\n") + farm.make("BACKEND=ip65", "ip65-blob") + gone = [o for o in LINK_OUTPUTS if not farm.exists(o)] + assert not gone, f"a maintenance goal deleted {gone}" + + +def test_ip65_skip_goals_do_not_run_the_blob_block(): + """`clean` and `ip65-libs` must not compare, write or delete (#220).""" + _require_toolchain() + for goal in ("clean", "ip65-libs"): + with Ip65Farm() as farm: + farm.blob_build() + farm.edit_same_second("ip65-build/ip65.cfg", "\n# skip\n") + stamps = {p: farm.path(p).read_bytes() + for p in (BLOB_STAMP, "ip65-build/ip65-c64-bin.stamp")} + farm.make("BACKEND=ip65", goal) + assert farm.exists(BLOB), f"`make {goal}` deleted the blob" + for p, b in stamps.items(): + assert farm.path(p).read_bytes() == b, f"`make {goal}` rewrote {p}" + + +def test_ip65_blob_dry_run_message_matches_the_real_branch(): + """The -n warning must say what the real branch would delete.""" + _require_toolchain() + with Ip65Farm() as farm: + farm.make(*IP65) + farm.edit_same_second("ip65-build/ip65.cfg", "\n# dry\n") + + def line(*goals): + out = farm.make(*goals, opts=("-n",)).stdout + return [l for l in out.splitlines() + if "would delete ip65-build/ip65-c64.bin" in l] + maint = line("BACKEND=ip65", "ip65-blob") + assert maint and "ip65_blob.o" in maint[0] and PRG not in maint[0], maint + real = line(*IP65) + assert real and "ip65_blob.o" in real[0] and PRG in real[0], real + + +def test_ip65_dry_run_t_on_a_libless_tree_creates_no_libs(): + """`make -t` must not turn a missing lib into an empty file (a file + target with no recipe): it fails by name, creates nothing, and a later + `make ip65-libs` + build still works.""" + _require_toolchain() + with Ip65Farm() as farm: + farm.blob_build() + for lib in IP65_LIBS: + farm.path(lib).unlink() + for goal in (("ip65-blob",), (BLOB,)): + proc = farm.make("BACKEND=ip65", *goal, opts=("-t",), check=False) + assert proc.returncode != 0, "`make -t` with missing libs succeeded" + assert "make ip65-libs" in proc.stdout + for lib in IP65_LIBS: + assert not farm.exists(lib), f"`make -t` created {lib}" + farm.make("ip65-libs") + farm.blob_build() + assert all(farm.exists(r) for r in IP65_LIBS) def test_unchanged_flags_rebuild_nothing(): @@ -1292,6 +1459,23 @@ def test_stale_link_order_is_rebuilt_to_the_clean_hash(): f"{farm.sha()} != clean build {oracle}") +def _ip65_build_snapshot(): + d = REPO / "ip65-build" + return {p.name: hashlib.sha256(p.read_bytes()).hexdigest() + for p in sorted(d.iterdir()) if p.is_file()} if d.is_dir() else {} + + +_REAL_IP65_BUILD_AT_IMPORT = _ip65_build_snapshot() + + +def test_zz_real_ip65_build_is_untouched_by_the_suite(): + """Every Farm owns a private ip65-build/; the checkout's must be exactly + as this module found it (the blob was once deleted by a Farm test). + Named zz so the standalone runner, which sorts, runs it last.""" + assert _ip65_build_snapshot() == _REAL_IP65_BUILD_AT_IMPORT, ( + "the suite changed the real ip65-build/") + + def main() -> int: print("=== build/flags.stamp ===") tests = [(name, fn) for name, fn in sorted(globals().items()) From f79dc28b28e694fec4350c394f4d3fdd5a50ed0a Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Sun, 18 Oct 2026 10:31:01 -0500 Subject: [PATCH 3/5] make: write the blob-bytes stamp in the blob rule; make-variable LD65 env (#309 round 2) - the blob rule writes ip65-c64-bin.stamp after ld65, so the FIRST same-second flip after a build reaches the PRG; the block-1 deletion of that stamp is gone - LD65_ENV digest reads the make variables LD65_LIB/LD65_OBJ/CC65_HOME so a command-line value is seen on make 3.81 - tests: first flip without warm-up, no-blob/blob-no-stamps checkouts, one invalidation per input change, absent bin stamp is recorded, bin-stamp -n text - CLAUDE.md sentence corrected Co-Authored-By: Claude Sonnet 5.5 --- CLAUDE.md | 5 +- Makefile | 5 +- tools/test_build_flags_stamp.py | 120 +++++++++++++++++++++++++------- 3 files changed, 101 insertions(+), 29 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 55987cf9..482455ec 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -32,8 +32,9 @@ Fresh clone (ip65 backend; UCI needs none of the ip65 steps): blob tracks stub, `ip65.cfg` and both libs by content (`ip65-build/ip65-c64.stamp`, #305 shape; `-n` writes nothing); `c64.lib` is covered only via the toolchain id and LD65_LIB/OBJ/CC65_HOME, not - hashed. The blob's own bytes are stamped too (`ip65-c64-bin.stamp`), so a - same-second byte flip reaches the PRG. + hashed. The blob's own bytes are stamped too (`ip65-c64-bin.stamp`, written + by the blob rule; a stampless tree records it at the next parse), so a + same-second byte flip reaches the PRG from the first parse after a build. - **A nested git worktree needs `git submodule update --init --recursive` before it can link ip65** — a fresh worktree has no submodule working trees, so the link dies by name. It fails LOUDLY; that is the whole diff --git a/Makefile b/Makefile index 0f0339d0..9a8bbd93 100644 --- a/Makefile +++ b/Makefile @@ -1080,7 +1080,7 @@ IP65_BLOB_OBJ := build/net/ip65/ip65_blob.o # ld65's own search-path environment (LD65_LIB, LD65_OBJ, CC65_HOME) can # swap c64.lib without moving TOOLCHAIN_ID, so it rides in the digest too. IP65_BLOB_STAMP_BODY := { printf 'TOOLCHAIN=%s\n' '$(TOOLCHAIN_ID)'; \ - printf 'LD65_ENV=%s|%s|%s\n' "$$LD65_LIB" "$$LD65_OBJ" "$$CC65_HOME"; \ + printf 'LD65_ENV=%s|%s|%s\n' '$(LD65_LIB)' '$(LD65_OBJ)' '$(CC65_HOME)'; \ for f in $(IP65_BUILD)/ip65_stub.s $(IP65_BUILD)/ip65.cfg $(IP65_LIBS); do \ (sha256sum $$f 2>/dev/null || shasum -a 256 $$f 2>/dev/null || echo "$$f MISSING"); \ done; } @@ -1098,7 +1098,7 @@ _ := $(shell $(IP65_BLOB_STAMP_BODY) > $(IP65_BLOB_STAMP).tmp; \ rm -f $(IP65_BLOB_STAMP).tmp; \ else \ mv $(IP65_BLOB_STAMP).tmp $(IP65_BLOB_STAMP); \ - rm -f $(IP65_BIN) $(IP65_BIN_STAMP) $(IP65_BLOB_OBJ) $(IP65_RM_LINK); \ + rm -f $(IP65_BIN) $(IP65_BLOB_OBJ) $(IP65_RM_LINK); \ fi) # The blob's own bytes, by content, for a blob changed OUTSIDE this rule (a # byte flip, the documented provenance test): blob -> ip65_blob.o is an mtime @@ -1135,6 +1135,7 @@ $(IP65_BIN): $(IP65_BUILD)/ip65_stub.s $(IP65_BUILD)/ip65.cfg $(IP65_LIBS) cd $(IP65_BUILD) && $(LD65) -C ip65.cfg -o ip65-c64.bin -m ip65-c64.map \ ip65_stub.o ../$(IP65_DIR)/ip65/ip65_tcp.lib \ ../$(IP65_DIR)/drivers/ip65_c64.lib c64.lib + $(IP65_BIN_SHA) > $(IP65_BIN_STAMP) run: $(PRG) $(VICE) -autostart $(PRG) diff --git a/tools/test_build_flags_stamp.py b/tools/test_build_flags_stamp.py index 3a8d7841..79d9e5a9 100644 --- a/tools/test_build_flags_stamp.py +++ b/tools/test_build_flags_stamp.py @@ -882,8 +882,17 @@ class Ip65Farm(Farm): Libs come from the checkout when built there, else from `make ip65-libs` run inside the copy.""" - def __init__(self): + def __init__(self, state="as-is"): + """state: "as-is" keeps whatever the checkout's ip65-build/ holds; + "blob-no-stamps" and "no-blob-no-stamps" model a fresh clone and a + pre-#309 tree, so no case may depend on checkout state.""" super().__init__() + d = self.dir / "ip65-build" + if state != "as-is": + for p in d.glob("*.stamp"): + p.unlink() + if state == "no-blob-no-stamps": + (d / "ip65-c64.bin").unlink(missing_ok=True) os.unlink(self.dir / "ip65") shutil.copytree(REPO / "ip65", self.dir / "ip65", symlinks=True) if not all(self.exists(r) for r in IP65_LIBS): @@ -1039,6 +1048,12 @@ def test_ip65_blob_toolchain_change_relinks(): proc = farm.make("BACKEND=ip65", "ip65-blob", f"CA65={other}") assert "ld65 " in proc.stdout, ( "a different assembler left the blob alone:\n" + proc.stdout) + # the command-line form: 3.81's $(shell) does not see it in its + # environment, so the digest must read the make variable + proc = farm.make("BACKEND=ip65", "ip65-blob", f"CA65={other}", + f"LD65_LIB={farm.dir / 'nolib2'}") + assert "ld65 " in proc.stdout, ( + "a command-line LD65_LIB was not seen:\n" + proc.stdout) # ld65's search-path environment can swap c64.lib without moving # TOOLCHAIN_ID; it rides in the digest proc = farm.make("BACKEND=ip65", "ip65-blob", f"CA65={other}", @@ -1075,26 +1090,66 @@ def test_ip65_blob_content_change_moves_the_prg(): assert farm.sha() == prg, "restoring the stub did not restore the PRG" +def _check_first_flip(farm): + """The FIRST same-second flip after a build must reach the PRG (no + warm-up parse), and restoring the byte must restore the PRG.""" + farm.make(*IP65) + prg = farm.sha() + blob = farm.path(BLOB) + data = blob.read_bytes() + ns = blob.stat().st_mtime_ns + for new in (bytes([data[100] ^ 0xFF]), bytes([data[100]])): + blob.write_bytes(data[:100] + new + data[101:]) + os.utime(blob, ns=(ns, ns)) + farm.make(*IP65) + if new == bytes([data[100]]): + assert farm.sha() == prg, "restoring the byte did not restore the PRG" + else: + assert farm.sha() != prg, "a flipped blob byte left a stale PRG" + + def test_ip65_blob_byte_flip_reaches_the_prg(): """The documented provenance test: flip one byte of the blob, same second (mtime restored). blob -> ip65_blob.o is an mtime edge; the blob's own - content stamp must close it.""" + content stamp, written by the blob rule, must close it.""" _require_toolchain() - with Ip65Farm() as farm: - farm.make(*IP65) - farm.make(*IP65) # a parse with the blob present records it - prg = farm.sha() - blob = farm.path(BLOB) - data = blob.read_bytes() - ns = blob.stat().st_mtime_ns - for new in (bytes([data[100] ^ 0xFF]), bytes([data[100]])): - blob.write_bytes(data[:100] + new + data[101:]) - os.utime(blob, ns=(ns, ns)) + with Ip65Farm("no-blob-no-stamps") as farm: + _check_first_flip(farm) + + +def test_ip65_cases_do_not_depend_on_checkout_state(): + """A fresh clone (no blob, no stamps) and a pre-stamp tree (blob, no + stamps) must behave exactly like the checkout the suite runs in.""" + _require_toolchain() + for state in ("no-blob-no-stamps", "blob-no-stamps"): + with Ip65Farm(state) as farm: + _check_first_flip(farm) + with Ip65Farm(state) as farm: + _check_skip_goals(farm) + + +def test_ip65_parse_after_a_build_invalidates_nothing(): + """Exactly one invalidation per input change: the parse right after a + build (input change, or a stampless tree) deletes nothing.""" + _require_toolchain() + for state in ("as-is", "blob-no-stamps"): + with Ip65Farm(state) as farm: farm.make(*IP65) - if new == bytes([data[100]]): - assert farm.sha() == prg, "restoring the byte did not restore the PRG" - else: - assert farm.sha() != prg, "a flipped blob byte left a stale PRG" + farm.edit_same_second("ip65-build/ip65.cfg", "\n# once\n") + farm.make(*IP65) + before = {o: farm.path(o).stat().st_mtime_ns for o in LINK_OUTPUTS} + blob_ns = farm.blob_mtime() + proc = farm.make(*IP65) + assert "ld65 " not in proc.stdout and "ca65 " not in proc.stdout, ( + f"{state}: a parse after a build invalidated again:\n" + proc.stdout) + assert farm.blob_mtime() == blob_ns + assert before == {o: farm.path(o).stat().st_mtime_ns for o in LINK_OUTPUTS} + # a stamp lost after a build is recorded, not treated as a change + farm.path("ip65-build/ip65-c64-bin.stamp").unlink() + proc = farm.make(*IP65) + assert "ld65 " not in proc.stdout and "ca65 " not in proc.stdout, ( + f"{state}: an absent bin stamp invalidated:\n" + proc.stdout) + assert farm.exists("ip65-build/ip65-c64-bin.stamp") def test_ip65_maintenance_goal_keeps_the_prg(): @@ -1109,19 +1164,24 @@ def test_ip65_maintenance_goal_keeps_the_prg(): assert not gone, f"a maintenance goal deleted {gone}" +def _check_skip_goals(farm): + for goal in ("clean", "ip65-libs"): + farm.blob_build() + farm.edit_same_second("ip65-build/ip65.cfg", "\n# skip %s\n" % goal) + stamps = {p: farm.path(p).read_bytes() + for p in (BLOB_STAMP, "ip65-build/ip65-c64-bin.stamp")} + farm.make("BACKEND=ip65", goal) + assert farm.exists(BLOB), f"`make {goal}` deleted the blob" + for p, b in stamps.items(): + assert farm.path(p).read_bytes() == b, f"`make {goal}` rewrote {p}" + + def test_ip65_skip_goals_do_not_run_the_blob_block(): """`clean` and `ip65-libs` must not compare, write or delete (#220).""" _require_toolchain() - for goal in ("clean", "ip65-libs"): + for _ in range(2): with Ip65Farm() as farm: - farm.blob_build() - farm.edit_same_second("ip65-build/ip65.cfg", "\n# skip\n") - stamps = {p: farm.path(p).read_bytes() - for p in (BLOB_STAMP, "ip65-build/ip65-c64-bin.stamp")} - farm.make("BACKEND=ip65", goal) - assert farm.exists(BLOB), f"`make {goal}` deleted the blob" - for p, b in stamps.items(): - assert farm.path(p).read_bytes() == b, f"`make {goal}` rewrote {p}" + _check_skip_goals(farm) def test_ip65_blob_dry_run_message_matches_the_real_branch(): @@ -1140,6 +1200,16 @@ def line(*goals): real = line(*IP65) assert real and "ip65_blob.o" in real[0] and PRG in real[0], real + # the blob-bytes stamp has its own warning + farm.blob_build() + farm.make(*IP65) + blob = farm.path(BLOB) + data, ns = blob.read_bytes(), blob.stat().st_mtime_ns + blob.write_bytes(data[:100] + bytes([data[100] ^ 0xFF]) + data[101:]) + os.utime(blob, ns=(ns, ns)) + out = farm.make(*IP65, opts=("-n",)).stdout + assert "differs from ip65-build/ip65-c64-bin.stamp" in out, out + def test_ip65_dry_run_t_on_a_libless_tree_creates_no_libs(): """`make -t` must not turn a missing lib into an empty file (a file From 33342af46ec1d311e192c1d85d78a6e176165da1 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Sun, 18 Oct 2026 11:05:50 -0500 Subject: [PATCH 4/5] make: an absent ip65 blob clears the stale object and PRG (#309 round 3) flip -> delete the blob -> rebuild left ip65_blob.o and the PRG from the flipped blob permanent when they were not older than the regenerated blob (same second on make 3.81). The blob-bytes block now deletes the object and (for real goals) the link outputs whenever the blob is absent at parse time. Test: test_ip65_deleted_blob_after_a_flip_recovers_the_prg. Co-Authored-By: Claude Sonnet 5.5 --- Makefile | 4 ++++ tools/test_build_flags_stamp.py | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/Makefile b/Makefile index 9a8bbd93..f1d7e18d 100644 --- a/Makefile +++ b/Makefile @@ -1111,7 +1111,11 @@ _ := $(shell if [ -f $(IP65_BIN) ]; then \ rm -f $(IP65_BLOB_OBJ) $(IP65_RM_LINK); \ fi; \ mv $(IP65_BIN_STAMP).tmp $(IP65_BIN_STAMP); \ + else rm -f $(IP65_BLOB_OBJ) $(IP65_RM_LINK); \ fi) +# An ABSENT blob is rebuilt (deterministically) newer-or-equal to an object that +# may have been assembled from a flipped one, and the rule's own stamp then +# matches: the stale object/PRG would be permanent. So absence clears them too. else ifneq ($(shell $(IP65_BLOB_STAMP_BODY) 2>/dev/null | cmp -s - $(IP65_BLOB_STAMP) >/dev/null 2>&1 || echo differs),) $(warning DRY RUN (-$(MAKE_OPT_LETTERS)): ip65 blob inputs differ from $(IP65_BLOB_STAMP).) diff --git a/tools/test_build_flags_stamp.py b/tools/test_build_flags_stamp.py index 79d9e5a9..a3c4f71c 100644 --- a/tools/test_build_flags_stamp.py +++ b/tools/test_build_flags_stamp.py @@ -1117,6 +1117,29 @@ def test_ip65_blob_byte_flip_reaches_the_prg(): _check_first_flip(farm) +def test_ip65_deleted_blob_after_a_flip_recovers_the_prg(): + """flip -> delete the blob -> rebuild: the regenerated (good) blob must + take the flipped PRG with it, even when the object and PRG look newer + than the new blob (same second on make 3.81).""" + _require_toolchain() + with Ip65Farm("no-blob-no-stamps") as farm: + farm.make(*IP65) + good = farm.sha() + blob = farm.path(BLOB) + data, ns = blob.read_bytes(), blob.stat().st_mtime_ns + blob.write_bytes(data[:100] + bytes([data[100] ^ 0xFF]) + data[101:]) + os.utime(blob, ns=(ns, ns)) + farm.make(*IP65) + assert farm.sha() != good + blob.unlink() + for rel in ("build/net/ip65/ip65_blob.o", PRG): + p = farm.path(rel) + fut = p.stat().st_mtime_ns + 60_000_000_000 + os.utime(p, ns=(fut, fut)) + farm.make(*IP65) + assert farm.sha() == good, "a deleted blob left the flipped PRG permanent" + + def test_ip65_cases_do_not_depend_on_checkout_state(): """A fresh clone (no blob, no stamps) and a pre-stamp tree (blob, no stamps) must behave exactly like the checkout the suite runs in.""" From 329894928e99519551384bcdc715e2ccc64170a9 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Sun, 18 Oct 2026 11:22:54 -0500 Subject: [PATCH 5/5] make: a missing object takes the PRG with it; an empty blob counts as absent (#309 round 4) - bytes block ends with `[ -f ip65_blob.o ] || rm -f $(IP65_RM_LINK)`: after `make ip65-blob` (maintenance, keeps the PRG per #220, deletes the object) the PRG can no longer outlive its object. - a 0-byte blob (`make -t` creates one) is deleted at parse, so the next real build recovers instead of dying on the size assert. - block comment corrected (absent blob no longer "record only"). Co-Authored-By: Claude Sonnet 5.5 --- Makefile | 19 ++++++++++------ tools/test_build_flags_stamp.py | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index f1d7e18d..72cb4323 100644 --- a/Makefile +++ b/Makefile @@ -1103,19 +1103,24 @@ _ := $(shell $(IP65_BLOB_STAMP_BODY) > $(IP65_BLOB_STAMP).tmp; \ # The blob's own bytes, by content, for a blob changed OUTSIDE this rule (a # byte flip, the documented provenance test): blob -> ip65_blob.o is an mtime # edge, and a same-second flip left the object, and the PRG, stale. A change -# deletes the object and link outputs. Absent blob or stamp = record only, so -# the rebuild after the block above does not trigger a second invalidation. -_ := $(shell if [ -f $(IP65_BIN) ]; then \ +# deletes the object and link outputs. A present blob with no stamp is only +# recorded (a pre-stamp tree). An ABSENT blob is rebuilt newer-or-equal to an +# object that may have been assembled from a flipped one, and the rule's own +# stamp then matches, so absence clears the object and link outputs too. An +# EMPTY blob (`make -t` touches one into existence) counts as absent. Last, +# a missing object always takes the link outputs with it: `make ip65-blob` +# (a maintenance goal, which keeps the PRG per #220) deletes only the object, +# and the PRG would otherwise outlive it; IP65_RM_LINK is empty for such goals. +_ := $(shell [ -s $(IP65_BIN) ] || rm -f $(IP65_BIN); \ + if [ -f $(IP65_BIN) ]; then \ $(IP65_BIN_SHA) > $(IP65_BIN_STAMP).tmp; \ if [ -f $(IP65_BIN_STAMP) ] && ! cmp -s $(IP65_BIN_STAMP).tmp $(IP65_BIN_STAMP); then \ rm -f $(IP65_BLOB_OBJ) $(IP65_RM_LINK); \ fi; \ mv $(IP65_BIN_STAMP).tmp $(IP65_BIN_STAMP); \ else rm -f $(IP65_BLOB_OBJ) $(IP65_RM_LINK); \ - fi) -# An ABSENT blob is rebuilt (deterministically) newer-or-equal to an object that -# may have been assembled from a flipped one, and the rule's own stamp then -# matches: the stale object/PRG would be permanent. So absence clears them too. + fi; \ + [ -f $(IP65_BLOB_OBJ) ] || rm -f $(IP65_RM_LINK)) else ifneq ($(shell $(IP65_BLOB_STAMP_BODY) 2>/dev/null | cmp -s - $(IP65_BLOB_STAMP) >/dev/null 2>&1 || echo differs),) $(warning DRY RUN (-$(MAKE_OPT_LETTERS)): ip65 blob inputs differ from $(IP65_BLOB_STAMP).) diff --git a/tools/test_build_flags_stamp.py b/tools/test_build_flags_stamp.py index a3c4f71c..43a7a583 100644 --- a/tools/test_build_flags_stamp.py +++ b/tools/test_build_flags_stamp.py @@ -1140,6 +1140,45 @@ def test_ip65_deleted_blob_after_a_flip_recovers_the_prg(): assert farm.sha() == good, "a deleted blob left the flipped PRG permanent" +def test_ip65_maintenance_rebuild_after_a_flip_recovers_the_prg(): + """flip -> make -> rm blob -> `make ip65-blob` -> make, with the PRG not + older than the reassembled object. The maintenance goal keeps the PRG + (#220) but deletes the object; the PRG must not outlive it.""" + _require_toolchain() + with Ip65Farm("no-blob-no-stamps") as farm: + farm.make(*IP65) + good = farm.sha() + blob = farm.path(BLOB) + data, ns = blob.read_bytes(), blob.stat().st_mtime_ns + blob.write_bytes(data[:100] + bytes([data[100] ^ 0xFF]) + data[101:]) + os.utime(blob, ns=(ns, ns)) + farm.make(*IP65) + assert farm.sha() != good + blob.unlink() + farm.make("BACKEND=ip65", "ip65-blob") + assert farm.exists(PRG), "the maintenance goal deleted the PRG (#220)" + fut = farm.path(PRG).stat().st_mtime_ns + 30_000_000_000 + os.utime(farm.path(PRG), ns=(fut, fut)) + farm.make(*IP65) + assert farm.sha() == good, "the flipped PRG outlived its deleted object" + + +def test_ip65_dry_run_t_blob_is_recovered_by_the_next_build(): + """`make -t` with the blob absent touches a 0-byte blob into existence; + the next real build must treat it as absent and recover on its own.""" + _require_toolchain() + with Ip65Farm("no-blob-no-stamps") as farm: + farm.make(*IP65) + good, blob_sha = farm.sha(), farm.sha(BLOB) + farm.path(BLOB).unlink() + farm.make("BACKEND=ip65", "ip65-blob", opts=("-t",), check=False) + assert farm.exists(BLOB) and farm.path(BLOB).stat().st_size == 0, ( + "`make -t` no longer creates the empty blob; the case is vacuous") + farm.make(*IP65) + assert farm.sha(BLOB) == blob_sha, "an empty blob was not rebuilt" + assert farm.sha() == good + + def test_ip65_cases_do_not_depend_on_checkout_state(): """A fresh clone (no blob, no stamps) and a pre-stamp tree (blob, no stamps) must behave exactly like the checkout the suite runs in."""