diff --git a/CMakeUserPresets.json.template b/CMakeUserPresets.json.template new file mode 100644 index 0000000..3dea9bb --- /dev/null +++ b/CMakeUserPresets.json.template @@ -0,0 +1,27 @@ +{ + "version": 6, + "cmakeMinimumRequired": { "major": 3, "minor": 25, "patch": 0 }, + "configurePresets": [ + { + "name": "win-local", + "displayName": "Windows • Debug (local — build dir outside OneDrive/spaces)", + "inherits": "windows-debug", + "binaryDir": "C:/Users/@USERNAME@/pdfeng-build/win-local" + }, + { + "name": "win-local-pdfium", + "displayName": "Windows • RelWithDebInfo + PDFium (static CRT — build dir outside OneDrive/spaces)", + "inherits": "windows-release", + "binaryDir": "C:/Users/@USERNAME@/pdfeng-build/win-local-pdfium", + "cacheVariables": { "PDFENGINE_WITH_PDFIUM": "ON" } + } + ], + "buildPresets": [ + { "name": "win-local", "configurePreset": "win-local" }, + { "name": "win-local-pdfium", "configurePreset": "win-local-pdfium" } + ], + "testPresets": [ + { "name": "win-local", "inherits": "common", "configurePreset": "win-local" }, + { "name": "win-local-pdfium", "inherits": "common", "configurePreset": "win-local-pdfium" } + ] +} diff --git a/scripts/build_cpp.ps1 b/scripts/build_cpp.ps1 index 5929160..90098ea 100644 --- a/scripts/build_cpp.ps1 +++ b/scripts/build_cpp.ps1 @@ -1,8 +1,46 @@ +param ( + [string]$Preset = "win-local-pdfium" +) + +$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) { - $vcpkgRoot = "C:\Users\furqa\vcpkg" + $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 was not set. Defaulting to $vcpkgRoot" -ForegroundColor Yellow + Write-Host "VCPKG_ROOT defaulted to: $vcpkgRoot" -ForegroundColor Yellow } $vcvars = "C:\Program Files (x86)\Microsoft Visual Studio\18\BuildTools\VC\Auxiliary\Build\vcvars64.bat" @@ -10,6 +48,16 @@ if (-not (Test-Path $vcvars)) { $vcvars = "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" } +if (-not (Test-Path $vcvars)) { + $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 (Test-Path $vcvars)) { Write-Error "Could not find vcvars64.bat. Please ensure Visual Studio or Build Tools is installed." exit 1 @@ -21,15 +69,15 @@ cmd /c "`"$vcvars`" && set" | Where-Object { $_ -match '=' } | ForEach-Object { [System.Environment]::SetEnvironmentVariable($name, $value, 'Process') } -Write-Host "Configuring CMake preset 'win-local-pdfium'..." -ForegroundColor Cyan -cmake --preset win-local-pdfium +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 win-local-pdfium +cmake --build --preset $Preset if ($LASTEXITCODE -ne 0) { Write-Error "Build failed." exit $LASTEXITCODE