Skip to content

Instantly share code, notes, and snippets.

@hsupu
Last active September 1, 2022 14:54
Show Gist options
  • Save hsupu/004ee2c620e37ef8b70530c3ef181375 to your computer and use it in GitHub Desktop.
Save hsupu/004ee2c620e37ef8b70530c3ef181375 to your computer and use it in GitHub Desktop.
Call cl.exe in ad-hoc way
# param (
# [Parameter(ValueFromRemainingArguments)]
# [AllowEmptyCollection()]
# [string[]]$Params
# )
param()
$ErrorActionPreference = 'Stop'
trap { throw $Error[0]; }
if ($env:PROCESSOR_ARCHITECTURE -ine "amd64") {
# x64 in %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
# x86 in %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe
Write-Error "Not in a x64 shell, please open a x64 pwsh instead."
exit(1)
}
if (-not (Test-Path env:VCINSTALLDIR)) {
# not work by now
# see: https://github.com/dotnet/msbuild/issues/1596
$env:DOTNET_CLI_UI_LANGUAGE = "en-US"
$env:VSLANG = "1033"
# chcp 65001
$vsInstance = Get-VSSetupInstance | Select-Object -First 1
if ($null -eq $vsInstance) {
Write-Error "VSSetupInstance not found"
exit(1)
}
# https://docs.microsoft.com/en-us/visualstudio/ide/reference/command-prompt-powershell?view=vs-2022
Import-Module (Join-Path $vsInstance.InstallationPath "Common7\Tools\Microsoft.VisualStudio.DevShell.dll")
$shParams = @{
VsInstanceId = $vsInstance.InstanceId;
Arch = "amd64";
HostArch = "amd64";
SkipAutomaticLocation = $true;
# DevCmdArguments = "-arch=amd64";
}
Enter-VsDevShell @shParams
}
# Push-Location $PSScriptRoot
# try {
# & cl @Params
# }
# finally {
# Pop-Location
# }
Write-Debug "cl $args"
& cl @args