<# .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