Skip to content

Commit 9a77441

Browse files
committed
Use msbuild from VS 2022 if available
- should ease local and TeamCity builds - make `%InstallDir%` unquoted (unlike `%vswhere%`) - shorten `%Path%` slightly - nits: - switch to 64bit `msbuild` - use script location more; no need to change directories
1 parent cb7628f commit 9a77441

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

build.cmd

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
@echo off
2-
pushd %~dp0
32
setlocal
43

54
if exist bin goto Build
@@ -29,7 +28,7 @@ for /f "usebackq tokens=*" %%i in (`%vswhere% -version 16 -latest -prerelease -p
2928
-requires Microsoft.Net.Component.4.5.2.TargetingPack ^
3029
-requires Microsoft.Net.Component.4.6.2.TargetingPack ^
3130
-property installationPath`) do (
32-
set InstallDir="%%i"
31+
set "InstallDir=%%i"
3332
)
3433

3534
if not DEFINED InstallDir (
@@ -38,49 +37,63 @@ if not DEFINED InstallDir (
3837
goto BuildFail
3938
)
4039

41-
REM Find or install MSBuild. Need v17.4 due to our .NET SDK choice.
42-
set "MSBuildVersion=17.4.1"
43-
set "Command=[System.Threading.Thread]::CurrentThread.CurrentCulture = ''"
44-
set "Command=%Command%; [System.Threading.Thread]::CurrentThread.CurrentUICulture = ''"
45-
set "Command=%Command%; try { & '%~dp0eng\GetXCopyMSBuild.ps1' %MSBuildVersion%; exit $LASTEXITCODE }"
46-
set "Command=%Command% catch { write-host $_; exit 1 }"
47-
PowerShell -NoProfile -NoLogo -ExecutionPolicy Bypass -Command "%Command%"
48-
if %ERRORLEVEL% neq 0 goto BuildFail
40+
REM Find a 64bit MSBuild and add it to path. Require v17.4 or later due to our .NET SDK choice.
41+
REM Check for VS2022 first.
42+
set InstallDir=
43+
for /f "usebackq tokens=*" %%i in (`%vswhere% -version 17.4 -latest -prerelease -products * ^
44+
-requires Microsoft.Component.MSBuild ^
45+
-property installationPath`) do (
46+
set "InstallDir=%%i"
47+
)
4948

50-
REM Add MSBuild to the path.
51-
set "PATH=%CD%\.msbuild\%MSBuildVersion%\tools\MSBuild\Current\Bin\;%PATH%"
49+
if DEFINED InstallDir (
50+
REM Add MSBuild to the path.
51+
set "PATH=%InstallDir%\MSBuild\Current\Bin\amd64;%PATH%"
52+
) else (
53+
REM Otherwise find or install an xcopy-able MSBuild.
54+
echo "Could not find a VS2022 installation with the necessary components (MSBuild). Falling back..."
55+
56+
set "MSBuildVersion=17.4.1"
57+
set "Command=[System.Threading.Thread]::CurrentThread.CurrentCulture = ''"
58+
set "Command=%Command%; [System.Threading.Thread]::CurrentThread.CurrentUICulture = ''"
59+
set "Command=%Command%; try { & '%~dp0eng\GetXCopyMSBuild.ps1' %MSBuildVersion%; exit $LASTEXITCODE }"
60+
set "Command=%Command% catch { write-host $_; exit 1 }"
61+
PowerShell -NoProfile -NoLogo -ExecutionPolicy Bypass -Command "%Command%"
62+
if %ERRORLEVEL% neq 0 goto BuildFail
63+
64+
REM Add MSBuild to the path.
65+
set "PATH=%~dp0.msbuild\%MSBuildVersion%\tools\MSBuild\Current\Bin\amd64;%PATH%"
66+
)
5267

5368
REM Configure NuGet operations to work w/in this repo i.e. do not pollute system packages folder.
5469
REM Note this causes two copies of packages restored using packages.config to land in this folder e.g.
5570
REM StyleCpy.5.0.0/ and stylecop/5.0.0/.
56-
set "NUGET_PACKAGES=%CD%\packages"
71+
set "NUGET_PACKAGES=%~dp0packages"
5772

5873
REM Are we running in a local dev environment (not on CI)?
5974
if DEFINED CI (set Desktop=false) else if DEFINED TEAMCITY_VERSION (set Desktop=false) else (set Desktop=true)
6075

6176
if "%1" == "" goto BuildDefaults
6277

63-
MSBuild Runtime.msbuild /m /nr:false /p:Platform="Any CPU" /p:Desktop=%Desktop% /v:M ^
78+
MSBuild "%~dp0Runtime.msbuild" /m /nr:false /p:Platform="Any CPU" /p:Desktop=%Desktop% /v:M ^
6479
/fl /fileLoggerParameters:LogFile=bin\msbuild.log;Verbosity=Normal /consoleLoggerParameters:Summary /t:%*
6580
if %ERRORLEVEL% neq 0 goto BuildFail
6681
goto BuildSuccess
6782

6883
:BuildDefaults
69-
MSBuild Runtime.msbuild /m /nr:false /p:Platform="Any CPU" /p:Desktop=%Desktop% /v:M ^
84+
MSBuild "%~dp0Runtime.msbuild" /m /nr:false /p:Platform="Any CPU" /p:Desktop=%Desktop% /v:M ^
7085
/fl /fileLoggerParameters:LogFile=bin\msbuild.log;Verbosity=Normal /consoleLoggerParameters:Summary
7186
if %ERRORLEVEL% neq 0 goto BuildFail
7287
goto BuildSuccess
7388

7489
:BuildFail
7590
echo.
7691
echo *** BUILD FAILED ***
77-
popd
7892
endlocal
7993
exit /B 999
8094

8195
:BuildSuccess
8296
echo.
8397
echo **** BUILD SUCCESSFUL ***
84-
popd
8598
endlocal
8699
exit /B 0

eng/GetXCopyMSBuild.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ function InitializeXCopyMSBuild([string]$packageVersion, [bool]$install, [string
3939
return Join-Path $packageDir 'tools'
4040
}
4141

42-
InitializeXCopyMSBuild -packageVersion $Version -install $true -ToolsDir (join-path $PWD .msbuild)
42+
$RepoRoot = Resolve-Path (Join-Path $PSScriptRoot '..\')
43+
InitializeXCopyMSBuild -packageVersion $Version -install $true -ToolsDir (join-path $RepoRoot .msbuild)

0 commit comments

Comments
 (0)