Files
ExecuteR/scripts/Build-ScreenHost.ps1
Alphaeus Mote 890bab034b
CI / build-test (push) Successful in 38m1s
CI / images (push) Has been skipped
Release / release (push) Successful in 19m58s
Release / images (push) Successful in 12m49s
fix: devices follow the real server address; static screen host
- PUBLIC_SERVER_URL and TRUSTED_HOSTS left at .env.example's sample
  (control.example.com, any RFC 2606 name) are ignored with a startup
  warning: links follow the forwarded headers and devices download from
  the server URL they already use. .env.example ships them empty and
  deploy.sh clears sample values from an existing .env.
- The screen host is built from its own folder so .cargo/config.toml's
  +crt-static applies; the shipped exe imported VCRUNTIME140.dll and died
  without output on devices without the Visual C++ runtime ("the screen
  host gave no answer"). The build now refuses an exe with CRT imports.
- A failed screen host probe is retried with backoff (30 s up to 10 min)
  and the inventory is republished once it answers; the warning and the
  reason carry the exit code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 20:02:03 -05:00

86 lines
3.8 KiB
PowerShell

<#
.SYNOPSIS
Builds the Remote View screen host (src/ScreenHost, Rust) for Windows x64 and
returns the path of ExecuteR-Screen.exe, or nothing when it cannot be built here.
The screen host ships beside the agent (zip, MSI, the portable package Windows
PE uses). Until src/ScreenHost exists in this branch, or where cargo is not
installed, this returns nothing and packaging goes on without it: the agent
then reports Remote View as unavailable ("Requires the screen component.").
Linux and macOS builds of the screen host are not packaged yet.
.EXAMPLE
$exe = ./scripts/Build-ScreenHost.ps1
#>
[CmdletBinding()]
param(
[string]$Target = 'x86_64-pc-windows-msvc',
# The build's one version (scripts/Get-Version.ps1) for the exe's version resource;
# without it the resource carries Cargo.toml's version.
[string]$Version,
# Fail instead of skipping when the source is there but cannot be built.
[switch]$Require
)
$ErrorActionPreference = 'Stop'
$repoRoot = Split-Path -Parent $PSScriptRoot
$source = Join-Path $repoRoot 'src/ScreenHost'
$manifest = Join-Path $source 'Cargo.toml'
if (-not (Test-Path $manifest)) {
Write-Host "Screen host: src/ScreenHost is not in this tree; packaging without it."
return
}
$cargo = Get-Command cargo -ErrorAction SilentlyContinue
if (-not $cargo) {
$userCargo = Join-Path $HOME '.cargo/bin/cargo.exe'
if (Test-Path $userCargo) { $cargo = Get-Item $userCargo }
}
if (-not $cargo) {
$message = 'Screen host: cargo is not installed (rustup, stable, x86_64-pc-windows-msvc); packaging without it.'
if ($Require) { throw $message }
Write-Warning $message
return
}
$cargoPath = if ($cargo -is [System.Management.Automation.CommandInfo]) { $cargo.Source } else { $cargo.FullName }
Write-Host "Screen host: cargo build --release --target $Target"
# build.rs reads EXECUTER_VERSION for the exe's file and product version.
$env:EXECUTER_VERSION = $Version
# Cargo reads .cargo/config.toml from the folder it runs in, not the manifest's: run it there,
# or the static CRT (+crt-static) is lost and the exe needs the Visual C++ runtime installed.
Push-Location $source
try {
& $cargoPath build --release --locked --target $Target --manifest-path $manifest | Out-Host
if ($LASTEXITCODE -ne 0) {
# A lock file is optional for the first builds of the crate.
& $cargoPath build --release --target $Target --manifest-path $manifest | Out-Host
}
}
finally {
Pop-Location
Remove-Item Env:\EXECUTER_VERSION -ErrorAction SilentlyContinue
}
if ($LASTEXITCODE -ne 0) { throw "Screen host build failed ($LASTEXITCODE)." }
$targetDir = if ($env:CARGO_TARGET_DIR) { $env:CARGO_TARGET_DIR } else { Join-Path $source 'target' }
$exe = Get-ChildItem (Join-Path $targetDir "$Target/release") -Filter 'ExecuteR-Screen.exe' -File -ErrorAction SilentlyContinue |
Select-Object -First 1
if (-not $exe) {
# The binary's name comes from the crate; take it case-insensitively.
$exe = Get-ChildItem (Join-Path $targetDir "$Target/release") -File -Filter '*.exe' |
Where-Object { $_.Name -ieq 'ExecuteR-Screen.exe' -or $_.Name -ieq 'executer-screen.exe' } |
Select-Object -First 1
}
if (-not $exe) { throw "Screen host: the build produced no ExecuteR-Screen.exe under $targetDir/$Target/release." }
# A device without the Visual C++ runtime (and Windows PE) cannot load an exe that imports it:
# it dies before printing anything ("the screen host gave no answer").
$imports = [regex]::Matches([Text.Encoding]::ASCII.GetString([IO.File]::ReadAllBytes($exe.FullName)),
'(?i)\b(vcruntime\d+\.dll|msvcp\d+\.dll|api-ms-win-crt-[a-z]+-l1-1-0\.dll)') | ForEach-Object Value | Sort-Object -Unique
if ($imports) { throw "Screen host: $($exe.Name) needs the C runtime ($($imports -join ', ')); it must be built with +crt-static." }
Write-Host "Screen host: $($exe.FullName)"
$exe.FullName