Skip to content

Commit 40a23e8

Browse files
authored
bpo-30687: Fixes build scripts to find msbuild.exe and stop relying on vcvarsall.bat (#2252)
* Fixes build scripts to find msbuild.exe and stop relying on vcvarsall.bat Also fixes bdist_wininst.vcxproj to use correct version in generated name.
1 parent eb81795 commit 40a23e8

File tree

10 files changed

+102
-51
lines changed

10 files changed

+102
-51
lines changed

Misc/NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,9 @@ Documentation
12801280
Build
12811281
-----
12821282

1283+
- bpo-30687: Locate msbuild.exe on Windows when building rather than
1284+
vcvarsall.bat
1285+
12831286
- bpo-20210: Support the *disabled* marker in Setup files. Extension modules
12841287
listed after this marker are not built at all, neither by the Makefile nor by
12851288
setup.py.

PC/bdist_wininst/bdist_wininst.vcxproj

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
<OutDir>$(PySourcePath)lib\distutils\command\</OutDir>
5959
<LinkIncremental>false</LinkIncremental>
6060
<TargetName>wininst-$(VisualStudioVersion)</TargetName>
61+
<TargetName Condition="$(PlatformToolset) == 'v140'">wininst-14.0</TargetName>
62+
<TargetName Condition="$(PlatformToolset) == 'v120'">wininst-12.0</TargetName>
63+
<TargetName Condition="$(PlatformToolset) == 'v110'">wininst-11.0</TargetName>
64+
<TargetName Condition="$(PlatformToolset) == 'v100'">wininst-10.0</TargetName>
6165
<TargetName Condition="$(Platform) == 'x64'">$(TargetName)-amd64</TargetName>
6266
<TargetExt>.exe</TargetExt>
6367
</PropertyGroup>

PC/bdist_wininst/build.bat

+4-7
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@ set PCBUILD=%~dp0..\..\PCBuild\
77

88
echo Building Lib\distutils\command\wininst-xx.0.exe
99

10-
call "%PCBUILD%env.bat" x86
11-
if errorlevel 1 goto :eof
10+
call "%PCBUILD%find_msbuild.bat" %MSBUILD%
11+
if ERRORLEVEL 1 (echo Cannot locate MSBuild.exe on PATH or as MSBUILD variable & exit /b 2)
1212

13-
msbuild "%D%bdist_wininst.vcxproj" "/p:SolutionDir=%PCBUILD%\" /p:Configuration=Release /p:Platform=Win32
13+
%MSBUILD% "%D%bdist_wininst.vcxproj" "/p:SolutionDir=%PCBUILD%\" /p:Configuration=Release /p:Platform=Win32
1414
if errorlevel 1 goto :eof
1515

1616

1717
echo Building Lib\distutils\command\wininst-xx.0-amd64.exe
1818

19-
call "%PCBUILD%env.bat" x86_amd64
20-
if errorlevel 1 goto :eof
21-
22-
msbuild "%D%bdist_wininst.vcxproj" "/p:SolutionDir=%PCBUILD%\" /p:Configuration=Release /p:Platform=x64
19+
%MSBUILD% "%D%bdist_wininst.vcxproj" "/p:SolutionDir=%PCBUILD%\" /p:Configuration=Release /p:Platform=x64

PCbuild/build.bat

+11-21
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ exit /b 127
5353
:Run
5454
setlocal
5555
set platf=Win32
56-
set vs_platf=x86
5756
set conf=Release
5857
set target=Build
5958
set dir=%~dp0
@@ -62,10 +61,6 @@ set verbose=/nologo /v:m
6261
set kill=
6362
set do_pgo=
6463
set pgo_job=-m test --pgo
65-
set on_64_bit=true
66-
67-
rem This may not be 100% accurate, but close enough.
68-
if "%ProgramFiles(x86)%"=="" (set on_64_bit=false)
6964

7065
:CheckOpts
7166
if "%~1"=="-h" goto Usage
@@ -95,18 +90,12 @@ if "%IncludeTkinter%"=="" set IncludeTkinter=true
9590

9691
if "%IncludeExternals%"=="true" call "%dir%get_externals.bat"
9792

98-
if "%platf%"=="x64" (
99-
if "%on_64_bit%"=="true" (
100-
rem This ought to always be correct these days...
101-
set vs_platf=amd64
102-
) else (
103-
if "%do_pgo%"=="true" (
104-
echo.ERROR: Cannot cross-compile with PGO
105-
echo. 32bit operating system detected, if this is incorrect,
106-
echo. make sure the ProgramFiles(x86^) environment variable is set
107-
exit /b 1
108-
)
109-
set vs_platf=x86_amd64
93+
if "%do_pgo%" EQU "true" if "%platf%" EQU "x64" (
94+
if "%PROCESSOR_ARCHITEW6432%" NEQ "AMD64" if "%PROCESSOR_ARCHITECTURE%" NEQ "AMD64" (
95+
echo.ERROR: Cannot cross-compile with PGO
96+
echo. 32bit operating system detected. Ensure your PROCESSOR_ARCHITECTURE
97+
echo. and PROCESSOR_ARCHITEW6432 environment variables are correct.
98+
exit /b 1
11099
)
111100
)
112101

@@ -115,7 +104,8 @@ if exist "%GIT%" set GITProperty=/p:GIT="%GIT%"
115104
if not exist "%GIT%" echo Cannot find Git on PATH & set GITProperty=
116105

117106
rem Setup the environment
118-
call "%dir%env.bat" %vs_platf% >nul
107+
call "%dir%find_msbuild.bat" %MSBUILD%
108+
if ERRORLEVEL 1 (echo Cannot locate MSBuild.exe on PATH or as MSBUILD variable & exit /b 2)
119109

120110
if "%kill%"=="true" call :Kill
121111

@@ -134,7 +124,7 @@ if "%do_pgo%"=="true" (
134124
goto Build
135125
:Kill
136126
echo on
137-
msbuild "%dir%\pythoncore.vcxproj" /t:KillPython %verbose%^
127+
%MSBUILD% "%dir%\pythoncore.vcxproj" /t:KillPython %verbose%^
138128
/p:Configuration=%conf% /p:Platform=%platf%^
139129
/p:KillPython=true
140130

@@ -146,7 +136,7 @@ rem Call on MSBuild to do the work, echo the command.
146136
rem Passing %1-9 is not the preferred option, but argument parsing in
147137
rem batch is, shall we say, "lackluster"
148138
echo on
149-
msbuild "%dir%pcbuild.proj" /t:%target% %parallel% %verbose%^
139+
%MSBUILD% "%dir%pcbuild.proj" /t:%target% %parallel% %verbose%^
150140
/p:Configuration=%conf% /p:Platform=%platf%^
151141
/p:IncludeExternals=%IncludeExternals%^
152142
/p:IncludeSSL=%IncludeSSL% /p:IncludeTkinter=%IncludeTkinter%^
@@ -158,4 +148,4 @@ goto :eof
158148

159149
:Version
160150
rem Display the current build version information
161-
msbuild "%dir%python.props" /t:ShowVersionInfo /v:m /nologo %1 %2 %3 %4 %5 %6 %7 %8 %9
151+
%MSBUILD% "%dir%python.props" /t:ShowVersionInfo /v:m /nologo %1 %2 %3 %4 %5 %6 %7 %8 %9

PCbuild/find_msbuild.bat

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
@rem
2+
@rem Searches for MSBuild.exe. This is the only tool we need to initiate
3+
@rem a build, so we no longer search for the full VC toolset.
4+
@rem
5+
@rem This file is supposed to modify the state of the caller (specifically
6+
@rem the MSBUILD variable), so we do not use setlocal or echo, and avoid
7+
@rem changing any other persistent state.
8+
@rem
9+
10+
@rem No arguments provided means do full search
11+
@if '%1' EQU '' goto :begin_search
12+
13+
@rem One argument may be the full path. Use a goto so we don't try to
14+
@rem parse the next if statement - incorrect quoting in the multi-arg
15+
@rem case can cause us to break immediately.
16+
@if '%2' EQU '' goto :one_arg
17+
18+
@rem Entire command line may represent the full path if quoting failed.
19+
@if exist "%*" (set MSBUILD="%*") & (set _Py_MSBuild_Source=environment) & goto :found
20+
@goto :begin_search
21+
22+
:one_arg
23+
@if exist "%~1" (set MSBUILD="%~1") & (set _Py_MSBuild_Source=environment) & goto :found
24+
25+
:begin_search
26+
@set MSBUILD=
27+
28+
@rem If msbuild.exe is on the PATH, assume that the user wants that one.
29+
@where msbuild > "%TEMP%\msbuild.loc" 2> nul && set /P MSBUILD= < "%TEMP%\msbuild.loc" & del "%TEMP%\msbuild.loc"
30+
@if exist "%MSBUILD%" set MSBUILD="%MSBUILD%" & (set _Py_MSBuild_Source=PATH) & goto :found
31+
32+
@rem VS 2017 sets exactly one install as the "main" install, so we may find MSBuild in there.
33+
@reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\SxS\VS7" /v 15.0 /reg:32 >nul 2>nul
34+
@if NOT ERRORLEVEL 1 @for /F "tokens=1,2*" %%i in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\SxS\VS7" /v 15.0 /reg:32') DO @(
35+
@if "%%i"=="15.0" @if exist "%%k\MSBuild\15.0\Bin\msbuild.exe" @(set MSBUILD="%%k\MSBuild\15.0\Bin\msbuild.exe")
36+
)
37+
@if exist %MSBUILD% (set _Py_MSBuild_Source=Visual Studio 2017 registry) & goto :found
38+
39+
@rem VS 2015 and earlier register MSBuild separately, so we can find it.
40+
@reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0" /v MSBuildToolsPath /reg:32 >nul 2>nul
41+
@if NOT ERRORLEVEL 1 @for /F "tokens=1,2*" %%i in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0" /v MSBuildToolsPath /reg:32') DO @(
42+
@if "%%i"=="MSBuildToolsPath" @if exist "%%k\msbuild.exe" @(set MSBUILD="%%k\msbuild.exe")
43+
)
44+
@if exist %MSBUILD% (set _Py_MSBuild_Source=registry) & goto :found
45+
46+
47+
@exit /b 1
48+
49+
:found
50+
@echo Using %MSBUILD% (found in the %_Py_MSBuild_Source%)
51+
@set _Py_MSBuild_Source=

PCbuild/python.vcxproj

+4-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
8484
<ImportGroup Label="ExtensionTargets">
8585
</ImportGroup>
86-
<Target Name="ValidateUcrtbase" AfterTargets="AfterBuild">
86+
<Target Name="ValidateUcrtbase" AfterTargets="AfterBuild" Condition="$(Configuration) != 'PGInstrument'">
8787
<PropertyGroup>
8888
<UcrtName>ucrtbase</UcrtName>
8989
<UcrtName Condition="'$(Configuration)' == 'Debug'">ucrtbased</UcrtName>
@@ -94,6 +94,8 @@ set PYTHONPATH=$(PySourcePath)Lib
9494
</Target>
9595
<Target Name="GeneratePythonBat" AfterTargets="AfterBuild">
9696
<PropertyGroup>
97+
<_PGOPath Condition="$(Configuration) == 'PGInstrument' and $(Platform) == 'Win32'">@set PATH=%PATH%%3B$(VCInstallDir)bin</_PGOPath>
98+
<_PGOPath Condition="$(Configuration) == 'PGInstrument' and $(Platform) == 'x64'">@set PATH=%PATH%%3B$(VCInstallDir)bin\amd64</_PGOPath>
9799
<_Content>@rem This script invokes the most recently built Python with all arguments
98100
@rem passed through to the interpreter. This file is generated by the
99101
@rem build process and any changes *will* be thrown away by the next
@@ -103,6 +105,7 @@ set PYTHONPATH=$(PySourcePath)Lib
103105
@echo Running $(Configuration)^|$(Platform) interpreter...
104106
@setlocal
105107
@set PYTHONHOME=$(PySourcePath)
108+
$(_PGOPath)
106109
@"$(OutDir)python$(PyDebugExt).exe" %*
107110
</_Content>
108111
<_ExistingContent Condition="Exists('$(PySourcePath)python.bat')">$([System.IO.File]::ReadAllText('$(PySourcePath)python.bat'))</_ExistingContent>

Tools/msi/build.bat

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ if "%~1" EQU "-r" (set REBUILD=-r) && shift && goto CheckOpts
2222
if not defined BUILDX86 if not defined BUILDX64 (set BUILDX86=1) && (set BUILDX64=1)
2323

2424
call "%D%get_externals.bat"
25-
26-
call "%PCBUILD%env.bat" x86
25+
call "%PCBUILD%find_msbuild.bat" %MSBUILD%
26+
if ERRORLEVEL 1 (echo Cannot locate MSBuild.exe on PATH or as MSBUILD variable & exit /b 2)
2727

2828
if defined BUILDX86 (
2929
call "%PCBUILD%build.bat" -d -e %REBUILD% %BUILDTEST%
@@ -44,7 +44,7 @@ if defined BUILDDOC (
4444
)
4545

4646
rem Build the launcher MSI separately
47-
msbuild "%D%launcher\launcher.wixproj" /p:Platform=x86
47+
%MSBUILD% "%D%launcher\launcher.wixproj" /p:Platform=x86
4848

4949
set BUILD_CMD="%D%bundle\snapshot.wixproj"
5050
if defined BUILDTEST (
@@ -58,11 +58,11 @@ if defined REBUILD (
5858
)
5959

6060
if defined BUILDX86 (
61-
msbuild %BUILD_CMD%
61+
%MSBUILD% %BUILD_CMD%
6262
if errorlevel 1 goto :eof
6363
)
6464
if defined BUILDX64 (
65-
msbuild /p:Platform=x64 %BUILD_CMD%
65+
%MSBUILD% /p:Platform=x64 %BUILD_CMD%
6666
if errorlevel 1 goto :eof
6767
)
6868

Tools/msi/buildrelease.bat

+8-7
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ if not exist "%GIT%" where git > "%TEMP%\git.loc" 2> nul && set /P GIT= < "%TEMP
6969
if not exist "%GIT%" echo Cannot find Git on PATH && exit /B 1
7070

7171
call "%D%get_externals.bat"
72+
call "%PCBUILD%find_msbuild.bat" %MSBUILD%
73+
if ERRORLEVEL 1 (echo Cannot locate MSBuild.exe on PATH or as MSBUILD variable & exit /b 2)
7274

7375
:builddoc
7476
if "%SKIPBUILD%" EQU "1" goto skipdoc
@@ -165,28 +167,27 @@ if not "%SKIPBUILD%" EQU "1" (
165167
@echo off
166168
)
167169

168-
call "%PCBUILD%env.bat"
169170
if "%OUTDIR_PLAT%" EQU "win32" (
170-
msbuild "%D%launcher\launcher.wixproj" /p:Platform=x86 %CERTOPTS% /p:ReleaseUri=%RELEASE_URI%
171+
%MSBUILD% "%D%launcher\launcher.wixproj" /p:Platform=x86 %CERTOPTS% /p:ReleaseUri=%RELEASE_URI%
171172
if errorlevel 1 exit /B
172173
) else if not exist "%PCBUILD%win32\en-us\launcher.msi" (
173-
msbuild "%D%launcher\launcher.wixproj" /p:Platform=x86 %CERTOPTS% /p:ReleaseUri=%RELEASE_URI%
174+
%MSBUILD% "%D%launcher\launcher.wixproj" /p:Platform=x86 %CERTOPTS% /p:ReleaseUri=%RELEASE_URI%
174175
if errorlevel 1 exit /B
175176
)
176177

177178
set BUILDOPTS=/p:Platform=%1 /p:BuildForRelease=true /p:DownloadUrl=%DOWNLOAD_URL% /p:DownloadUrlBase=%DOWNLOAD_URL_BASE% /p:ReleaseUri=%RELEASE_URI%
178-
msbuild "%D%bundle\releaselocal.wixproj" /t:Rebuild %BUILDOPTS% %CERTOPTS% /p:RebuildAll=true
179+
%MSBUILD% "%D%bundle\releaselocal.wixproj" /t:Rebuild %BUILDOPTS% %CERTOPTS% /p:RebuildAll=true
179180
if errorlevel 1 exit /B
180-
msbuild "%D%bundle\releaseweb.wixproj" /t:Rebuild %BUILDOPTS% %CERTOPTS% /p:RebuildAll=false
181+
%MSBUILD% "%D%bundle\releaseweb.wixproj" /t:Rebuild %BUILDOPTS% %CERTOPTS% /p:RebuildAll=false
181182
if errorlevel 1 exit /B
182183

183184
if defined BUILDZIP (
184-
msbuild "%D%make_zip.proj" /t:Build %BUILDOPTS% %CERTOPTS% /p:OutputPath="%BUILD%en-us"
185+
%MSBUILD% "%D%make_zip.proj" /t:Build %BUILDOPTS% %CERTOPTS% /p:OutputPath="%BUILD%en-us"
185186
if errorlevel 1 exit /B
186187
)
187188

188189
if defined BUILDNUGET (
189-
msbuild "%D%..\nuget\make_pkg.proj" /t:Build /p:Configuration=Release /p:Platform=%1 /p:OutputPath="%BUILD%en-us"
190+
%MSBUILD% "%D%..\nuget\make_pkg.proj" /t:Build /p:Configuration=Release /p:Platform=%1 /p:OutputPath="%BUILD%en-us"
190191
if errorlevel 1 exit /B
191192
)
192193

Tools/msi/uploadrelease.bat

+8-7
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,17 @@ if defined NOGPG (
4949
echo Found gpg2.exe at %GPG%
5050
)
5151

52-
call "%PCBUILD%env.bat" > nul 2> nul
52+
call "%PCBUILD%find_msbuild.bat" %MSBUILD%
53+
if ERRORLEVEL 1 (echo Cannot locate MSBuild.exe on PATH or as MSBUILD variable & exit /b 2)
5354
pushd "%D%"
54-
msbuild /v:m /nologo uploadrelease.proj /t:Upload /p:Platform=x86 %PURGE_OPTION%
55-
msbuild /v:m /nologo uploadrelease.proj /t:Upload /p:Platform=x64 /p:IncludeDoc=false %PURGE_OPTION%
55+
%MSBUILD% /v:m /nologo uploadrelease.proj /t:Upload /p:Platform=x86 %PURGE_OPTION%
56+
%MSBUILD% /v:m /nologo uploadrelease.proj /t:Upload /p:Platform=x64 /p:IncludeDoc=false %PURGE_OPTION%
5657
if not defined NOTEST (
57-
msbuild /v:m /nologo uploadrelease.proj /t:Test /p:Platform=x86
58-
msbuild /v:m /nologo uploadrelease.proj /t:Test /p:Platform=x64
58+
%MSBUILD% /v:m /nologo uploadrelease.proj /t:Test /p:Platform=x86
59+
%MSBUILD% /v:m /nologo uploadrelease.proj /t:Test /p:Platform=x64
5960
)
60-
msbuild /v:m /nologo uploadrelease.proj /t:ShowHashes /p:Platform=x86
61-
msbuild /v:m /nologo uploadrelease.proj /t:ShowHashes /p:Platform=x64 /p:IncludeDoc=false
61+
%MSBUILD% /v:m /nologo uploadrelease.proj /t:ShowHashes /p:Platform=x86
62+
%MSBUILD% /v:m /nologo uploadrelease.proj /t:ShowHashes /p:Platform=x64 /p:IncludeDoc=false
6263
popd
6364
exit /B 0
6465

Tools/nuget/build.bat

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ if "%~1" EQU "-p" (set PACKAGES=%PACKAGES% %~2) && shift && shift && goto CheckO
2121
if not defined BUILDX86 if not defined BUILDX64 (set BUILDX86=1) && (set BUILDX64=1)
2222

2323
call "%D%..\msi\get_externals.bat"
24-
call "%PCBUILD%env.bat" x86
24+
call "%PCBUILD%find_msbuild.bat" %MSBUILD%
25+
if ERRORLEVEL 1 (echo Cannot locate MSBuild.exe on PATH or as MSBUILD variable & exit /b 2)
2526

2627
if defined PACKAGES set PACKAGES="/p:Packages=%PACKAGES%"
2728

@@ -30,7 +31,7 @@ if defined BUILDX86 (
3031
) else if not exist "%PCBUILD%win32\python.exe" call "%PCBUILD%build.bat" -e
3132
if errorlevel 1 goto :eof
3233

33-
msbuild "%D%make_pkg.proj" /p:Configuration=Release /p:Platform=x86 %OUTPUT% %PACKAGES%
34+
%MSBUILD% "%D%make_pkg.proj" /p:Configuration=Release /p:Platform=x86 %OUTPUT% %PACKAGES%
3435
if errorlevel 1 goto :eof
3536
)
3637

@@ -39,7 +40,7 @@ if defined BUILDX64 (
3940
) else if not exist "%PCBUILD%amd64\python.exe" call "%PCBUILD%build.bat" -p x64 -e
4041
if errorlevel 1 goto :eof
4142

42-
msbuild "%D%make_pkg.proj" /p:Configuration=Release /p:Platform=x64 %OUTPUT% %PACKAGES%
43+
%MSBUILD% "%D%make_pkg.proj" /p:Configuration=Release /p:Platform=x64 %OUTPUT% %PACKAGES%
4344
if errorlevel 1 goto :eof
4445
)
4546

0 commit comments

Comments
 (0)