Files
aevolution/tools/client/package-windows.ps1
T

199 lines
11 KiB
PowerShell

# M9.8e reproducible Windows x64 package of the engineering client (ADR-0086).
# Builds the client-package preset (static MSVC runtime, /Brepro) at the current
# commit, runs its client/network tests, stages an explicit allowlist with licence
# texts, build identity and SHA256SUMS, refuses credentials and profile paths, and
# writes a deterministic ZIP plus its .sha256. Publication is a separate step.
param(
[string]$OutputDirectory = '',
# Package uncommitted tracked changes; the identity is then marked "-dirty".
[switch]$AllowDirty,
# Delete build/client-package first (reproducibility checks).
[switch]$Clean
)
$ErrorActionPreference = 'Stop'
. (Join-Path $PSScriptRoot 'package-common.ps1')
$root = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
if (!$OutputDirectory) { $OutputDirectory = Join-Path $root 'build\packages' }
$utf8 = New-Object Text.UTF8Encoding $false
function Invoke-Git([string[]]$Arguments) {
$output = & git -C $root @Arguments
if ($LASTEXITCODE -ne 0) { throw "git $($Arguments -join ' ') failed" }
$output
}
function Write-Text([string]$Path, [string[]]$Lines) {
[IO.File]::WriteAllText($Path, (($Lines -join "`r`n") + "`r`n"), $utf8)
}
# Lines from the first line matching $From up to and including the first later line
# matching $To (or the end of the file when $To is empty).
function Get-Block([string]$Path, [string]$From, [string]$To = '') {
$lines = [IO.File]::ReadAllLines($Path)
$start = -1
for ($i = 0; $i -lt $lines.Length; ++$i) { if ($lines[$i] -match $From) { $start = $i; break } }
if ($start -lt 0) { throw "Notice not found in $Path" }
$end = $lines.Length - 1
if ($To) {
$end = -1
for ($i = $start; $i -lt $lines.Length; ++$i) { if ($lines[$i] -match $To) { $end = $i; break } }
if ($end -lt 0) { throw "Notice end not found in $Path" }
}
$lines[$start..$end]
}
function Read-Pin([string]$File, [string]$Name) {
$text = [IO.File]::ReadAllText((Join-Path $root "tools\$File"))
if ($text -notmatch "set\($Name `"([^`"]+)`"\)") { throw "$Name not found in $File" }
$matches[1]
}
$commit = (Invoke-Git @('rev-parse', 'HEAD')).Trim()
$dirty = [bool](Invoke-Git @('status', '--porcelain', '--untracked-files=no'))
if ($dirty -and !$AllowDirty) {
throw 'Tracked files have uncommitted changes: commit them, or pass -AllowDirty (identity marked dirty).'
}
$epoch = [long](Invoke-Git @('show', '-s', '--format=%ct', 'HEAD')).Trim()
$commitTime = [DateTimeOffset]::FromUnixTimeSeconds($epoch)
$identity = 'g' + $commit.Substring(0, 12) + $(if ($dirty) { '-dirty' } else { '' })
$name = "evo-client-windows-x64-$identity"
$build = Join-Path $root 'build\client-package'
if ($Clean -and (Test-Path -LiteralPath $build)) { Remove-Item -LiteralPath $build -Recurse -Force }
$log = Join-Path $root 'build\client-package.log'
New-Item -ItemType Directory -Force (Split-Path -Parent $log) | Out-Null
# A separate process (stderr in its own file): Windows PowerShell 5.1 would turn
# harmless native stderr lines into terminating errors here.
$check = Start-Process -FilePath 'powershell.exe' -NoNewWindow -Wait -PassThru `
-RedirectStandardOutput $log -RedirectStandardError "$log.err" -ArgumentList @(
'-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', "`"$(Join-Path $root 'tools\windows-check.ps1')`"",
'-Preset', 'client-package', '-Action', 'check', '-Define', "EVO_BUILD_IDENTITY=$identity")
if ($check.ExitCode -ne 0) { throw "Package build or tests failed; see $log and $log.err" }
$summary = Select-String -LiteralPath $log -Pattern '(\d+)% tests passed, (\d+) tests failed out of (\d+)' | Select-Object -Last 1
if (!$summary -or $summary.Matches[0].Groups[2].Value -ne '0') { throw "No passing test summary in $log" }
$tests = "client-package CTest (labels client|network): $($summary.Matches[0].Groups[3].Value) passed, 0 failed"
$exe = Join-Path $build 'evo-client.exe'
$version = (& $exe --version | Out-String).Trim()
if ($LASTEXITCODE -ne 0 -or $version -notmatch [regex]::Escape("evo-client $identity ")) {
throw "evo-client --version does not report $identity"
}
$cache = [IO.File]::ReadAllText((Join-Path $build 'CMakeCache.txt'))
if ($cache -notmatch 'CMAKE_MSVC_RUNTIME_LIBRARY:[A-Z]+=MultiThreaded\r?\n') { throw 'Package build is not using the static runtime' }
if ($cache -notmatch 'CMAKE_COMMAND:INTERNAL=(.+?)\r?\n') { throw 'CMAKE_COMMAND not in the cache' }
$cmakeVersion = ((& $matches[1] --version | Select-Object -First 1) -replace '^cmake version ', '').Trim()
$compilerFile = Get-ChildItem -LiteralPath (Join-Path $build 'CMakeFiles') -Recurse -Filter CMakeCXXCompiler.cmake | Select-Object -First 1
$compilerText = if ($compilerFile) { [IO.File]::ReadAllText($compilerFile.FullName) } else { '' }
if ($compilerText -notmatch 'set\(CMAKE_CXX_COMPILER_ID "MSVC"\)' -or
$compilerText -notmatch 'set\(CMAKE_CXX_COMPILER_VERSION "([0-9.]+)"\)') { throw 'MSVC compiler version unknown' }
$compiler = $matches[1]
$sdlVersion = Read-Pin 'sdl3-pin.cmake' 'EVO_SDL_VERSION'
$imguiVersion = Read-Pin 'imgui-pin.cmake' 'EVO_IMGUI_VERSION'
$mbedVersion = Read-Pin 'mbedtls-pin.cmake' 'EVO_MBEDTLS_VERSION'
$sdl = Join-Path $root "build\external\sdl3\SDL3-$sdlVersion"
$imgui = Join-Path $root "build\external\imgui\imgui-$imguiVersion"
$mbed = Join-Path $root "build\external\mbedtls\mbedtls-$mbedVersion"
foreach ($source in @($sdl, $imgui, $mbed)) {
if (!(Test-Path -LiteralPath (Join-Path $source 'evo-verified.sha256'))) { throw "Unverified dependency source: $source" }
}
New-Item -ItemType Directory -Force $OutputDirectory | Out-Null
$stage = Join-Path $OutputDirectory $name
if (Test-Path -LiteralPath $stage) { Remove-Item -LiteralPath $stage -Recurse -Force }
$licenses = Join-Path $stage 'LICENSES'
New-Item -ItemType Directory -Force $licenses | Out-Null
Copy-Item -LiteralPath $exe -Destination $stage
Copy-Item -LiteralPath (Join-Path $build 'SDL3.dll') -Destination $stage
# Repository templates are re-written with CRLF so checkout line-ending settings
# cannot change the package bytes.
Write-Text (Join-Path $stage 'THIRD-PARTY-NOTICES.txt') ([IO.File]::ReadAllLines((Join-Path $PSScriptRoot 'THIRD-PARTY-NOTICES.txt')))
# Licence texts, copied from the verified sources (never hand-written).
Copy-Item -LiteralPath (Join-Path $sdl 'LICENSE.txt') -Destination (Join-Path $licenses 'SDL3.txt')
Write-Text (Join-Path $licenses 'SDL3-hidapi.txt') (
[IO.File]::ReadAllLines((Join-Path $sdl 'src\hidapi\LICENSE.txt')) + @('', '----', '') +
[IO.File]::ReadAllLines((Join-Path $sdl 'src\hidapi\LICENSE-bsd.txt')))
Copy-Item -LiteralPath (Join-Path $sdl 'src\video\yuv2rgb\LICENSE') -Destination (Join-Path $licenses 'SDL3-yuv2rgb.txt')
Write-Text (Join-Path $licenses 'SDL3-stb_image.txt') (
Get-Block (Join-Path $sdl 'src\video\stb_image.h') '^This software is available under 2 licenses')
$khronos = Join-Path $sdl 'src\video\khronos'
$khronosLines = @('Khronos API headers bundled with SDL (src/video/khronos).', '',
'KHR/khrplatform.h:') + (Get-Block (Join-Path $khronos 'KHR\khrplatform.h') 'Copyright \(c\) 2008' '^\*\*/$|^\*/$') +
@('', 'Vulkan, EGL and OpenGL ES headers (SPDX-License-Identifier: Apache-2.0; full text in Apache-2.0.txt):')
foreach ($header in @('vulkan\vulkan_core.h', 'EGL\egl.h', 'GLES2\gl2.h', 'GLES3\gl3.h')) {
$copyright = Select-String -LiteralPath (Join-Path $khronos $header) -Pattern 'Copyright .*Khronos' | Select-Object -First 1
if (!$copyright) { throw "No Khronos copyright in $header" }
$khronosLines += " $($header.Replace('\', '/')): $($copyright.Line.Trim(' *'))"
}
Write-Text (Join-Path $licenses 'Khronos.txt') $khronosLines
Write-Text (Join-Path $licenses 'Apache-2.0.txt') (
Get-Block (Join-Path $mbed 'LICENSE') '^\s+Apache License\s*$' 'END OF TERMS AND CONDITIONS')
Copy-Item -LiteralPath (Join-Path $mbed 'LICENSE') -Destination (Join-Path $licenses 'MbedTLS.txt')
Copy-Item -LiteralPath (Join-Path $imgui 'LICENSE.txt') -Destination (Join-Path $licenses 'DearImGui.txt')
Copy-Item -LiteralPath (Join-Path $imgui 'docs\FONTS.md') -Destination (Join-Path $licenses 'DearImGui-FONTS.md')
$draw = Join-Path $imgui 'imgui_draw.cpp'
Write-Text (Join-Path $licenses 'DearImGui-embedded-fonts.txt') (
@('Font data embedded in imgui_draw.cpp (MIT; MIT text as in DearImGui.txt):', '') +
(Get-Block $draw '^// MIT License / Copyright \(c\) 2004, 2005 Tristan Grimmer' '^// Download') + @('') +
(Get-Block $draw '^// Based on ProggyForever' '^// MIT license / Copyright'))
$stb = @()
foreach ($header in @('imstb_rectpack.h', 'imstb_textedit.h', 'imstb_truetype.h')) {
$stb += @("== $header ==") + (Get-Block (Join-Path $imgui $header) '^This software is available under 2 licenses') + @('')
}
Write-Text (Join-Path $licenses 'DearImGui-stb.txt') $stb
$readme = [IO.File]::ReadAllText((Join-Path $PSScriptRoot 'README-package.txt'))
$readme = $readme.Replace('{{IDENTITY}}', $identity).Replace('{{COMMIT}}', $commit).Replace(
'{{COMMIT_TIME}}', $commitTime.UtcDateTime.ToString('yyyy-MM-dd HH:mm:ss'))
[IO.File]::WriteAllText((Join-Path $stage 'README.txt'), $readme.Replace("`r`n", "`n").Replace("`n", "`r`n"), $utf8)
$pin = { param($file, $var) Read-Pin $file $var }
Write-Text (Join-Path $stage 'BUILD-INFO.txt') @(
"identity: $identity",
"commit: $commit",
"commit_time_utc: $($commitTime.UtcDateTime.ToString('yyyy-MM-ddTHH:mm:ssZ'))",
"dirty: $($dirty.ToString().ToLowerInvariant())",
'target: windows-x64',
'cmake_preset: client-package',
"cmake: $cmakeVersion",
"msvc_compiler: $compiler",
'msvc_runtime: MultiThreaded (static)',
'deterministic: /Brepro',
"sdl3: $sdlVersion sha256=$(& $pin 'sdl3-pin.cmake' 'EVO_SDL_SHA256')",
"dear_imgui: $imguiVersion sha256=$(& $pin 'imgui-pin.cmake' 'EVO_IMGUI_SHA256')",
"mbedtls: $mbedVersion sha256=$(& $pin 'mbedtls-pin.cmake' 'EVO_MBEDTLS_SHA256')",
"tests: $tests",
"version_output: $version")
$sums = foreach ($relative in (Get-RelativeFiles $stage)) {
"$(Get-Sha256 (Join-Path $stage $relative)) $relative"
}
[IO.File]::WriteAllText((Join-Path $stage 'SHA256SUMS.txt'), (($sums -join "`n") + "`n"), $utf8)
Assert-PackageContent $stage
Assert-NoSecrets $stage
foreach ($binary in @('evo-client.exe', 'SDL3.dll')) { Assert-AllowedImports (Join-Path $stage $binary) }
# Deterministic ZIP: sorted entries under one top folder, commit time stamps.
Add-Type -AssemblyName System.IO.Compression
$zip = Join-Path $OutputDirectory "$name.zip"
if (Test-Path -LiteralPath $zip) { Remove-Item -LiteralPath $zip -Force }
$stream = [IO.File]::Open($zip, [IO.FileMode]::CreateNew)
try {
$archive = New-Object IO.Compression.ZipArchive($stream, [IO.Compression.ZipArchiveMode]::Create)
try {
$stamp = New-Object DateTimeOffset($commitTime.UtcDateTime.Ticks, [TimeSpan]::Zero)
foreach ($relative in (Get-RelativeFiles $stage)) {
$entry = $archive.CreateEntry("$name/$relative", [IO.Compression.CompressionLevel]::Optimal)
$entry.LastWriteTime = $stamp
$out = $entry.Open()
try {
$bytes = [IO.File]::ReadAllBytes((Join-Path $stage $relative))
$out.Write($bytes, 0, $bytes.Length)
} finally { $out.Dispose() }
}
} finally { $archive.Dispose() }
} finally { $stream.Dispose() }
$zipHash = Get-Sha256 $zip
[IO.File]::WriteAllText("$zip.sha256", "$zipHash $name.zip`n", $utf8)
Write-Output "package: $zip"
Write-Output "sha256: $zipHash"
Write-Output "identity: $identity"