<# .SYNOPSIS Pack the sources of the copyleft MSYS2 packages whose DLLs a runtime bundles, as a separate release asset. .DESCRIPTION Reads the "# Packages" list from a runtime's MSYS2-PACKAGES.txt, looks up each package's source name (%BASE%) and licenses in the local pacman database, and keeps the packages whose license requires source (GPL / LGPL / MPL) unless an alternative licence without those can be chosen (for example "GPL OR FTL"). For each kept package it downloads -.src.tar.zst and its .sig from repo.msys2.org (cached in -CacheDir), checks the signature against the MSYS2 keyring with gpgv, and packs them into reims-runtime-msys2-src-.tar.zst in -OutDir. The archive's hash is added to -OutDir\SHA256SUMS.txt (an existing line for it is replaced). Published in the same GitHub Release as the runtime, so the sources are offered from the same place as the binaries (GPLv2 §3, LGPL-2.1 §6). .EXAMPLE pwsh -File scripts/pack-msys2-sources.ps1 -PackagesFile MSYS2-PACKAGES.txt -Commit 3731afd -OutDir D:\Data\reims-go\releases\3731afd-candidate #> [CmdletBinding()] param( [Parameter(Mandatory)] [string]$PackagesFile, [Parameter(Mandatory)] [string]$Commit, [Parameter(Mandatory)] [string]$OutDir, [string]$Msys2 = 'D:\DevTools\msys64', [string]$CacheDir = 'D:\Data\reims-go\releases\msys2-sources-cache', [string]$TempRoot = 'D:\Build\Temp', [string]$Mirror = 'https://repo.msys2.org/mingw/sources' ) $ErrorActionPreference = 'Stop' $localDb = Join-Path $Msys2 'var\lib\pacman\local' $bash = Join-Path $Msys2 'usr\bin\bash.exe' $zstd = Join-Path $Msys2 'usr\bin\zstd.exe' function Write-Utf8([string]$path, [string]$text) { [IO.File]::WriteAllText($path, $text.Replace("`r`n", "`n"), [Text.UTF8Encoding]::new($false)) } function Read-Desc([string]$name, [string]$version) { $file = Join-Path $localDb "$name-$version\desc" if (-not (Test-Path -LiteralPath $file)) { throw "$name $version is not in the local pacman database ($file): was MSYS2 updated after the runtime was built?" } $fields = @{} $key = $null foreach ($line in Get-Content -LiteralPath $file) { if ($line -match '^%(\w+)%$') { $key = $Matches[1]; $fields[$key] = @(); continue } if ($key -and $line -ne '') { $fields[$key] += $line } } return $fields } # A licence entry needs source unless one OR-alternative is free of copyleft. # Entries prefixed "documentation:" cover only docs, which aren't shipped. function Test-NeedsSource([string[]]$licenses) { foreach ($entry in $licenses) { if ($entry -like 'documentation:*') { continue } $expr = $entry -replace '^spdx:', '' $alternatives = $expr -split '\s+OR\s+' $allCopyleft = $true foreach ($alt in $alternatives) { if ($alt -notmatch 'GPL|MPL') { $allCopyleft = $false } } if ($allCopyleft) { return $true } } return $false } function To-MsysPath([string]$winPath) { $p = $winPath -replace '\\', '/' if ($p -match '^([A-Za-z]):(.*)$') { return '/' + $Matches[1].ToLower() + $Matches[2] } return $p } # ── 1. Which packages need source ──────────────────────────── $text = Get-Content -Raw -LiteralPath $PackagesFile $section = ($text -split '# Packages[^\n]*\n', 2)[1] if (-not $section) { throw "No '# Packages' section in $PackagesFile" } $packages = $section -split "`n" | Where-Object { $_ -match '^\S+ \S+$' } | ForEach-Object { $n, $v = $_ -split ' ' $d = Read-Desc $n $v [pscustomobject]@{ Name = $n Version = $v Base = $d['BASE'][0] Licenses = $d['LICENSE'] Needs = (Test-NeedsSource $d['LICENSE']) } } $needed = @($packages | Where-Object Needs | Sort-Object Base, Version -Unique) Write-Host ("==> {0} of {1} packages need source; {2} source archives" -f @($packages | Where-Object Needs).Count, $packages.Count, $needed.Count) # ── 2. Download and verify ─────────────────────────────────── New-Item -ItemType Directory -Force -Path $CacheDir, $OutDir | Out-Null foreach ($p in $needed) { $file = "$($p.Base)-$($p.Version).src.tar.zst" foreach ($f in @($file, "$file.sig")) { $dest = Join-Path $CacheDir $f if (-not (Test-Path -LiteralPath $dest)) { Write-Host " downloading $f" Invoke-WebRequest -Uri "$Mirror/$f" -OutFile "$dest.part" -UserAgent 'reims-go-packager' Move-Item -LiteralPath "$dest.part" -Destination $dest -Force } } $cache = To-MsysPath $CacheDir $out = & $bash -lc "LC_ALL=C gpgv --keyring /etc/pacman.d/gnupg/pubring.gpg '$cache/$file.sig' '$cache/$file' 2>&1" if ($LASTEXITCODE -ne 0) { throw "Signature check failed for ${file}:`n$out" } $signer = ($out | Select-String 'Good signature from "([^"]+)"').Matches.Groups[1].Value $p | Add-Member -NotePropertyName File -NotePropertyValue $file $p | Add-Member -NotePropertyName Signer -NotePropertyValue $signer } # ── 3. Pack ────────────────────────────────────────────────── $name = "reims-runtime-msys2-src-$Commit" $stage = Join-Path $TempRoot "pack-$name" if (Test-Path -LiteralPath $stage) { Remove-Item -Recurse -Force -LiteralPath $stage } $dir = Join-Path $stage $name New-Item -ItemType Directory -Force -Path (Join-Path $dir 'sources') | Out-Null foreach ($p in $needed) { Copy-Item -LiteralPath (Join-Path $CacheDir $p.File) -Destination (Join-Path $dir 'sources') Copy-Item -LiteralPath (Join-Path $CacheDir "$($p.File).sig") -Destination (Join-Path $dir 'sources') } $rows = foreach ($p in ($packages | Sort-Object Name)) { $lic = ($p.Licenses -join '; ') -replace '\|', '/' $src = if ($p.Needs) { "``sources/$("$($p.Base)-$($p.Version).src.tar.zst")``" } else { '— (permissive licence; text in the runtime''s licenses/)' } "| ``$($p.Name -replace '^mingw-w64-clang-x86_64-', '')`` | $($p.Version) | $lic | $src |" } Write-Utf8 (Join-Path $dir 'README.md') @" # MSYS2 package sources for reims-runtime-win-x64-$Commit The runtime's ``qemu/bin`` bundles DLLs from MSYS2 CLANG64 packages. This archive holds, unmodified, the source packages of every one whose licence requires source (GPL, LGPL or MPL with no permissive alternative), exactly as MSYS2 publishes them at $Mirror, each with its detached signature (``.sig``), checked with gpgv against the MSYS2 keyring when this archive was made. A ``.src.tar.zst`` contains the PKGBUILD, the upstream source tarball and MSYS2's patches: ``makepkg-mingw`` in an MSYS2 shell rebuilds the package. | Package | Version | Licence | Source | | :-- | :-- | :-- | :-- | $($rows -join "`n") "@ $tar = Join-Path $TempRoot "$name.tar" & tar.exe -cf $tar -C $stage $name if ($LASTEXITCODE -ne 0) { throw 'tar failed' } $archive = Join-Path $OutDir "$name.tar.zst" & $zstd -T0 -3 -q -f $tar -o $archive # contents are already compressed if ($LASTEXITCODE -ne 0) { throw 'zstd failed' } Remove-Item -LiteralPath $tar -Force Remove-Item -Recurse -Force -LiteralPath $stage # ── 4. SHA256SUMS.txt ──────────────────────────────────────── $hash = (Get-FileHash -Algorithm SHA256 -LiteralPath $archive).Hash.ToLowerInvariant() $sums = Join-Path $OutDir 'SHA256SUMS.txt' $lines = @() if (Test-Path -LiteralPath $sums) { $lines = @(Get-Content -LiteralPath $sums | Where-Object { $_ -and $_ -notmatch " $([regex]::Escape("$name.tar.zst"))$" }) } $lines += "$hash $name.tar.zst" Write-Utf8 $sums (($lines -join "`n") + "`n") Write-Host '==> Done' -ForegroundColor Green '{0} {1:N1} MB ({2} source archives, all signatures good)' -f $archive, ((Get-Item $archive).Length / 1MB), $needed.Count