Skip to content

Commit 91f23be

Browse files
zoobazware
authored andcommitted
bpo-30687: Fixes build scripts to find msbuild.exe and stop relying on vcvarsall.bat (python#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. (cherry-picked from parts of 40a23e8)
1 parent 3e9dc95 commit 91f23be

File tree

4 files changed

+78
-25
lines changed

4 files changed

+78
-25
lines changed

PCbuild/build.bat

+18-22
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ echo.given.
1010
echo.
1111
echo.After the flags recognized by this script, up to 9 arguments to be passed
1212
echo.directly to MSBuild may be passed. If the argument contains an '=', the
13-
echo.entire argument must be quoted (e.g. `%~nx0 "/p:PlatformToolset=v100"`)
13+
echo.entire argument must be quoted (e.g. `%~nx0 "/p:PlatformToolset=v100"`).
14+
echo.Alternatively you can put extra flags for MSBuild in a file named
15+
echo.`msbuild.rsp` in the `PCbuild` directory, one flag per line. This file
16+
echo.will be picked automatically by MSBuild. Flags put in this file does not
17+
echo.need to be quoted. You can still use environment variables inside the
18+
echo.response file.
1419
echo.
1520
echo.Available flags:
1621
echo. -h Display this help message
@@ -47,7 +52,6 @@ exit /b 127
4752
:Run
4853
setlocal
4954
set platf=Win32
50-
set vs_platf=x86
5155
set conf=Release
5256
set target=Build
5357
set dir=%~dp0
@@ -56,10 +60,6 @@ set verbose=/nologo /v:m
5660
set kill=
5761
set do_pgo=
5862
set pgo_job=-m test.regrtest --pgo
59-
set on_64_bit=true
60-
61-
rem This may not be 100% accurate, but close enough.
62-
if "%ProgramFiles(x86)%"=="" (set on_64_bit=false)
6363

6464
:CheckOpts
6565
if "%~1"=="-h" goto Usage
@@ -89,18 +89,12 @@ if "%IncludeBsddb%"=="" set IncludeBsddb=true
8989

9090
if "%IncludeExternals%"=="true" call "%dir%get_externals.bat"
9191

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

@@ -109,26 +103,28 @@ if exist "%GIT%" set GITProperty=/p:GIT="%GIT%"
109103
if not exist "%GIT%" echo Cannot find Git on PATH & set GITProperty=
110104

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

114109
if "%kill%"=="true" call :Kill
115110

116111
if "%do_pgo%"=="true" (
117112
set conf=PGInstrument
118-
call :Build
113+
call :Build %1 %2 %3 %4 %5 %6 %7 %8 %9
119114
del /s "%dir%\*.pgc"
120115
del /s "%dir%\..\Lib\*.pyc"
121116
echo on
122117
call "%dir%\..\python.bat" %pgo_job%
123118
@echo off
124119
call :Kill
125120
set conf=PGUpdate
121+
set target=Build
126122
)
127123
goto Build
128124

129125
:Kill
130126
echo on
131-
msbuild "%dir%\pythoncore.vcxproj" /t:KillPython %verbose%^
127+
%MSBUILD% "%dir%\pythoncore.vcxproj" /t:KillPython %verbose%^
132128
/p:Configuration=%conf% /p:Platform=%platf%^
133129
/p:KillPython=true
134130

@@ -140,7 +136,7 @@ rem Call on MSBuild to do the work, echo the command.
140136
rem Passing %1-9 is not the preferred option, but argument parsing in
141137
rem batch is, shall we say, "lackluster"
142138
echo on
143-
msbuild "%dir%pcbuild.proj" /t:%target% %parallel% %verbose%^
139+
%MSBUILD% "%dir%pcbuild.proj" /t:%target% %parallel% %verbose%^
144140
/p:Configuration=%conf% /p:Platform=%platf%^
145141
/p:IncludeExternals=%IncludeExternals%^
146142
/p:IncludeSSL=%IncludeSSL% /p:IncludeTkinter=%IncludeTkinter%^

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

+5
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,18 @@
9393
</ImportGroup>
9494
<Target Name="GeneratePythonBat" AfterTargets="AfterBuild">
9595
<PropertyGroup>
96+
<_PGOPath Condition="$(Configuration) == 'PGInstrument' and $(Platform) == 'Win32'">@set PATH=%PATH%%3B$(VCInstallDir)bin</_PGOPath>
97+
<_PGOPath Condition="$(Configuration) == 'PGInstrument' and $(Platform) == 'x64'">@set PATH=%PATH%%3B$(VCInstallDir)bin\amd64</_PGOPath>
9698
<_Content>@rem This script invokes the most recently built Python with all arguments
9799
@rem passed through to the interpreter. This file is generated by the
98100
@rem build process and any changes *will* be thrown away by the next
99101
@rem rebuild.
100102
@rem This is only meant as a convenience for developing CPython
101103
@rem and using it outside of that context is ill-advised.
102104
@echo Running $(Configuration)^|$(Platform) interpreter...
105+
@setlocal
106+
@set PYTHONHOME=$(PySourcePath)
107+
$(_PGOPath)
103108
@"$(OutDir)python$(PyDebugExt).exe" %*
104109
</_Content>
105110
<_ExistingContent Condition="Exists('$(PySourcePath)python.bat')">$([System.IO.File]::ReadAllText('$(PySourcePath)python.bat'))</_ExistingContent>

Tools/nuget/build.bat

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +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
if not defined NUGET where nuget -q || echo Cannot find nuget.exe on PATH and NUGET is not set. && exit /B 1
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)
2426
if not defined PYTHON set PYTHON=py -3
2527

2628
@%PYTHON% -c "" >nul 2>nul
@@ -29,7 +31,6 @@ if not defined PYTHON set PYTHON=py -3
2931
set PYTHON="%D%obj\python\tools\python.exe"
3032
)
3133

32-
call "%PCBUILD%env.bat" x86
3334

3435
if defined PACKAGES set PACKAGES="/p:Packages=%PACKAGES%"
3536

@@ -38,7 +39,7 @@ if defined BUILDX86 (
3839
) else if not exist "%PCBUILD%python.exe" call "%PCBUILD%build.bat" -e
3940
if errorlevel 1 goto :eof
4041

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

@@ -47,7 +48,7 @@ if defined BUILDX64 (
4748
) else if not exist "%PCBUILD%amd64\python.exe" call "%PCBUILD%build.bat" -p x64 -e
4849
if errorlevel 1 goto :eof
4950

50-
msbuild "%D%make_pkg.proj" /p:Configuration=Release /p:Platform=x64 %OUTPUT% %PACKAGES%
51+
%MSBUILD% "%D%make_pkg.proj" /p:Configuration=Release /p:Platform=x64 %OUTPUT% %PACKAGES%
5152
if errorlevel 1 goto :eof
5253
)
5354

0 commit comments

Comments
 (0)