| 1 | #requires -Version 7.0 |
| 2 | [CmdletBinding()] |
| 3 | param( |
| 4 | [string]$DependencyCache, |
| 5 | [string]$OutputDirectory, |
| 6 | [string]$MsysDirectory = $env:CAGE_MSYS2_ROOT |
| 7 | ) |
| 8 | . (Join-Path $PSScriptRoot 'windows-common.ps1') |
| 9 | if (-not $IsWindows) { throw 'Build the Windows QEMU runtime in an MSYS2 UCRT64 environment on Windows.' } |
| 10 | $repository = Split-Path $PSScriptRoot -Parent |
| 11 | if (-not $DependencyCache) { $DependencyCache = Join-Path $repository '_output/windows-deps' } |
| 12 | if (-not $OutputDirectory) { $OutputDirectory = Join-Path $DependencyCache 'qemu-cage' } |
| 13 | $DependencyCache = [IO.Path]::GetFullPath($DependencyCache) |
| 14 | $OutputDirectory = [IO.Path]::GetFullPath($OutputDirectory) |
| 15 | $dependencies = Get-Content (Join-Path $repository 'packaging/windows/dependencies.json') -Raw | ConvertFrom-Json |
| 16 | $patchDirectory = Join-Path $repository 'patches/qemu' |
| 17 | $inputs = @((Join-Path $PSScriptRoot 'build-qemu-windows.sh'), $PSCommandPath) + @(Get-ChildItem $patchDirectory -Filter '*.patch' | Sort-Object Name | ForEach-Object FullName) |
| 18 | $fingerprintText = $dependencies.qemuSource.hash + $dependencies.qemuKeycodemap.hash + (($inputs | ForEach-Object { (Get-FileHash $_ -Algorithm SHA256).Hash }) -join '') |
| 19 | $fingerprint = [Convert]::ToHexString([Security.Cryptography.SHA256]::HashData([Text.Encoding]::UTF8.GetBytes($fingerprintText))) |
| 20 | $manifestPath = Join-Path $OutputDirectory 'cage-build.json' |
| 21 | if (Test-Path $manifestPath) { |
| 22 | $manifest = Get-Content $manifestPath -Raw | ConvertFrom-Json |
| 23 | if ($manifest.fingerprint -eq $fingerprint) { |
| 24 | foreach ($file in $manifest.files) { |
| 25 | if ((Get-FileHash (Join-Path $OutputDirectory $file.name) -Algorithm SHA256).Hash -ne $file.sha256) { |
| 26 | throw 'The cached QEMU runtime differs from its build manifest. Use a new output directory.' |
| 27 | } |
| 28 | } |
| 29 | Write-Host "Verified Windows QEMU runtime at $OutputDirectory" |
| 30 | return |
| 31 | } |
| 32 | } |
| 33 | if (-not $MsysDirectory) { |
| 34 | $MsysDirectory = @('C:\msys64', 'D:\msys64') | Where-Object { Test-Path (Join-Path $_ 'ucrt64/bin/gcc.exe') } | Select-Object -First 1 |
| 35 | } |
| 36 | if (-not $MsysDirectory -or -not (Test-Path (Join-Path $MsysDirectory 'ucrt64/bin/pkg-config.exe'))) { |
| 37 | throw 'Set CAGE_MSYS2_ROOT to MSYS2 with the UCRT64 QEMU build dependencies described in docs/windows.md.' |
| 38 | } |
| 39 | $archive = Get-VerifiedGitArchive $dependencies.qemuSource $DependencyCache |
| 40 | $stage = Join-Path $repository ('_output/windows-qemu-' + [guid]::NewGuid().ToString('N')) |
| 41 | New-Item -ItemType Directory -Path $stage -Force | Out-Null |
| 42 | Invoke-BuildTool 'tar.exe' @('-xf', $archive, '-C', $stage) |
| 43 | $source = Join-Path $stage $dependencies.qemuSource.prefix.TrimEnd('/') |
| 44 | $keymapArchive = Get-VerifiedDependency $dependencies.qemuKeycodemap $DependencyCache |
| 45 | Invoke-BuildTool 'tar.exe' @('-xf', $keymapArchive, '-C', (Join-Path $source 'subprojects')) |
| 46 | Move-Item (Join-Path $source ('subprojects/keycodemapdb-' + $dependencies.qemuKeycodemap.version)) (Join-Path $source 'subprojects/keycodemapdb') |
| 47 | $savedSystem, $savedHere = $env:MSYSTEM, $env:CHERE_INVOKING |
| 48 | try { |
| 49 | $env:MSYSTEM = 'UCRT64' |
| 50 | $env:CHERE_INVOKING = '1' |
| 51 | Invoke-BuildTool (Join-Path $MsysDirectory 'usr/bin/bash.exe') @('-l', (Join-Path $PSScriptRoot 'build-qemu-windows.sh'), |
| 52 | $source, (Join-Path $stage 'build'), (Join-Path $stage 'runtime'), $patchDirectory) |
| 53 | $built = Join-Path $stage 'runtime' |
| 54 | $files = @(Get-ChildItem $built -File | ForEach-Object { |
| 55 | @{ name = $_.Name; sha256 = (Get-FileHash $_.FullName -Algorithm SHA256).Hash } |
| 56 | }) |
| 57 | @{ fingerprint = $fingerprint; source = $dependencies.qemuSource.commit; files = $files } | |
| 58 | ConvertTo-Json -Depth 4 | Set-Content (Join-Path $built 'cage-build.json') |
| 59 | New-Item -ItemType Directory -Path $OutputDirectory -Force | Out-Null |
| 60 | Copy-Item (Join-Path $built '*') $OutputDirectory -Force |
| 61 | } finally { |
| 62 | $env:MSYSTEM = $savedSystem |
| 63 | $env:CHERE_INVOKING = $savedHere |
| 64 | } |
| 65 | Write-Host "Built Windows QEMU runtime at $OutputDirectory" |