param([switch]$Test, [switch]$Integration, [switch]$Ocr, [switch]$Stage) $ErrorActionPreference = 'Stop' $root = $PSScriptRoot $compiler = Join-Path $root '.tools\w64devkit\bin\g++.exe' if (!(Test-Path $compiler)) { $compiler = (Get-Command g++ -ErrorAction Stop).Source } $env:PATH = (Split-Path $compiler) + ';' + $env:PATH $outDir = Join-Path $root 'build' New-Item -ItemType Directory -Force $outDir | Out-Null $executablePath = if ($Stage) { Join-Path $outDir 'staged\KSwitcher.exe' } else { Join-Path $outDir 'KSwitcher.exe' } New-Item -ItemType Directory -Force (Split-Path $executablePath) | Out-Null Push-Location $root try { $common = @('-std=c++20','-Os','-DNDEBUG','-DUNICODE','-D_UNICODE','-DWIN32_LEAN_AND_MEAN','-DNOMINMAX','-D_WIN32_WINNT=0x0A00','-Wall','-Wextra','-Wno-missing-field-initializers','-Isrc','-Ivendor','-ffunction-sections','-fdata-sections') $libs = @('-luser32','-lshell32','-ladvapi32','-lcrypt32','-limm32','-lcomctl32','-lgdi32','-lole32','-loleaut32','-ltaskschd','-lcomdlg32','-luuid','-lwindowscodecs','-lruntimeobject','-luiautomationcore') foreach ($name in @('core','platform','autostart','autocorrect','storage','ocr','richtext','update','app','ui','main')) { & $compiler @common -c "src/$name.cpp" -o "build/$name.o" if ($LASTEXITCODE) { throw "Compilation failed: $name" } } & windres --codepage=65001 resources/app.rc -O coff -o build/app-res.o if ($LASTEXITCODE) { throw 'Resource compilation failed' } New-Item -ItemType Directory -Force build/release | Out-Null & $compiler -mwindows -municode -static -s '-Wl,--gc-sections' build/core.o build/platform.o build/autostart.o build/autocorrect.o build/storage.o build/ocr.o build/richtext.o build/update.o build/app.o build/ui.o build/main.o build/app-res.o @libs -o $executablePath if ($LASTEXITCODE) { throw 'Link failed' } Copy-Item -LiteralPath $executablePath -Destination build/release/KSwitcher.exe -Force if ($Test) { & $compiler @common tests/tests.cpp build/core.o build/platform.o build/autostart.o build/autocorrect.o build/storage.o build/ocr.o build/richtext.o build/update.o @libs -static -s -o build/KSwitcherTests.exe if ($LASTEXITCODE) { throw 'Test build failed' } & .\build\KSwitcherTests.exe if ($LASTEXITCODE) { throw 'Tests failed' } & $compiler @common tests/input.cpp build/core.o build/platform.o build/autostart.o build/autocorrect.o build/storage.o build/ocr.o build/richtext.o build/update.o build/app.o build/ui.o build/app-res.o @libs -static -s -o build/KSwitcherInputTests.exe if ($LASTEXITCODE) { throw 'Input test build failed' } & .\build\KSwitcherInputTests.exe if ($LASTEXITCODE) { throw 'Input tests failed' } & "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File tests/update.ps1 -Executable $executablePath if ($LASTEXITCODE) { throw 'Updater tests failed' } } if ($Integration) { & $compiler @common tests/integration.cpp build/core.o build/platform.o build/autostart.o build/autocorrect.o build/storage.o build/ocr.o build/richtext.o build/update.o build/app.o build/ui.o build/app-res.o @libs -static -s -o build/KSwitcherIntegration.exe if ($LASTEXITCODE) { throw 'Integration test build failed' } & .\build\KSwitcherIntegration.exe if ($LASTEXITCODE) { throw 'Integration tests failed' } } if ($Ocr) { & $compiler @common tests/ocr.cpp build/core.o build/platform.o build/ocr.o @libs -static -s -o build/KSwitcherOcrTests.exe if ($LASTEXITCODE) { throw 'OCR test build failed' } & .\build\KSwitcherOcrTests.exe if ($LASTEXITCODE) { throw 'OCR tests failed' } } foreach ($folder in @('build/dictionaries','build/release/dictionaries','build/staged/dictionaries')) { New-Item -ItemType Directory -Force $folder | Out-Null Copy-Item -Path dictionaries/* -Destination $folder -Force } Get-Item build/release/KSwitcher.exe | Select-Object FullName,Length } finally { Pop-Location }