Skip to content

Commit f68e64d

Browse files
committed
Auto install missing msvc toolset with specified version
1 parent b5c2322 commit f68e64d

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

1k/1kiss.ps1

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -881,31 +881,40 @@ $vs_installs = ConvertFrom-Json "$(&$vswhere -version "$vs_major.0" -format 'jso
881881
$vs_installer = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\setup.exe"
882882
$vs_path = $vs_installs[0].installationPath
883883
884+
function install_msvc_comp($comp_id) {
885+
Write-Host "Installing $comp_id into $vs_path ..."
886+
&$vs_installer modify --quiet --installPath $vs_path --add $comp_id | Out-Host
887+
if ($?) {
888+
Write-Host "Install $comp_id success."
889+
}
890+
else {
891+
Write-Error "Install $comp_id fail!"
892+
exit 1
893+
}
894+
}
895+
896+
# msvc
884897
$vs_arch = @{x64 = 'x86.x64'; x86 = 'x86.x64'; arm64 = 'ARM64'; arm = 'ARM' }[$arch]
885898
$msvc_comp_id = "Microsoft.VisualStudio.Component.VC.$ver.$vs_ver.$vs_arch" # refer to: https://learn.microsoft.com/en-us/visualstudio/install/workload-component-id-vs-build-tools?view=vs-2022
886-
Write-Host "Installing $msvc_comp_id ..."
887-
&$vs_installer modify --quiet --installPath $vs_path --add $msvc_comp_id | Out-Host
899+
install_msvc_comp $msvc_comp_id
888900
889-
if ($?) {
890-
Write-Host "Install $msvc_comp_id success."
891-
}
892-
else {
893-
Write-Error "Install $msvc_comp_id fail!"
894-
exit 1
895-
}
901+
# mfc
902+
$suffix = @{x64 = ''; x86 = ''; arm64 = '.ARM64'; arm = '.ARM' }[$arch]
903+
$mfc_comp_id = "Microsoft.VisualStudio.Component.VC.$ver.$vs_ver.MFC$suffix"
904+
install_msvc_comp $mfc_comp_id
896905
'@
897-
$1k.println("Installing $ver', please press YES in UAC dialog, and don't close popup install window ...")
906+
$1k.println("Installing $ver, please press YES in UAC dialog, and don't close popup install window ...")
898907
$__install_script = [System.IO.Path]::GetTempFileName() + '.ps1'
899908
[System.IO.File]::WriteAllText($__install_script, $__install_code)
900909
$process = Start-Process powershell -ArgumentList "-File `"`"$__install_script`"`" -ver $ver -arch $arch" -Verb runas -PassThru -Wait
901910
$install_ret = $process.ExitCode
902911
[System.IO.File]::Delete($__install_script)
903912

904913
if ($install_ret -eq 0) {
905-
$1k.println("Install msvc-$ver' succeed")
914+
$1k.println("Install msvc-$ver succeed")
906915
}
907916
else {
908-
throw "Install msvc-$ver' fail!"
917+
throw "Install msvc-$ver fail!"
909918
}
910919
}
911920

@@ -1454,7 +1463,23 @@ function setup_msvc() {
14541463
if (!$manifest['msvc'].EndsWith('+')) { $dev_cmd_args += " -vcvars_ver=$cl_ver" }
14551464

14561465
Import-Module "$vs_path\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"
1457-
Enter-VsDevShell -VsInstanceId $Global:VS_INST.instanceId -SkipAutomaticLocation -DevCmdArguments $dev_cmd_args
1466+
$1k.println("Enter vs dev shell ...")
1467+
Enter-VsDevShell -VsInstanceId $Global:VS_INST.instanceId -SkipAutomaticLocation -DevCmdArguments $dev_cmd_args -ErrorAction SilentlyContinue
1468+
if ($?) {
1469+
$1k.println('Enter vs dev shell success.')
1470+
}
1471+
else {
1472+
# vs2022 x64,x86 share same msvc component
1473+
install_msvc $cl_ver 'x64'
1474+
$1k.println("Enter vs dev shell ...")
1475+
Enter-VsDevShell -VsInstanceId $Global:VS_INST.instanceId -SkipAutomaticLocation -DevCmdArguments $dev_cmd_args -ErrorAction SilentlyContinue
1476+
if ($?) {
1477+
$1k.println('Enter vs dev shell success.')
1478+
}
1479+
else {
1480+
throw "Enter vs dev shell fail, please check your vs installation"
1481+
}
1482+
}
14581483

14591484
$cl_prog, $cl_ver = find_prog -name 'msvc' -cmd 'cl' -silent $true -usefv $true
14601485
$1k.println("Using msvc: $cl_prog, version: $cl_ver")

0 commit comments

Comments
 (0)