Rebuild only what changed when installing a commit on a phone

phone-install's container now keeps the times of files the commit did not
change and the Gradle and CMake build directories between runs, and the
Android source generator keeps the times of C files it emits unchanged, so
a small change no longer recompiles the whole app.
This commit is contained in:
waozi committed 2026-10-30 02:14:37 -03:00
1 parent 3bcf2c0998
commit 566f2bee8e
4 files changed
+76 -7

No files matched your search

+33 -2
View File
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
"""Materialize a fixed commit only inside the existing release container overlay."""
from pathlib import Path
import json
import os
import shutil
import stat
@@ -21,7 +22,35 @@ def remove_read_only(function, path, error):
function(path)
def prepare(root, revision):
def restore_unchanged_times(git, root, record):
"""Give each file whose content matches the last run its earlier time.
Materializing writes every file anew, so make and CMake would rebuild
everything. The record maps each path to its blob and the time it first
held that blob; changed files keep the current time."""
previous = json.loads(record.read_text()) if record.exists() else {}
current = {}
for entry in git("ls-files", "--stage", "-z").split(b"\0"):
if not entry or entry.startswith(b"160000 "):
continue
info, path = entry.split(b"\t", 1)
mode, blob = info.split()[:2]
name = os.fsdecode(path)
identity = mode.decode() + " " + blob.decode()
known = previous.get(name)
if known and known[0] == identity:
os.utime(root / name, ns=(known[1], known[1]), follow_symlinks=False)
current[name] = known
else:
current[name] = [identity, (root / name).lstat().st_mtime_ns]
record.parent.mkdir(parents=True, exist_ok=True)
temporary = record.with_name(record.name + ".tmp")
temporary.write_text(json.dumps(current))
temporary.replace(record)
print("release-ready: kept the times of unchanged files", flush=True)
def prepare(root, revision, times_record=None):
root = Path(root).resolve(strict=True)
filesystem = subprocess.check_output(
["stat", "-f", "-c", "%T", root], text=True).strip()
@@ -60,11 +89,13 @@ def prepare(root, revision):
git("checkout-index", "--all", "--force")
git("diff", "--exit-code", commit, "--", ".")
print("release-ready: fixed commit materialized in private overlay", flush=True)
if times_record:
restore_unchanged_times(git, root, root / times_record)
if __name__ == "__main__":
try:
prepare(sys.argv[1], sys.argv[2])
prepare(sys.argv[1], sys.argv[2], sys.argv[3] if len(sys.argv) > 3 else None)
except (OSError, ValueError, subprocess.CalledProcessError) as error:
print("release-ready: " + str(error), file=sys.stderr)
sys.exit(2)
+13 -1
View File
@@ -20,8 +20,13 @@ sources = sorted((root / "src").rglob("*.zi"))
sources.append(root / "apps/practice/hold_reminder.zi")
if list((root / "src").rglob("*.c")) or list((root / "src").rglob("*.kry")):
raise SystemExit("Android application implementation must be current Zi")
# The compiler writes every file anew. Outputs with the same bytes as before
# get their earlier times back, so the native build recompiles only changes.
previous = args.output.with_name(args.output.name + ".previous")
if previous.exists():
shutil.rmtree(previous)
if args.output.exists():
shutil.rmtree(args.output)
args.output.rename(previous)
args.output.mkdir(parents=True)
command = ["sh", str(root / "scripts/run-ziran.sh"), str(args.compiler),
"--no-main", "--root", str(root)]
@@ -43,6 +48,13 @@ if completed.returncode != 0:
# The compiler already printed precise diagnostics; avoid repeating the
# entire application source list inside a Python exception traceback.
raise SystemExit(completed.returncode)
for output in args.output.rglob("*"):
earlier = previous / output.relative_to(args.output)
if output.is_file() and earlier.is_file() and output.read_bytes() == earlier.read_bytes():
times = earlier.stat()
os.utime(output, ns=(times.st_atime_ns, times.st_mtime_ns))
if previous.exists():
shutil.rmtree(previous)
outputs = sorted(args.output.rglob("*.c"))
if not outputs:
raise SystemExit("Zi compiler emitted no Android native sources")
+7 -2
View File
@@ -4,7 +4,10 @@
# the locked packages and none of the uncommitted work in the checkout or its
# local package overrides), sign it with this machine's Android debug key so
# it updates the installed app with its data kept, install it, check that the
# phone holds exactly that APK, and open it from the launcher.
# phone holds exactly that APK, and open it from the launcher. Builds are
# incremental: files the commit left unchanged keep their earlier times, and
# the Gradle and CMake build directories stay in build/release-ready-phone/,
# so after a small change only that change rebuilds.
#
# sh scripts/phone-install.sh HEAD on the Pixel 3a
# sh scripts/phone-install.sh --device moto HEAD on the Moto e6 play
@@ -72,7 +75,9 @@ fi
if [ -z "$apk" ]; then
commit=$(git rev-parse "$revision^{commit}")
echo "phone-install: building $(git log -1 --format='%h %s' "$commit")"
RELEASE_READY_REVISION=$commit RELEASE_READY_OUTPUT=$output \
# A failed build must not leave an earlier APK to install.
find "$output/build/android" -name "app*-$abi-debug.apk" -delete 2>/dev/null || true
RELEASE_READY_REVISION=$commit RELEASE_READY_OUTPUT=$output RELEASE_READY_INCREMENTAL=1 \
sh scripts/release-ready.sh ANDROID_ABI="$abi" android-debug
apk=$(find "$output/build/android" -name "app*-$abi-debug.apk" | head -n 1)
[ -f "$apk" ] || fail "the build left no app-$abi-debug.apk in $output/build/android"
+23 -2
View File
@@ -21,6 +21,9 @@
#
# sh scripts/release-ready.sh every covered job; writes the stamp
# sh scripts/release-ready.sh TARGET... only these make targets; no stamp
# RELEASE_READY_INCREMENTAL=1 (targets only) keeps the times of files the
# commit did not change and the Gradle and CMake build directories between
# runs, so a rebuild compiles only what changed (scripts/phone-install.sh).
# RELEASE_READY_DNS optionally supplies a resolver when rootless DNS is broken.
set -eu
@@ -57,11 +60,24 @@ print(module.release_content(module.ROOT, sys.argv[1]))
PY_CONTENT
)
snapshot=$(cat scripts/check-release-overlay.py)
incremental=""
if [ -n "${RELEASE_READY_INCREMENTAL:-}" ]; then
[ "$#" -gt 0 ] || { echo "release-ready: RELEASE_READY_INCREMENTAL needs make targets; full runs start clean" >&2; exit 2; }
incremental=1
fi
# Runs inside the container, from this variable rather than the script file,
# because resetting the overlay to HEAD may rewrite this script.
inside='set -eu
python3 -c "$RELEASE_READY_SNAPSHOT" /src "$RELEASE_READY_COMMIT"
if [ -n "$RELEASE_READY_INCREMENTAL" ]; then
python3 -c "$RELEASE_READY_SNAPSHOT" /src "$RELEASE_READY_COMMIT" build/incremental/source-times.json
for directory in droid/.gradle droid/.kotlin droid/build droid/app/build droid/app/.cxx; do
mkdir -p "build/incremental/$directory"
ln -s "/src/build/incremental/$directory" "$directory"
done
else
python3 -c "$RELEASE_READY_SNAPSHOT" /src "$RELEASE_READY_COMMIT"
fi
failed=""
job() {
name=$1
@@ -98,7 +114,11 @@ if [ -n "$failed" ]; then
exit 1
fi
git diff --exit-code "$RELEASE_READY_COMMIT" -- .
printf "\nrelease-ready: every covered local CI job passed\n"'
if [ "$#" -gt 0 ]; then
printf "\nrelease-ready: make %s passed\n" "$*"
else
printf "\nrelease-ready: every covered local CI job passed\n"
fi'
mkdir -p "$output/build" "$output/home"
sync_server=""
@@ -161,6 +181,7 @@ podman run --rm --pull=never --userns=keep-id \
-v "$sync_mount_source:/sync-server:O" \
$android_mount \
-e HOME=/home/ubuntu -e CI=true -e RELEASE_READY_COMMIT="$commit" \
-e RELEASE_READY_INCREMENTAL="$incremental" \
-e RELEASE_READY_SNAPSHOT="$snapshot" \
-w /src "$image" sh -c "$inside" release-ready "$@" || status=$?