Skip to content

Commit f086456

Browse files
[windows] add Embeddable Python to build.ps1
(cherry picked from commit df431c7)
1 parent a6aa30f commit f086456

File tree

1 file changed

+69
-12
lines changed

1 file changed

+69
-12
lines changed

utils/build.ps1

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,25 @@ $KnownPythons = @{
348348
URL = "https://www.nuget.org/api/v2/package/pythonarm64/3.9.10";
349349
SHA256 = "429ada77e7f30e4bd8ff22953a1f35f98b2728e84c9b1d006712561785641f69";
350350
};
351-
}
351+
};
352+
"3.10.1" = @{
353+
AMD64 = @{
354+
URL = "https://www.nuget.org/api/v2/package/python/3.10.1";
355+
SHA256 = "987a0e446d68900f58297bc47dc7a235ee4640a49dace58bc9f573797d3a8b33";
356+
};
357+
AMD64_Embedded = @{
358+
URL = "https://www.python.org/ftp/python/3.10.1/python-3.10.1-embed-amd64.zip";
359+
SHA256 = "502670dcdff0083847abf6a33f30be666594e7e5201cd6fccd4a523b577403de";
360+
};
361+
ARM64 = @{
362+
URL = "https://www.nuget.org/api/v2/package/pythonarm64/3.10.1";
363+
SHA256 = "16becfccedf1269ff0b8695a13c64fac2102a524d66cecf69a8f9229a43b10d3";
364+
};
365+
ARM64_Embedded = @{
366+
URL = "https://www.python.org/ftp/python/3.10.1/python-3.10.1-embed-arm64.zip";
367+
SHA256 = "1f9e215fe4e8f22a8e8fba1859efb1426437044fb3103ce85794630e3b511bc2";
368+
};
369+
};
352370
}
353371

