Skip to content

[3.5] bpo-30687: Fixes build scripts to find msbuild.exe and stop relying on vcvarsall.bat #2281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ Library
Windows
-------

- bpo-30687: Locate msbuild.exe on Windows when building rather than
vcvarsall.bat

- Issue #29392: Prevent crash when passing invalid arguments into msvcrt module.

C API
Expand Down
4 changes: 4 additions & 0 deletions PC/bdist_wininst/bdist_wininst.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
<OutDir>$(PySourcePath)lib\distutils\command\</OutDir>
<LinkIncremental>false</LinkIncremental>
<TargetName>wininst-$(VisualStudioVersion)</TargetName>
<TargetName Condition="$(PlatformToolset) == 'v140'">wininst-14.0</TargetName>
<TargetName Condition="$(PlatformToolset) == 'v120'">wininst-12.0</TargetName>
<TargetName Condition="$(PlatformToolset) == 'v110'">wininst-11.0</TargetName>
<TargetName Condition="$(PlatformToolset) == 'v100'">wininst-10.0</TargetName>
<TargetName Condition="$(Platform) == 'x64'">$(TargetName)-amd64</TargetName>
<TargetExt>.exe</TargetExt>
</PropertyGroup>
Expand Down
11 changes: 4 additions & 7 deletions PC/bdist_wininst/build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ set PCBUILD=%~dp0..\..\PCBuild\

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

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

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


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

call "%PCBUILD%env.bat" x86_amd64
if errorlevel 1 goto :eof

msbuild "%D%bdist_wininst.vcxproj" "/p:SolutionDir=%PCBUILD%\" /p:Configuration=Release /p:Platform=x64
%MSBUILD% "%D%bdist_wininst.vcxproj" "/p:SolutionDir=%PCBUILD%\" /p:Configuration=Release /p:Platform=x64
32 changes: 11 additions & 21 deletions PCbuild/build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ exit /b 127
:Run
setlocal
set platf=Win32
set vs_platf=x86
set conf=Release
set target=Build
set dir=%~dp0
Expand All @@ -57,10 +56,6 @@ set verbose=/nologo /v:m
set kill=
set do_pgo=
set pgo_job=-m test --pgo
set on_64_bit=true

rem This may not be 100% accurate, but close enough.
if "%ProgramFiles(x86)%"=="" (set on_64_bit=false)

:CheckOpts
if "%~1"=="-h" goto Usage
Expand Down Expand Up @@ -90,18 +85,12 @@ if "%IncludeTkinter%"=="" set IncludeTkinter=true

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

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

Expand All @@ -110,7 +99,8 @@ if exist "%GIT%" set GITProperty=/p:GIT="%GIT%"
if not exist "%GIT%" echo Cannot find Git on PATH & set GITProperty=

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

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

Expand All @@ -129,7 +119,7 @@ goto Build

:Kill
echo on
msbuild "%dir%\pythoncore.vcxproj" /t:KillPython %verbose%^
%MSBUILD% "%dir%\pythoncore.vcxproj" /t:KillPython %verbose%^
/p:Configuration=%conf% /p:Platform=%platf%^
/p:KillPython=true

Expand All @@ -141,7 +131,7 @@ rem Call on MSBuild to do the work, echo the command.
rem Passing %1-9 is not the preferred option, but argument parsing in
rem batch is, shall we say, "lackluster"
echo on
msbuild "%dir%pcbuild.proj" /t:%target% %parallel% %verbose%^
%MSBUILD% "%dir%pcbuild.proj" /t:%target% %parallel% %verbose%^
/p:Configuration=%conf% /p:Platform=%platf%^
/p:IncludeExternals=%IncludeExternals%^
/p:IncludeSSL=%IncludeSSL% /p:IncludeTkinter=%IncludeTkinter%^
Expand All @@ -153,4 +143,4 @@ goto :eof

:Version
rem Display the current build version information
msbuild "%dir%python.props" /t:ShowVersionInfo /v:m /nologo %1 %2 %3 %4 %5 %6 %7 %8 %9
%MSBUILD% "%dir%python.props" /t:ShowVersionInfo /v:m /nologo %1 %2 %3 %4 %5 %6 %7 %8 %9
51 changes: 51 additions & 0 deletions PCbuild/find_msbuild.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
@rem
@rem Searches for MSBuild.exe. This is the only tool we need to initiate
@rem a build, so we no longer search for the full VC toolset.
@rem
@rem This file is supposed to modify the state of the caller (specifically
@rem the MSBUILD variable), so we do not use setlocal or echo, and avoid
@rem changing any other persistent state.
@rem

@rem No arguments provided means do full search
@if '%1' EQU '' goto :begin_search

@rem One argument may be the full path. Use a goto so we don't try to
@rem parse the next if statement - incorrect quoting in the multi-arg
@rem case can cause us to break immediately.
@if '%2' EQU '' goto :one_arg

