param( [string]$EuroScopeSdkDir = "$env:APPDATA\EuroScope\PlugIn", [string]$ZigExe = "" ) $ErrorActionPreference = "Stop" $projectDir = Split-Path -Parent $MyInvocation.MyCommand.Path $source = Join-Path $projectDir "src\VatgerGrpBridgePortable.cpp" $header = Join-Path $EuroScopeSdkDir "EuroScopePlugIn.h" $euroScopeLibrary = Join-Path $EuroScopeSdkDir "EuroScopePlugInDll.lib" $distDir = Join-Path $projectDir "dist" $buildDir = Join-Path $env:TEMP "vatger-grp-bridge-build-0.1.0" $zigVersion = "0.16.0" $zigArchiveHash = "68659EB5F1E4EB1437A722F1DD889C5A322C9954607F5EDCF337BC3684A75A7E" if (-not (Test-Path -LiteralPath $header) -or -not (Test-Path -LiteralPath $euroScopeLibrary)) { throw "EuroScope-SDK nicht gefunden. Erwartet: $header und $euroScopeLibrary" } if (-not $ZigExe) { $toolDir = Join-Path $env:TEMP "vatger-grp-bridge-zig-$zigVersion" $archive = Join-Path $toolDir "zig.zip" $expanded = Join-Path $toolDir "expanded" $candidate = Join-Path $expanded "zig-x86_64-windows-$zigVersion\zig.exe" if (-not (Test-Path -LiteralPath $candidate)) { New-Item -ItemType Directory -Force -Path $toolDir | Out-Null Invoke-WebRequest -UseBasicParsing ` -Uri "https://ziglang.org/download/$zigVersion/zig-x86_64-windows-$zigVersion.zip" ` -OutFile $archive $actualHash = (Get-FileHash -Algorithm SHA256 -LiteralPath $archive).Hash if ($actualHash -ne $zigArchiveHash) { throw "Zig-Archiv hat eine unerwartete SHA-256-Prüfsumme." } Expand-Archive -LiteralPath $archive -DestinationPath $expanded -Force } $ZigExe = $candidate } $resolvedZig = (Resolve-Path -LiteralPath $ZigExe).Path $zigRoot = Split-Path -Parent $resolvedZig $kernelDefinition = Join-Path $zigRoot "lib\libc\mingw\lib32\kernel32.def" $winHttpDefinition = Join-Path $zigRoot "lib\libc\mingw\lib32\winhttp.def" if (-not (Test-Path -LiteralPath $kernelDefinition) -or -not (Test-Path -LiteralPath $winHttpDefinition)) { throw "Die benötigten Win32-Importdefinitionen fehlen in dieser Zig-Version." } New-Item -ItemType Directory -Force -Path $buildDir, $distDir | Out-Null $kernelLibrary = Join-Path $buildDir "kernel32.lib" $winHttpLibrary = Join-Path $buildDir "winhttp.lib" $object = Join-Path $buildDir "VatgerGrpBridge.obj" $map = Join-Path $buildDir "VatgerGrpBridge.map" $dll = Join-Path $distDir "VatgerGrpBridge.dll" $importLibrary = Join-Path $buildDir "VatgerGrpBridge.lib" & $resolvedZig dlltool -k -m i386 -d $kernelDefinition -l $kernelLibrary if ($LASTEXITCODE -ne 0) { throw "kernel32-Importbibliothek konnte nicht erzeugt werden." } & $resolvedZig dlltool -k -m i386 -d $winHttpDefinition -l $winHttpLibrary if ($LASTEXITCODE -ne 0) { throw "winhttp-Importbibliothek konnte nicht erzeugt werden." } & $resolvedZig c++ ` -target x86-windows-msvc ` -nostdlib ` -nostdinc ` -fno-exceptions ` -fno-rtti ` -fno-sanitize=undefined ` -mstack-probe-size=65536 ` -O2 ` -Wno-everything ` -I $EuroScopeSdkDir ` -c $source ` -o $object if ($LASTEXITCODE -ne 0) { throw "Portable Bridge konnte nicht kompiliert werden." } & $resolvedZig lld-link ` /dll ` /machine:x86 ` /subsystem:windows ` /entry:DllMain@12 ` /opt:ref ` /opt:icf ` "/map:$map" ` "/implib:$importLibrary" ` "/out:$dll" ` $object ` $euroScopeLibrary ` $kernelLibrary ` $winHttpLibrary if ($LASTEXITCODE -ne 0) { throw "Portable Bridge konnte nicht gelinkt werden." } $bytes = [System.IO.File]::ReadAllBytes($dll) $peOffset = [BitConverter]::ToInt32($bytes, 0x3c) $machine = [BitConverter]::ToUInt16($bytes, $peOffset + 4) $mapText = Get-Content -LiteralPath $map -Raw if ($machine -ne 0x014c -or $mapText -notmatch 'EuroScopePlugInInit' -or $mapText -notmatch 'EuroScopePlugInExit') { throw "Erzeugte Datei ist keine gültige Win32-EuroScope-Bridge." } $hash = (Get-FileHash -Algorithm SHA256 -LiteralPath $dll).Hash [System.IO.File]::WriteAllText( (Join-Path $distDir "VatgerGrpBridge.dll.sha256"), "$hash VatgerGrpBridge.dll`r`n", [System.Text.UTF8Encoding]::new($false) ) Write-Host "Gebaut und geprüft: $dll" Write-Host "SHA-256: $hash"