Replace the MSYS2/UCRT64 toolchain with the portable w64devkit kit: bootstrap a pinned kit, build with its sh/make/gcc, and keep the compiler's temporary files inside work/. Update upstream to 6.1.0 and add a portable-prefix patch: when the configured prefix is gone, csc, csi, chicken, and the chicken-* tools derive include, library, and repository paths from the running executable. A released zip now works wherever it is unpacked, which it did not before. Signed-off-by: Pavel Kulyov <kulyov.pavel@gmail.com>
295 lines
11 KiB
PowerShell
295 lines
11 KiB
PowerShell
# Build and publish a MinGW CHICKEN prefix.
|
|
# .\ck.ps1 bootstrap | fetch-sources | patch | build | test | publish | clean
|
|
# .\ck.ps1 publish -Latest
|
|
param(
|
|
[Parameter(Mandatory = $true, Position = 0)]
|
|
[ValidateSet('bootstrap', 'fetch-sources', 'patch', 'build', 'test', 'publish', 'clean')]
|
|
[string]$Command,
|
|
|
|
[switch]$Latest
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
$Root = $PSScriptRoot
|
|
$Work = Join-Path $Root 'work'
|
|
$Src = Join-Path $Work 'src'
|
|
$Prefix = Join-Path $Work 'prefix'
|
|
$Dist = Join-Path $Work 'dist'
|
|
$ConfigPath = Join-Path $Root 'chicken.json'
|
|
|
|
# The toolchain is a portable folder, not an installed package.
|
|
$KitVersion = '2.11.0'
|
|
$KitSha256 = '54c2bb9962e8a30c8270e34c5efc3d5fee579f53087fa7d435b95fbba2af7b2b'
|
|
$KitUrl = "https://github.com/skeeto/w64devkit/releases/download/v$KitVersion/w64devkit-x64-$KitVersion.7z.exe"
|
|
$Kit = Join-Path $Work 'w64devkit'
|
|
|
|
function Get-ProjectConfig {
|
|
Get-Content -Raw -LiteralPath $ConfigPath | ConvertFrom-Json
|
|
}
|
|
|
|
function Save-ProjectConfig($Config) {
|
|
$json = $Config | ConvertTo-Json -Depth 6
|
|
$utf8 = New-Object System.Text.UTF8Encoding $false
|
|
[System.IO.File]::WriteAllText($ConfigPath, $json + "`n", $utf8)
|
|
}
|
|
|
|
$Config = Get-ProjectConfig
|
|
$UpstreamVersion = $Config.upstream.version
|
|
$UpstreamUrl = "https://code.call-cc.org/releases/$UpstreamVersion/chicken-$UpstreamVersion.tar.gz"
|
|
|
|
function Import-DotEnv {
|
|
$path = Join-Path $Root '.env'
|
|
if (-not (Test-Path -LiteralPath $path)) { return }
|
|
foreach ($line in Get-Content -LiteralPath $path) {
|
|
if ($line -match '^\s*#' -or $line -match '^\s*$') { continue }
|
|
$parts = $line.Split('=', 2)
|
|
if ($parts.Count -lt 2) { continue }
|
|
$name = $parts[0].Trim()
|
|
if ([string]::IsNullOrEmpty([Environment]::GetEnvironmentVariable($name))) {
|
|
$value = $parts[1].Trim().Trim('"').Trim("'")
|
|
Set-Item -Path "Env:$name" -Value $value
|
|
}
|
|
}
|
|
}
|
|
|
|
function Get-KitRoot {
|
|
$candidates = @()
|
|
if ($env:W64DEVKIT_ROOT) { $candidates += $env:W64DEVKIT_ROOT }
|
|
$candidates += $Kit
|
|
foreach ($candidate in $candidates) {
|
|
if (-not $candidate) { continue }
|
|
if (Test-Path -LiteralPath (Join-Path $candidate 'bin\sh.exe')) {
|
|
return (Resolve-Path -LiteralPath $candidate).Path
|
|
}
|
|
}
|
|
Write-Error "w64devkit not found. Run: .\ck.ps1 bootstrap (or set W64DEVKIT_ROOT)."
|
|
exit 1
|
|
}
|
|
|
|
function Invoke-Kit([string]$Script) {
|
|
$kit = Get-KitRoot
|
|
$env:PATH = (Join-Path $kit 'bin') + ';' + $env:PATH
|
|
# Keep the compiler's temporary files inside the tree: the build
|
|
# need not depend on the machine's %TEMP% being writable.
|
|
$tmp = Join-Path $Work '.tmp'
|
|
New-Item -ItemType Directory -Force -Path $tmp | Out-Null
|
|
$env:TEMP = $tmp
|
|
$env:TMP = $tmp
|
|
$env:TMPDIR = $tmp -replace '\\', '/'
|
|
$root = $Root -replace '\\', '/'
|
|
& (Join-Path $kit 'bin\sh.exe') -c "cd '$root' && $Script"
|
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
}
|
|
|
|
function Get-LatestUpstream {
|
|
$index = 'https://code.call-cc.org/releases/current/'
|
|
$page = & curl.exe -fsSL $index
|
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
$text = if ($page -is [array]) { $page -join "`n" } else { [string]$page }
|
|
$found = [regex]::Matches($text, 'chicken-(\d+(?:\.\d+){2,})\.tar\.gz') |
|
|
ForEach-Object { $_.Groups[1].Value } |
|
|
Select-Object -Unique
|
|
if (-not $found) {
|
|
Write-Error "no CHICKEN tarball linked from $index"
|
|
exit 1
|
|
}
|
|
$found | Sort-Object { [version]$_ } -Descending | Select-Object -First 1
|
|
}
|
|
|
|
function Get-Tarball {
|
|
Join-Path $Work "chicken-$($Config.upstream.version).tar.gz"
|
|
}
|
|
|
|
function Get-FileSha256([string]$Path) {
|
|
(Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLower()
|
|
}
|
|
|
|
function Invoke-Bootstrap {
|
|
if (Test-Path -LiteralPath (Join-Path $Kit 'bin\sh.exe')) {
|
|
Write-Host "toolchain already in $Kit"
|
|
return
|
|
}
|
|
New-Item -ItemType Directory -Force -Path $Work | Out-Null
|
|
$sfx = Join-Path $Work "w64devkit-x64-$KitVersion.7z.exe"
|
|
if (-not (Test-Path -LiteralPath $sfx)) {
|
|
Write-Host "downloading w64devkit $KitVersion"
|
|
& curl.exe -fL --retry 2 -o $sfx $KitUrl
|
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
}
|
|
$actual = Get-FileSha256 $sfx
|
|
if ($actual -ne $KitSha256) {
|
|
Write-Error "w64devkit sha256 mismatch: got $actual, want $KitSha256"
|
|
exit 1
|
|
}
|
|
Write-Host "unpacking $Kit"
|
|
New-Item -ItemType Directory -Force -Path $Kit | Out-Null
|
|
$process = Start-Process -FilePath $sfx -ArgumentList @('-y', "-o$Kit") -Wait -PassThru -WindowStyle Hidden
|
|
if ($process.ExitCode -ne 0) {
|
|
Write-Error "w64devkit extractor failed with exit code $($process.ExitCode)"
|
|
exit 1
|
|
}
|
|
# The self-extractor puts everything under a w64devkit/ folder.
|
|
$nested = Join-Path $Kit 'w64devkit'
|
|
if (Test-Path -LiteralPath $nested) {
|
|
Get-ChildItem -LiteralPath $nested | Move-Item -Destination $Kit -Force
|
|
Remove-Item -LiteralPath $nested -Force
|
|
}
|
|
if (-not (Test-Path -LiteralPath (Join-Path $Kit 'bin\sh.exe'))) {
|
|
Write-Error "w64devkit archive has no bin\sh.exe"
|
|
exit 1
|
|
}
|
|
Write-Host "toolchain in $Kit"
|
|
}
|
|
|
|
function Invoke-FetchSources {
|
|
param([switch]$Latest)
|
|
|
|
New-Item -ItemType Directory -Force -Path $Work | Out-Null
|
|
$previousVersion = [string]$Config.upstream.version
|
|
$previousSha = [string]$Config.upstream.sha256
|
|
if ($Latest) {
|
|
$Config.upstream.version = Get-LatestUpstream
|
|
$script:UpstreamVersion = $Config.upstream.version
|
|
$script:UpstreamUrl = "https://code.call-cc.org/releases/$UpstreamVersion/chicken-$UpstreamVersion.tar.gz"
|
|
Write-Host "latest release is $UpstreamVersion"
|
|
}
|
|
$tarball = Get-Tarball
|
|
$expected = if ($Latest) { '' } else { $previousSha }
|
|
$have = Test-Path -LiteralPath $tarball
|
|
$actual = if ($have) { Get-FileSha256 $tarball } else { '' }
|
|
$matches = -not $Latest -and $have -and $expected -and ($actual -eq $expected.ToLower())
|
|
if (-not $matches) {
|
|
Write-Host "downloading $UpstreamUrl"
|
|
& curl.exe -fL --retry 2 -o $tarball $UpstreamUrl
|
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
$actual = Get-FileSha256 $tarball
|
|
}
|
|
if ($Latest -or -not $previousSha) {
|
|
if ($Config.upstream.version -ne $previousVersion -or $actual -ne $previousSha.ToLower()) {
|
|
$Config.upstream.sha256 = $actual
|
|
Save-ProjectConfig $Config
|
|
Write-Host "recorded $UpstreamVersion $actual in chicken.json"
|
|
}
|
|
} elseif ($actual -ne $expected.ToLower()) {
|
|
Write-Error "sha256 mismatch for $($Config.upstream.version). Clear upstream.sha256 in chicken.json to record the new tarball."
|
|
exit 1
|
|
}
|
|
if (Test-Path -LiteralPath $Src) {
|
|
Remove-Item -LiteralPath $Src -Recurse -Force
|
|
}
|
|
New-Item -ItemType Directory -Force -Path $Src | Out-Null
|
|
& "$env:SystemRoot\System32\tar.exe" -xzf $tarball -C $Src --strip-components=1
|
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
Write-Host "sources in $Src"
|
|
}
|
|
|
|
function Invoke-Patch {
|
|
if (-not (Test-Path -LiteralPath (Join-Path $Src 'configure'))) {
|
|
Write-Error "no sources; run: .\ck.ps1 fetch-sources"
|
|
exit 1
|
|
}
|
|
$stamp = Join-Path $Src '.patched'
|
|
if (Test-Path -LiteralPath $stamp) {
|
|
Write-Host "already patched"
|
|
return
|
|
}
|
|
$patches = Get-ChildItem -Path (Join-Path $Root 'patches') -Filter '*.patch' | Sort-Object Name
|
|
Push-Location $Root
|
|
try {
|
|
foreach ($patch in $patches) {
|
|
Write-Host "applying $($patch.Name)"
|
|
git apply --unsafe-paths --directory=work/src -p1 -- $patch.FullName
|
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
}
|
|
} finally {
|
|
Pop-Location
|
|
}
|
|
$generated = Join-Path $Src 'posixwin.c'
|
|
if (Test-Path -LiteralPath $generated) {
|
|
(Get-Item -LiteralPath $generated).LastWriteTime = Get-Date
|
|
}
|
|
Set-Content -LiteralPath $stamp -Value "patched`n" -Encoding ascii
|
|
Write-Host "patched"
|
|
}
|
|
|
|
function Invoke-Build {
|
|
Invoke-Kit 'sh ./build.sh'
|
|
}
|
|
|
|
function Invoke-Test {
|
|
Invoke-Kit 'sh ./build.sh check'
|
|
}
|
|
|
|
function Get-Revision {
|
|
[string]$Config.revision
|
|
}
|
|
|
|
function Invoke-Publish {
|
|
param([switch]$Latest)
|
|
|
|
Import-DotEnv
|
|
if ([string]::IsNullOrEmpty($env:GITEA_TOKEN)) {
|
|
Write-Error "GITEA_TOKEN is not set. Put it in .env (not committed)."
|
|
exit 1
|
|
}
|
|
Invoke-FetchSources -Latest:$Latest
|
|
Invoke-Patch
|
|
Invoke-Build
|
|
Invoke-Test
|
|
|
|
if (-not (Test-Path -LiteralPath (Join-Path $Prefix 'bin\csc.exe'))) {
|
|
Write-Error "build did not install csc.exe"
|
|
exit 1
|
|
}
|
|
New-Item -ItemType Directory -Force -Path $Dist | Out-Null
|
|
$zipName = "chicken-$UpstreamVersion-mingw-x86_64.zip"
|
|
$zip = Join-Path $Dist $zipName
|
|
if (Test-Path -LiteralPath $zip) { Remove-Item -LiteralPath $zip -Force }
|
|
& "$env:SystemRoot\System32\tar.exe" -a -cf $zip -C $Prefix .
|
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
|
|
$tag = "$($Config.upstream.version)-win$(Get-Revision)"
|
|
$headers = @{ Authorization = "token $($env:GITEA_TOKEN)" }
|
|
$api = "$($Config.gitea.host)/api/v1/repos/$($Config.gitea.owner)/$($Config.gitea.repo)"
|
|
try {
|
|
Invoke-RestMethod -Headers $headers -Uri "$api/releases/tags/$tag" | Out-Null
|
|
Write-Error "release $tag already exists. Bump revision."
|
|
exit 1
|
|
} catch {
|
|
$code = $_.Exception.Response.StatusCode.value__
|
|
if ($code -ne 404) { throw }
|
|
}
|
|
$body = @{
|
|
tag_name = $tag
|
|
target_commitish = 'main'
|
|
name = "CHICKEN $UpstreamVersion for Windows"
|
|
body = "MinGW-w64 x86_64 prefix. Unpack anywhere and run bin/csc.exe."
|
|
draft = $false
|
|
prerelease = $false
|
|
} | ConvertTo-Json
|
|
$release = Invoke-RestMethod -Method Post -Headers $headers -Uri "$api/releases" -Body $body -ContentType 'application/json; charset=utf-8'
|
|
$assetUri = "$api/releases/$($release.id)/assets?name=$zipName"
|
|
$bytes = [System.IO.File]::ReadAllBytes($zip)
|
|
Invoke-RestMethod -Method Post -Headers $headers -Uri $assetUri -Body $bytes -ContentType 'application/octet-stream' | Out-Null
|
|
Write-Host "published $tag"
|
|
}
|
|
|
|
switch ($Command) {
|
|
'bootstrap' { Invoke-Bootstrap }
|
|
'fetch-sources' { Invoke-FetchSources -Latest:$Latest }
|
|
'patch' { Invoke-Patch }
|
|
'build' { Invoke-Build }
|
|
'test' { Invoke-Test }
|
|
'publish' { Invoke-Publish -Latest:$Latest }
|
|
'clean' {
|
|
foreach ($target in @($Src, $Prefix, $Dist)) {
|
|
if (Test-Path -LiteralPath $target) {
|
|
Remove-Item -LiteralPath $target -Recurse -Force
|
|
}
|
|
}
|
|
Get-ChildItem -Path $Work -Filter 'chicken-*.tar.gz' -ErrorAction SilentlyContinue |
|
|
Remove-Item -Force
|
|
Write-Host "removed build outputs in $Work (kept $Kit)"
|
|
}
|
|
}
|