From 9a8a7764188fc283fcaca0344da70dd517c482a5 Mon Sep 17 00:00:00 2001 From: YXC0915 Date: Fri, 4 Dec 2026 18:45:23 +0800 Subject: [PATCH 1/2] Cache Windows runtime compilation and batch MSYS2 package lookups Reuse compiler objects and Cargo downloads across Alpha jobs, then link and package the exact runtime inputs on every build. Query package ownership, versions and licenses in batches and avoid recompressing the uploaded archives. Checked the workflow with actionlint and checked the MSYS2 lookup with mock and installed-package tests. --- .github/workflows/runtime-preview.yml | 46 +++++++++++++++- .../windows-runtime/scripts/pack-runtime.ps1 | 49 ++++++++++++++--- .../scripts/read-msys2-package-metadata.sh | 48 +++++++++++++++++ .../test-read-msys2-package-metadata.sh | 53 +++++++++++++++++++ 4 files changed, 186 insertions(+), 10 deletions(-) create mode 100644 packaging/windows-runtime/scripts/read-msys2-package-metadata.sh create mode 100644 packaging/windows-runtime/scripts/test-read-msys2-package-metadata.sh diff --git a/.github/workflows/runtime-preview.yml b/.github/workflows/runtime-preview.yml index 3da044b70..9c44c987f 100644 --- a/.github/workflows/runtime-preview.yml +++ b/.github/workflows/runtime-preview.yml @@ -106,14 +106,17 @@ jobs: msystem: CLANG64 update: true install: >- - git zstd tar mingw-w64-clang-x86_64-toolchain + git curl diffutils make zstd tar mingw-w64-clang-x86_64-toolchain mingw-w64-clang-x86_64-cmake mingw-w64-clang-x86_64-meson mingw-w64-clang-x86_64-ninja mingw-w64-clang-x86_64-pkgconf mingw-w64-clang-x86_64-glib2 mingw-w64-clang-x86_64-pixman mingw-w64-clang-x86_64-gtk3 mingw-w64-clang-x86_64-libslirp - mingw-w64-clang-x86_64-libusb mingw-w64-clang-x86_64-libva + mingw-w64-clang-x86_64-libusb mingw-w64-clang-x86_64-usbredir + mingw-w64-clang-x86_64-gnutls mingw-w64-clang-x86_64-dtc + mingw-w64-clang-x86_64-libva mingw-w64-clang-x86_64-zstd mingw-w64-clang-x86_64-mesa mingw-w64-clang-x86_64-vulkan-loader mingw-w64-clang-x86_64-python mingw-w64-clang-x86_64-python-pip + mingw-w64-clang-x86_64-ccache mingw-w64-clang-x86_64-sccache - name: Check out the runtime forks and apply dependency patches if: steps.plan.outputs.skip != 'true' @@ -179,14 +182,52 @@ jobs: "XDG_DATA_HOME=$(Join-Path $env:RUNNER_TEMP 'xdg-data')" >> $env:GITHUB_ENV "XDG_CACHE_HOME=$(Join-Path $env:RUNNER_TEMP 'xdg-cache')" >> $env:GITHUB_ENV + - name: Identify the Windows build toolchain + if: steps.plan.outputs.skip != 'true' + id: toolchain + shell: msys2 {0} + run: | + set -euo pipefail + cd "$(cygpath -u "$UPSTREAM_DIR")" + fingerprint="$({ LC_ALL=C pacman -Q; cat rust-toolchain.toml; } | sha256sum)" + echo "fingerprint=${fingerprint%% *}" >> "$GITHUB_OUTPUT" + echo "Build toolchain: ${fingerprint%% *}" + + - name: Cache compiler objects and Cargo downloads + if: steps.plan.outputs.skip != 'true' + uses: actions/cache@v4 + with: + path: | + ${{ runner.temp }}/compiler-cache/ccache + ${{ runner.temp }}/compiler-cache/sccache + ${{ runner.temp }}/cargo/registry/index + ${{ runner.temp }}/cargo/registry/cache + ${{ runner.temp }}/cargo/git/db + key: runtime-build-v1-${{ runner.arch }}-${{ steps.toolchain.outputs.fingerprint }}-${{ needs.resolve.outputs.commit }}-${{ github.sha }} + restore-keys: | + runtime-build-v1-${{ runner.arch }}-${{ steps.toolchain.outputs.fingerprint }}- + - name: Build upstream runtime in MSYS2 CLANG64 if: steps.plan.outputs.skip != 'true' shell: msys2 {0} env: REIMS_INSTALL_SKIP_HOST_CHECKS: "1" + CCACHE_MAXSIZE: 750M + CCACHE_COMPILERCHECK: content + SCCACHE_CACHE_SIZE: 1G + SCCACHE_ALLOW_INCREMENTAL: "1" run: | set -euo pipefail cd "$(cygpath -u "$UPSTREAM_DIR")" + # The installer selects clang/clang++ itself. MSYS2's ccache + # compiler shims forward those names to the real CLANG64 tools. + export PATH="$MINGW_PREFIX/lib/ccache/bin:$PATH" + export CCACHE_DIR="$(cygpath -m "$RUNNER_TEMP")/compiler-cache/ccache" + export SCCACHE_DIR="$(cygpath -m "$RUNNER_TEMP")/compiler-cache/sccache" + export RUSTC_WRAPPER="$(cygpath -m "$MINGW_PREFIX/bin/sccache.exe")" + ccache --zero-stats + sccache --start-server + trap 'ccache --show-stats; sccache --stop-server' EXIT # Upstream's own non-interactive CLANG64 build script. bash scripts/install-ctl.sh --yes util_repo="$(cygpath -u "$REIMS_UTIL_DIR")" @@ -242,6 +283,7 @@ jobs: path: | ${{ runner.temp }}/releases/reims-runtime-*.tar.zst ${{ runner.temp }}/releases/SHA256SUMS.txt + compression-level: 0 retention-days: 14 - name: Publish alpha prerelease (never stable) diff --git a/packaging/windows-runtime/scripts/pack-runtime.ps1 b/packaging/windows-runtime/scripts/pack-runtime.ps1 index 86fb40b5b..c929145a6 100644 --- a/packaging/windows-runtime/scripts/pack-runtime.ps1 +++ b/packaging/windows-runtime/scripts/pack-runtime.ps1 @@ -360,16 +360,51 @@ if (Test-Path -LiteralPath (Join-Path $ReimsRepo 'LICENSES')) { Write-Host '==> Listing MSYS2 packages of bundled DLLs' $bash = Join-Path $Msys2 'usr\bin\bash.exe' $dlls = @(Get-ChildItem -LiteralPath "$bundleDir\qemu\bin" -Filter '*.dll' -File | Sort-Object Name) +$metadataScript = (Join-Path $PSScriptRoot 'read-msys2-package-metadata.sh').Replace('\', '/') +$dllPaths = @($dlls | ForEach-Object { "/clang64/bin/$($_.Name)" }) +$expectedPaths = [Collections.Generic.HashSet[string]]::new([StringComparer]::Ordinal) +foreach ($path in $dllPaths) { [void]$expectedPaths.Add($path) } +$rows = @(& $bash -lc 'script=$1; shift; source "$script"' -- $metadataScript @dllPaths) +if ($LASTEXITCODE -ne 0) { throw "MSYS2 package metadata lookup failed ($LASTEXITCODE)" } +$owners = @{} +$licensesByPackage = @{} +foreach ($row in $rows) { + $type, $key, $value, $version = $row -split "`t", 4 + switch ($type) { + 'owned' { + if (!$expectedPaths.Remove($key) -or !$value -or !$version) { + throw "Unexpected MSYS2 ownership row: $row" + } + $owners[$key] = [pscustomobject]@{ Package = $value; Version = $version } + } + 'unowned' { + if (!$expectedPaths.Remove($key) -or $value) { + throw "Unexpected MSYS2 ownership row: $row" + } + } + 'license' { + if (!$key -or !$value -or !$value.StartsWith('/clang64/share/licenses/', [StringComparison]::Ordinal) -or $version) { + throw "Unexpected MSYS2 license row: $row" + } + if (!$licensesByPackage.ContainsKey($key)) { + $licensesByPackage[$key] = [Collections.Generic.List[string]]::new() + } + $licensesByPackage[$key].Add($value) + } + default { throw "Unexpected MSYS2 package metadata row: $row" } + } +} +if ($expectedPaths.Count -ne 0) { throw "MSYS2 ownership lookup did not answer for $($expectedPaths -join ', ')" } + $pkgLines = [Collections.Generic.List[string]]::new() $pkgSet = [ordered]@{} $pkgNames = [ordered]@{} foreach ($d in $dlls) { - $owner = (& $bash -lc "LC_ALL=C pacman -Qqo /clang64/bin/$($d.Name) 2>/dev/null" | Select-Object -First 1) + $owner = $owners["/clang64/bin/$($d.Name)"] if ($owner) { - $ver = (& $bash -lc "LC_ALL=C pacman -Q $owner" | Select-Object -First 1).Split(' ')[1] - $pkgLines.Add("$($d.Name)`t$owner`t$ver") - $pkgSet["$owner $ver"] = $true - $pkgNames[$owner] = $true + $pkgLines.Add("$($d.Name)`t$($owner.Package)`t$($owner.Version)") + $pkgSet["$($owner.Package) $($owner.Version)"] = $true + $pkgNames[$owner.Package] = $true } else { $pkgLines.Add("$($d.Name)`t(not from an MSYS2 package: built with qemu, source in the source package)`t-") } @@ -377,9 +412,7 @@ foreach ($d in $dlls) { # Bundle each MSYS2 package's installed license texts under licenses/msys2/. $msysLicRoot = Join-Path $bundleDir 'licenses\msys2' foreach ($owner in $pkgNames.Keys) { - $licFiles = @(& $bash -lc "LC_ALL=C pacman -Ql $owner 2>/dev/null" | ForEach-Object { - if ($_ -match '^\S+\s+(/clang64/share/licenses/.+)$' -and -not $Matches[1].EndsWith('/')) { $Matches[1] } - }) + $licFiles = $licensesByPackage[$owner] foreach ($lf in $licFiles) { $rel = $lf.Substring('/clang64/share/licenses/'.Length) $srcFile = Join-Path $Msys2 ($lf.TrimStart('/') -replace '/', '\') diff --git a/packaging/windows-runtime/scripts/read-msys2-package-metadata.sh b/packaging/windows-runtime/scripts/read-msys2-package-metadata.sh new file mode 100644 index 000000000..f21ee1f67 --- /dev/null +++ b/packaging/windows-runtime/scripts/read-msys2-package-metadata.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# Pacman's ownership query returns the owner and installed version together. +set -euo pipefail +export LC_ALL=C +if (($# == 0)); then + exit 0 +fi + +status=0 +ownership=$(pacman -Qo -- "$@" 2>&1) || status=$? +owned='^(/clang64/bin/.+) is owned by ([^[:space:]]+) ([^[:space:]]+)$' +unowned='^error: No package owns (/clang64/bin/.+)$' +declare -A seen=() +packages=() +missing=0 +while IFS= read -r line; do + if [[ $line =~ $owned ]]; then + path=${BASH_REMATCH[1]} + package=${BASH_REMATCH[2]} + version=${BASH_REMATCH[3]} + printf 'owned\t%s\t%s\t%s\n' "$path" "$package" "$version" + if [[ -z ${seen[$package]+yes} ]]; then + seen[$package]=1 + packages+=("$package") + fi + elif [[ $line =~ $unowned ]]; then + printf 'unowned\t%s\n' "${BASH_REMATCH[1]}" + ((missing += 1)) + else + printf 'Unexpected pacman ownership response: %s\n' "$line" >&2 + exit 1 + fi +done <<< "$ownership" +if ((status != missing % 256)); then + printf 'pacman ownership query exited with %d for %d unowned files\n' "$status" "$missing" >&2 + exit 1 +fi + +if ((${#packages[@]})); then + pacman -Ql -- "${packages[@]}" | awk ' + { + package = $1 + sub(/^[^[:space:]]+[[:space:]]+/, "") + if ($0 ~ /^\/clang64\/share\/licenses\/.+/ && $0 !~ /\/$/) + printf "license\t%s\t%s\n", package, $0 + } + ' +fi diff --git a/packaging/windows-runtime/scripts/test-read-msys2-package-metadata.sh b/packaging/windows-runtime/scripts/test-read-msys2-package-metadata.sh new file mode 100644 index 000000000..30aeb8a6d --- /dev/null +++ b/packaging/windows-runtime/scripts/test-read-msys2-package-metadata.sh @@ -0,0 +1,53 @@ +#!/usr/bin/env bash +# Exercise the batched queries and their failure handling without a package database. +set -euo pipefail +helper="$(dirname "$0")/read-msys2-package-metadata.sh" +mode=ok + +pacman() { + case "$*" in + '-Qo -- /clang64/bin/a.dll /clang64/bin/local.dll /clang64/bin/b.dll') + echo '/clang64/bin/a.dll is owned by same-package 2:1.0-3' + echo 'error: No package owns /clang64/bin/local.dll' >&2 + echo '/clang64/bin/b.dll is owned by same-package 2:1.0-3' + case $mode in + query-failure) echo 'error: invalid local database' >&2; return 2 ;; + wrong-status) return 0 ;; + esac + return 1 + ;; + '-Ql -- same-package') + echo 'same-package /clang64/bin/a.dll' + echo 'same-package /clang64/share/licenses/example/' + echo 'same-package /clang64/share/licenses/example/ text' + if [[ $mode == list-failure ]]; then + echo 'error: failed to read package file list' >&2 + return 1 + fi + ;; + *) + printf 'Unexpected pacman invocation: %s\n' "$*" >&2 + return 255 + ;; + esac +} + +read_metadata() ( + source "$helper" "$@" +) + +paths=(/clang64/bin/a.dll /clang64/bin/local.dll /clang64/bin/b.dll) +expected=$'owned\t/clang64/bin/a.dll\tsame-package\t2:1.0-3\nunowned\t/clang64/bin/local.dll\nowned\t/clang64/bin/b.dll\tsame-package\t2:1.0-3\nlicense\tsame-package\t/clang64/share/licenses/example/ text' +actual=$(read_metadata "${paths[@]}") +if [[ $actual != "$expected" ]]; then + printf 'Metadata did not match:\n%s\n' "$actual" >&2 + exit 1 +fi +[[ -z $(read_metadata) ]] +for mode in query-failure wrong-status list-failure; do + if read_metadata "${paths[@]}" >/dev/null 2>&1; then + printf 'Accepted an unexpected pacman failure: %s\n' "$mode" >&2 + exit 1 + fi +done +printf 'MSYS2 package metadata checks passed\n' From 4f31eee07bd303f3eb085ed3540b859f9a5ad9a2 Mon Sep 17 00:00:00 2001 From: YXC0915 Date: Fri, 4 Dec 2026 19:27:10 +0800 Subject: [PATCH 2/2] Route native Ninja compiler calls through ccache Windows searches the launching executable directory before PATH. Stage the CI copy of Ninja alongside the installed ccache shims and point ccache at the original CLANG64 toolchain. Log the detailed compiler counters. --- .github/workflows/runtime-preview.yml | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/.github/workflows/runtime-preview.yml b/.github/workflows/runtime-preview.yml index 9c44c987f..bd1445136 100644 --- a/.github/workflows/runtime-preview.yml +++ b/.github/workflows/runtime-preview.yml @@ -195,7 +195,7 @@ jobs: - name: Cache compiler objects and Cargo downloads if: steps.plan.outputs.skip != 'true' - uses: actions/cache@v4 + uses: actions/cache@v5 with: path: | ${{ runner.temp }}/compiler-cache/ccache @@ -219,15 +219,20 @@ jobs: run: | set -euo pipefail cd "$(cygpath -u "$UPSTREAM_DIR")" - # The installer selects clang/clang++ itself. MSYS2's ccache - # compiler shims forward those names to the real CLANG64 tools. - export PATH="$MINGW_PREFIX/lib/ccache/bin:$PATH" - export CCACHE_DIR="$(cygpath -m "$RUNNER_TEMP")/compiler-cache/ccache" - export SCCACHE_DIR="$(cygpath -m "$RUNNER_TEMP")/compiler-cache/sccache" + # Native Ninja searches its own directory for the installer's bare + # clang/clang++ names before PATH. Put the cache shims beside its CI copy. + temp_dir="$(cygpath -u "$RUNNER_TEMP")" + tools_dir="$temp_dir/compiler-tools" + mkdir -p "$tools_dir" + cp "$MINGW_PREFIX/bin/ninja.exe" "$MINGW_PREFIX/lib/ccache/bin/"clang{,++}.exe "$tools_dir/" + export PATH="$tools_dir:$PATH" + export CCACHE_PATH="$(cygpath -m "$MINGW_PREFIX/bin")" + export CCACHE_DIR="$(cygpath -m "$temp_dir")/compiler-cache/ccache" + export SCCACHE_DIR="$(cygpath -m "$temp_dir")/compiler-cache/sccache" export RUSTC_WRAPPER="$(cygpath -m "$MINGW_PREFIX/bin/sccache.exe")" ccache --zero-stats sccache --start-server - trap 'ccache --show-stats; sccache --stop-server' EXIT + trap 'ccache --show-stats --verbose; sccache --stop-server' EXIT # Upstream's own non-interactive CLANG64 build script. bash scripts/install-ctl.sh --yes util_repo="$(cygpath -u "$REIMS_UTIL_DIR")"