Files
chicken6-4-win/ck.ps1

207 lines
7.1 KiB
PowerShell

# Build and publish a UCRT64 CHICKEN 6 prefix.
# .\ck.ps1 fetch-sources | patch | build | test | publish | clean
param(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateSet('fetch-sources', 'patch', 'build', 'test', 'publish', 'clean')]
[string]$Command
)
$ErrorActionPreference = 'Stop'
$Root = $PSScriptRoot
$Work = Join-Path $Root 'work'
$Src = Join-Path $Work 'src'
$Prefix = Join-Path $Work 'prefix'
$Dist = Join-Path $Work 'dist'
$UpstreamVersion = '6.0.0'
$UpstreamSha256 = '92835552b1b687ad26737e429b5aba36510bf429f8816ec0f6d336c8cb41f443'
$UpstreamUrl = "https://code.call-cc.org/releases/$UpstreamVersion/chicken-$UpstreamVersion.tar.gz"
$Gitea = 'https://git.kotobank.ch'
$Owner = 'pkulev'
$RepoName = 'chicken6-4-win'
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 Find-Bash {
$roots = @()
if ($env:MSYS2_ROOT) { $roots += $env:MSYS2_ROOT }
$roots += (Join-Path $env:USERPROFILE 'scoop\apps\msys2\current')
$roots += 'C:\msys64'
foreach ($candidateRoot in $roots) {
if (-not $candidateRoot) { continue }
$candidate = Join-Path $candidateRoot 'usr\bin\bash.exe'
if (Test-Path -LiteralPath $candidate) { return $candidate }
}
Write-Error "MSYS2 bash.exe not found. Install MSYS2 (scoop install msys2) or set MSYS2_ROOT."
exit 1
}
function To-Msys([string]$Path) {
$full = (Resolve-Path -LiteralPath $Path).Path -replace '\\', '/'
if ($full -match '^([A-Za-z]):(.*)$') {
return '/' + $Matches[1].ToLower() + $Matches[2]
}
return $full
}
function Invoke-Ucrt([string]$Script) {
$bash = Find-Bash
$env:MSYSTEM = 'UCRT64'
$env:CHERE_INVOKING = '1'
$env:MSYS2_PATH_TYPE = 'strict'
$unix = To-Msys $Root
& $bash -lc "cd '$unix' && $Script"
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
}
function Get-Tarball {
Join-Path $Work "chicken-$UpstreamVersion.tar.gz"
}
function Test-Sha256([string]$Path, [string]$Expected) {
$actual = (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLower()
if ($actual -ne $Expected.ToLower()) {
Write-Error "sha256 mismatch for $Path"
exit 1
}
}
function Invoke-FetchSources {
New-Item -ItemType Directory -Force -Path $Work | Out-Null
$tarball = Get-Tarball
$fresh = -not (Test-Path -LiteralPath $tarball)
if (-not $fresh) {
$actual = (Get-FileHash -LiteralPath $tarball -Algorithm SHA256).Hash.ToLower()
$fresh = $actual -ne $UpstreamSha256.ToLower()
}
if ($fresh) {
Write-Host "downloading $UpstreamUrl"
& curl.exe -fL --retry 2 -o $tarball $UpstreamUrl
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
}
Test-Sha256 $tarball $UpstreamSha256
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) {
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-Ucrt './build.sh'
}
function Invoke-Test {
Invoke-Ucrt 'export PATH="$(cygpath -u "$PWD/work/prefix")/bin:$PATH"; make -C work/src check'
}
function Get-Revision {
$path = Join-Path $Root 'revision'
return (Get-Content -LiteralPath $path -Raw).Trim()
}
function Invoke-Publish {
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
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-ucrt64-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 = "$UpstreamVersion-win$(Get-Revision)"
$headers = @{ Authorization = "token $($env:GITEA_TOKEN)" }
$api = "$Gitea/api/v1/repos/$Owner/$RepoName"
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 = "UCRT64 x86_64 prefix. Unpack anywhere; csc needs ``-libdir <prefix>/lib``."
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) {
'fetch-sources' { Invoke-FetchSources }
'patch' { Invoke-Patch }
'build' { Invoke-Build }
'test' { Invoke-Test }
'publish' { Invoke-Publish }
'clean' {
if (Test-Path -LiteralPath $Work) {
Remove-Item -LiteralPath $Work -Recurse -Force
}
Write-Host "removed $Work"
}
}