| 1 | #requires -Version 7.0 |
| 2 | [CmdletBinding()] |
| 3 | param( |
| 4 | [switch]$Release, |
| 5 | [switch]$Sign, |
| 6 | [switch]$SkipUI, |
| 7 | [string]$OutputDirectory, |
| 8 | [string]$DependencyCache |
| 9 | ) |
| 10 | . (Join-Path $PSScriptRoot 'windows-common.ps1') |
| 11 | $repository = Split-Path $PSScriptRoot -Parent |
| 12 | if (-not $OutputDirectory) { $OutputDirectory = Join-Path $repository '_output/windows-amd64/app' } |
| 13 | if (-not $DependencyCache) { $DependencyCache = Join-Path $repository '_output/windows-deps' } |
| 14 | $OutputDirectory = [IO.Path]::GetFullPath($OutputDirectory) |
| 15 | if (Test-Path -LiteralPath (Join-Path $OutputDirectory 'Uninstall.exe')) { throw 'Do not build over an installed application. Choose a separate output directory and use the installer to upgrade.' } |
| 16 | $version = Get-CageVersion $repository |
| 17 | $signed = $Release -or $Sign |
| 18 | if ($signed) { Assert-WindowsSigningConfiguration } |
| 19 | $go = Get-BuildCommand @('go') |
| 20 | $dotnet = Get-BuildCommand @('dotnet') |
| 21 | $node = Get-BuildCommand @('node') |
| 22 | $npm = Get-BuildCommand @('npm.cmd', 'npm') |
| 23 | $sevenZip = Get-BuildCommand @('7z.exe', '7zz', '7z') |
| 24 | $dependencies = Get-Content -LiteralPath (Join-Path $repository 'packaging/windows/dependencies.json') -Raw | ConvertFrom-Json |
| 25 | $stage = Join-Path $repository ("_output/windows-build-" + [guid]::NewGuid().ToString('N')) |
| 26 | $savedGoOS, $savedGoArch, $savedCGO = $env:GOOS, $env:GOARCH, $env:CGO_ENABLED |
| 27 | New-Item -ItemType Directory -Path $stage -Force | Out-Null |
| 28 | Push-Location $repository |
| 29 | try { |
| 30 | if (-not $SkipUI) { |
| 31 | Invoke-BuildTool $npm @('--prefix', 'ui', 'ci', '--no-audit', '--no-fund') |
| 32 | Invoke-BuildTool $npm @('run', '--prefix', 'ui', 'build') |
| 33 | } else { |
| 34 | Invoke-BuildTool $node @('ui/scripts/verify-build.mjs') |
| 35 | } |
| 36 | $limaVersion = (Get-Content 'internal/limaembed/lima-version' -Raw).Trim() |
| 37 | $assets = Join-Path $repository 'internal/limaembed/assets' |
| 38 | $guest = Join-Path $assets 'lima-guestagent.Linux-x86_64' |
| 39 | $env:GOOS = 'linux'; $env:GOARCH = 'amd64'; $env:CGO_ENABLED = '0' |
| 40 | Invoke-BuildTool $go @('build', '-trimpath', '-ldflags', "-s -w -X github.com/lima-vm/lima/v2/pkg/version.Version=$limaVersion", '-o', $guest, 'github.com/lima-vm/lima/v2/cmd/lima-guestagent') |
| 41 | $inputStream = [IO.File]::OpenRead($guest) |
| 42 | $outputStream = [IO.File]::Create("$guest.gz") |
| 43 | try { |
| 44 | $gzip = [IO.Compression.GZipStream]::new($outputStream, [IO.Compression.CompressionLevel]::SmallestSize, $true) |
| 45 | try { $inputStream.CopyTo($gzip) } finally { $gzip.Dispose() } |
| 46 | } finally { $inputStream.Dispose(); $outputStream.Dispose() } |
| 47 | Remove-Item -LiteralPath $guest |
| 48 | New-Item -ItemType Directory -Path (Join-Path $assets 'templates') -Force | Out-Null |
| 49 | Copy-Item 'third_party/lima/templates/*' (Join-Path $assets 'templates') -Recurse -Force |
| 50 | $app = Join-Path $stage 'app' |
| 51 | $resources = Join-Path $app 'resources' |
| 52 | New-Item -ItemType Directory -Path $resources -Force | Out-Null |
| 53 | New-Item -ItemType Directory -Path (Join-Path $resources 'runtime') -Force | Out-Null |
| 54 | $guestSetup = New-Item -ItemType Directory -Path (Join-Path $resources 'guest-os') |
| 55 | Copy-Item -LiteralPath 'scripts/guest-os/new-windows-test-vm.ps1' -Destination (Join-Path $guestSetup.FullName 'new-windows-guest.ps1') |
| 56 | Copy-Item -LiteralPath 'scripts/guest-os/windows-host-image.ps1' -Destination $guestSetup.FullName |
| 57 | foreach ($file in @('windows-guest-media.ps1', 'install-guest-powershell.cmd', 'setup-windows-guest.ps1', 'complete-windows-guest.ps1', 'install-windows-host-manager.ps1', 'install-windows-workbench-components.ps1', 'initialize-windows-workbench-user.ps1', 'windows-host-enrollment.ps1', 'maintain-windows-workbench.ps1', 'update-windows-workbench-components.ps1', 'publish-windows-component-manifest.ps1', 'windows-workbench-packages.ps1')) { |
| 58 | Copy-Item -LiteralPath (Join-Path 'scripts/guest-os' $file) -Destination $guestSetup.FullName |
| 59 | } |
| 60 | $bridgeSetup = New-Item -ItemType Directory -Path (Join-Path $guestSetup.FullName 'guest-clipboard-windows') |
| 61 | foreach ($file in @('install.ps1', 'install-network.ps1')) { |
| 62 | Copy-Item -LiteralPath (Join-Path 'scripts/guest-os/guest-clipboard-windows' $file) -Destination $bridgeSetup.FullName |
| 63 | } |
| 64 | & (Join-Path $PSScriptRoot 'guest-os/build-windows-workbench-payload.ps1') -Destination (Join-Path $guestSetup.FullName 'payload') |
| 65 | $tailnetAssets = Join-Path $repository 'internal/tailnetassets/assets' |
| 66 | New-Item -ItemType Directory -Path $tailnetAssets -Force | Out-Null |
| 67 | foreach ($tailnetArch in @('amd64','arm64')) { |
| 68 | $env:GOOS = 'linux'; $env:GOARCH = $tailnetArch; $env:CGO_ENABLED = '0' |
| 69 | $tailnetOutput = Join-Path $stage ('cage-tailnet-linux-'+$tailnetArch) |
| 70 | Push-Location (Join-Path $repository 'tools/tailnet') |
| 71 | try { Invoke-BuildTool $go @('build','-trimpath','-ldflags','-s -w','-o',$tailnetOutput,'.') } finally { Pop-Location } |
| 72 | $source = [IO.File]::OpenRead($tailnetOutput) |
| 73 | $target = [IO.File]::Create((Join-Path $tailnetAssets ('cage-tailnet-linux-'+$tailnetArch+'.gz'))) |
| 74 | try { $compressed = [IO.Compression.GZipStream]::new($target,[IO.Compression.CompressionMode]::Compress); try { $source.CopyTo($compressed) } finally { $compressed.Dispose() } } finally { $source.Dispose(); $target.Dispose() } |
| 75 | } |
| 76 | $env:GOARCH = 'amd64' |
| 77 | $env:GOOS = 'windows' |
| 78 | $tags = 'external_hcs,external_wsl2,yq_noxml,yq_nolua,yq_noini,yq_nohcl,yq_notoml,yq_noprops,yq_nocsv,yq_nouri,yq_nobase64,yq_nosh,yq_noshell' |
| 79 | $ldflags = "-s -w -X github.com/sylvinus/agent-vm/internal/version.Version=$version -X github.com/lima-vm/lima/v2/pkg/version.Version=$limaVersion" |
| 80 | Invoke-BuildTool $go @('build', '-trimpath', '-tags', $tags, '-ldflags', $ldflags, '-o', (Join-Path $resources 'cage.exe'), './cmd/cage') |
| 81 | Invoke-BuildTool (Join-Path $resources 'cage.exe') @('verify-vm-assets') |
| 82 | Invoke-BuildTool (Join-Path $resources 'cage.exe') @('verify-web-assets') |
| 83 | Invoke-BuildTool $dotnet @('publish', 'desktop/windows/Cage.Windows.csproj', '-c', 'Release', '-r', 'win-x64', '--self-contained', 'true', |
| 84 | '-p:EnableWindowsTargeting=true', '-p:PublishSingleFile=true', '-p:RestoreLockedMode=true', "-p:Version=$version", "-p:InformationalVersion=$version", '-o', $app) |
| 85 | Get-ChildItem -LiteralPath $app -File | Where-Object { $_.Extension -in @('.pdb', '.xml') } | Remove-Item -Force |
| 86 | node scripts/bundle-cloud-kit.mjs $resources |
| 87 | if ($LASTEXITCODE -ne 0) { throw "Cloud deployment kit verification failed." } |
| 88 | $coreArchive = Get-VerifiedDependency $dependencies.mihomo $DependencyCache |
| 89 | $core = Join-Path $stage 'mihomo' |
| 90 | Expand-BuildArchive $coreArchive $core $sevenZip |
| 91 | $coreExe = @(Get-ChildItem -LiteralPath $core -Filter '*.exe' -Recurse) |
| 92 | if ($coreExe.Count -ne 1) { throw 'Unexpected Mihomo archive layout.' } |
| 93 | Copy-Item -LiteralPath $coreExe[0].FullName -Destination (Join-Path $resources 'mihomo.exe') |
| 94 | $sshArchive = Get-VerifiedDependency $dependencies.openssh $DependencyCache |
| 95 | $ssh = Join-Path $stage 'openssh' |
| 96 | Expand-BuildArchive $sshArchive $ssh $sevenZip |
| 97 | $sshTarget = Join-Path $resources 'runtime/openssh' |
| 98 | Copy-Item -LiteralPath (Join-Path $ssh 'OpenSSH-Win64') -Destination $sshTarget -Recurse |
| 99 | # No SSH service is installed. Only the client is used by Cage/Lima. |
| 100 | foreach ($serverFile in @('sshd.exe', 'sshd-session.exe', 'sshd-auth.exe', 'install-sshd.ps1', 'uninstall-sshd.ps1')) { |
| 101 | Remove-Item -LiteralPath (Join-Path $sshTarget $serverFile) -Force -ErrorAction SilentlyContinue |
| 102 | } |
| 103 | $qemuArchive = Get-VerifiedDependency $dependencies.qemu $DependencyCache |
| 104 | $qemuTarget = Join-Path $resources 'runtime/qemu' |
| 105 | Expand-BuildArchive $qemuArchive $qemuTarget $sevenZip |
| 106 | $qemuBuilt = Join-Path $DependencyCache 'qemu-cage' |
| 107 | & (Join-Path $PSScriptRoot 'build-qemu-windows.ps1') -DependencyCache $DependencyCache -OutputDirectory $qemuBuilt |
| 108 | $runtimeManifest = Get-Content (Join-Path $qemuBuilt 'cage-build.json') -Raw | ConvertFrom-Json |
| 109 | $ownedRuntime = @($runtimeManifest.files | ForEach-Object { $_.name }) + @('cage-build.json') |
| 110 | foreach ($name in $ownedRuntime) { |
| 111 | if ([IO.Path]::GetFileName($name) -ne $name) { throw 'Invalid QEMU runtime manifest path.' } |
| 112 | Copy-Item -LiteralPath (Join-Path $qemuBuilt $name) -Destination $qemuTarget -Force |
| 113 | } |
| 114 | # The two executables above are linked against the built dependency closure. |
| 115 | # Vendor DLLs/executables outside that closure have no runtime consumer. |
| 116 | Get-ChildItem -LiteralPath $qemuTarget -File | Where-Object { |
| 117 | $_.Extension -in @('.exe', '.dll') -and $_.Name -notin $ownedRuntime |
| 118 | } | Remove-Item -Force |
| 119 | # Ship the x64 VM only, with its original firmware, DLLs and notices. |
| 120 | Get-ChildItem -LiteralPath $qemuTarget -Filter 'qemu-system-*.exe' | Where-Object { $_.Name -ne 'qemu-system-x86_64.exe' } | Remove-Item -Force |
| 121 | Get-ChildItem -LiteralPath (Join-Path $qemuTarget 'share') -File | |
| 122 | Where-Object { $_.Name -match '^edk2-(?:aarch64|arm|loongarch64|riscv)-.*\.fd$' } | Remove-Item -Force |
| 123 | foreach ($file in @('qemu-system-x86_64.exe', 'qemu-img.exe', 'share/efi-virtio.rom', 'share/kvmvapic.bin')) { |
| 124 | if (-not (Test-Path -LiteralPath (Join-Path $qemuTarget $file))) { throw "QEMU archive lacks $file" } |
| 125 | } |
| 126 | $firmwareDirectories = & (Join-Path $qemuTarget 'qemu-system-x86_64.exe') '-L' 'help' |
| 127 | if ($LASTEXITCODE -ne 0) { throw 'Could not inspect QEMU firmware directories.' } |
| 128 | $bundledFirmware = [IO.Path]::GetFullPath((Join-Path $qemuTarget 'share')).TrimEnd('\', '/') |
| 129 | if (-not @($firmwareDirectories | Where-Object { [IO.Path]::GetFullPath($_).TrimEnd('\', '/') -eq $bundledFirmware }).Count) { |
| 130 | throw 'QEMU cannot locate its bundled firmware after relocation.' |
| 131 | } |
| 132 | Remove-Item -LiteralPath (Join-Path $qemuTarget '$PLUGINSDIR') -Recurse -Force -ErrorAction SilentlyContinue |
| 133 | Remove-Item -LiteralPath (Join-Path $qemuTarget 'qemu-uninstall.exe') -Force -ErrorAction SilentlyContinue |
| 134 | $webviewArchive = Get-VerifiedDependency $dependencies.webview2 $DependencyCache |
| 135 | Copy-Item -LiteralPath $webviewArchive -Destination (Join-Path $resources 'runtime/MicrosoftEdgeWebView2RuntimeInstallerX64.exe') |
| 136 | Invoke-BuildTool $node @('scripts/collect-app-licenses.mjs', (Join-Path $resources 'licenses')) |
| 137 | $coreSourceArchive = Get-VerifiedDependency $dependencies.mihomoSource $DependencyCache |
| 138 | $coreSource = Join-Path $stage 'mihomo-source' |
| 139 | Expand-BuildArchive $coreSourceArchive $coreSource $sevenZip |
| 140 | $coreTar = @(Get-ChildItem -LiteralPath $coreSource -Filter '*.tar') |
| 141 | if ($coreTar.Count -ne 1) { throw 'Unexpected Mihomo source archive layout.' } |
| 142 | Expand-BuildArchive $coreTar[0].FullName $coreSource $sevenZip |
| 143 | $coreLicense = Join-Path $coreSource ("mihomo-" + $dependencies.mihomo.version.TrimStart('v') + '/LICENSE') |
| 144 | if (-not (Test-Path -LiteralPath $coreLicense -PathType Leaf)) { throw 'Mihomo source license is missing.' } |
| 145 | $coreNotices = Join-Path $resources 'licenses/mihomo' |
| 146 | New-Item -ItemType Directory -Path $coreNotices -Force | Out-Null |
| 147 | Copy-Item -LiteralPath $coreLicense -Destination (Join-Path $coreNotices 'LICENSE') |
| 148 | [IO.File]::WriteAllText((Join-Path $coreNotices 'NOTICE'), "Mihomo $($dependencies.mihomo.version), unmodified separate executable.`nSource: $($dependencies.mihomo.source)`nLicense: GPL-3.0; corresponding source accompanies the installer.`n") |
| 149 | # Runtime packs and WebView2 are absent from the frontend license collector. |
| 150 | $resolvedAssets = Get-Content -LiteralPath 'desktop/windows/obj/project.assets.json' -Raw | ConvertFrom-Json |
| 151 | $nuget = @($resolvedAssets.packageFolders.PSObject.Properties.Name)[0] |
| 152 | $runtimeVersion = $dependencies.dotnet.runtimeVersion |
| 153 | $runtimePacks = @($resolvedAssets.project.frameworks.PSObject.Properties.Value.downloadDependencies | |
| 154 | Where-Object { $_.name -in @('Microsoft.NETCore.App.Runtime.win-x64', 'Microsoft.WindowsDesktop.App.Runtime.win-x64') }) |
| 155 | if ($runtimePacks.Count -ne 2 -or @($runtimePacks | Where-Object { $_.version -ne "[$runtimeVersion, $runtimeVersion]" }).Count -ne 0) { |
| 156 | throw 'Resolved .NET runtime differs from packaging/windows/dependencies.json. Use SDK 10.0.403 or review and update the runtime pin.' |
| 157 | } |
| 158 | $webviewVersion = ((Get-Content -LiteralPath 'desktop/windows/packages.lock.json' -Raw | ConvertFrom-Json).dependencies.PSObject.Properties.Value | |
| 159 | ForEach-Object { $_.PSObject.Properties } | Where-Object { $_.Name -eq 'Microsoft.Web.WebView2' } | Select-Object -First 1).Value.resolved |
| 160 | foreach ($package in @("microsoft.netcore.app.runtime.win-x64/$runtimeVersion", "microsoft.windowsdesktop.app.runtime.win-x64/$runtimeVersion", "microsoft.web.webview2/$webviewVersion")) { |
| 161 | $source = Join-Path $nuget $package |
| 162 | $target = Join-Path $resources "licenses/nuget/$package" |
| 163 | New-Item -ItemType Directory -Path $target -Force | Out-Null |
| 164 | $notices = @(Get-ChildItem -LiteralPath $source -File | Where-Object { $_.Name -match '^(LICENSE|NOTICE|THIRD.?PARTY)' }) |
| 165 | if ($notices.Count -eq 0) { throw "Missing runtime license: $package" } |
| 166 | $notices | Copy-Item -Destination $target |
| 167 | } |
| 168 | Copy-Item -LiteralPath 'packaging/windows/dependencies.json' -Destination (Join-Path $resources 'dependencies.json') |
| 169 | Copy-Item -LiteralPath 'packaging/windows/README.txt' -Destination (Join-Path $app 'README.txt') |
| 170 | Copy-Item -LiteralPath 'LICENSE' -Destination (Join-Path $app 'LICENSE.txt') |
| 171 | [IO.File]::WriteAllText((Join-Path $app 'Cage.install-id'), 'io.github.lee-xeo.claude-cage') |
| 172 | [IO.File]::WriteAllText((Join-Path $app 'version.txt'), $version) |
| 173 | if ($signed) { |
| 174 | $signTool = Get-SigningTool |
| 175 | foreach ($file in @('Cage.exe', 'resources/cage.exe', 'resources/mihomo.exe')) { Sign-CageFile (Join-Path $app $file) $signTool } |
| 176 | } else { |
| 177 | [IO.File]::WriteAllText((Join-Path $app 'DEVELOPMENT-BUILD.txt'), "Unsigned development build. Automatic update installation is disabled. Windows 10/11 runtime acceptance is still required.`n") |
| 178 | } |
| 179 | Assert-WindowsWorkbench $app |
| 180 | $previousOutput = $null |
| 181 | if (Test-Path -LiteralPath $OutputDirectory) { |
| 182 | $marker = Join-Path $OutputDirectory 'Cage.install-id' |
| 183 | if (-not (Test-Path -LiteralPath $marker) -or (Get-Content -LiteralPath $marker -Raw) -ne 'io.github.lee-xeo.claude-cage') { |
| 184 | throw 'Output directory exists and is not a Cage build.' |
| 185 | } |
| 186 | $previousOutput = $OutputDirectory + '.previous-' + [guid]::NewGuid().ToString('N') |
| 187 | Move-Item -LiteralPath $OutputDirectory -Destination $previousOutput |
| 188 | } |
| 189 | New-Item -ItemType Directory -Path (Split-Path $OutputDirectory -Parent) -Force | Out-Null |
| 190 | try { Move-Item -LiteralPath $app -Destination $OutputDirectory } |
| 191 | catch { |
| 192 | if ($previousOutput -and -not (Test-Path -LiteralPath $OutputDirectory)) { Move-Item -LiteralPath $previousOutput -Destination $OutputDirectory } |
| 193 | throw |
| 194 | } |
| 195 | if ($previousOutput) { Remove-Item -LiteralPath $previousOutput -Recurse -Force } |
| 196 | Write-Host "Built Windows x64 application: $OutputDirectory" |
| 197 | } finally { |
| 198 | Pop-Location |
| 199 | $env:GOOS = $savedGoOS; $env:GOARCH = $savedGoArch; $env:CGO_ENABLED = $savedCGO |
| 200 | if (Test-Path -LiteralPath $stage) { |
| 201 | try { Remove-Item -LiteralPath $stage -Recurse -Force -ErrorAction Stop } |
| 202 | catch { Write-Warning "Temporary build files remain at $stage. Cleanup did not complete: $($_.Exception.Message)" } |
| 203 | } |
| 204 | } |