| 1 | # build.ps1 - Windows MinGW-w64 static build for plugin_sqlite |
| 2 | # |
| 3 | # Usage (from the repo root, PowerShell): |
| 4 | # powershell -ExecutionPolicy Bypass -File build.ps1 |
| 5 | # |
| 6 | # Produces .\plugin_sqlite.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 | # No extra Win32 import libraries are needed: the SQLite amalgamation is |
| 9 | # compiled in and the plugin talks to no socket. |
| 10 | # |
| 11 | # The SQLite amalgamation is a ~9 MB single translation unit; compiling it at |
| 12 | # -O1 keeps the build near a minute on a laptop while the plugin itself is I/O |
| 13 | # bound, so -O2 in the engine buys nothing anybody can measure. |
| 14 | |
| 15 | $ErrorActionPreference = "Stop" |
| 16 | $ScriptDir = $PSScriptRoot |
| 17 | Push-Location $ScriptDir |
| 18 | try { |
| 19 | $Src = Join-Path $ScriptDir "src\plugin_sqlite.c" |
| 20 | $Amalg = Join-Path $ScriptDir "vendor\sqlite3.c" |
| 21 | $Out = Join-Path $ScriptDir "plugin_sqlite.exe" |
| 22 | $Cc = "x86_64-w64-mingw32-gcc" |
| 23 | |
| 24 | foreach ($p in @($Src, $Amalg)) { |
| 25 | if (-not (Test-Path $p)) { throw "source not found: $p" } |
| 26 | } |
| 27 | |
| 28 | # Prefer MinGW-w64, fall back to a plain gcc (WinLibs / MSYS2 UCRT) if present. |
| 29 | if (-not (Get-Command $Cc -ErrorAction SilentlyContinue)) { |
| 30 | if (Test-Path "C:\gcc16\bin\gcc.exe") { |
| 31 | $Cc = "C:\gcc16\bin\gcc.exe" |
| 32 | } elseif (Get-Command gcc -ErrorAction SilentlyContinue) { |
| 33 | $Cc = "gcc" |
| 34 | } else { |
| 35 | throw "no C compiler found (need x86_64-w64-mingw32-gcc or gcc)" |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | $Defs = @("-DSQLITE_OMIT_LOAD_EXTENSION", "-DSQLITE_THREADSAFE=0", |
| 40 | "-DSQLITE_OMIT_DEPRECATED", "-DSQLITE_DQS=0", |
| 41 | "-DSQLITE_DEFAULT_MEMSTATUS=0", "-DSQLITE_OMIT_SHARED_CACHE", |
| 42 | "-DSQLITE_MAX_ATTACHED=0") |
| 43 | |
| 44 | Write-Host "==> compiling the SQLite amalgamation (this takes a minute)" |
| 45 | & $Cc -O1 -std=c99 -Ivendor @Defs -static -c $Amalg -o sqlite3.win.o |
| 46 | if ($LASTEXITCODE -ne 0) { throw "amalgamation compile failed (exit $LASTEXITCODE)" } |
| 47 | |
| 48 | Write-Host "==> building plugin_sqlite.exe with $Cc" |
| 49 | & $Cc -O2 -std=c99 -Wall -Wextra -Isrc -Ivendor @Defs -static ` |
| 50 | -o $Out $Src sqlite3.win.o |
| 51 | if ($LASTEXITCODE -ne 0) { throw "compile failed (exit $LASTEXITCODE)" } |
| 52 | |
| 53 | if (-not (Test-Path $Out)) { throw "build produced no output" } |
| 54 | |
| 55 | # Sanity: a real PE starts with 'MZ'. An HTML error page saved as .exe does not. |
| 56 | $bytes = [System.IO.File]::ReadAllBytes($Out) |
| 57 | if ($bytes.Length -lt 2 -or $bytes[0] -ne 0x4D -or $bytes[1] -ne 0x5A) { |
| 58 | throw "output is not a PE executable (missing MZ header)" |
| 59 | } |
| 60 | |
| 61 | Write-Host "==> metadata check" |
| 62 | & $Out --meta |
| 63 | if ($LASTEXITCODE -ne 0) { throw "--meta failed" } |
| 64 | |
| 65 | $size = (Get-Item $Out).Length |
| 66 | Write-Host "" |
| 67 | Write-Host "Built $Out ($size bytes)" |
| 68 | Write-Host "Install: Copy-Item plugin_sqlite.exe `"`$env:LOCALAPPDATA\scorpiox\plugins\`"" |
| 69 | } |
| 70 | finally { |
| 71 | Pop-Location |
| 72 | } |