From 7d3fdec4d78f86947b4e1dc163575ccb98df0a6f Mon Sep 17 00:00:00 2001 From: Charles Zablit Date: Mon, 4 Aug 2025 15:36:03 +0100 Subject: [PATCH] [windows] upgrade to Python 3.10 embeddable --- utils/build.ps1 | 60 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/utils/build.ps1 b/utils/build.ps1 index b5a4375a61fc6..0155aa4f3ec88 100644 --- a/utils/build.ps1 +++ b/utils/build.ps1 @@ -129,7 +129,7 @@ param [string] $PinnedSHA256 = "", [string] $PinnedVersion = "", [ValidatePattern('^\d+(\.\d+)*$')] - [string] $PythonVersion = "3.9.10", + [string] $PythonVersion = "3.10.1", [ValidatePattern("^r(?:[1-9]|[1-9][0-9])(?:[a-z])?$")] [string] $AndroidNDKVersion = "r27c", [ValidatePattern("^\d+\.\d+\.\d+(?:-\w+)?")] @@ -332,6 +332,24 @@ $KnownPythons = @{ URL = "https://www.nuget.org/api/v2/package/pythonarm64/3.9.10"; SHA256 = "429ada77e7f30e4bd8ff22953a1f35f98b2728e84c9b1d006712561785641f69"; }; + }; + "3.10.1" = @{ + AMD64 = @{ + URL = "https://www.nuget.org/api/v2/package/python/3.10.1"; + SHA256 = "987a0e446d68900f58297bc47dc7a235ee4640a49dace58bc9f573797d3a8b33"; + }; + AMD64_Embedded = @{ + URL = "https://www.python.org/ftp/python/3.10.1/python-3.10.1-embed-amd64.zip"; + SHA256 = "502670dcdff0083847abf6a33f30be666594e7e5201cd6fccd4a523b577403de"; + }; + ARM64 = @{ + URL = "https://www.nuget.org/api/v2/package/pythonarm64/3.10.1"; + SHA256 = "16becfccedf1269ff0b8695a13c64fac2102a524d66cecf69a8f9229a43b10d3"; + }; + ARM64_Embedded = @{ + URL = "https://www.python.org/ftp/python/3.10.1/python-3.10.1-embed-arm64.zip"; + SHA256 = "1f9e215fe4e8f22a8e8fba1859efb1426437044fb3103ce85794630e3b511bc2"; + }; } } @@ -597,6 +615,10 @@ function Get-PythonPath([Hashtable] $Platform) { return [IO.Path]::Combine("$BinaryCache\", "Python$($Platform.Architecture.CMakeName)-$PythonVersion") } +function Get-EmbeddedPythonPath([Hashtable] $Platform) { + return [IO.Path]::Combine("$BinaryCache\", "EmbeddedPython$($Platform.Architecture.CMakeName)-$PythonVersion") +} + function Get-PythonExecutable { return [IO.Path]::Combine((Get-PythonPath $BuildPlatform), "tools", "python.exe") } @@ -605,6 +627,10 @@ function Get-PythonScriptsPath { return [IO.Path]::Combine((Get-PythonPath $BuildPlatform), "tools", "Scripts") } +function Get-EmbeddedPythonInstallDir() { + return [IO.Path]::Combine("$ImageRoot\", "Program Files", "Swift", "Python-$PythonVersion") +} + function Get-Syft { return $KnownSyft[$SyftVersion][$BuildArchName] } @@ -1098,11 +1124,33 @@ function Get-Dependencies { return $KnownPythons[$PythonVersion].$ArchName } + function Get-KnownEmbeddedPython([string] $ArchName) { + if (-not $KnownPythons.ContainsKey($PythonVersion)) { + throw "Unknown python version: $PythonVersion" + } + if (-not $KnownPythons[$PythonVersion].ContainsKey("${ArchName}_Embedded")) { + return $null + } + return $KnownPythons[$PythonVersion]["${ArchName}_Embedded"] + } + function Install-Python([string] $ArchName) { $Python = Get-KnownPython $ArchName DownloadAndVerify $Python.URL "$BinaryCache\Python$ArchName-$PythonVersion.zip" $Python.SHA256 if (-not $ToBatch) { - Expand-ZipFile Python$ArchName-$PythonVersion.zip "$BinaryCache" Python$ArchName-$PythonVersion + Expand-ZipFile "Python$ArchName-$PythonVersion.zip" "$BinaryCache" "Python$ArchName-$PythonVersion" + } + } + + function Install-EmbeddedPython([string] $ArchName) { + $Python = Get-KnownEmbeddedPython $ArchName + if ($null -eq $Python) { + Write-Output "Python $PythonVersion does not have an embeddable version." + return + } + DownloadAndVerify $Python.URL "$BinaryCache\EmbeddedPython$ArchName-$PythonVersion.zip" $Python.SHA256 + if (-not $ToBatch) { + Expand-ZipFile "EmbeddedPython$ArchName-$PythonVersion.zip" "$BinaryCache" "EmbeddedPython$ArchName-$PythonVersion" } } @@ -1153,6 +1201,7 @@ function Get-Dependencies { } Install-Python $HostArchName + Install-EmbeddedPython $HostArchName if ($IsCrossCompiling) { Install-Python $BuildArchName } @@ -3531,6 +3580,12 @@ function Install-HostToolchain() { Copy-Item -Force ` -Path $SwiftDriver ` -Destination "$($HostPlatform.ToolchainInstallRoot)\usr\bin\swiftc.exe" + + # Copy embeddable Python + New-Item -Type Directory -Path "$(Get-EmbeddedPythonInstallDir)" -ErrorAction Ignore | Out-Null + Copy-Item -Force -Recurse ` + -Path "$(Get-EmbeddedPythonPath $HostPlatform)\*" ` + -Destination "$(Get-EmbeddedPythonInstallDir)" } function Build-Inspect([Hashtable] $Platform) { @@ -3601,6 +3656,7 @@ function Build-Installer([Hashtable] $Platform) { INCLUDE_SWIFT_DOCC = $INCLUDE_SWIFT_DOCC; SWIFT_DOCC_BUILD = "$(Get-ProjectBinaryCache $HostPlatform DocC)\release"; SWIFT_DOCC_RENDER_ARTIFACT_ROOT = "${SourceCache}\swift-docc-render-artifact"; + PythonRoot = "$(Get-EmbeddedPythonInstallDir)" } Invoke-IsolatingEnvVars {