diff --git a/CLAUDE.md b/CLAUDE.md index 8099621..8523337 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -57,10 +57,11 @@ Fresh clone (ip65 backend; UCI needs none of the ip65 steps): mtimes, not the command line, so this used to be a discipline you only had to forget once. It is now an enforced invariant: `build/flags.stamp` holds the fully expanded `CA65FLAGS`/`LD65FLAGS` (`LD65FLAGS` carries `$(CFG)`, so -`BACKEND=` and the profile cfg variants ride along), is content-compared -**at Makefile parse time**, and on any change deletes every `.o` and the -PRG. Absence, not an mtime — which is what macOS GNU Make 3.81's 1-second -resolution cannot defeat. Both silent failure modes the old rule guarded +`BACKEND=` and the profile cfg variants ride along) plus the cfg's sha256 +(#305: a cfg edit rebuilds), is content-compared **at Makefile parse +time**, and on any change deletes every `.o` and the PRG. Absence, +not an mtime — which is what macOS GNU Make 3.81's 1-second resolution +cannot defeat. Both silent failure modes the old rule guarded against are closed, and `tools/test_build_flags_stamp.py` pins each against a `make clean` oracle: the mixed link (`HTTPS_SNI=` invalidating `boot.o` but not `http.o`, which cost a false negative on #141) and no link at all diff --git a/Makefile b/Makefile index 01703e6..dffa86c 100644 --- a/Makefile +++ b/Makefile @@ -609,7 +609,8 @@ build/net/ip65/ip65_blob.o: $(IP65_BIN) # hand-kept list of knob names: every flag that reaches the toolchain does so # through CA65FLAGS or LD65FLAGS (which carries $(CFG), hence BACKEND and the # onchip/comb cfg variants), so a new knob added at the top of this file is -# covered on the day it is written. The archive list and the three exported +# covered on the day it is written. The cfg's CONTENT is stamped too (an edit +# to the file moves no command line). The archive list and the three exported # X25519 segment names are added because they steer build inputs without # appearing on either command line. # @@ -743,6 +744,11 @@ STAMP_SKIP := $(if $(MAKECMDGOALS),$(if $(filter-out $(STAMP_EXEMPT_GOALS),$(MAK TOOLCHAIN_ID := $(shell bash tools/integration/toolchain_id.sh \ 'CA65=$(or $(CA65),ca65)' 'AR65=$(or $(AR65),ar65)' 'LD65=$(or $(LD65),ld65)') +# The linker cfg by CONTENT: LD65FLAGS carries only its NAME, so an edit to +# the selected cfg moved nothing the stamp saw and the PRG stayed "up to date". +# Content, not an mtime or a $(CFG) prerequisite: 3.81's 1-second mtimes. +CFG_SHA256 := $(firstword $(shell (sha256sum $(CFG) 2>/dev/null || shasum -a 256 $(CFG)) 2>/dev/null)) + FLAGS_STAMP_BODY := printf '%s\n' \ 'c64-https build flags. Generated by the Makefile; do not edit.' \ 'A change to any line deletes every object and the PRG at parse time.' \ @@ -754,6 +760,7 @@ FLAGS_STAMP_BODY := printf '%s\n' \ 'BACKEND=$(BACKEND)' \ 'CA65FLAGS=$(CA65FLAGS)' \ 'LD65FLAGS=$(LD65FLAGS)' \ + 'CFG_SHA256=$(CFG_SHA256)' \ 'SIBLING_LIB_ARCHIVES=$(SIBLING_LIB_ARCHIVES)' \ 'X25519_SEG_FE25519=$(X25519_SEG_FE25519)' \ 'X25519_SEG_LADDER=$(X25519_SEG_LADDER)' \ diff --git a/tools/test_build_flags_stamp.py b/tools/test_build_flags_stamp.py index 0625748..f578c78 100644 --- a/tools/test_build_flags_stamp.py +++ b/tools/test_build_flags_stamp.py @@ -781,6 +781,87 @@ def test_hand_set_makeflags_is_refused_without_touching_the_tree(): ) +class CfgFarm(Farm): + """A Farm whose cfg/ is a private COPY, so a test may edit a linker cfg.""" + + def __init__(self): + super().__init__() + os.unlink(self.dir / "cfg") + shutil.copytree(REPO / "cfg", self.dir / "cfg") + + def append_comment(self, rel): + """A comment-only edit: the cfg's text changes, the link's bytes do not.""" + with open(self.path(rel), "a") as fh: + fh.write("\n# edited by test_build_flags_stamp\n") + + +# Every cfg a build can select (Makefile: CFG, and the two variants it swaps +# in), each with the flags that select it. ip65 is left out for cost, as in +# FARM_LINKS: it needs the ip65 libs built first. +CFG_PROFILES = ( + ("cfg/c64-https-uci.cfg", ("BACKEND=uci",)), + ("cfg/c64-https-uci.cfg", UCI), # onchip alone keeps the base cfg + ("cfg/c64-https-uci-onchip.cfg", UCI + ("USE_NISTCURVES_ONCHIP_COMB=1", + "COLD_BANK=0")), + ("cfg/c64-https-uci-onchip-cold.cfg", UCI + ("USE_NISTCURVES_ONCHIP_COMB=1",)), + ("cfg/c64-https-uci-m3.cfg", ("BACKEND=uci-m3",)), +) + + +def test_cfg_edit_relinks_every_link_output(): + """Editing only the selected linker cfg must relink, without `make clean`. + + The PRG's prerequisites were objects and archives; the cfg reached the + stamp only as the NAME in LD65FLAGS, so an edit to its contents left + `make` at "Nothing to be done" and the old image, map, labels and dbg in + place. The edit here is comment-only, so the relinked PRG must also be + byte-identical. The inverse is pinned in the same case: an untouched cfg + relinks nothing. + """ + _require_toolchain() + for cfg, flags in CFG_PROFILES: + with CfgFarm() as farm: + farm.make(*flags) + before = farm.sha() + old = {o: farm.path(o).stat().st_mtime_ns for o in LINK_OUTPUTS} + proc = farm.make(*flags) + assert "ld65 " not in proc.stdout, ( + f"{cfg}: an unchanged cfg relinked:\n" + proc.stdout) + assert all(farm.path(o).stat().st_mtime_ns == old[o] + for o in LINK_OUTPUTS), f"{cfg}: unchanged cfg touched outputs" + # No sleep, on purpose: a same-second edit is invisible to an + # mtime prerequisite on make 3.81, so only a content stamp passes. + farm.append_comment(cfg) + proc = farm.make(*flags) + assert "ld65 " in proc.stdout, ( + f"{cfg} edited, {flags}: make did not relink:\n" + proc.stdout) + for o in LINK_OUTPUTS: + assert farm.exists(o), f"{cfg}: {o} missing after relink" + assert farm.path(o).stat().st_mtime_ns != old[o], ( + f"{cfg}: {o} is stale after a cfg edit") + assert farm.sha() == before, f"{cfg}: a comment edit moved the PRG" + + +def test_ip65_cfg_edit_changes_the_stamp_and_clears_outputs(): + """The ip65 cfg, without an ip65 link: the goal is build/flags.stamp.""" + _require_toolchain() + with CfgFarm() as farm: + farm.make(*UCI) + farm.make("BACKEND=ip65", STAMP, check=False) + line = lambda: [l for l in farm.path(STAMP).read_text().splitlines() + if l.startswith("CFG_SHA256=")] + before = line() + assert len(before) == 1 and len(before[0]) > len("CFG_SHA256=") + 8, ( + "flags.stamp has no CFG_SHA256 line") + for o in LINK_OUTPUTS: + farm.path(o).write_text("planted") + farm.append_comment("cfg/c64-https-ip65.cfg") + farm.make("BACKEND=ip65", STAMP, check=False) + assert line() != before, "ip65 cfg edit did not move CFG_SHA256" + for o in LINK_OUTPUTS: + assert not farm.exists(o), f"{o} survived an ip65 cfg edit" + + def test_unchanged_flags_rebuild_nothing(): """The inverse property: the stamp must not make every build a rebuild.""" _require_toolchain()