Skip to content

Commit 190416c

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 190416c

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

build.cmd

Lines changed: 25 additions & 10 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,65 @@ if not DEFINED InstallDir (
3837
goto BuildFail
3938
)
4039

41-
REM Find or install MSBuild. Need v17.4 due to our .NET SDK choice.
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+
)
48+
49+
if DEFINED InstallDir (
50+
REM Add MSBuild to the path.
51+
set "PATH=%InstallDir%\MSBuild\Current\Bin\amd64;%PATH%"
52+
goto FoundMSBuild
53+
)
54+
55+
REM Otherwise find or install an xcopy-able MSBuild.
56+
echo "Could not find a VS2022 installation with the necessary components (MSBuild). Falling back..."
57+
4258
set "MSBuildVersion=17.4.1"
4359
set "Command=[System.Threading.Thread]::CurrentThread.CurrentCulture = ''"
4460
set "Command=%Command%; [System.Threading.Thread]::CurrentThread.CurrentUICulture = ''"
4561
set "Command=%Command%; try { & '%~dp0eng\GetXCopyMSBuild.ps1' %MSBuildVersion%; exit $LASTEXITCODE }"
4662
set "Command=%Command% catch { write-host $_; exit 1 }"
47-
PowerShell -NoProfile -NoLogo -ExecutionPolicy Bypass -Command "%Command%"
63+
PowerShell -NoProfile -NoLogo -ExecutionPolicy Bypass -Command "%Command%"
4864
if %ERRORLEVEL% neq 0 goto BuildFail
4965

5066
REM Add MSBuild to the path.
51-
set "PATH=%CD%\.msbuild\%MSBuildVersion%\tools\MSBuild\Current\Bin\;%PATH%"
67+
set "PATH=%~dp0.msbuild\%MSBuildVersion%\tools\MSBuild\Current\Bin\amd64;%PATH%"
5268

69+
:FoundMSBuild
5370
REM Configure NuGet operations to work w/in this repo i.e. do not pollute system packages folder.
5471
REM Note this causes two copies of packages restored using packages.config to land in this folder e.g.
5572
REM StyleCpy.5.0.0/ and stylecop/5.0.0/.
56-
set "NUGET_PACKAGES=%CD%\packages"
73+
set "NUGET_PACKAGES=%~dp0packages"
5774

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

6178
if "%1" == "" goto BuildDefaults
6279

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

6885
:BuildDefaults
69-
MSBuild Runtime.msbuild /m /nr:false /p:Platform="Any CPU" /p:Desktop=%Desktop% /v:M ^
86+
MSBuild "%~dp0Runtime.msbuild" /m /nr:false /p:Platform="Any CPU" /p:Desktop=%Desktop% /v:M ^
7087
/fl /fileLoggerParameters:LogFile=bin\msbuild.log;Verbosity=Normal /consoleLoggerParameters:Summary
7188
if %ERRORLEVEL% neq 0 goto BuildFail
7289
goto BuildSuccess
7390

7491
:BuildFail
7592
echo.
7693
echo *** BUILD FAILED ***
77-
popd
7894
endlocal
7995
exit /B 999
8096

8197
:BuildSuccess
8298
echo.
8399
echo **** BUILD SUCCESSFUL ***
84-
popd
85100
endlocal
86101
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)