354372
$PythonWheels = @{
@@ -527,6 +545,10 @@ function Get-PythonPath([Hashtable] $Platform) {
527545
return [IO.Path]::Combine("$BinaryCache\", "Python$($Platform.Architecture.CMakeName)-$PythonVersion")
528546
}
529547

548+
function Get-EmbeddedPythonPath([Hashtable] $Platform) {
549+
return [IO.Path]::Combine("$BinaryCache\", "EmbeddedPython$($Platform.Architecture.CMakeName)-$PythonVersion")
550+
}
551+
530552
function Get-PythonExecutable {
531553
return [IO.Path]::Combine((Get-PythonPath $BuildPlatform), "tools", "python.exe")
532554
}
@@ -535,6 +557,10 @@ function Get-PythonScriptsPath {
535557
return [IO.Path]::Combine((Get-PythonPath $BuildPlatform), "tools", "Scripts")
536558
}
537559

560+
function Get-EmbeddedPythonInstallDir() {
561+
return [IO.Path]::Combine("$ImageRoot\", "Program Files", "Swift", "Python-$PythonVersion")
562+
}
563+
538564
function Get-InstallDir([Hashtable] $Platform) {
539565
if ($Platform -eq $HostPlatform) {
540566
return [IO.Path]::Combine("$ImageRoot\", "Program Files", "Swift")
@@ -852,6 +878,10 @@ function Invoke-VsDevShell([Hashtable] $Platform) {
852878
}
853879
}
854880

881+
function Get-PythonLibName() {
882+
return "python{0}{1}" -f ([System.Version]$PythonVersion).Major, ([System.Version]$PythonVersion).Minor
883+
}
884+
855885
function Get-Dependencies {
856886
Write-Host "[$([DateTime]::Now.ToString("yyyy-MM-dd HH:mm:ss"))] Fetch-Dependencies ..." -ForegroundColor Cyan
857887
$ProgressPreference = "SilentlyContinue"
@@ -967,19 +997,32 @@ function Get-Dependencies {
967997
New-Item -ItemType Directory -ErrorAction Ignore $BinaryCache\toolchains | Out-Null
968998
Export-Toolchain "$PinnedToolchain.exe" $BinaryCache $PinnedToolchain
969999

970-
function Get-KnownPython([string] $ArchName) {
1000+
function Get-KnownPython([string] $ArchName, [bool] $EmbeddedPython = $false) {
9711001
if (-not $KnownPythons.ContainsKey($PythonVersion)) {
9721002
throw "Unknown python version: $PythonVersion"
9731003
}
974-
return $KnownPythons[$PythonVersion].$ArchName
1004+
$Key = $(if ($EmbeddedPython) { "${ArchName}_Embedded" } else { $ArchName })
1005+
return $KnownPythons[$PythonVersion][$Key]
9751006
}
9761007

977-
function Install-Python([string] $ArchName) {
978-
$Python = Get-KnownPython $ArchName
979-
DownloadAndVerify $Python.URL "$BinaryCache\Python$ArchName-$PythonVersion.zip" $Python.SHA256
1008+
function Install-Python([string] $ArchName, [bool] $EmbeddedPython = $false) {
1009+
$Python = Get-KnownPython $ArchName $EmbeddedPython
1010+
$FileName = $(if ($EmbeddedPython) { "EmbeddedPython$ArchName-$PythonVersion" } else { "Python$ArchName-$PythonVersion" })
1011+
DownloadAndVerify $Python.URL "$BinaryCache\$FileName.zip" $Python.SHA256
9801012
if (-not $ToBatch) {
981-
Expand-ZipFile Python$ArchName-$PythonVersion.zip "$BinaryCache" Python$ArchName-$PythonVersion
1013+
Expand-ZipFile "$FileName.zip" "$BinaryCache" "$FileName"
1014+
Write-Success "$ArchName Python $PythonVersion"
9821015
}
1016+
if (-not $EmbeddedPython) {
1017+
return
1018+
}
1019+
$PythonPTHPath = "$BinaryCache/$FileName/$(Get-PythonLibName)._pth"
1020+
$PythonPTHContent = [System.IO.File]::ReadAllText($PythonPTHPath).Replace("#import site","import site")
1021+
[System.IO.File]::WriteAllText($PythonPTHPath, $PythonPTHContent)
1022+
$GetPipURL = "https://bootstrap.pypa.io/get-pip.py"
1023+
$GetPipPath = "$BinaryCache/$FileName/get-pip.py"
1024+
$WebClient.DownloadFile($GetPipURL, $GetPipPath)
1025+
& "$BinaryCache/$FileName/python.exe" $GetPipPath
9831026
}
9841027

9851028
function Install-PIPIfNeeded() {
@@ -1017,12 +1060,13 @@ function Get-Dependencies {
10171060
}
10181061
}
10191062

1063+
# Ensure Python modules that are required as host build tools
10201064
Install-Python $HostArchName
1065+
Install-Python $HostArchName $true
10211066
if ($IsCrossCompiling) {
10221067
Install-Python $BuildArchName
1068+
Install-Python $BuildArchName $true
10231069
}
1024-
1025-
# Ensure Python modules that are required as host build tools
10261070
Install-PythonModules
10271071

10281072
if ($Android) {
@@ -1706,7 +1750,7 @@ function Load-LitTestOverrides($Filename) {
17061750
function Get-CompilersDefines([Hashtable] $Platform, [switch] $Test) {
17071751
$BuildTools = [IO.Path]::Combine((Get-ProjectBinaryCache $BuildPlatform BuildTools), "bin")
17081752
$PythonRoot = [IO.Path]::Combine((Get-PythonPath $Platform), "tools")
1709-
$PythonLibName = "python{0}{1}" -f ([System.Version]$PythonVersion).Major, ([System.Version]$PythonVersion).Minor
1753+
$PythonLibName = Get-PythonLibName
17101754

17111755
$TestDefines = if ($Test) {
17121756
@{
@@ -1745,6 +1789,7 @@ function Get-CompilersDefines([Hashtable] $Platform, [switch] $Test) {
17451789
LLDB_PYTHON_EXE_RELATIVE_PATH = "python.exe";
17461790
LLDB_PYTHON_EXT_SUFFIX = ".pyd";
17471791
LLDB_PYTHON_RELATIVE_PATH = "lib/site-packages";
1792+
LLDB_PYTHON_DLL_RELATIVE_PATH = "../../../../Python-$PythonVersion";
17481793
LLDB_TABLEGEN = (Join-Path -Path $BuildTools -ChildPath "lldb-tblgen.exe");
17491794
LLDB_TEST_MAKE = "$BinaryCache\GnuWin32Make-4.4.1\bin\make.exe";
17501795
LLVM_CONFIG_PATH = (Join-Path -Path $BuildTools -ChildPath "llvm-config.exe");
@@ -1758,6 +1803,7 @@ function Get-CompilersDefines([Hashtable] $Platform, [switch] $Test) {
17581803
Python3_INCLUDE_DIR = "$PythonRoot\include";
17591804
Python3_LIBRARY = "$PythonRoot\libs\$PythonLibName.lib";
17601805
Python3_ROOT_DIR = $PythonRoot;
1806+
Python3_VERSION = $PythonVersion;
17611807
SWIFT_TOOLCHAIN_VERSION = "${ToolchainIdentifier}";
17621808
SWIFT_BUILD_SWIFT_SYNTAX = "YES";
17631809
SWIFT_CLANG_LOCATION = (Get-PinnedToolchainToolsDir);
@@ -3072,8 +3118,18 @@ function Install-HostToolchain() {
30723118

30733119
# Switch to swift-driver
30743120
$SwiftDriver = ([IO.Path]::Combine((Get-ProjectBinaryCache $HostPlatform Driver), "bin", "swift-driver.exe"))
3075-
Copy-Item -Force $SwiftDriver "$($HostPlatform.ToolchainInstallRoot)\usr\bin\swift.exe"
3076-
Copy-Item -Force $SwiftDriver "$($HostPlatform.ToolchainInstallRoot)\usr\bin\swiftc.exe"
3121+
Copy-Item -Force `
3122+
-Path $SwiftDriver `
3123+
-Destination "$($HostPlatform.ToolchainInstallRoot)\usr\bin\swift.exe"
3124+
Copy-Item -Force `
3125+
-Path $SwiftDriver `
3126+
-Destination "$($HostPlatform.ToolchainInstallRoot)\usr\bin\swiftc.exe"
3127+
3128+
# Copy embeddable Python
3129+
New-Item -Type Directory -Path "$(Get-EmbeddedPythonInstallDir)" -ErrorAction Ignore | Out-Null
3130+
Copy-Item -Force -Recurse `
3131+
-Path "$(Get-EmbeddedPythonPath $HostPlatform)\*" `
3132+
-Destination "$(Get-EmbeddedPythonInstallDir)"
30773133
}
30783134

30793135
function Build-Inspect([Hashtable] $Platform) {
@@ -3146,6 +3202,7 @@ function Build-Installer([Hashtable] $Platform) {
31463202
INCLUDE_SWIFT_DOCC = $INCLUDE_SWIFT_DOCC;
31473203
SWIFT_DOCC_BUILD = "$(Get-ProjectBinaryCache $HostPlatform DocC)\release";
31483204
SWIFT_DOCC_RENDER_ARTIFACT_ROOT = "${SourceCache}\swift-docc-render-artifact";
3205+
PythonVersion = $PythonVersion
31493206
}
31503207

31513208
Invoke-IsolatingEnvVars {

0 commit comments

Comments
 (0)