Detect Visual Studio automatically in Windows packaging script
authorPierrick Bouvier <pierrick.bouvier@linaro.org>
Thu, 20 Oct 2022 14:29:15 +0000 (16:29 +0200)
committerTobias Hieta <tobias@hieta.se>
Thu, 20 Oct 2022 14:29:36 +0000 (16:29 +0200)
Instead of hardcoding several VS paths, use vswhere.exe (available from
VS 2017) to get latest version available.

Reviewed By: hans, thieta

Differential Revision: https://reviews.llvm.org/D135873

llvm/utils/release/build_llvm_release.bat

index 01502a0..9ffcf9c 100755 (executable)
@@ -83,14 +83,18 @@ REM   https://github.com/swig/swig/issues/769
 REM\r
 \r
 :: Detect Visual Studio\r
-set vsdevcmd=\r
-set vs_2019_prefix=C:\Program Files (x86)\Microsoft Visual Studio\2019\r
-:: try potential activated visual studio, then 2019, with different editions\r
-call :find_visual_studio "%VSINSTALLDIR%"\r
-call :find_visual_studio "%vs_2019_prefix%\Enterprise"\r
-call :find_visual_studio "%vs_2019_prefix%\Professional"\r
-call :find_visual_studio "%vs_2019_prefix%\Community"\r
-call :find_visual_studio "%vs_2019_prefix%\BuildTools"\r
+set vsinstall=\r
+set vswhere=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe\r
+\r
+if "%VSINSTALLDIR%" NEQ "" (\r
+  echo using enabled Visual Studio installation\r
+  set "vsinstall=%VSINSTALLDIR%"\r
+) else (\r
+  echo using vswhere to detect Visual Studio installation\r
+  FOR /F "delims=" %%r IN ('^""%vswhere%" -nologo -latest -products "*" -all -property installationPath^"') DO set vsinstall=%%r\r
+)\r
+set "vsdevcmd=%vsinstall%\Common7\Tools\VsDevCmd.bat"\r
+\r
 if not exist "%vsdevcmd%" (\r
   echo Can't find any installation of Visual Studio\r
   exit /b 1\r
@@ -359,14 +363,3 @@ exit /b 0
 \r
 :parse_args_done\r
 exit /b 0\r
-::==============================================================================\r
-:find_visual_studio\r
-set "vs_install=%~1"\r
-if "%vs_install%" == "" exit /b 1\r
-\r
-if "%vsdevcmd%" NEQ "" exit /b 0 :: already found\r
-\r
-set "candidate=%vs_install%\Common7\Tools\VsDevCmd.bat"\r
-echo trying VS devcmd: %candidate%\r
-if exist "%candidate%" set "vsdevcmd=%candidate%"\r
-exit /b 0\r