|
| 1 | +# Find and enter a Visual Studio development environment. |
| 2 | +# Required to use Ninja instead of msbuild on our build agents. |
| 3 | +function Enter-VsDevEnv { |
| 4 | + [CmdletBinding()] |
| 5 | + param( |
| 6 | + [Parameter()] |
| 7 | + [switch]$Prerelease, |
| 8 | + [Parameter()] |
| 9 | + [string]$architecture = "x64" |
| 10 | + ) |
| 11 | + |
| 12 | + $ErrorActionPreference = 'Stop' |
| 13 | + |
| 14 | + if ($null -eq (Get-InstalledModule -name 'VSSetup' -ErrorAction SilentlyContinue)) { |
| 15 | + Install-Module -Name 'VSSetup' -Scope CurrentUser -SkipPublisherCheck -Force |
| 16 | + } |
| 17 | + Import-Module -Name 'VSSetup' |
| 18 | + |
| 19 | + Write-Verbose 'Searching for VC++ instances' |
| 20 | + $vsinfo = ` |
| 21 | + Get-VSSetupInstance -All -Prerelease:$Prerelease ` |
| 22 | + | Select-VSSetupInstance ` |
| 23 | + -Latest -Product * ` |
| 24 | + -Require 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64' |
| 25 | + |
| 26 | + $vspath = $vsinfo.InstallationPath |
| 27 | + |
| 28 | + switch ($env:PROCESSOR_ARCHITECTURE) { |
| 29 | + "amd64" { $hostarch = "x64" } |
| 30 | + "x86" { $hostarch = "x86" } |
| 31 | + "arm64" { $hostarch = "arm64" } |
| 32 | + default { throw "Unknown architecture: $switch" } |
| 33 | + } |
| 34 | + |
| 35 | + $devShellModule = "$vspath\Common7\Tools\Microsoft.VisualStudio.DevShell.dll" |
| 36 | + |
| 37 | + Import-Module -Global -Name $devShellModule |
| 38 | + |
| 39 | + Write-Verbose 'Setting up environment variables' |
| 40 | + Enter-VsDevShell -VsInstanceId $vsinfo.InstanceId -SkipAutomaticLocation ` |
| 41 | + -devCmdArguments "-arch=$architecture -host_arch=$hostarch" |
| 42 | + |
| 43 | + Set-Item -Force -path "Env:\Platform" -Value $architecture |
| 44 | + |
| 45 | + remove-Module Microsoft.VisualStudio.DevShell, VSSetup |
| 46 | +} |
| 47 | + |
| 48 | +Enter-VsDevEnv |
0 commit comments