diff options
| author | Alexander Karatarakis <alkarata@microsoft.com> | 2018-01-22 18:19:30 -0800 |
|---|---|---|
| committer | Alexander Karatarakis <alkarata@microsoft.com> | 2018-01-23 14:16:06 -0800 |
| commit | 0df743d6252efda732c00daa48cef28acbf3f078 (patch) | |
| tree | 60182da2a0193af6396bcf5f1b67f0d980cc8935 /scripts | |
| parent | a5fe308ea3c29c5f8715b8f0c78571759541c9b5 (diff) | |
| download | vcpkg-0df743d6252efda732c00daa48cef28acbf3f078.tar.gz vcpkg-0df743d6252efda732c00daa48cef28acbf3f078.zip | |
[bootstrap] Build vcpkg in a clean environment
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/VcpkgPowershellUtils-ClearEnvironment.ps1 | 52 | ||||
| -rw-r--r-- | scripts/VcpkgPowershellUtils.ps1 | 27 | ||||
| -rw-r--r-- | scripts/bootstrap.ps1 | 7 |
3 files changed, 82 insertions, 4 deletions
diff --git a/scripts/VcpkgPowershellUtils-ClearEnvironment.ps1 b/scripts/VcpkgPowershellUtils-ClearEnvironment.ps1 new file mode 100644 index 000000000..5c922572b --- /dev/null +++ b/scripts/VcpkgPowershellUtils-ClearEnvironment.ps1 @@ -0,0 +1,52 @@ +# Capture environment variables for the System and User. Also add some special/built-in variables. +# These will be used to synthesize a clean environment +$specialEnvironmentMap = @{ "SystemDrive"=$env:SystemDrive; "SystemRoot"=$env:SystemRoot; "UserProfile"=$env:UserProfile } # These are built-in and not set in the registry +$machineEnvironmentMap = [Environment]::GetEnvironmentVariables('Machine') # HKEY_CURRENT_USER\Environment +$userEnvironmentMap = [Environment]::GetEnvironmentVariables('User') # HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment + +# Identify the keySet of environment variable names +$nameSet = ($specialEnvironmentMap.Keys + $machineEnvironmentMap.Keys + $userEnvironmentMap.Keys) | Sort-Object | Select-Object -Unique + +# Any environment variable in the $nameSet should be restored to its original value +foreach ($name in $nameSet) +{ + if ($specialEnvironmentMap.ContainsKey($name)) + { + [Environment]::SetEnvironmentVariable($name, $specialEnvironmentMap[$name], 'Process') + continue; + } + + # PATH needs to be concatenated as it has values in both machine and user environment. Any other values should be set. + if ($name -match 'path') + { + $pathValuePartial = @() + # Machine values before user values + $pathValuePartial += $machineEnvironmentMap[$name] -split ';' + $pathValuePartial += $userEnvironmentMap[$name] -split ';' + $pathValue = $pathValuePartial -join ';' + [Environment]::SetEnvironmentVariable($name, $pathValue, 'Process') + continue; + } + + if ($userEnvironmentMap.ContainsKey($name)) + { + [Environment]::SetEnvironmentVariable($name, $userEnvironmentMap[$name], 'Process') + continue; + } + + if ($machineEnvironmentMap.ContainsKey($name)) + { + [Environment]::SetEnvironmentVariable($name, $machineEnvironmentMap[$name], 'Process') + continue; + } + + throw "Unreachable: Unknown variable $name" +} + +# Any environment variable NOT in the $nameSet should be removed +$processEnvironmentMap = [Environment]::GetEnvironmentVariables('Process') +$variablesForRemoval = $processEnvironmentMap.Keys | Where-Object {$nameSet -notcontains $_} +foreach ($name in $variablesForRemoval) +{ + [Environment]::SetEnvironmentVariable($name, $null, 'Process') +} diff --git a/scripts/VcpkgPowershellUtils.ps1 b/scripts/VcpkgPowershellUtils.ps1 index 7a6a67c2c..ec1e8b92b 100644 --- a/scripts/VcpkgPowershellUtils.ps1 +++ b/scripts/VcpkgPowershellUtils.ps1 @@ -168,10 +168,33 @@ function vcpkgInvokeCommand() { param ( [Parameter(Mandatory=$true)][string]$executable, [string]$arguments = "", - [switch]$wait) + [Parameter(Mandatory=$true)][switch]$wait) Write-Verbose "Executing: ${executable} ${arguments}" - $process = Start-Process -FilePath $executable -ArgumentList $arguments -PassThru -NoNewWindow + $process = Start-Process -FilePath "`"$executable`"" -ArgumentList $arguments -PassThru -NoNewWindow + if ($wait) + { + Wait-Process -InputObject $process + $ec = $process.ExitCode + Write-Verbose "Execution terminated with exit code $ec." + } +} + +function vcpkgInvokeCommandClean() +{ + param ( [Parameter(Mandatory=$true)][string]$executable, + [string]$arguments = "", + [Parameter(Mandatory=$true)][switch]$wait) + + Write-Verbose "Clean-Executing: ${executable} ${arguments}" + $scriptsDir = split-path -parent $script:MyInvocation.MyCommand.Definition + $cleanEnvScript = "$scriptsDir\VcpkgPowershellUtils-ClearEnvironment.ps1" + $command = "& `"$cleanEnvScript`"; & `"$executable`" $arguments" + $bytes = [System.Text.Encoding]::Unicode.GetBytes($command) + $encodedCommand = [Convert]::ToBase64String($bytes) + $arg = "-encodedCommand $encodedCommand" + + $process = Start-Process -FilePath powershell.exe -ArgumentList $arg -PassThru -NoNewWindow if ($wait) { Wait-Process -InputObject $process diff --git a/scripts/bootstrap.ps1 b/scripts/bootstrap.ps1 index 2c05f1adb..bf1dcba8e 100644 --- a/scripts/bootstrap.ps1 +++ b/scripts/bootstrap.ps1 @@ -5,6 +5,7 @@ param( ) $scriptsDir = split-path -parent $script:MyInvocation.MyCommand.Definition +. "$scriptsDir\VcpkgPowershellUtils.ps1" $vcpkgRootDir = & $scriptsDir\findFileRecursivelyUp.ps1 $scriptsDir .vcpkg-root Write-Verbose("vcpkg Path " + $vcpkgRootDir) @@ -52,9 +53,11 @@ try "/p:PlatformToolset=$platformToolset", "/p:TargetPlatformVersion=$windowsSDK", "/m", - "dirs.proj") + "dirs.proj") -join " " + + # vcpkgInvokeCommandClean cmd "/c echo %PATH%" -wait:$true + vcpkgInvokeCommandClean $msbuildExe $arguments -wait:$true - & $msbuildExe $arguments if ($LASTEXITCODE -ne 0) { Write-Error "Building vcpkg.exe failed. Please ensure you have installed Visual Studio with the Desktop C++ workload and the Windows SDK for Desktop C++." |
