| 1 | #requires -Version 7.0 |
| 2 | [CmdletBinding()] |
| 3 | param([string]$MsysDirectory = $env:CAGE_MSYS2_ROOT) |
| 4 | . (Join-Path $PSScriptRoot 'windows-common.ps1') |
| 5 | if (-not $IsWindows) { throw 'Prepare the corresponding runtime sources on Windows.' } |
| 6 | $repository = Split-Path $PSScriptRoot -Parent |
| 7 | if (-not $MsysDirectory) { |
| 8 | $MsysDirectory = @('C:\msys64', 'D:\msys64') | Where-Object { Test-Path (Join-Path $_ 'ucrt64/bin/pkg-config.exe') } | Select-Object -First 1 |
| 9 | } |
| 10 | if (-not $MsysDirectory) { throw 'Set CAGE_MSYS2_ROOT to the MSYS2 UCRT64 build environment.' } |
| 11 | $runtime = Join-Path $repository '_output/windows-deps/qemu-cage' |
| 12 | & (Join-Path $PSScriptRoot 'build-qemu-windows.ps1') -OutputDirectory $runtime -MsysDirectory $MsysDirectory |
| 13 | $runtimeIdentity = Get-Content (Join-Path $runtime 'cage-build.json') -Raw | ConvertFrom-Json |
| 14 | $runtimeManifestHash = (Get-FileHash -LiteralPath (Join-Path $runtime 'cage-build.json') -Algorithm SHA256).Hash |
| 15 | $collectorHash = (Get-FileHash -LiteralPath (Join-Path $PSScriptRoot 'bundle-qemu-sources.sh') -Algorithm SHA256).Hash + (Get-FileHash -LiteralPath $PSCommandPath -Algorithm SHA256).Hash |
| 16 | $directory = Join-Path $repository '_output/windows-qemu-sources' |
| 17 | $archive = Join-Path $directory 'full-source.tar.gz' |
| 18 | $record = Join-Path $directory 'source-identity.json' |
| 19 | if ((Test-Path -LiteralPath $record) -and (Test-Path -LiteralPath $archive)) { |
| 20 | $identity = Get-Content -LiteralPath $record -Raw | ConvertFrom-Json |
| 21 | if ($identity.schema -ne 2 -or $identity.fingerprint -ne $runtimeIdentity.fingerprint -or $identity.runtimeManifestHash -ne $runtimeManifestHash -or $identity.collectorHash -ne $collectorHash -or $identity.sha256 -ne (Get-FileHash -LiteralPath $archive -Algorithm SHA256).Hash) { |
| 22 | throw 'Cached QEMU sources differ from their verified runtime identity.' |
| 23 | } |
| 24 | Write-Host 'Verified cached QEMU corresponding sources.' |
| 25 | return |
| 26 | } |
| 27 | New-Item -ItemType Directory -Path $directory -Force | Out-Null |
| 28 | $savedMsystem, $savedChere, $savedNode = $env:MSYSTEM, $env:CHERE_INVOKING, $env:CAGE_NODE |
| 29 | try { |
| 30 | $env:MSYSTEM = 'UCRT64'; $env:CHERE_INVOKING = '1' |
| 31 | $env:CAGE_NODE = Get-BuildCommand @('node.exe', 'node') |
| 32 | Invoke-BuildTool (Join-Path $MsysDirectory 'usr/bin/bash.exe') @('-l', |
| 33 | (Join-Path $PSScriptRoot 'bundle-qemu-sources.sh'), $repository, $runtime, $archive) |
| 34 | } finally { $env:MSYSTEM = $savedMsystem; $env:CHERE_INVOKING = $savedChere; $env:CAGE_NODE = $savedNode } |
| 35 | $identity = @{ schema = 2; fingerprint = $runtimeIdentity.fingerprint; runtimeManifestHash = $runtimeManifestHash; collectorHash = $collectorHash; sha256 = (Get-FileHash -LiteralPath $archive -Algorithm SHA256).Hash } |
| 36 | $identity | ConvertTo-Json | Set-Content -LiteralPath "$record.partial" -Encoding utf8 |
| 37 | Move-Item -LiteralPath "$record.partial" -Destination $record -Force |
| 38 | Write-Host 'Collected and verified QEMU corresponding sources before application signing.' |