Files
build_toochain/install_script/ccache/ccache.ps1
T
2026-07-07 15:23:39 +08:00

52 lines
2.2 KiB
PowerShell

$ErrorActionPreference = "Stop"
$toolsDir = "D:\ae\tools"
$installDir = "D:\ae\tools\ccache"
$cacheDir = "D:\ae\tools\ccache-cache"
$tmpDir = Join-Path $env:TEMP "ccache_install"
$apiUrl = "https://api.github.com/repos/ccache/ccache/releases/latest"
New-Item -ItemType Directory -Force -Path $toolsDir | Out-Null
New-Item -ItemType Directory -Force -Path $cacheDir | Out-Null
if(Test-Path $tmpDir) {
Remove-Item -Recurse -Force $tmpDir
}
New-Item -ItemType Directory -Force -Path $tmpDir | Out-Null
Write-Host "获取 ccache 最新版本信息..."
$release = Invoke-RestMethod -Uri $apiUrl -Headers @{ "User-Agent" = "PowerShell" }
$asset = $release.assets | Where-Object { $_.name -match "windows-x86_64\.zip$" } | Select-Object -First 1
if($null -eq $asset) {
throw "没有找到 Windows x86_64 版本的 ccache zip 包"
}
$zipFile = Join-Path $tmpDir $asset.name
Write-Host "下载 $($asset.name)..."
Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $zipFile
Write-Host "解压..."
Expand-Archive -Path $zipFile -DestinationPath $tmpDir -Force
$ccacheExe = Get-ChildItem -Path $tmpDir -Filter "ccache.exe" -Recurse | Select-Object -First 1
if($null -eq $ccacheExe) {
throw "解压后没有找到 ccache.exe"
}
if(Test-Path $installDir) {
Remove-Item -Recurse -Force $installDir
}
New-Item -ItemType Directory -Force -Path $installDir | Out-Null
Copy-Item -Path (Join-Path $ccacheExe.DirectoryName "*") -Destination $installDir -Recurse -Force
[Environment]::SetEnvironmentVariable("CCACHE_DIR", $cacheDir, "User")
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
$paths = $userPath -split ";"
if($paths -notcontains $installDir) {
if([string]::IsNullOrWhiteSpace($userPath)) {
$newPath = $installDir
} else {
$newPath = "$userPath;$installDir"
}
[Environment]::SetEnvironmentVariable("Path", $newPath, "User")
}
$env:Path = "$installDir;$env:Path"
$env:CCACHE_DIR = $cacheDir
& "$installDir\ccache.exe" --version
& "$installDir\ccache.exe" -M 20G
& "$installDir\ccache.exe" -s
Remove-Item -Recurse -Force $tmpDir
Write-Host "ccache 已安装到: $installDir"
Write-Host "ccache 缓存目录: $cacheDir"
Write-Host "已经写入用户 PATH,新开的 PowerShell 窗口可直接使用 ccache"