From ed95374957b62b49a8d94863b5b4d821973a8d85 Mon Sep 17 00:00:00 2001 From: Yaraslau Tamashevich Date: Sat, 28 Nov 2026 18:01:46 +0100 Subject: [PATCH 1/4] Make the project a dimension of the warm-up publish.yml named pace in its resolve, warm and publish stages. Each project in warm-targets.json now declares its repo, the path its own CI checks it out at, and how it is warmed, and the workflow loops over projects: one warm job per group of each project warmed in CI, and an assemble step over the groups of every project. Seed images are cpp-ci-seed--, which for pace is the name they already had, and the stores stay at /.cow in the image. The docker run of a warm job moves into scripts/run-warm-container.sh so that a warm-up elsewhere can share it, pace's CPM bootstrap prefetch into a per-project hook, and its fallback-version check into the manifest. Seed images are built by scripts/build-seed-image.sh, which labels them with the toolchain fingerprint of the image they were warmed under; the assemble step warns when a seed's differs from the image's. A seed that is byte-identical to the previous one is still the previous image's layer, relabelled. warm-project.sh configures every preset from an empty build directory, since a reused checkout would otherwise keep the cache variables of an earlier configure. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01No1NzndCGtpCccptcyk6Tb --- .github/workflows/publish.yml | 292 +++++++++++++++++-------------- scripts/build-seed-image.sh | 56 ++++++ scripts/fetch-previous-seed.sh | 27 +++ scripts/projects/pace/prepare.sh | 39 +++++ scripts/run-warm-container.sh | 97 ++++++++++ scripts/toolchain-fingerprint.sh | 27 +++ scripts/warm-project.sh | 26 ++- warm-targets.json | 6 +- 8 files changed, 427 insertions(+), 143 deletions(-) create mode 100755 scripts/build-seed-image.sh create mode 100755 scripts/fetch-previous-seed.sh create mode 100755 scripts/projects/pace/prepare.sh create mode 100755 scripts/run-warm-container.sh create mode 100755 scripts/toolchain-fingerprint.sh diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index ec44737..dd7f27f 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -17,18 +17,8 @@ concurrency: env: IMAGE: ghcr.io/lastrada-software/cpp-ci - SEED_IMAGE_PREFIX: ghcr.io/lastrada-software/cpp-ci-seed-pace - # Where pace's own CI checks pace out: its $GITHUB_WORKSPACE on a hosted - # runner. The warm-up builds at exactly this path, because two things in a - # pace compile depend on it and pace-cc cannot map either away: - # - tests/CheckoutPaths.hpp spells the checkout path, and 21 gcc-release - # test units include it; - # - CMake names the object of a source outside its target's directory (zstd, - # lz4, catch2's main) by a path relative to the top-level source tree only - # when the source is inside it, and by its absolute path otherwise. pace CI - # keeps CPM's source cache inside the checkout, so the warm-up must too. - # Warmed elsewhere, those 43 units missed on every CI run. - PACE_CI_WORKSPACE: /home/runner/work/pace/pace + # A seed image is --. + SEED_IMAGE_PREFIX: ghcr.io/lastrada-software/cpp-ci-seed jobs: # Resolve immutable inputs and the warm-up groups. Pull requests build and @@ -39,7 +29,8 @@ jobs: base_image: ${{ steps.inputs.outputs.base_image }} pace_ref: ${{ steps.inputs.outputs.pace_ref }} publish: ${{ steps.inputs.outputs.publish }} - groups: ${{ steps.inputs.outputs.groups }} + warm_matrix: ${{ steps.inputs.outputs.warm_matrix }} + seeds: ${{ steps.inputs.outputs.seeds }} steps: - uses: actions/checkout@v4 @@ -60,13 +51,26 @@ jobs: echo "attempt $attempt failed:"; printf '%s\n' "$out"; sleep 15 done test -n "$base_digest" - pace_ref="$(git ls-remote https://github.com/LASTRADA-Software/pace.git refs/heads/master | awk '{print $1}')" - test -n "$pace_ref" - groups="$(jq -c '[.projects[] | select(.name=="pace") | .groups[].name]' warm-targets.json)" + # The commit each project that is warmed here is built at: its + # default branch's head. + refs='{}' + while IFS=$'\t' read -r name repo; do + ref="$(git ls-remote "https://github.com/${repo}.git" HEAD | awk '{print $1}')" + test -n "$ref" + refs="$(jq -c --arg n "$name" --arg r "$ref" '.[$n] = $r' <<<"$refs")" + done < <(jq -r '.projects[] | select(.warm=="ci") | [.name, .repo] | @tsv' warm-targets.json) + # pace-cc and pace-kv in the image are built from pace's head too. + pace_ref="$(jq -er '.pace' <<<"$refs")" + # One warm job per group of each project warmed in CI. + warm_matrix="$(jq -c --argjson refs "$refs" '[.projects[] | select(.warm=="ci") | . as $p | .groups[] + | {project: $p.name, group: .name, repo: $p.repo, ci_workspace: $p.ci_workspace, ref: $refs[$p.name]}]' warm-targets.json)" + # Every group of every project is assembled into the image, whoever warms it. + seeds="$(jq -c '[.projects[] | . as $p | .groups[] | {project: $p.name, group: .name, warm: $p.warm}]' warm-targets.json)" { echo "base_image=ubuntu:24.04@${base_digest}" echo "pace_ref=${pace_ref}" - echo "groups=${groups}" + echo "warm_matrix=${warm_matrix}" + echo "seeds=${seeds}" if [ "${GITHUB_EVENT_NAME}" = "pull_request" ]; then echo "publish=false" else @@ -180,10 +184,12 @@ jobs: shell: bash run: docker push "${IMAGE}:${GITHUB_SHA}-base" - # Warm one group of pace's presets. The groups run in parallel, each into its own - # store. Each job starts from the previous published seed for its group, so only - # translation units whose inputs changed are recompiled. Any failing build fails - # its group, and no candidate seed is pushed for it. + # Warm one group of one project's presets, for each project warmed in CI (a + # project with "warm": "local" is warmed by scripts/warm-local.sh instead). The + # groups run in parallel, each into its own store. Each job starts from the + # previous published seed for its group, so only translation units whose inputs + # changed are recompiled. Any failing build fails its group, and no candidate + # seed is pushed for it. warm: needs: [resolve, build] if: needs.resolve.outputs.publish == 'true' @@ -192,10 +198,25 @@ jobs: strategy: fail-fast: true matrix: - group: ${{ fromJSON(needs.resolve.outputs.groups) }} + include: ${{ fromJSON(needs.resolve.outputs.warm_matrix) }} permissions: contents: read packages: write + env: + PROJECT: ${{ matrix.project }} + GROUP: ${{ matrix.group }} + # Where the project's own CI checks it out: its $GITHUB_WORKSPACE on a + # hosted runner. The warm-up builds at exactly this path, because things + # in a build depend on it and pace-cc cannot map them away. For pace: + # - tests/CheckoutPaths.hpp spells the checkout path, and 21 gcc-release + # test units include it; + # - CMake names the object of a source outside its target's directory + # (zstd, lz4, catch2's main) by a path relative to the top-level source + # tree only when the source is inside it, and by its absolute path + # otherwise. pace CI keeps CPM's source cache inside the checkout, so + # the warm-up must too. + # Warmed elsewhere, those 43 units missed on every CI run. + CI_WORKSPACE: ${{ matrix.ci_workspace }} steps: - uses: actions/checkout@v4 with: @@ -205,12 +226,12 @@ jobs: # host, so the cache keys see the same layout as the warm-up. - uses: actions/checkout@v4 with: - repository: LASTRADA-Software/pace - ref: ${{ needs.resolve.outputs.pace_ref }} - path: pace-src - # Full history, as pace's CI checks out: the version comes from - # `git describe`, and Pace/Core/Version.hpp spells its triple, which - # some 60 units include. A shallow clone falls back to 0.0.0, so + repository: ${{ matrix.repo }} + ref: ${{ matrix.ref }} + path: project-src + # Full history, as the projects' CI checks out: pace takes its version + # from `git describe`, and Pace/Core/Version.hpp spells its triple, + # which some 60 units include. A shallow clone falls back to 0.0.0, so # those units were warmed under keys CI never asks for. The warm-up # container runs as root on this runner-owned checkout, so it also # marks the checkout a git safe.directory; without that, git refuses @@ -218,14 +239,14 @@ jobs: fetch-depth: 0 # actions/checkout only writes under $GITHUB_WORKSPACE, so move the - # checkout to the path pace's CI builds at (see PACE_CI_WORKSPACE). - - name: Place pace at its CI path + # checkout to the path the project's CI builds at (see CI_WORKSPACE). + - name: Place the project at its CI path shell: bash run: | set -euo pipefail - test ! -e "$PACE_CI_WORKSPACE" || { echo "::error::$PACE_CI_WORKSPACE already exists"; exit 1; } - mkdir -p "$(dirname "$PACE_CI_WORKSPACE")" - mv "$GITHUB_WORKSPACE/pace-src" "$PACE_CI_WORKSPACE" + test ! -e "$CI_WORKSPACE" || { echo "::error::$CI_WORKSPACE already exists"; exit 1; } + mkdir -p "$(dirname "$CI_WORKSPACE")" + mv "$GITHUB_WORKSPACE/project-src" "$CI_WORKSPACE" - uses: docker/login-action@v3 with: @@ -237,128 +258,81 @@ jobs: shell: bash run: docker pull "${IMAGE}:${GITHUB_SHA}-base" - # pace's configure downloads the CPM bootstrap from a GitHub release asset, - # and that endpoint returns 5xx often enough to fail whole groups. Fetch it - # here instead, into the CPM source cache inside the checkout, where pace's - # CI keeps it too (CPM_SOURCE_CACHE); CMake skips its own download when the file is - # already present with the expected hash. If the release asset stays - # unavailable, rebuild it from the git tag the way CPM's release workflow - # does (substitute the version). pace's pinned hash is checked either way. - - name: Prefetch CPM bootstrap + # A project's own preparation of its checkout (scripts/projects//prepare.sh), + # when it has one. pace's fetches the CPM bootstrap. + - name: Prepare the project checkout shell: bash run: | set -euo pipefail - cpm="$PACE_CI_WORKSPACE/cmake/CPM.cmake" - version="$(sed -n 's/^set(CPM_DOWNLOAD_VERSION \([^)]*\))/\1/p' "$cpm")" - hash="$(sed -n 's/^set(CPM_HASH_SUM "\([0-9a-f]*\)")/\1/p' "$cpm")" - test -n "$version" && test -n "$hash" - dest="$PACE_CI_WORKSPACE/.cache/CPM/cpm/CPM_${version}.cmake" - mkdir -p "$(dirname "$dest")" - verify() { echo "${hash} ${dest}" | sha256sum -c --status -; } - if curl -fsSL --retry 4 --retry-all-errors --retry-delay 10 --retry-max-time 180 \ - -o "$dest" \ - "https://github.com/cpm-cmake/CPM.cmake/releases/download/v${version}/CPM.cmake" \ - && verify; then - echo "CPM ${version} bootstrap fetched from the release asset" + hook="cpp-ci-image/scripts/projects/${PROJECT}/prepare.sh" + if [ -x "$hook" ]; then + WORKSPACE="$CI_WORKSPACE" "$hook" else - echo "::warning::CPM release asset unavailable; rebuilding it from tag v${version}" - src="$RUNNER_TEMP/cpm-src" - rm -rf "$src" - for attempt in 1 2 3; do - git -c advice.detachedHead=false clone --quiet --depth 1 --branch "v${version}" \ - https://github.com/cpm-cmake/CPM.cmake.git "$src" && break - rm -rf "$src"; sleep $((attempt * 15)) - done - sed "s/1\.0\.0-development-version/${version}/g" "$src/cmake/CPM.cmake" > "$dest" - verify || { echo "::error::rebuilt CPM bootstrap does not match pace's hash"; exit 1; } + echo "no preparation for ${PROJECT}" fi - name: Fetch previous seed for this group id: previous shell: bash - env: - GROUP: ${{ matrix.group }} run: | set -euo pipefail work="$RUNNER_TEMP/warm" mkdir -p "$work" - prev="${SEED_IMAGE_PREFIX}-${GROUP}:latest" - if docker pull "$prev" >/dev/null 2>&1; then - cid="$(docker create "$prev" /cache-copy)" - docker cp "$cid:/cache.cow" "$work/previous.cow" - docker rm "$cid" >/dev/null - echo "found previous seed: $(stat -c %s "$work/previous.cow") bytes" + if cpp-ci-image/scripts/fetch-previous-seed.sh "${SEED_IMAGE_PREFIX}-${PROJECT}-${GROUP}:latest" "$work/previous.cow"; then echo "seed=$work/previous.cow" >> "$GITHUB_OUTPUT" else - echo "no previous seed for $GROUP; starting empty" + echo "no previous seed for ${PROJECT}/${GROUP}; starting empty" echo "seed=" >> "$GITHUB_OUTPUT" fi - - name: Warm pace group + - name: Warm the group shell: bash env: - GROUP: ${{ matrix.group }} SEED: ${{ steps.previous.outputs.seed }} run: | set -euo pipefail - work="$RUNNER_TEMP/warm" - src="$PACE_CI_WORKSPACE" - manifest="cpp-ci-image/warm-targets.json" - jq -r --arg g "$GROUP" '.projects[] | select(.name=="pace") | .groups[] | select(.name==$g) - | .presets[] | [.name, (.configure_args | join(" "))] | @tsv' "$manifest" > "$work/presets.tsv" - cat "$work/presets.tsv" seed_args=() if [ -n "$SEED" ]; then - cp "$SEED" "$work/seed-in.cow" - seed_args=(-e SEED_STORE=/warm/seed-in.cow) + seed_args=(--seed "$SEED") fi - mkdir -p "$work/out" - docker run --rm \ - -v "$src:$src" \ - -v "$work:/warm" \ - -v "$GITHUB_WORKSPACE/cpp-ci-image/scripts:/scripts:ro" \ - -e WORKSPACE="$src" \ - -e CPM_SOURCE_CACHE="$src/.cache/CPM" \ - -e GIT_CONFIG_COUNT=1 \ - -e GIT_CONFIG_KEY_0=safe.directory \ - -e GIT_CONFIG_VALUE_0="$src" \ - -e STORE=/warm/out/cache.cow \ - -e PRESETS_FILE=/warm/presets.tsv \ - -e WARM_MAX_DISK="${WARM_MAX_DISK:-4g}" \ - "${seed_args[@]}" \ - -w "$src" \ - "${IMAGE}:${GITHUB_SHA}-base" \ - bash /scripts/warm-project.sh - ls -l "$work/out/cache.cow" + cpp-ci-image/scripts/run-warm-container.sh \ + --image "${IMAGE}:${GITHUB_SHA}-base" \ + --project "$PROJECT" --group "$GROUP" \ + --src "$CI_WORKSPACE" --work "$RUNNER_TEMP/warm" \ + "${seed_args[@]}" # When every compile hit, the store comes out byte-identical to the previous - # seed. Re-tag the previous seed image instead of building a new one, so its - # layer digest stays the same and nothing is uploaded again downstream. + # seed. The seed image is then the previous one plus labels, so its layer + # digest stays the same and nothing is uploaded again downstream. - name: Push candidate seed for this group shell: bash env: - GROUP: ${{ matrix.group }} SEED: ${{ steps.previous.outputs.seed }} + PROJECT_REF: ${{ matrix.ref }} run: | set -euo pipefail - img="${SEED_IMAGE_PREFIX}-${GROUP}" - out="$RUNNER_TEMP/warm/out/cache.cow" - if [ -n "$SEED" ] && cmp -s "$SEED" "$out"; then - echo "store unchanged; re-tagging the previous seed" - docker tag "${img}:latest" "${img}:${GITHUB_SHA}" - else - ctx="$RUNNER_TEMP/seed-ctx-$GROUP" - mkdir -p "$ctx" - cp "$out" "$ctx/cache.cow" - printf 'FROM scratch\nCOPY cache.cow /cache.cow\n' > "$ctx/Dockerfile" - docker build -t "${img}:${GITHUB_SHA}" "$ctx" + img="${SEED_IMAGE_PREFIX}-${PROJECT}-${GROUP}" + previous=() + if [ -n "$SEED" ]; then + previous=(--previous "$SEED" --previous-image "${img}:latest") fi + cpp-ci-image/scripts/build-seed-image.sh \ + --tag "${img}:${GITHUB_SHA}" \ + --store "$RUNNER_TEMP/warm/out/cache.cow" \ + --base-image "${IMAGE}:${GITHUB_SHA}-base" \ + --project-ref "$PROJECT_REF" \ + "${previous[@]}" docker push "${img}:${GITHUB_SHA}" # Layer the group seeds on top of the -base image, smoke-test them with that # image's pace-kv, and push. Runs only after every warm-up group succeeded, so a # failed group keeps the previous image, with its previous seeds, as `latest`. # + # A seed of a project warmed locally is the latest one its warm-up pushed. It + # is optional: a group with none yet, or one that this image's pace-kv cannot + # open, is left out with a warning, and a seed warmed under another toolchain + # than this image's draws a warning too, since it will not hit. + # # The base layers are already in the registry, and each group is its own layer, # so a push uploads only the groups whose store changed. buildkit pushes straight # to the registry: a layer the registry already holds is neither downloaded from @@ -370,6 +344,8 @@ jobs: contents: read packages: write steps: + - uses: actions/checkout@v4 + - uses: docker/setup-buildx-action@v3 - uses: docker/login-action@v3 @@ -381,39 +357,70 @@ jobs: - name: Assemble seed context from candidate group seeds shell: bash env: - GROUPS: ${{ needs.resolve.outputs.groups }} + SEEDS: ${{ needs.resolve.outputs.seeds }} run: | set -euo pipefail ctx="$RUNNER_TEMP/seed-ctx" - mkdir -p "$ctx/pace" + mkdir -p "$ctx" { echo 'ARG BASE' echo 'FROM ${BASE}' } > "$ctx/Dockerfile" - for group in $(jq -r '.[]' <<<"$GROUPS"); do - img="${SEED_IMAGE_PREFIX}-${group}:${GITHUB_SHA}" - docker pull "$img" - cid="$(docker create "$img" /cache-copy)" - docker cp "$cid:/cache.cow" "$ctx/pace/${group}.cow" + : > "$RUNNER_TEMP/local-seeds" + listing="$(scripts/toolchain-fingerprint.sh "${IMAGE}:${GITHUB_SHA}-base")" + echo "$listing" + base_fp="$(sed -n 's/^toolchain-sha256: //p' <<<"$listing")" + test -n "$base_fp" + while IFS=$'\t' read -r project group warm; do + img="${SEED_IMAGE_PREFIX}-${project}-${group}" + if [ "$warm" = "ci" ]; then + tag="$GITHUB_SHA" + docker pull "${img}:${tag}" + else + tag="latest" + if ! err="$(docker pull "${img}:${tag}" 2>&1 >/dev/null)"; then + echo "::warning::no ${project}/${group} seed available (${err}); assembling without it" + continue + fi + echo "${project}/${group}" >> "$RUNNER_TEMP/local-seeds" + fi + seed_fp="$(docker image inspect --format '{{index .Config.Labels "org.lastrada.seed.toolchain"}}' "${img}:${tag}")" + if [ -z "$seed_fp" ]; then + echo "::warning::${project}/${group} seed carries no toolchain label; it may not hit under this image's compilers" + elif [ "$seed_fp" != "$base_fp" ]; then + echo "::warning::${project}/${group} seed was warmed under toolchain ${seed_fp}, this image has ${base_fp}; it will not hit until it is warmed again" + fi + mkdir -p "$ctx/${project}" + cid="$(docker create "${img}:${tag}" /cache-copy)" + docker cp "$cid:/cache.cow" "$ctx/${project}/${group}.cow" docker rm "$cid" >/dev/null - docker rmi "$img" >/dev/null - echo "COPY pace/${group}.cow /opt/pace/seed-cache/pace/${group}.cow" >> "$ctx/Dockerfile" - done - ls -l "$ctx/pace" + docker rmi "${img}:${tag}" >/dev/null + echo "COPY ${project}/${group}.cow /opt/pace/seed-cache/${project}/${group}.cow" >> "$ctx/Dockerfile" + done < <(jq -r '.[] | [.project, .group, .warm] | @tsv' <<<"$SEEDS") + ls -lR "$ctx" cat "$ctx/Dockerfile" # Each seed must open with the pace-kv it ships beside, which is the base - # image's. Open a copy, so the shipped store is not modified. + # image's. Open a copy, so the shipped store is not modified. A store of a + # locally warmed project that does not open (a storage format change since + # it was warmed) is rejected and dropped from the image; one of a project + # warmed here fails the publish. - name: Smoke test seeds shell: bash run: | set -euo pipefail - docker run --rm -v "$RUNNER_TEMP/seed-ctx/pace:/seeds:ro" "${IMAGE}:${GITHUB_SHA}-base" \ + ctx="$RUNNER_TEMP/seed-ctx" + out="$RUNNER_TEMP/seed-rejected" + mkdir -p "$out" + : > "$out/rejected" + docker run --rm -v "$ctx:/seeds:ro" -v "$out:/out" \ + -v "$RUNNER_TEMP/local-seeds:/local-seeds:ro" "${IMAGE}:${GITHUB_SHA}-base" \ bash -euo pipefail -c ' shopt -s nullglob - stores=(/seeds/*.cow) + stores=(/seeds/*/*.cow) test "${#stores[@]}" -gt 0 for store in "${stores[@]}"; do + rel="${store#/seeds/}" cp "$store" /tmp/seed-check.cow port=6675 pace-kv --bind=127.0.0.1 --port=$port --storage=/tmp/seed-check.cow \ @@ -423,11 +430,23 @@ jobs: if (exec 3<>/dev/tcp/127.0.0.1/$port) 2>/dev/null; then break; fi sleep 0.2 done - kill -0 "$KV" || { echo "seed $store did not open"; exit 1; } - kill -TERM "$KV"; wait "$KV" || true + if kill -0 "$KV"; then + kill -TERM "$KV"; wait "$KV" || true + elif grep -qx "${rel%.cow}" /local-seeds; then + echo "::warning::seed $rel did not open with this image'"'"'s pace-kv; leaving it out" + echo "$rel" >> /out/rejected + else + echo "seed $store did not open"; exit 1 + fi rm -f /tmp/seed-check.cow done ' + while read -r rel; do + [ -n "$rel" ] || continue + rm "$ctx/$rel" + sed -i "\\#^COPY ${rel} #d" "$ctx/Dockerfile" + done < "$out/rejected" + cat "$ctx/Dockerfile" - uses: docker/build-push-action@v6 with: @@ -446,17 +465,18 @@ jobs: cache-to: type=inline # Promote on master only: the published group seeds become the starting - # point for the next warm-up. Branch runs never move `latest`. + # point for the next warm-up. Branch runs never move `latest`. Seeds of a + # locally warmed project are not promoted here; their warm-up owns them. - name: Promote seeds and image to latest if: github.ref == 'refs/heads/master' shell: bash env: - GROUPS: ${{ needs.resolve.outputs.groups }} + SEEDS: ${{ needs.resolve.outputs.seeds }} run: | set -euo pipefail docker buildx imagetools create --tag "${IMAGE}:latest" "${IMAGE}:${GITHUB_SHA}" - for group in $(jq -r '.[]' <<<"$GROUPS"); do + while IFS=$'\t' read -r project group; do docker buildx imagetools create \ - --tag "${SEED_IMAGE_PREFIX}-${group}:latest" \ - "${SEED_IMAGE_PREFIX}-${group}:${GITHUB_SHA}" - done + --tag "${SEED_IMAGE_PREFIX}-${project}-${group}:latest" \ + "${SEED_IMAGE_PREFIX}-${project}-${group}:${GITHUB_SHA}" + done < <(jq -r '.[] | select(.warm=="ci") | [.project, .group] | @tsv' <<<"$SEEDS") diff --git a/scripts/build-seed-image.sh b/scripts/build-seed-image.sh new file mode 100755 index 0000000..13d35fd --- /dev/null +++ b/scripts/build-seed-image.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# Build the seed image for one group's store: a single-layer image holding +# /cache.cow, labelled with the toolchain it was warmed under. +# +# Usage: +# build-seed-image.sh --tag NAME:TAG --store FILE --base-image IMAGE \ +# [--previous FILE --previous-image NAME:TAG] [--project-ref SHA] +# +# When --previous is byte-identical to --store, the image is the previous seed +# image plus new labels, so its layer digest stays the same and nothing is +# uploaded again for it. LABEL adds no filesystem layer. +# +# Labels: +# org.lastrada.seed.toolchain toolchain-fingerprint.sh's hash for --base-image +# org.lastrada.seed.base-image the digest (else the id) of --base-image +# org.lastrada.seed.project-ref --project-ref, when given +set -euo pipefail + +HERE="$(cd "$(dirname "$0")" && pwd)" +tag="" store="" base="" previous="" previous_image="" ref="" +while [ $# -gt 0 ]; do + case "$1" in + --tag) tag="$2" ;; + --store) store="$2" ;; + --base-image) base="$2" ;; + --previous) previous="$2" ;; + --previous-image) previous_image="$2" ;; + --project-ref) ref="$2" ;; + *) echo "unknown option: $1" >&2; exit 2 ;; + esac + shift 2 +done +for v in tag store base; do + [ -n "${!v}" ] || { echo "--${v//_/-} is required" >&2; exit 2; } +done + +listing="$("$HERE/toolchain-fingerprint.sh" "$base")" +printf '%s\n' "$listing" >&2 +fingerprint="$(sed -n 's/^toolchain-sha256: //p' <<<"$listing")" +test -n "$fingerprint" +base_id="$(docker image inspect --format '{{if .RepoDigests}}{{index .RepoDigests 0}}{{else}}{{.Id}}{{end}}' "$base")" + +ctx="$(mktemp -d)" +trap 'rm -rf "$ctx"' EXIT +labels="LABEL org.lastrada.seed.toolchain=\"$fingerprint\" org.lastrada.seed.base-image=\"$base_id\"" +if [ -n "$ref" ]; then + labels+=" org.lastrada.seed.project-ref=\"$ref\"" +fi +if [ -n "$previous" ] && [ -n "$previous_image" ] && cmp -s "$previous" "$store"; then + echo "store unchanged; relabelling the previous seed" + printf 'FROM %s\n%s\n' "$previous_image" "$labels" > "$ctx/Dockerfile" +else + cp "$store" "$ctx/cache.cow" + printf 'FROM scratch\nCOPY cache.cow /cache.cow\n%s\n' "$labels" > "$ctx/Dockerfile" +fi +docker build -t "$tag" "$ctx" diff --git a/scripts/fetch-previous-seed.sh b/scripts/fetch-previous-seed.sh new file mode 100755 index 0000000..c460513 --- /dev/null +++ b/scripts/fetch-previous-seed.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# Extract the store from a published seed image. +# +# Usage: fetch-previous-seed.sh [--strict] SEED_IMAGE DEST +# +# Exit status 0 with DEST written: found. Exit status 1 with nothing written: no +# such image, so the warm-up starts empty. By default any pull failure counts as +# "no such image". With --strict only a registry answer that says the image does +# not exist does; an authentication or network failure fails the script (exit +# 2), so a warm-up that would silently start from nothing stops instead. +set -euo pipefail + +strict="" +if [ "${1:-}" = "--strict" ]; then strict=1; shift; fi +image="$1" dest="$2" + +if ! err="$(docker pull "$image" 2>&1 >/dev/null)"; then + if [ -n "$strict" ] && ! grep -qiE 'manifest unknown|not found|name unknown' <<<"$err"; then + echo "cannot pull $image: $err" >&2 + exit 2 + fi + exit 1 +fi +cid="$(docker create "$image" /cache-copy)" +docker cp "$cid:/cache.cow" "$dest" +docker rm "$cid" >/dev/null +echo "found previous seed $image: $(stat -c %s "$dest") bytes" diff --git a/scripts/projects/pace/prepare.sh b/scripts/projects/pace/prepare.sh new file mode 100755 index 0000000..891839e --- /dev/null +++ b/scripts/projects/pace/prepare.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +# Prepare pace's checkout before its warm-up container runs (host side). +# +# Usage: prepare.sh with WORKSPACE set to the checkout directory. +# +# pace's configure downloads the CPM bootstrap from a GitHub release asset, and +# that endpoint returns 5xx often enough to fail whole groups. Fetch it here +# instead, into the CPM source cache inside the checkout, where pace's CI keeps +# it too (CPM_SOURCE_CACHE); CMake skips its own download when the file is +# already present with the expected hash. If the release asset stays +# unavailable, rebuild it from the git tag the way CPM's release workflow does +# (substitute the version). pace's pinned hash is checked either way. +set -euo pipefail + +: "${WORKSPACE:?WORKSPACE is required}" +cpm="$WORKSPACE/cmake/CPM.cmake" +version="$(sed -n 's/^set(CPM_DOWNLOAD_VERSION \([^)]*\))/\1/p' "$cpm")" +hash="$(sed -n 's/^set(CPM_HASH_SUM "\([0-9a-f]*\)")/\1/p' "$cpm")" +test -n "$version" && test -n "$hash" +dest="$WORKSPACE/.cache/CPM/cpm/CPM_${version}.cmake" +mkdir -p "$(dirname "$dest")" +verify() { echo "${hash} ${dest}" | sha256sum -c --status -; } +if curl -fsSL --retry 4 --retry-all-errors --retry-delay 10 --retry-max-time 180 \ + -o "$dest" \ + "https://github.com/cpm-cmake/CPM.cmake/releases/download/v${version}/CPM.cmake" \ + && verify; then + echo "CPM ${version} bootstrap fetched from the release asset" +else + echo "::warning::CPM release asset unavailable; rebuilding it from tag v${version}" + src="$(mktemp -d)" + trap 'rm -rf "$src"' EXIT + for attempt in 1 2 3; do + git -c advice.detachedHead=false clone --quiet --depth 1 --branch "v${version}" \ + https://github.com/cpm-cmake/CPM.cmake.git "$src/cpm" && break + rm -rf "$src/cpm"; sleep $((attempt * 15)) + done + sed "s/1\.0\.0-development-version/${version}/g" "$src/cpm/cmake/CPM.cmake" > "$dest" + verify || { echo "::error::rebuilt CPM bootstrap does not match pace's hash"; exit 1; } +fi diff --git a/scripts/run-warm-container.sh b/scripts/run-warm-container.sh new file mode 100755 index 0000000..522bb15 --- /dev/null +++ b/scripts/run-warm-container.sh @@ -0,0 +1,97 @@ +#!/usr/bin/env bash +# Run scripts/warm-project.sh for one group of one project, inside the cpp-ci +# image. The publish workflow's warm jobs and scripts/warm-local.sh both go +# through here, so a warm-up on a developer machine and one on a runner use the +# same mounts, environment and preset list. +# +# The checkout is mounted at the project's ci_workspace (warm-targets.json), the +# path the project's own CI builds at: cache keys see that layout, and a few +# compiles embed it. On a runner the checkout already sits there. +# +# Usage: +# run-warm-container.sh --image IMAGE --project NAME --group GROUP \ +# --src DIR --work DIR [--seed FILE] [--jobs N] [--user UID:GID] \ +# [--manifest FILE] +# +# --src host checkout of the project (mounted at its ci_workspace) +# --work host scratch directory: presets.tsv and the previous seed are put +# here, and the finished store comes out as /out/cache.cow +# --seed previous store to start from (copied into --work) +# --user run the container as this user instead of root, so files it leaves +# in --src and --work belong to the caller +set -euo pipefail + +HERE="$(cd "$(dirname "$0")" && pwd)" +manifest="$HERE/../warm-targets.json" +image="" project="" group="" src="" work="" seed="" jobs="" user="" +while [ $# -gt 0 ]; do + case "$1" in + --image) image="$2" ;; + --project) project="$2" ;; + --group) group="$2" ;; + --src) src="$2" ;; + --work) work="$2" ;; + --seed) seed="$2" ;; + --jobs) jobs="$2" ;; + --user) user="$2" ;; + --manifest) manifest="$2" ;; + *) echo "unknown option: $1" >&2; exit 2 ;; + esac + shift 2 +done +for v in image project group src work; do + [ -n "${!v}" ] || { echo "--${v//_/-} is required" >&2; exit 2; } +done +case "$src$work" in /*/*) ;; *) echo "--src and --work must be absolute" >&2; exit 2 ;; esac + +project_field() { + jq -r --arg p "$project" ".projects[] | select(.name==\$p) | $1" "$manifest" +} +ci_workspace="$(project_field '.ci_workspace')" +cpm_cache="$(project_field '.cpm_source_cache // ""')" +forbid="$(project_field '.forbid_configure_pattern // ""')" +[ -n "$ci_workspace" ] || { echo "project $project has no ci_workspace in $manifest" >&2; exit 1; } + +mkdir -p "$work/out" +rm -f "$work/out/cache.cow" +jq -r --arg p "$project" --arg g "$group" '.projects[] | select(.name==$p) | .groups[] | select(.name==$g) + | .presets[] | [.name, (.configure_args | join(" "))] | @tsv' "$manifest" > "$work/presets.tsv" +test -s "$work/presets.tsv" || { echo "no presets for $project/$group in $manifest" >&2; exit 1; } +cat "$work/presets.tsv" + +env_args=() +if [ -n "$cpm_cache" ]; then + env_args+=(-e "CPM_SOURCE_CACHE=$ci_workspace/$cpm_cache") +fi +if [ -n "$forbid" ]; then + env_args+=(-e "WARM_FORBID_CONFIGURE_PATTERN=$forbid") +fi +if [ -n "$jobs" ]; then + env_args+=(-e "WARM_JOBS=$jobs") +fi +if [ -n "$user" ]; then + # No passwd entry or writable home for the mapped user in the image. + env_args+=(--user "$user" -e HOME=/warm/home) + mkdir -p "$work/home" +fi +if [ -n "$seed" ]; then + cp "$seed" "$work/seed-in.cow" + env_args+=(-e SEED_STORE=/warm/seed-in.cow) +fi + +docker run --rm \ + -v "$src:$ci_workspace" \ + -v "$work:/warm" \ + -v "$HERE:/scripts:ro" \ + -e WORKSPACE="$ci_workspace" \ + -e GIT_CONFIG_COUNT=1 \ + -e GIT_CONFIG_KEY_0=safe.directory \ + -e GIT_CONFIG_VALUE_0="$ci_workspace" \ + -e STORE=/warm/out/cache.cow \ + -e PRESETS_FILE=/warm/presets.tsv \ + -e WARM_MAX_DISK="${WARM_MAX_DISK:-4g}" \ + "${env_args[@]}" \ + -w "$ci_workspace" \ + "$image" \ + bash /scripts/warm-project.sh +ls -l "$work/out/cache.cow" diff --git a/scripts/toolchain-fingerprint.sh b/scripts/toolchain-fingerprint.sh new file mode 100755 index 0000000..0329602 --- /dev/null +++ b/scripts/toolchain-fingerprint.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# Print the toolchain fingerprint of an image: what pace-cc folds into every +# cache key for each compiler a project builds with. A seed warmed under one +# fingerprint does not hit under another, so the fingerprint is recorded on +# every seed image and compared when the seeds are assembled into an image. +# +# Usage: +# toolchain-fingerprint.sh inside the image +# toolchain-fingerprint.sh IMAGE on the host: runs the same inside IMAGE +# +# One line per compiler, then a final line `toolchain-sha256: ` over them. +set -euo pipefail + +if [ $# -ge 1 ]; then + exec docker run --rm -v "$(cd "$(dirname "$0")" && pwd):/scripts:ro" "$1" bash /scripts/toolchain-fingerprint.sh +fi + +lines="" +for compiler in clang++-22 clang-22 g++-14 gcc-14 g++-15 gcc-15; do + if ! command -v "$compiler" >/dev/null 2>&1; then + lines+="$compiler: absent"$'\n' + continue + fi + lines+="$compiler: $(pace-cc --print-toolchain-fingerprint "$compiler")"$'\n' +done +printf '%s' "$lines" +printf 'toolchain-sha256: %s\n' "$(printf '%s' "$lines" | sha256sum | cut -d' ' -f1)" diff --git a/scripts/warm-project.sh b/scripts/warm-project.sh index f88bf8c..e481d5b 100755 --- a/scripts/warm-project.sh +++ b/scripts/warm-project.sh @@ -21,6 +21,11 @@ # SEED_STORE previous store to start from # WARM_MAX_DISK on-disk cap for the store (default 4g). The cap evicts the # least recently used entries, so a reused store stays bounded. +# WARM_JOBS parallel compile jobs (default: nproc) +# WARM_FORBID_CONFIGURE_PATTERN +# extended regex; a configure whose output matches it fails the +# warm-up (the project's marker for a build that cannot be +# keyed like its CI, such as a fallback version string) set -euo pipefail : "${WORKSPACE:?WORKSPACE is required}" @@ -28,6 +33,8 @@ set -euo pipefail : "${PRESETS_FILE:?PRESETS_FILE is required}" SEED_STORE="${SEED_STORE:-}" WARM_MAX_DISK="${WARM_MAX_DISK:-4g}" +WARM_JOBS="${WARM_JOBS:-$(nproc)}" +WARM_FORBID_CONFIGURE_PATTERN="${WARM_FORBID_CONFIGURE_PATTERN:-}" HERE="$(cd "$(dirname "$0")" && pwd)" PORT="${WARM_PORT:-6674}" LOGDIR="$(dirname "$STORE")" @@ -110,6 +117,13 @@ while IFS=$'\t' read -r -u 3 preset preset_args; do read -r -a extra_args <<<"${preset_args:-}" binary_rel="$(python3 "$HERE/resolve-binary-dir.py" "$WORKSPACE/CMakePresets.json" "$preset")" export PACE_BINARY_DIR="$WORKSPACE/$binary_rel" + # A reused checkout may hold a build tree configured with other flags, and + # CMake keeps the cache variables of the earlier configure. Configure from + # nothing, so the preset gets exactly its configure_args. + case "$PACE_BINARY_DIR" in + "$WORKSPACE"/?*) rm -rf "$PACE_BINARY_DIR" ;; + *) echo "::error::binary dir $PACE_BINARY_DIR is outside $WORKSPACE" >&2; exit 1 ;; + esac echo "::group::configure $preset" configure_log="$LOGDIR/configure-$preset.log" # Configure downloads dependencies (the CPM bootstrap, CPM packages) from @@ -152,15 +166,15 @@ while IFS=$'\t' read -r -u 3 preset preset_args; do fi cat "$configure_log" echo "::endgroup::" - # pace spells its version in a header many units include. A warm-up that - # could not read it from git (a shallow clone, or git refusing the checkout) - # caches every one of them under a key CI never computes. - if grep -q "\[Version\] string: 0\.0\.0-unknown" "$configure_log"; then - echo "::error::$preset configured with the fallback version 0.0.0-unknown; git describe did not answer in $WORKSPACE" >&2 + # A warm-up that configured in a state its CI never does (for pace, a + # version that fell back to 0.0.0-unknown because git could not describe the + # checkout) caches many units under keys CI never computes. + if [ -n "$WARM_FORBID_CONFIGURE_PATTERN" ] && grep -qE "$WARM_FORBID_CONFIGURE_PATTERN" "$configure_log"; then + echo "::error::$preset configure output matches /$WARM_FORBID_CONFIGURE_PATTERN/ in $WORKSPACE" >&2 exit 1 fi echo "::group::build $preset" - if ! cmake --build "$PACE_BINARY_DIR" -j"$(nproc)"; then + if ! cmake --build "$PACE_BINARY_DIR" -j"$WARM_JOBS"; then echo "::error::build failed for $preset; pace-kv log follows" >&2 cat "$LOGDIR/pace-kv.log" >&2 || true exit 1 diff --git a/warm-targets.json b/warm-targets.json index d0ab16f..ed1c86d 100644 --- a/warm-targets.json +++ b/warm-targets.json @@ -1,9 +1,13 @@ { - "_comment": "Presets warmed into the compile-cache seed. Only pace is onboarded; see README. Windows presets are out of scope (Linux-only image). Coverage presets are excluded: pace-cc binds coverage objects to the checkout root. Each group is warmed by its own parallel job into its own store; a group's store is one file of the seed (pace/.cow). A preset's configure_args must match the flags pace's .github/workflows/build.yml configures that preset with: a flag that changes a compile definition (PACE_ENABLE_TLS does, for every translation unit) changes every cache key, so a seed warmed without it never hits.", + "_comment": "Presets warmed into the compile-cache seed, per project. A project declares its repo, the path its own CI checks it out at (ci_workspace: the warm-up builds at exactly that path), and how it is warmed: \"ci\" runs in publish.yml's warm jobs, \"local\" is warmed on a developer machine by scripts/warm-local.sh and only assembled by publish.yml. cpm_source_cache is the CPM source cache, relative to the checkout, as the project's CI keeps it. forbid_configure_pattern (optional) is a regex that fails the warm-up when the configure output matches it. Windows presets are out of scope (Linux-only image). Coverage presets are excluded: pace-cc binds coverage objects to the checkout root. Each group is warmed into its own store; a group's store is one file of the seed (/.cow). A preset's configure_args must match the flags the project's CI configures that preset with, because a flag that changes a compile definition changes every cache key, so a seed warmed without it never hits.", "projects": [ { "name": "pace", "repo": "LASTRADA-Software/pace", + "ci_workspace": "/home/runner/work/pace/pace", + "warm": "ci", + "cpm_source_cache": ".cache/CPM", + "forbid_configure_pattern": "\\[Version\\] string: 0\\.0\\.0-unknown", "groups": [ { "name": "gcc", From fc8712fb36ef771109599843a093787396228533 Mon Sep 17 00:00:00 2001 From: Yaraslau Tamashevich Date: Sat, 28 Nov 2026 18:01:53 +0100 Subject: [PATCH 2/4] Onboard morph, warmed on a developer machine A full morph build does not fit a hosted runner, so morph is declared "warm": "local": publish.yml only assembles the seeds that scripts/warm-local.sh pushes. The script checks the project out at the manifest's CI path through a bind mount, starts from the previous seed, runs the same container as the CI warm jobs, and pushes only with --push, after every group has built. A user service and timer run it nightly at 00:30 UTC, ahead of the 02:23 UTC publish. morph's groups are the Linux presets of its linux-compilers and linux-sanitizers jobs, with the flags of those jobs' configure lines. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01No1NzndCGtpCccptcyk6Tb --- local/systemd/cpp-ci-warm-morph.service | 14 +++ local/systemd/cpp-ci-warm-morph.timer | 12 +++ scripts/warm-local.sh | 124 ++++++++++++++++++++++++ warm-targets.json | 90 ++++++++++++++++- 4 files changed, 237 insertions(+), 3 deletions(-) create mode 100644 local/systemd/cpp-ci-warm-morph.service create mode 100644 local/systemd/cpp-ci-warm-morph.timer create mode 100755 scripts/warm-local.sh diff --git a/local/systemd/cpp-ci-warm-morph.service b/local/systemd/cpp-ci-warm-morph.service new file mode 100644 index 0000000..0aa8bfb --- /dev/null +++ b/local/systemd/cpp-ci-warm-morph.service @@ -0,0 +1,14 @@ +[Unit] +Description=Warm morph's compile-cache seed and push it to the cpp-ci seed registry +Wants=network-online.target docker.service +After=network-online.target docker.service + +[Service] +Type=oneshot +# Assumes this repository is cloned at ~/repo/cpp-ci-image; point ExecStart at +# the real clone with `systemctl --user edit cpp-ci-warm-morph.service`. +ExecStart=%h/repo/cpp-ci-image/scripts/warm-local.sh morph --push +# A warm-up that starts from scratch takes hours. +TimeoutStartSec=infinity +Nice=10 +IOSchedulingClass=idle diff --git a/local/systemd/cpp-ci-warm-morph.timer b/local/systemd/cpp-ci-warm-morph.timer new file mode 100644 index 0000000..329262c --- /dev/null +++ b/local/systemd/cpp-ci-warm-morph.timer @@ -0,0 +1,12 @@ +[Unit] +Description=Nightly warm-up of morph's compile-cache seed + +[Timer] +# The publish workflow runs at 02:23 UTC and assembles the latest pushed morph +# seeds into the image; this finishes well before when it takes under two hours. +OnCalendar=*-*-* 00:30:00 UTC +# A night the machine was off is made up at the next boot or login. +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/scripts/warm-local.sh b/scripts/warm-local.sh new file mode 100755 index 0000000..f48b26b --- /dev/null +++ b/scripts/warm-local.sh @@ -0,0 +1,124 @@ +#!/usr/bin/env bash +# Warm a project's compile-cache seed on this machine. +# +# For projects whose warm-up does not fit a hosted runner ("warm": "local" in +# warm-targets.json). The publish workflow only assembles the seeds this pushes. +# +# Usage: warm-local.sh [options] PROJECT [GROUP...] +# +# --ref REF project commit, tag or branch (default: the remote's HEAD) +# --image IMAGE toolchain image (default: ghcr.io/lastrada-software/cpp-ci:latest) +# --jobs N parallel compile jobs (default: nproc) +# --checkout-dir DIR host directory for the checkout and scratch space +# (default: ${XDG_CACHE_HOME:-~/.cache}/cpp-ci-warm/PROJECT) +# --manifest FILE targets manifest (default: warm-targets.json beside scripts/) +# --no-pull use the local copy of the image +# --push push each seed as :latest and :. Without it +# the seed images are only built locally. +# +# Every group is warmed and its seed image built before anything is pushed, so a +# failing group leaves the registry as it was. Each group starts from the +# previous published seed. +set -euo pipefail + +HERE="$(cd "$(dirname "$0")" && pwd)" +manifest="$HERE/../warm-targets.json" +SEED_REGISTRY="${SEED_REGISTRY:-ghcr.io/lastrada-software/cpp-ci-seed}" +image="${CPP_CI_IMAGE:-ghcr.io/lastrada-software/cpp-ci:latest}" +ref="" jobs="$(nproc)" checkout_dir="" pull=1 push="" +args=() +while [ $# -gt 0 ]; do + case "$1" in + --ref) ref="$2"; shift ;; + --image) image="$2"; shift ;; + --jobs) jobs="$2"; shift ;; + --checkout-dir) checkout_dir="$2"; shift ;; + --manifest) manifest="$2"; shift ;; + --no-pull) pull="" ;; + --push) push=1 ;; + -h|--help) sed -n '2,/^set -/p' "$0" | sed '$d;s/^# \{0,1\}//'; exit 0 ;; + -*) echo "unknown option: $1" >&2; exit 2 ;; + *) args+=("$1") ;; + esac + shift +done +[ "${#args[@]}" -ge 1 ] || { echo "usage: warm-local.sh [options] PROJECT [GROUP...]" >&2; exit 2; } +project="${args[0]}" +groups=("${args[@]:1}") + +repo="$(jq -er --arg p "$project" '.projects[] | select(.name==$p) | .repo' "$manifest")" \ + || { echo "unknown project: $project" >&2; exit 2; } +if [ "${#groups[@]}" -eq 0 ]; then + mapfile -t groups < <(jq -r --arg p "$project" '.projects[] | select(.name==$p) | .groups[].name' "$manifest") +fi +for g in "${groups[@]}"; do + jq -e --arg p "$project" --arg g "$g" '.projects[] | select(.name==$p) | .groups[] | select(.name==$g)' \ + "$manifest" >/dev/null || { echo "project $project has no group $g" >&2; exit 2; } +done +checkout_dir="${checkout_dir:-${XDG_CACHE_HOME:-$HOME/.cache}/cpp-ci-warm/$project}" +src="$checkout_dir/src" +mkdir -p "$checkout_dir" +checkout_dir="$(cd "$checkout_dir" && pwd)" +src="$checkout_dir/src" + +# The project at one commit. A reused checkout keeps its ignored .cache (CPM's +# source cache) and nothing else untracked, so no build tree or stray file of an +# earlier run reaches this one. +url="https://github.com/$repo.git" +if [ ! -d "$src/.git" ]; then + git clone --quiet "$url" "$src" +fi +git -C "$src" fetch --quiet --tags --force origin '+refs/heads/*:refs/remotes/origin/*' +if [ -z "$ref" ]; then + sha="$(git ls-remote "$url" HEAD | awk '{print $1}')" +else + sha="$(git -C "$src" rev-parse --verify --quiet "origin/$ref^{commit}" || git -C "$src" rev-parse --verify "$ref^{commit}")" +fi +test -n "$sha" +git -C "$src" checkout --quiet --force --detach "$sha" +git -C "$src" clean -ffdxq -e .cache +test "$(git -C "$src" rev-parse HEAD)" = "$sha" +echo "$project at $sha ($src)" + +if [ -n "$pull" ]; then + docker pull "$image" +fi + +if [ -x "$HERE/projects/$project/prepare.sh" ]; then + WORKSPACE="$src" "$HERE/projects/$project/prepare.sh" +fi + +built=() +for group in "${groups[@]}"; do + echo "=== warming $project/$group ===" + seed_image="$SEED_REGISTRY-$project-$group" + work="$checkout_dir/work/$group" + rm -rf "$work" + mkdir -p "$work" + seed_args=() + previous=() + if "$HERE/fetch-previous-seed.sh" --strict "$seed_image:latest" "$work/previous.cow"; then + seed_args=(--seed "$work/previous.cow") + previous=(--previous "$work/previous.cow" --previous-image "$seed_image:latest") + else + status=$? + [ "$status" -eq 1 ] || exit "$status" + echo "no previous seed for $project/$group; starting empty" + fi + "$HERE/run-warm-container.sh" --manifest "$manifest" --image "$image" --project "$project" --group "$group" \ + --src "$src" --work "$work" --jobs "$jobs" --user "$(id -u):$(id -g)" \ + "${seed_args[@]}" + "$HERE/build-seed-image.sh" --tag "$seed_image:$sha" --store "$work/out/cache.cow" \ + --base-image "$image" --project-ref "$sha" "${previous[@]}" + built+=("$seed_image") +done + +for img in "${built[@]}"; do + if [ -n "$push" ]; then + docker tag "$img:$sha" "$img:latest" + docker push "$img:$sha" + docker push "$img:latest" + else + echo "built $img:$sha; with --push it would also push $img:latest" + fi +done diff --git a/warm-targets.json b/warm-targets.json index ed1c86d..f8183a2 100644 --- a/warm-targets.json +++ b/warm-targets.json @@ -1,5 +1,5 @@ { - "_comment": "Presets warmed into the compile-cache seed, per project. A project declares its repo, the path its own CI checks it out at (ci_workspace: the warm-up builds at exactly that path), and how it is warmed: \"ci\" runs in publish.yml's warm jobs, \"local\" is warmed on a developer machine by scripts/warm-local.sh and only assembled by publish.yml. cpm_source_cache is the CPM source cache, relative to the checkout, as the project's CI keeps it. forbid_configure_pattern (optional) is a regex that fails the warm-up when the configure output matches it. Windows presets are out of scope (Linux-only image). Coverage presets are excluded: pace-cc binds coverage objects to the checkout root. Each group is warmed into its own store; a group's store is one file of the seed (/.cow). A preset's configure_args must match the flags the project's CI configures that preset with, because a flag that changes a compile definition changes every cache key, so a seed warmed without it never hits.", + "_comment": "Presets warmed into the compile-cache seed, per project. A project declares its repo, the path its own CI checks it out at (ci_workspace: the warm-up builds at exactly that path), and how it is warmed: \"ci\" runs in publish.yml's warm jobs, \"local\" is warmed on a developer machine by scripts/warm-local.sh and only assembled by publish.yml. cpm_source_cache is the CPM source cache, relative to the checkout, as the project's CI keeps it. forbid_configure_pattern (optional) is a regex that fails the warm-up when the configure output matches it. Windows presets are out of scope (Linux-only image). Coverage presets are excluded: pace-cc binds coverage objects to the checkout root. Each group is warmed into its own store; a group's store is one file of the seed (/.cow). A preset's configure_args must match the flags the project's CI configures that preset with, because a flag that changes a compile definition changes every cache key, so a seed warmed without it never hits. The one deliberate difference is morph's gcc-release: its CI makes gcc-15 the default gcc with update-alternatives, which the image does not, so the same compiler is selected by flag.", "projects": [ { "name": "pace", @@ -82,11 +82,95 @@ ] } ] + }, + { + "name": "morph", + "repo": "LASTRADA-Software/morph", + "ci_workspace": "/home/runner/work/morph/morph", + "warm": "local", + "cpm_source_cache": ".cache/cpm", + "groups": [ + { + "name": "clang", + "presets": [ + { + "name": "clang-debug", + "configure_args": [ + "-DCMAKE_C_COMPILER=clang-22", + "-DCMAKE_CXX_COMPILER=clang++-22" + ] + }, + { + "name": "clang-release", + "configure_args": [ + "-DCMAKE_C_COMPILER=clang-22", + "-DCMAKE_CXX_COMPILER=clang++-22" + ] + }, + { + "name": "clang-shared-debug", + "configure_args": [ + "-DCMAKE_C_COMPILER=clang-22", + "-DCMAKE_CXX_COMPILER=clang++-22" + ] + } + ] + }, + { + "name": "gcc", + "presets": [ + { + "name": "gcc-release", + "configure_args": [ + "-DCMAKE_C_COMPILER=gcc-15", + "-DCMAKE_CXX_COMPILER=g++-15" + ] + } + ] + }, + { + "name": "sanitizers", + "presets": [ + { + "name": "clang-asan", + "configure_args": [ + "-DMORPH_LINK_JOBS=1", + "-DMORPH_BUILD_NET=ON", + "-DMORPH_BUILD_TUI=ON", + "-DMORPH_BUILD_OFFLINE_SQLITE=ON", + "-DCMAKE_C_COMPILER=clang-22", + "-DCMAKE_CXX_COMPILER=clang++-22" + ] + }, + { + "name": "clang-ubsan", + "configure_args": [ + "-DMORPH_LINK_JOBS=1", + "-DMORPH_BUILD_NET=ON", + "-DMORPH_BUILD_TUI=ON", + "-DMORPH_BUILD_OFFLINE_SQLITE=ON", + "-DCMAKE_C_COMPILER=clang-22", + "-DCMAKE_CXX_COMPILER=clang++-22" + ] + }, + { + "name": "clang-tsan", + "configure_args": [ + "-DMORPH_LINK_JOBS=1", + "-DMORPH_BUILD_NET=ON", + "-DMORPH_BUILD_TUI=ON", + "-DMORPH_BUILD_OFFLINE_SQLITE=ON", + "-DCMAKE_C_COMPILER=clang-22", + "-DCMAKE_CXX_COMPILER=clang++-22" + ] + } + ] + } + ] } ], "not_onboarded": { "Lightweight": "Test define embeds an absolute build-tree path (LightweightTest). Tracked in LASTRADA-Software/Lightweight#693.", - "formula-cpp": "No pace-cc integration; USE_COMPILER_CACHE is ignored. Tracked in LASTRADA-Software/formula-cpp#36.", - "morph": "Not yet measured." + "formula-cpp": "No pace-cc integration; USE_COMPILER_CACHE is ignored. Tracked in LASTRADA-Software/formula-cpp#36." } } From e6a3cb2ad8337b4bd06555764e3a1becddcc8f60 Mon Sep 17 00:00:00 2001 From: Yaraslau Tamashevich Date: Sat, 28 Nov 2026 18:01:53 +0100 Subject: [PATCH 3/4] Pin the compilers to the versions the seeds are keyed on pace-cc keys a cache entry on the compiler it fronts, so a nightly rebuild that picked up a newer clang or gcc point release would silently invalidate every seed, including those warmed on a developer machine. clang-22, clang-tidy-22, llvm-22, gcc-14 and gcc-15 are installed at exact versions, and the build fails when the installed version differs. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01No1NzndCGtpCccptcyk6Tb --- Dockerfile | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4c6b93d..d07d84a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,6 +25,21 @@ FROM ${BASE_IMAGE} AS toolchain ARG LLVM_VERSION=22 +# Exact package versions of the compilers. pace-cc keys every cache entry on the +# compiler it fronts, so a seed warmed against one patch release does not hit +# under another, and an unpinned nightly rebuild would pick up a new release and +# silently re-key every seed (including the ones warmed on a developer machine). +# apt keeps only the newest version of a package in its index: when upstream +# publishes a newer one, the build fails below with "Version ... was not found" +# instead of shipping a different compiler. To bump, read the new versions from +# `docker run --rm sh -c 'apt-get update -q && apt-cache policy clang-22 +# gcc-14 gcc-15'`, set them here, publish, and warm the locally warmed projects +# against the new image: the publish job warns about every seed whose toolchain +# label no longer matches the image's. +ARG CLANG_PKG_VERSION="1:22.1.8~++20260714014902+ca7933e47d3a-1~exp1~20260714135019.80" +ARG GCC14_PKG_VERSION="14.2.0-4ubuntu2~24.04.1" +ARG GCC15_PKG_VERSION="15.2.0-14ubuntu1~24~ppa1" + RUN printf 'Acquire::Retries "5";\n' > /etc/apt/apt.conf.d/80-retries \ && apt-get update \ && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ @@ -35,8 +50,8 @@ RUN printf 'Acquire::Retries "5";\n' > /etc/apt/apt.conf.d/80-retries \ curl \ file \ gawk \ - g++-14 \ - gcc-14 \ + g++-14="${GCC14_PKG_VERSION}" \ + gcc-14="${GCC14_PKG_VERSION}" \ git \ gpg \ iproute2 \ @@ -86,13 +101,17 @@ RUN printf 'Acquire::Retries "5";\n' > /etc/apt/apt.conf.d/80-retries \ && chmod +x llvm.sh \ && ./llvm.sh ${LLVM_VERSION} \ && apt-get install -y --no-install-recommends \ - clang-${LLVM_VERSION} \ - clang-tidy-${LLVM_VERSION} \ - llvm-${LLVM_VERSION} \ + clang-${LLVM_VERSION}="${CLANG_PKG_VERSION}" \ + clang-tidy-${LLVM_VERSION}="${CLANG_PKG_VERSION}" \ + llvm-${LLVM_VERSION}="${CLANG_PKG_VERSION}" \ && rm llvm.sh \ && add-apt-repository -y ppa:ubuntu-toolchain-r/test \ && apt-get update \ - && apt-get install -y --no-install-recommends gcc-15 g++-15 \ + && apt-get install -y --no-install-recommends gcc-15="${GCC15_PKG_VERSION}" g++-15="${GCC15_PKG_VERSION}" \ + && for pkg in clang-${LLVM_VERSION}:"${CLANG_PKG_VERSION}" gcc-14:"${GCC14_PKG_VERSION}" gcc-15:"${GCC15_PKG_VERSION}"; do \ + test "$(dpkg-query -W -f='${Version}' "${pkg%%:*}")" = "${pkg#*:}" \ + || { echo "${pkg%%:*} is not the pinned ${pkg#*:}"; exit 1; }; \ + done \ && test -d /usr/lib/gcc/x86_64-linux-gnu/14 \ && for driver in clang clang++; do \ printf -- '--gcc-install-dir=/usr/lib/gcc/x86_64-linux-gnu/14\n' \ From ff700742ce517cbe7f86f71d44077ee165005852 Mon Sep 17 00:00:00 2001 From: Yaraslau Tamashevich Date: Sat, 28 Nov 2026 18:27:50 +0100 Subject: [PATCH 4/4] Document the morph onboarding, the local warm-up and the toolchain pins Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01No1NzndCGtpCccptcyk6Tb --- README.md | 158 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 137 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index a8f9efd..89c1177 100644 --- a/README.md +++ b/README.md @@ -20,25 +20,36 @@ authenticated `pace-kv` alone does not distinguish cache readers from writers. `publish.yml` runs in four stages: -1. **resolve** pins an immutable Ubuntu base-image digest and the exact `pace` - `master` commit, and reads the warm-up groups from `warm-targets.json`. +1. **resolve** pins an immutable Ubuntu base-image digest and the head commit of + every project warmed in CI (`pace` is also the commit `pace-cc` and `pace-kv` + are built from), and reads the warm-up groups from `warm-targets.json`. 2. **build** builds the toolchain image with no seed, smoke-tests it, and pushes it as `-base`. Pull requests stop here. -3. **warm** runs one job per group in parallel, inside the base image. Each job - starts from the previously published seed for its group, so only translation - units whose inputs changed are recompiled. Each job pushes its result as a - candidate seed image, `cpp-ci-seed-pace-:`. A store that came - out byte-identical to the previous seed re-tags the previous seed image, so +3. **warm** runs one job per group of each project with `"warm": "ci"`, in + parallel, inside the base image. Each job starts from the previously + published seed for its group, so only translation units whose inputs changed + are recompiled. Each job pushes its result as a candidate seed image, + `cpp-ci-seed--:`. A store that came out byte-identical + to the previous seed gives an image that is the previous one plus labels, so its layer is not uploaded again. -4. **publish** smoke-tests the candidate seeds with the base image's `pace-kv`, - adds them to `-base` as one layer per group, and pushes `` - directly from buildkit, so layers the registry already holds are skipped. On - `master` it also moves `latest` and each group's seed to the new candidates. +4. **publish** collects the seeds of every group of every project: the + candidates of the warm jobs, and the latest `cpp-ci-seed--` + of each `"warm": "local"` project. It smoke-tests them with the base image's + `pace-kv`, adds them to `-base` as one layer per group, and pushes + `` directly from buildkit, so layers the registry already holds are + skipped. On `master` it also moves `latest` and each CI-warmed group's seed + to the new candidates. If any warm-up group fails, `publish` does not run, and `latest` stays on the previous image with its previous seeds. Consumers pick up each new image automatically, so no changes are needed in their repositories. +A locally warmed seed is optional to the publish. A group with no seed pushed +yet is left out with a warning, a seed this image's `pace-kv` cannot open is +dropped with a warning, and a seed warmed under another toolchain than the +image's (see [Toolchain pinning](#toolchain-pinning)) is assembled with a +warning, because it will not hit. + ## Seed warmer The warmer builds each configured project's presets with the compile cache @@ -46,19 +57,26 @@ enabled, into single-file `pace-kv` stores. Each store is one group of presets, and the stores are baked into the image as `/opt/pace/seed-cache//.cow`. A consumer copies the store for the group it builds. -**Status:** `pace` is the first project onboarded. The other three are not yet -onboarded. The blockers are tracked upstream: +**Status:** `pace` (warmed in CI) and `morph` (warmed locally) are onboarded. The +other two are not yet. The blockers are tracked upstream: - `Lightweight`: a test compile definition embeds an absolute build-tree path, so cache entries are not shared across checkouts. See [Lightweight#693](https://github.com/LASTRADA-Software/Lightweight/issues/693). - `formula-cpp`: no cache integration. See [formula-cpp#36](https://github.com/LASTRADA-Software/formula-cpp/issues/36). -- `morph`: not yet measured. ### Manifest -[`warm-targets.json`](warm-targets.json) lists the groups. `pace` has three: +[`warm-targets.json`](warm-targets.json) lists the projects. A project declares +its `repo`, its `ci_workspace` (the path its own CI checks it out at, which the +warm-up builds at), its `cpm_source_cache` (relative to the checkout, as its CI +keeps it), and how it is warmed: `"warm": "ci"` runs in `publish.yml`'s warm +jobs, `"warm": "local"` is warmed by [`scripts/warm-local.sh`](scripts/warm-local.sh) +on a developer machine. Seed images are named +`ghcr.io/lastrada-software/cpp-ci-seed--`. + +`pace` has four groups: | Group | Presets | |---|---| @@ -75,26 +93,48 @@ key of every translation unit it reaches (`PACE_ENABLE_TLS` adds a PUBLIC define to the `Pace` library, so it reaches all of them), and a seed warmed without it never hits. +`morph` has three. Its presets are the ones morph's `ci.yml` builds on Linux, +with the flags of that job's `cmake --preset` line (minus the self-hosted +runner's `PACE_AUTO_INSTALL` flag): + +| Group | Presets | `ci.yml` job | +|---|---|---| +| `clang` | clang-debug, clang-release, clang-shared-debug | `linux-compilers` | +| `gcc` | gcc-release | `linux-compilers` | +| `sanitizers` | clang-asan, clang-ubsan, clang-tsan | `linux-sanitizers` | + +`gcc-release` is the one preset that differs from its job: `ci.yml` makes GCC 15 +the default `gcc` with `update-alternatives`, which this image does not (its +`gcc` is GCC 14), so the preset selects `gcc-15`/`g++-15` by flag. A job that +runs in this image must pass the same flag. + Coverage presets are excluded, because `pace-cc` binds coverage objects to the checkout root and they never hit from another checkout. Windows presets are out of scope. ### What a group job does -[`scripts/warm-project.sh`](scripts/warm-project.sh) runs inside the base image: +[`scripts/run-warm-container.sh`](scripts/run-warm-container.sh) starts the +container, for the CI warm jobs and for `warm-local.sh` alike: the checkout is +mounted at the project's `ci_workspace`, the preset list is written from the +manifest, and the environment is the same. It runs +[`scripts/warm-project.sh`](scripts/warm-project.sh) inside the base image: - starts a local `pace-kv` on a single-file store, with an on-disk cap of `WARM_MAX_DISK` (default `4g`), which evicts least-recently-used entries; - if the previous seed exists, starts from a copy of it. A store the new `pace-kv` refuses (for example after a storage format change) is discarded, and the group starts empty; -- for each preset, configures with `-DUSE_COMPILER_CACHE=ON -DENABLE_TIDY=OFF` +- for each preset, removes the preset's build directory (a reused checkout must + not carry the cache variables of an earlier configure), then configures with `-DUSE_COMPILER_CACHE=ON -DENABLE_TIDY=OFF` plus the preset's `configure_args`, and builds it. clang-tidy is off because it re-runs on every file, even on a cache hit, and adds nothing to the store. It doesn't change the compile command, so the seeded entries still hit for consumers that build with tidy on; -- fails fast: a failing configure or build stops the group, and a configure that - disables the cache fails it too; +- fails fast: a failing configure or build stops the group, a configure that + disables the cache fails it too, and so does one whose output matches the + project's `forbid_configure_pattern` (for `pace`, the fallback version + `0.0.0-unknown`, which means `git describe` found nothing); - fails if `pace-cc` records no statistics, so an uncached warm-up is never published; - stops `pace-kv` with SIGTERM, so batched writes are flushed, and reopens the @@ -109,6 +149,80 @@ checkout path, and CMake names the object files of zstd, lz4 and catch2's main b an absolute path when CPM's cache is outside the source tree. Warmed at any other path, those 43 gcc-release units miss on every CI run. +### Local warm-up + +`morph` does not fit a hosted runner (16 GB, 4 vCPU, 330 minutes), so a developer +machine warms it and pushes the seeds. The publish workflow assembles whatever +was pushed last. + +```sh +scripts/warm-local.sh [--jobs N] [--ref REF] [--image IMAGE] [--push] morph [group...] +``` + +It checks the project out at `--ref` (default: the remote's `HEAD`) under +`~/.cache/cpp-ci-warm//src` (`--checkout-dir` moves it), pulls the image +(`cpp-ci:latest`, or `--image`) and each group's previous seed, and runs +`run-warm-container.sh` with the checkout bind-mounted at the project's +`ci_workspace` and the container running as the calling user. A group's seed +image is built with +[`scripts/build-seed-image.sh`](scripts/build-seed-image.sh), which labels it +with the toolchain fingerprint, the digest of the image it was warmed against and +the project commit. + +All groups are warmed and built before anything is pushed, so a failing group +leaves the registry as it was. Only with `--push` does it push each seed as +`:latest` and `:`; without it, it builds the images locally and +prints what it would push. The previous seed is fetched strictly: an +authentication or network failure stops the run, and only "manifest unknown" +means there is none yet. + +Pushing needs a prior `docker login ghcr.io` with a token that may write +packages in the organization. + +**Nightly.** [`local/systemd/`](local/systemd/) holds a user service and timer +that run `warm-local.sh morph --push` at 00:30 UTC, ahead of the 02:23 UTC +publish, with `Persistent=true` so a night the machine was off is made up. To +install: + +```sh +mkdir -p ~/.config/systemd/user +cp local/systemd/cpp-ci-warm-morph.{service,timer} ~/.config/systemd/user/ +# If this clone is not at ~/repo/cpp-ci-image, point ExecStart at it: +systemctl --user edit cpp-ci-warm-morph.service +systemctl --user daemon-reload +systemctl --user enable --now cpp-ci-warm-morph.timer +loginctl enable-linger "$USER" # run while logged out +``` + +`systemctl --user list-timers` shows the next run and `journalctl --user -u +cpp-ci-warm-morph` its output. + +**Using the seed from morph's CI.** A morph job uses a seed when it runs in +`ghcr.io/lastrada-software/cpp-ci:latest`, needs `packages: read`, selects the +group's store (`seed: clang|gcc|sanitizers`, as pace's `build.yml` does), leaves +the compiler launcher unset (a `-DCMAKE_*_COMPILER_LAUNCHER=sccache` flag +bypasses `pace-cc`), checks out at `/home/runner/work/morph/morph`, and passes +the same configure flags as the manifest lists for the preset. + +### Toolchain pinning + +`pace-cc` keys each cache entry on the compiler it fronts, so a seed only hits +under the exact compilers it was warmed with. The `Dockerfile` therefore pins +`clang-22` (with `clang-tidy-22` and `llvm-22`), `gcc-14` and `gcc-15` to exact +package versions (`CLANG_PKG_VERSION`, `GCC14_PKG_VERSION`, `GCC15_PKG_VERSION`) +and fails the build if the installed version differs. clang comes from +`apt.llvm.org`, `gcc-14` from Ubuntu noble, and `gcc-15` from the +`ubuntu-toolchain-r/test` PPA; each keeps only its newest version in its index, +so a pinned build fails once upstream publishes a newer one, and the last good +image stays `latest` until someone bumps the pin. + +To bump: set the new versions in the `Dockerfile`, let `publish` build and +publish the image, then warm the locally warmed projects again. Until then the +publish step warns, for every seed whose `org.lastrada.seed.toolchain` label +(the hash printed by [`scripts/toolchain-fingerprint.sh`](scripts/toolchain-fingerprint.sh), +over `pace-cc --print-toolchain-fingerprint` of each compiler) differs from the +image's, that the seed will not hit. + ### Decisions - **Parallel groups, one store each.** `pace-kv` cannot merge stores, so each @@ -127,8 +241,10 @@ path, those 43 gcc-release units miss on every CI run. - **Groups stay under the job limit.** The clang group ran for 5 h 30 min in run 42095583072 and was cancelled by the 330-minute `timeout-minutes`, which skipped `publish`. Its gate and tracy presets moved to `clang-extra`. -- **Warm-up runs inside `publish.yml`,** not on a separate schedule. The seed is - produced by the same `pace-cc`, `pace-kv`, and compilers it is baked beside. +- **CI-warmed warm-ups run inside `publish.yml`,** not on a separate schedule. + The seed is produced by the same `pace-cc`, `pace-kv`, and compilers it is + baked beside. A locally warmed seed cannot be, which is what the toolchain + label and the pins are for. Cache keys include the compiler banner, and `pace-kv` refuses stores written with a different storage format. - **Credentials.** The warm-up uses a local `pace-kv`, so it needs no