setup-sccache.ps1

  1#Requires -Version 5.1
  2$ErrorActionPreference = "Stop"
  3
  4$SCCACHE_VERSION = "v0.10.0"
  5$SCCACHE_DIR = "./target/sccache"
  6
  7function Install-Sccache {
  8    New-Item -ItemType Directory -Path $SCCACHE_DIR -Force | Out-Null
  9
 10    $sccachePath = Join-Path $SCCACHE_DIR "sccache.exe"
 11
 12    if (Test-Path $sccachePath) {
 13        Write-Host "sccache already cached: $(& $sccachePath --version)"
 14    }
 15    else {
 16        Write-Host "Installing sccache ${SCCACHE_VERSION} from GitHub releases..."
 17
 18        $arch = if ([Environment]::Is64BitOperatingSystem) { "x86_64" } else { "i686" }
 19        $archive = "sccache-${SCCACHE_VERSION}-${arch}-pc-windows-msvc.zip"
 20        $basename = "sccache-${SCCACHE_VERSION}-${arch}-pc-windows-msvc"
 21        $url = "https://github.com/mozilla/sccache/releases/download/${SCCACHE_VERSION}/${archive}"
 22
 23        $tempDir = Join-Path $env:TEMP "sccache-install"
 24        New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
 25
 26        try {
 27            $archivePath = Join-Path $tempDir $archive
 28            Invoke-WebRequest -Uri $url -OutFile $archivePath
 29            Expand-Archive -Path $archivePath -DestinationPath $tempDir
 30
 31            $extractedPath = Join-Path $tempDir $basename "sccache.exe"
 32            Move-Item -Path $extractedPath -Destination $sccachePath -Force
 33
 34            Write-Host "Installed sccache: $(& $sccachePath --version)"
 35        }
 36        finally {
 37            Remove-Item -Recurse -Force $tempDir -ErrorAction SilentlyContinue
 38        }
 39    }
 40
 41    $absolutePath = (Resolve-Path $SCCACHE_DIR).Path
 42    if ($env:GITHUB_PATH) {
 43        $absolutePath | Out-File -FilePath $env:GITHUB_PATH -Append -Encoding utf8
 44    }
 45    $env:PATH = "$absolutePath;$env:PATH"
 46
 47    # Verify sccache is available in PATH - fail fast if not
 48    $sccacheCmd = Get-Command sccache -ErrorAction SilentlyContinue
 49    if (-not $sccacheCmd) {
 50        Write-Host "::error::sccache was installed but is not found in PATH"
 51        Write-Host "PATH: $env:PATH"
 52        Write-Host "Expected location: $absolutePath"
 53        if (Test-Path (Join-Path $absolutePath "sccache.exe")) {
 54            Write-Host "sccache.exe exists at expected location but is not in PATH"
 55            Write-Host "Directory contents:"
 56            Get-ChildItem $absolutePath | ForEach-Object { Write-Host "  $_" }
 57        } else {
 58            Write-Host "sccache.exe NOT found at expected location"
 59        }
 60        exit 1
 61    }
 62}
 63
 64function Configure-Sccache {
 65    if (-not $env:R2_ACCOUNT_ID) {
 66        Write-Host "R2_ACCOUNT_ID not set, skipping sccache configuration"
 67        return
 68    }
 69
 70    # Verify sccache is available before configuring
 71    $sccacheCmd = Get-Command sccache -ErrorAction SilentlyContinue
 72    if (-not $sccacheCmd) {
 73        Write-Host "::error::sccache not found in PATH, cannot configure RUSTC_WRAPPER"
 74        Write-Host "PATH: $env:PATH"
 75        exit 1
 76    }
 77
 78    Write-Host "Configuring sccache with Cloudflare R2..."
 79
 80    $bucket = if ($env:SCCACHE_BUCKET) { $env:SCCACHE_BUCKET } else { "sccache-zed" }
 81    $keyPrefix = if ($env:SCCACHE_KEY_PREFIX) { $env:SCCACHE_KEY_PREFIX } else { "sccache/" }
 82    $baseDir = if ($env:GITHUB_WORKSPACE) { $env:GITHUB_WORKSPACE } else { (Get-Location).Path }
 83
 84    # Use the absolute path to sccache binary for RUSTC_WRAPPER to avoid
 85    # any PATH race conditions between GITHUB_PATH and GITHUB_ENV
 86    $sccacheBin = (Get-Command sccache).Source
 87
 88    # Set in current process
 89    $env:SCCACHE_ENDPOINT = "https://$($env:R2_ACCOUNT_ID).r2.cloudflarestorage.com"
 90    $env:SCCACHE_BUCKET = $bucket
 91    $env:SCCACHE_REGION = "auto"
 92    $env:SCCACHE_S3_KEY_PREFIX = $keyPrefix
 93    $env:SCCACHE_BASEDIR = $baseDir
 94    $env:AWS_ACCESS_KEY_ID = $env:R2_ACCESS_KEY_ID
 95    $env:AWS_SECRET_ACCESS_KEY = $env:R2_SECRET_ACCESS_KEY
 96    $env:RUSTC_WRAPPER = $sccacheBin
 97
 98    # Also write to GITHUB_ENV for subsequent steps
 99    if ($env:GITHUB_ENV) {
100        @(
101            "SCCACHE_ENDPOINT=$($env:SCCACHE_ENDPOINT)"
102            "SCCACHE_BUCKET=$($env:SCCACHE_BUCKET)"
103            "SCCACHE_REGION=$($env:SCCACHE_REGION)"
104            "SCCACHE_S3_KEY_PREFIX=$($env:SCCACHE_S3_KEY_PREFIX)"
105            "SCCACHE_BASEDIR=$($env:SCCACHE_BASEDIR)"
106            "AWS_ACCESS_KEY_ID=$($env:AWS_ACCESS_KEY_ID)"
107            "AWS_SECRET_ACCESS_KEY=$($env:AWS_SECRET_ACCESS_KEY)"
108            "RUSTC_WRAPPER=$($env:RUSTC_WRAPPER)"
109        ) | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
110    }
111
112    Write-Host "✓ sccache configured with Cloudflare R2 (bucket: $bucket)"
113}
114
115function Show-Config {
116    Write-Host "=== sccache configuration ==="
117    Write-Host "sccache version: $(sccache --version)"
118    Write-Host "sccache path: $((Get-Command sccache).Source)"
119    Write-Host "RUSTC_WRAPPER: $($env:RUSTC_WRAPPER ?? '<not set>')"
120    Write-Host "SCCACHE_BUCKET: $($env:SCCACHE_BUCKET ?? '<not set>')"
121    Write-Host "SCCACHE_ENDPOINT: $($env:SCCACHE_ENDPOINT ?? '<not set>')"
122    Write-Host "SCCACHE_REGION: $($env:SCCACHE_REGION ?? '<not set>')"
123    Write-Host "SCCACHE_S3_KEY_PREFIX: $($env:SCCACHE_S3_KEY_PREFIX ?? '<not set>')"
124    Write-Host "SCCACHE_BASEDIR: $($env:SCCACHE_BASEDIR ?? '<not set>')"
125
126    if ($env:AWS_ACCESS_KEY_ID) {
127        Write-Host "AWS_ACCESS_KEY_ID: <set>"
128    }
129    else {
130        Write-Host "AWS_ACCESS_KEY_ID: <not set>"
131    }
132
133    if ($env:AWS_SECRET_ACCESS_KEY) {
134        Write-Host "AWS_SECRET_ACCESS_KEY: <set>"
135    }
136    else {
137        Write-Host "AWS_SECRET_ACCESS_KEY: <not set>"
138    }
139
140    Write-Host "=== sccache stats ==="
141    sccache --show-stats
142}
143
144Install-Sccache
145Configure-Sccache
146Show-Config