param ( [string]$Preset = "win-local" ) $ErrorActionPreference = 'Stop' $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path # --- Auto-generate CMakeUserPresets.json if missing --- $UserPresetsPath = Join-Path $ScriptDir "..\CMakeUserPresets.json" if (-not (Test-Path $UserPresetsPath)) { $TemplatePath = Join-Path $ScriptDir "..\CMakeUserPresets.json.template" if (Test-Path $TemplatePath) { Write-Host "CMakeUserPresets.json not found. Auto-generating it from template for user: $env:USERNAME..." -ForegroundColor Cyan $content = Get-Content $TemplatePath -Raw $content = $content -replace "@USERNAME@", $env:USERNAME [System.IO.File]::WriteAllText($UserPresetsPath, $content) Write-Host "Generated CMakeUserPresets.json successfully." -ForegroundColor Green } } # --- Resolve vcpkg root --- $vcpkgRoot = $env:VCPKG_ROOT if (-not $vcpkgRoot) { $candidates = @( "C:\vcpkg", "C:\src\vcpkg", "C:\Users\$env:USERNAME\vcpkg", "C:\Users\furqa\vcpkg" ) foreach ($cand in $candidates) { if (Test-Path $cand) { $vcpkgRoot = $cand break } } if (-not $vcpkgRoot) { Write-Error "VCPKG_ROOT was not set and vcpkg could not be found at standard locations. Please set the VCPKG_ROOT environment variable." exit 1 } $env:VCPKG_ROOT = $vcpkgRoot Write-Host "VCPKG_ROOT defaulted to: $vcpkgRoot" -ForegroundColor Yellow } $vswhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer\vswhere.exe' if (Test-Path $vswhere) { $vsPath = (& $vswhere -latest -prerelease -products * -property installationPath | Select-Object -First 1) if ($vsPath) { $vcvars = Join-Path $vsPath "VC\Auxiliary\Build\vcvars64.bat" } } if (-not $vcvars -or -not (Test-Path $vcvars)) { $vcvars = "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" } if (-not (Test-Path $vcvars)) { $vcvars = "C:\Program Files\Microsoft Visual Studio\18\Community\VC\Auxiliary\Build\vcvars64.bat" } if (-not (Test-Path $vcvars)) { Write-Error "Could not find vcvars64.bat. Please ensure Visual Studio or Build Tools is installed." exit 1 } Write-Host "Loading MSVC environment variables using $vcvars..." -ForegroundColor Cyan cmd /c "`"$vcvars`" && set" | Where-Object { $_ -match '=' } | ForEach-Object { $name, $value = $_ -split '=', 2 [System.Environment]::SetEnvironmentVariable($name, $value, 'Process') } Write-Host "Configuring CMake preset '$Preset'..." -ForegroundColor Cyan cmake --preset $Preset if ($LASTEXITCODE -ne 0) { Write-Error "CMake configuration failed." exit $LASTEXITCODE } Write-Host "Building all targets..." -ForegroundColor Cyan cmake --build --preset $Preset if ($LASTEXITCODE -ne 0) { Write-Error "Build failed." exit $LASTEXITCODE } Write-Host "Success! The python extension is compiled and copied to the gateway." -ForegroundColor Green