Skip to content

Commit 146e58c

Browse files
adrastogiAditya Rastogi
andauthored
Add Visual Studio detection logic to build.cmd (#542)
* Add detection logic for VS * PR feedback (add call to usage in error path) * PR feedback (tightening output) --------- Co-authored-by: Aditya Rastogi <[email protected]>
1 parent 8adfdf3 commit 146e58c

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

build.cmd

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
@echo off
22
if "%1"=="/?" goto :usage
33
if "%1"=="-?" goto :usage
4-
if "%VSINSTALLDIR%" == "" goto :usage
54

65
setlocal enabledelayedexpansion enableextensions
76

@@ -14,6 +13,13 @@ set sample_filter=%3\
1413
if "%platform%"=="" set platform=x64
1514
if "%configuration%"=="" set configuration=Release
1615

16+
call :EnsureDeveloperCommandPrompt
17+
18+
if "%VSINSTALLDIR%"=="" (
19+
echo Visual Studio Developer Command Prompt not detected and automatic initialization failed.
20+
goto :usage
21+
)
22+
1723
if not exist ".\.nuget" mkdir ".\.nuget"
1824
if not exist ".\.nuget\nuget.exe" powershell -Command "Invoke-WebRequest https://dist.nuget.org/win-x86-commandline/latest/nuget.exe -OutFile .\.nuget\nuget.exe"
1925

@@ -64,9 +70,46 @@ endlocal
6470

6571
goto :eof
6672

73+
:EnsureDeveloperCommandPrompt
74+
if not "%VSINSTALLDIR%"=="" goto :eof
75+
76+
echo Visual Studio Developer Command Prompt not detected. Attempting to initialize automatically...
77+
78+
set "VS_INSTALL_PATH="
79+
for %%P in ("%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" "%ProgramFiles%\Microsoft Visual Studio\Installer\vswhere.exe") do (
80+
if exist %%~P (
81+
for /f "usebackq tokens=*" %%I in (`"%%~P" -latest -requires Microsoft.Component.MSBuild -property installationPath`) do (
82+
if not defined VS_INSTALL_PATH (
83+
set "VS_INSTALL_PATH=%%~I"
84+
)
85+
)
86+
if defined VS_INSTALL_PATH goto :VsInstallFound
87+
)
88+
)
89+
goto :EnsureDeveloperCommandPromptEnd
90+
91+
:VsInstallFound
92+
if exist "%VS_INSTALL_PATH%\Common7\Tools\VsDevCmd.bat" (
93+
set "VSDEVCMD_ARGS=-no_logo"
94+
if /i "%platform%"=="x86" set "VSDEVCMD_ARGS=!VSDEVCMD_ARGS! -arch=x86"
95+
if /i "%platform%"=="win32" set "VSDEVCMD_ARGS=!VSDEVCMD_ARGS! -arch=x86"
96+
if /i "%platform%"=="x64" set "VSDEVCMD_ARGS=!VSDEVCMD_ARGS! -arch=x64"
97+
if /i "%platform%"=="arm" set "VSDEVCMD_ARGS=!VSDEVCMD_ARGS! -arch=arm"
98+
if /i "%platform%"=="arm64" set "VSDEVCMD_ARGS=!VSDEVCMD_ARGS! -arch=arm64"
99+
call "%VS_INSTALL_PATH%\Common7\Tools\VsDevCmd.bat" !VSDEVCMD_ARGS!
100+
)
101+
102+
:EnsureDeveloperCommandPromptEnd
103+
if "%VSINSTALLDIR%"=="" (
104+
echo Failed to automatically configure the Visual Studio environment.
105+
echo Please manually run it from a Developer Command Prompt instead.
106+
)
107+
goto :eof
108+
67109
:usage
68110
echo Usage:
69-
echo This script should be run under a Visual Studio Developer Command Prompt.
111+
echo This script tries to initialize a Visual Studio Developer Command Prompt.
112+
echo If that fails, please manually run it from a Developer Command Prompt instead.
70113
echo.
71114
echo build.cmd [Platform] [Configuration] [Sample]
72115
echo.

0 commit comments

Comments
 (0)