# SPDX-License-Identifier: MPL-2.0 # Messages en ASCII : compatible Windows PowerShell 5.1 sans BOM. <# .SYNOPSIS Build local Windows : configure, compile et teste un preset CMake. .DESCRIPTION Charge l'environnement MSVC et utilise CMake et Ninja fournis par Visual Studio 2022 (pas de runner CI). Presets : windows-msvc-debug, windows-msvc-release, windows-msvc-asan, windows-msvc-prototypes (voir CMakePresets.json). .PARAMETER Preset Preset CMake. Defaut : windows-msvc-debug. .PARAMETER Clean Supprime build\ avant de configurer. .PARAMETER NoTests Compile sans lancer les tests. .PARAMETER BuildDir Dossier de build a utiliser a la place de build\ (builds paralleles). .PARAMETER Target Cibles CMake a compiler (toutes par defaut). Exige -BuildDir. .PARAMETER TestRegex Ne lance que les tests dont le nom correspond (ctest -R). Exige -BuildDir. .PARAMETER Help Affiche cette aide. .EXAMPLE .\scripts\build.ps1 -Preset windows-msvc-asan #> [CmdletBinding()] param( [string]$Preset = "windows-msvc-debug", [switch]$Clean, [switch]$NoTests, [string]$BuildDir, [string[]]$Target, [string]$TestRegex, [Alias("h", "?")] [switch]$Help ) if ($Help) { Get-Help $PSCommandPath -Detailed exit 0 } $ErrorActionPreference = "Stop" $root = Split-Path -Parent $PSScriptRoot function Initialize-VsEnvironment { $vswhere = Join-Path ${env:ProgramFiles(x86)} "Microsoft Visual Studio\Installer\vswhere.exe" if (-not (Test-Path $vswhere)) { throw "vswhere introuvable : Visual Studio n'est pas installe." } $vs = & $vswhere -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath if (-not $vs) { throw "Aucune installation Visual Studio avec les outils C++ x64." } if (-not (Get-Command cl.exe -ErrorAction SilentlyContinue)) { Import-Module (Join-Path $vs "Common7\Tools\Microsoft.VisualStudio.DevShell.dll") Microsoft.VisualStudio.DevShell\Enter-VsDevShell -VsInstallPath $vs -SkipAutomaticLocation ` -DevCmdArguments "-arch=x64 -host_arch=x64" | Out-Null } # CMake et Ninja de Visual Studio en tete du PATH. $cmakeExt = Join-Path $vs "Common7\IDE\CommonExtensions\Microsoft\CMake" $env:PATH = "$cmakeExt\CMake\bin;$cmakeExt\Ninja;$env:PATH" } function Invoke-Step([string]$label, [scriptblock]$cmd) { Write-Host "==> $label" -ForegroundColor Cyan & $cmd if ($LASTEXITCODE -ne 0) { throw "Echec : $label (code $LASTEXITCODE)" } } Initialize-VsEnvironment Push-Location $root try { $dir = if ($BuildDir) { $BuildDir } else { Join-Path $root "build\$Preset" } if ($Clean -and (Test-Path $dir)) { Remove-Item -Recurse -Force $dir } if ($BuildDir) { Invoke-Step "configure $Preset -> $dir" { cmake --preset $Preset -B $dir } if ($Target) { # Via -File, "a,b" arrive en une seule chaine : on decoupe sur les virgules. $targets = @($Target | ForEach-Object { $_ -split ',' } | Where-Object { $_ }) Invoke-Step "build $dir ($($targets -join ', '))" { cmake --build $dir --target $targets } } else { Invoke-Step "build $dir" { cmake --build $dir } } if (-not $NoTests) { if ($TestRegex) { Invoke-Step "test $dir (-R $TestRegex)" { ctest --test-dir $dir --output-on-failure --no-tests=error -R $TestRegex } } else { Invoke-Step "test $dir" { ctest --test-dir $dir --output-on-failure --no-tests=error } } } } else { Invoke-Step "configure $Preset" { cmake --preset $Preset } Invoke-Step "build $Preset" { cmake --build --preset $Preset } if (-not $NoTests) { Invoke-Step "test $Preset" { ctest --preset $Preset } } } } finally { Pop-Location }