| 1 | # build.ps1 - Windows MinGW-w64 static build for plugin_netcheck |
| 2 | # |
| 3 | # Usage (from the repo root, PowerShell): |
| 4 | # powershell -ExecutionPolicy Bypass -File build.ps1 |
| 5 | # |
| 6 | # Produces .\plugin_netcheck.exe (static PE, runs on any Windows 10/11 x64 node). |
| 7 | # Requires x86_64-w64-mingw32-gcc or a WinLibs/UCRT gcc on PATH (or C:\gcc16\bin). |
| 8 | # |
| 9 | # The link needs -lws2_32 and -liphlpapi: Winsock for getaddrinfo/connect and |
| 10 | # GetAdaptersAddresses for the interface table. |
| 11 | |
| 12 | $ErrorActionPreference = "Stop" |
| 13 | $ScriptDir = $PSScriptRoot |
| 14 | Push-Location $ScriptDir |
| 15 | try { |
| 16 | $Src = Join-Path $ScriptDir "src\plugin_netcheck.c" |
| 17 | $Out = Join-Path $ScriptDir "plugin_netcheck.exe" |
| 18 | $Cc = "x86_64-w64-mingw32-gcc" |
| 19 | |
| 20 | if (-not (Test-Path $Src)) { throw "source not found: $Src" } |
| 21 | |
| 22 | # Prefer MinGW-w64, fall back to a plain gcc (WinLibs / MSYS2 UCRT) if present. |
| 23 | if (-not (Get-Command $Cc -ErrorAction SilentlyContinue)) { |
| 24 | if (Test-Path "C:\gcc16\bin\gcc.exe") { |
| 25 | $Cc = "C:\gcc16\bin\gcc.exe" |
| 26 | } elseif (Get-Command gcc -ErrorAction SilentlyContinue) { |
| 27 | $Cc = "gcc" |
| 28 | } else { |
| 29 | throw "no C compiler found (need x86_64-w64-mingw32-gcc or gcc)" |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | # Regenerate the page header so the binary always matches src/ui.html. |
| 34 | if (Get-Command python -ErrorAction SilentlyContinue) { |
| 35 | python tools\gen_ui.py |
| 36 | } |
| 37 | |
| 38 | Write-Host "==> building plugin_netcheck.exe with $Cc" |
| 39 | & $Cc -O2 -std=c99 -Wall -Wextra -Wno-overlength-strings -I src -static ` |
| 40 | -o $Out $Src -lws2_32 -liphlpapi |
| 41 | if ($LASTEXITCODE -ne 0) { throw "compile failed (exit $LASTEXITCODE)" } |
| 42 | |
| 43 | if (-not (Test-Path $Out)) { throw "build produced no output" } |
| 44 | |
| 45 | # Sanity: a real PE starts with 'MZ'. An HTML error page saved as .exe does not. |
| 46 | $bytes = [System.IO.File]::ReadAllBytes($Out) |
| 47 | if ($bytes.Length -lt 2 -or $bytes[0] -ne 0x4D -or $bytes[1] -ne 0x5A) { |
| 48 | throw "output is not a PE executable (missing MZ header)" |
| 49 | } |
| 50 | |
| 51 | Write-Host "==> ok: $Out ($($bytes.Length) bytes)" |
| 52 | Write-Host " smoke test: .\plugin_netcheck.exe --meta" |
| 53 | } finally { |
| 54 | Pop-Location |
| 55 | } |