Skip to content

Instantly share code, notes, and snippets.

@BeErikk
Last active August 4, 2023 14:25
Show Gist options
  • Save BeErikk/786df70f63c578a8acfeb75c0da60e64 to your computer and use it in GitHub Desktop.
Save BeErikk/786df70f63c578a8acfeb75c0da60e64 to your computer and use it in GitHub Desktop.
Windows console script to enable the MS compiler with custom environment settings
@echo off
@title MS C++ compiler
:: Windows console script to enable the MS compiler with custom environment settings
:: Jerker Bäck 2017
:: delims:vv vvv vv v
:: line : PlatformIdentity = "UAP, Version=10.0.19041.0"
:: tokens: ^1 ^2 ^3 ^4
setlocal
set vswhereprog="C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"
%vswhereprog% -latest -property installationPath | wsl.exe sed 's:\\:/:g' | wsl.exe tr -d '\r\n' > %TEMP%\vspath.tmp
endlocal
set /p vsinstalldir=<%TEMP%\vspath.tmp
set /p vctoolsversion=<"%vsinstalldir%/VC/Auxiliary/Build/Microsoft.VCToolsVersion.default.txt"
echo %vctoolsversion%|wsl.exe tr -d '\r\n' > %TEMP%\vspath.tmp
set /p vctoolsversion=<%TEMP%\vspath.tmp
set vctools=%vsinstalldir%/VC/Tools/MSVC/%vctoolsversion%/
reg.exe query "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows Kits\Installed Roots" -f KitsRoot10 | wsl.exe sed -n 's:.*REG_SZ *\([[:print:]]*\).*:\1:p' | wsl.exe sed 's:\\:/:g' | wsl.exe tr -d '\r\n' > %TEMP%\vspath.tmp
set /p kitsroot10=<%TEMP%\vspath.tmp
for /f "tokens=4 delims=^=," %%i in ('find.exe "PlatformIdentity" ^< "%kitsroot10%SDKManifest.xml"') do set kitsversion=%%i
set kitsversion=%kitsversion:"=%
echo %kitsversion%|wsl.exe tr -d ' \r\n' > %TEMP%\vspath.tmp
set /p kitsversion=<%TEMP%\vspath.tmp
del %TEMP%\vspath.tmp
set userlib=F:/libraries/
@echo.
echo Using MSVCtools version: %vctoolsversion%
echo Using Windows SDK version: %kitsversion%
@echo.
set INCLUDE=%vctools%include;%vctools%atlmfc/include;%kitsroot10%Include/%kitsversion%/ucrt;%kitsroot10%Include/%kitsversion%/um;%kitsroot10%Include/%kitsversion%/shared;%kitsroot10%Include/%kitsversion%/km;%kitsroot10%Include/%kitsversion%/winrt;%kitsroot10%Include/%kitsversion%/cppwinrt;%vsinstalldir%/VC/Auxiliary/VS/include;%vsinstalldir%/VC/Auxiliary/VS/UnitTest/include;%userlib%include;%userlib%include/ntnative;%userlib%include/wtl;
set LIB=%vctools%lib/x64;%vctools%atlmfc/lib/x64;%kitsroot10%Lib/%kitsversion%/ucrt/x64;%kitsroot10%Lib/%kitsversion%/um/x64;%kitsroot10%Lib/%kitsversion%/km/x64;%vsinstalldir%/VC/Auxiliary/VS/UnitTest/lib/x64;%userlib%lib/x64;
set LIB32=%vctools%lib/x86;%vctools%atlmfc/lib/x86;%kitsroot10%Lib/%kitsversion%/ucrt/x86;%kitsroot10%Lib/%kitsversion%/um/x86;%kitsroot10%Lib/%kitsversion%/km/x86;%vsinstalldir%/VC/Auxiliary/VS/UnitTest/lib/x86;%userlib%lib/x86;
set orgPATH=%PATH%
set PATH64=%vctools%bin/HostX64/x64;%kitsroot10%bin/%kitsversion%/x64;%kitsroot10%bin/x64
set PATH32=%vctools%bin/Hostx86/x86;%kitsroot10%bin/%kitsversion%/x86;%kitsroot10%bin/x86
set PATH=%PATH64%;%vsinstalldir%/Common7/IDE;%vsinstalldir%/Common7/Tools;%vsinstalldir%/Common7/IDE/VC/VCPackages;%vsinstalldir%/Common7/IDE/CommonExtensions/Microsoft/TestWindow;%PATH%
@BeErikk
Copy link
Author

BeErikk commented Apr 24, 2021

This script allows a stripped-down custom environment for the MS compiler tools (vs the Visual Studio prompt).
It's not very fast but faster than Visual Studio prompt.

There are imo two cumbersome settings in current Visual Studio installations:

  • %vsinstalldir%/VC/Auxiliary/Build/Microsoft.VCToolsVersion.default.txt
  • %kitsroot10%SDKManifest.xml

which gives the necessary compiler version and SDK tools version numbers.

In the script, I use wsl.exe sed and find.exe to get the numbers and wsl.exe tr to strip CRLN
So it has dependencies of WSL. I tried several alternatives but this is one way to do it.

Another way is to symlink the compiler to say C:\develop\current\vctools and manually update the symlink at each Visual Studio update. This has the advantage of giving a short path without spaces, but is of course less elegant. (I do both). As a side note MS really could have registered these two numbers somewhere more script friendly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment