Skip to content

Commit ab04b3a

Browse files
authored
[build.ps1] don't use host Python any more (#76594)
* [build.ps1] don't use host Python any more * [build.ps1] fix compatibility with Windows Powershell * Try to address CI failure
1 parent 8e53ccd commit ab04b3a

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

utils/build.ps1

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,6 @@ $WiXVersion = "4.0.5"
194194
# Avoid $env:ProgramFiles in case this script is running as x86
195195
$UnixToolsBinDir = "$env:SystemDrive\Program Files\Git\usr\bin"
196196

197-
$python = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Shared\Python39_64\python.exe"
198-
if (-not (Test-Path $python)) {
199-
$python = (where.exe python) | Select-Object -First 1
200-
if (-not (Test-Path $python)) {
201-
throw "Python.exe not found"
202-
}
203-
}
204-
205197
if ($Android -and ($AndroidSDKs.Length -eq 0)) {
206198
# Enable all android SDKs by default.
207199
$AndroidSDKs = @("aarch64","armv7","i686","x86_64")
@@ -358,6 +350,10 @@ function Get-BisonExecutable {
358350
return Join-Path -Path $BinaryCache -ChildPath "win_flex_bison\win_bison.exe"
359351
}
360352

353+
function Get-PythonExecutable {
354+
return Join-Path -Path $BinaryCache -ChildPath "Python$($HostArch.CMakeName)-$PythonVersion\tools\python.exe"
355+
}
356+
361357
function Get-InstallDir($Arch) {
362358
if ($Arch -eq $HostArch) {
363359
$ProgramFilesName = "Program Files"
@@ -747,10 +743,43 @@ function Fetch-Dependencies {
747743
}
748744
}
749745

746+
function Ensure-PythonModules($Python) {
747+
# First ensure pip is installed, else bootstrap it
748+
try {
749+
Invoke-Program -OutNull $Python -m pip *> $null
750+
} catch {
751+
Write-Output "Installing pip ..."
752+
Invoke-Program -OutNull $Python '-I' -m ensurepip -U --default-pip
753+
}
754+
# 'packaging' is required for building LLVM 18+
755+
try {
756+
Invoke-Program -OutNull $Python -c 'import packaging' *> $null
757+
} catch {
758+
$WheelURL = "https://files.pythonhosted.org/packages/08/aa/cc0199a5f0ad350994d660967a8efb233fe0416e4639146c089643407ce6/packaging-24.1-py3-none-any.whl"
759+
$WheelHash = "5b8f2217dbdbd2f7f384c41c628544e6d52f2d0f53c6d0c3ea61aa5d1d7ff124"
760+
DownloadAndVerify $WheelURL "$BinaryCache\python\packaging-24.1-py3-none-any.whl" $WheelHash
761+
Write-Output "Installing 'packaging-24.1-py3-none-any.whl' ..."
762+
Invoke-Program -OutNull $Python '-I' -m pip install "$BinaryCache\python\packaging-24.1-py3-none-any.whl" --disable-pip-version-check
763+
}
764+
# 'setuptools' provides 'distutils' module for Python 3.12+, required for SWIG support
765+
# https://github.com/swiftlang/llvm-project/issues/9289
766+
try {
767+
Invoke-Program -OutNull $Python -c 'import distutils' *> $null
768+
} catch {
769+
$WheelURL = "https://files.pythonhosted.org/packages/ff/ae/f19306b5a221f6a436d8f2238d5b80925004093fa3edea59835b514d9057/setuptools-75.1.0-py3-none-any.whl"
770+
$WheelHash = "35ab7fd3bcd95e6b7fd704e4a1539513edad446c097797f2985e0e4b960772f2"
771+
DownloadAndVerify $WheelURL "$BinaryCache\python\setuptools-75.1.0-py3-none-any.whl" $WheelHash
772+
Write-Output "Installing 'setuptools-75.1.0-py3-none-any.whl' ..."
773+
Invoke-Program -OutNull $Python '-I' -m pip install "$BinaryCache\python\setuptools-75.1.0-py3-none-any.whl" --disable-pip-version-check
774+
}
775+
}
776+
750777
Download-Python $HostArchName
751778
if ($IsCrossCompiling) {
752779
Download-Python $BuildArchName
753780
}
781+
# Ensure Python modules that are required as host build tools
782+
Ensure-PythonModules "$(Get-PythonExecutable)"
754783

755784
if ($Android) {
756785
# Only a specific NDK version is supported right now.
@@ -1456,6 +1485,9 @@ function Build-Compilers() {
14561485
}
14571486
}
14581487

1488+
$PythonRoot = "$BinaryCache\Python$($Arch.CMakeName)-$PythonVersion\tools"
1489+
$PythonLibName = "python{0}{1}" -f ([System.Version]$PythonVersion).Major, ([System.Version]$PythonVersion).Minor
1490+
14591491
# The STL in VS 17.10 requires Clang 17 or higher, but Swift toolchains prior to version 6 include older versions
14601492
# of Clang. If bootstrapping with an older toolchain, we need to relax to relax this requirement with
14611493
# ALLOW_COMPILER_AND_STL_VERSION_MISMATCH.
@@ -1487,10 +1519,10 @@ function Build-Compilers() {
14871519
LLVM_NATIVE_TOOL_DIR = $BuildTools;
14881520
LLVM_TABLEGEN = (Join-Path $BuildTools -ChildPath "llvm-tblgen.exe");
14891521
LLVM_USE_HOST_TOOLS = "NO";
1490-
Python3_EXECUTABLE = "$python";
1491-
Python3_INCLUDE_DIR = "$BinaryCache\Python$($Arch.CMakeName)-$PythonVersion\tools\include";
1492-
Python3_LIBRARY = "$BinaryCache\Python$($Arch.CMakeName)-$PythonVersion\tools\libs\python39.lib";
1493-
Python3_ROOT_DIR = "$BinaryCache\Python$($Arch.CMakeName)-$PythonVersion\tools";
1522+
Python3_EXECUTABLE = (Get-PythonExecutable);
1523+
Python3_INCLUDE_DIR = "$PythonRoot\include";
1524+
Python3_LIBRARY = "$PythonRoot\libs\$PythonLibName.lib";
1525+
Python3_ROOT_DIR = $PythonRoot;
14941526
SWIFT_BUILD_SWIFT_SYNTAX = "YES";
14951527
SWIFT_CLANG_LOCATION = (Get-PinnedToolchainTool);
14961528
SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY = "YES";
@@ -1815,7 +1847,7 @@ function Build-Runtime([Platform]$Platform, $Arch) {
18151847
})
18161848
}
18171849

1818-
Invoke-Program $python -c "import plistlib; print(str(plistlib.dumps({ 'DefaultProperties': { 'DEFAULT_USE_RUNTIME': 'MD' } }), encoding='utf-8'))" `
1850+
Invoke-Program "$(Get-PythonExecutable)" -c "import plistlib; print(str(plistlib.dumps({ 'DefaultProperties': { 'DEFAULT_USE_RUNTIME': 'MD' } }), encoding='utf-8'))" `
18191851
-OutFile "$($Arch.SDKInstallRoot)\SDKSettings.plist"
18201852
}
18211853

@@ -2046,7 +2078,7 @@ function Build-Testing([Platform]$Platform, $Arch, [switch]$Test = $false) {
20462078

20472079
function Write-PlatformInfoPlist($Arch) {
20482080
$PList = Join-Path -Path $Arch.PlatformInstallRoot -ChildPath "Info.plist"
2049-
Invoke-Program $python -c "import plistlib; print(str(plistlib.dumps({ 'DefaultProperties': { 'XCTEST_VERSION': 'development', 'SWIFT_TESTING_VERSION': 'development', 'SWIFTC_FLAGS': ['-use-ld=lld'] } }), encoding='utf-8'))" `
2081+
Invoke-Program "$(Get-PythonExecutable)" -c "import plistlib; print(str(plistlib.dumps({ 'DefaultProperties': { 'XCTEST_VERSION': 'development', 'SWIFT_TESTING_VERSION': 'development', 'SWIFTC_FLAGS': ['-use-ld=lld'] } }), encoding='utf-8'))" `
20502082
-OutFile "$PList"
20512083
}
20522084

0 commit comments

Comments
 (0)