@rem Entire command line may represent the full path if quoting failed.
@if exist "%*" (set MSBUILD="%*") & (set _Py_MSBuild_Source=environment) & goto :found
@goto :begin_search

:one_arg
@if exist "%~1" (set MSBUILD="%~1") & (set _Py_MSBuild_Source=environment) & goto :found

:begin_search
@set MSBUILD=

@rem If msbuild.exe is on the PATH, assume that the user wants that one.
@where msbuild > "%TEMP%\msbuild.loc" 2> nul && set /P MSBUILD= < "%TEMP%\msbuild.loc" & del "%TEMP%\msbuild.loc"
@if exist "%MSBUILD%" set MSBUILD="%MSBUILD%" & (set _Py_MSBuild_Source=PATH) & goto :found

@rem VS 2017 sets exactly one install as the "main" install, so we may find MSBuild in there.
@reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\SxS\VS7" /v 15.0 /reg:32 >nul 2>nul
@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 @(
@if "%%i"=="15.0" @if exist "%%k\MSBuild\15.0\Bin\msbuild.exe" @(set MSBUILD="%%k\MSBuild\15.0\Bin\msbuild.exe")
)
@if exist %MSBUILD% (set _Py_MSBuild_Source=Visual Studio 2017 registry) & goto :found

@rem VS 2015 and earlier register MSBuild separately, so we can find it.
@reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0" /v MSBuildToolsPath /reg:32 >nul 2>nul
@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 @(
@if "%%i"=="MSBuildToolsPath" @if exist "%%k\msbuild.exe" @(set MSBUILD="%%k\msbuild.exe")
)
@if exist %MSBUILD% (set _Py_MSBuild_Source=registry) & goto :found


@exit /b 1

:found
@echo Using %MSBUILD% (found in the %_Py_MSBuild_Source%)
@set _Py_MSBuild_Source=
5 changes: 4 additions & 1 deletion PCbuild/python.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
<Target Name="ValidateUcrtbase" AfterTargets="AfterBuild">
<Target Name="ValidateUcrtbase" AfterTargets="AfterBuild" Condition="$(Configuration) != 'PGInstrument'">
<PropertyGroup>
<UcrtName>ucrtbase</UcrtName>
<UcrtName Condition="'$(Configuration)' == 'Debug'">ucrtbased</UcrtName>
Expand All @@ -94,6 +94,8 @@ set PYTHONPATH=$(PySourcePath)Lib
</Target>
<Target Name="GeneratePythonBat" AfterTargets="AfterBuild">
<PropertyGroup>
<_PGOPath Condition="$(Configuration) == 'PGInstrument' and $(Platform) == 'Win32'">@set PATH=%PATH%%3B$(VCInstallDir)bin</_PGOPath>
<_PGOPath Condition="$(Configuration) == 'PGInstrument' and $(Platform) == 'x64'">@set PATH=%PATH%%3B$(VCInstallDir)bin\amd64</_PGOPath>
<_Content>@rem This script invokes the most recently built Python with all arguments
@rem passed through to the interpreter. This file is generated by the
@rem build process and any changes *will* be thrown away by the next
Expand All @@ -103,6 +105,7 @@ set PYTHONPATH=$(PySourcePath)Lib
@echo Running $(Configuration)^|$(Platform) interpreter...
@setlocal
@set PYTHONHOME=$(PySourcePath)
$(_PGOPath)
@"$(OutDir)python$(PyDebugExt).exe" %*
</_Content>
<_ExistingContent Condition="Exists('$(PySourcePath)python.bat')">$([System.IO.File]::ReadAllText('$(PySourcePath)python.bat'))</_ExistingContent>
Expand Down
10 changes: 5 additions & 5 deletions Tools/msi/build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ if "%~1" EQU "-r" (set REBUILD=-r) && shift && goto CheckOpts
if not defined BUILDX86 if not defined BUILDX64 (set BUILDX86=1) && (set BUILDX64=1)

call "%D%get_externals.bat"

call "%PCBUILD%env.bat" x86
call "%PCBUILD%find_msbuild.bat" %MSBUILD%
if ERRORLEVEL 1 (echo Cannot locate MSBuild.exe on PATH or as MSBUILD variable & exit /b 2)

if defined BUILDX86 (
call "%PCBUILD%build.bat" -d -e %REBUILD% %BUILDTEST%
Expand All @@ -44,7 +44,7 @@ if defined BUILDDOC (
)

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

set BUILD_CMD="%D%bundle\snapshot.wixproj"
if defined BUILDTEST (
Expand All @@ -58,11 +58,11 @@ if defined REBUILD (
)

if defined BUILDX86 (
msbuild %BUILD_CMD%
%MSBUILD% %BUILD_CMD%
if errorlevel 1 goto :eof
)
if defined BUILDX64 (
msbuild /p:Platform=x64 %BUILD_CMD%
%MSBUILD% /p:Platform=x64 %BUILD_CMD%
if errorlevel 1 goto :eof
)

Expand Down
Loading