Skip to content

Commit 2eb46ff

Browse files
[windows] add Embeddable Python to build.ps1
1 parent a1e33e1 commit 2eb46ff

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed

utils/build.ps1

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,19 @@ $KnownPythons = @{
415415
URL = "https://www.nuget.org/api/v2/package/python/3.10.1";
416416
SHA256 = "987a0e446d68900f58297bc47dc7a235ee4640a49dace58bc9f573797d3a8b33";
417417
};
418+
AMD64_Embedded = @{
419+
URL = "https://www.python.org/ftp/python/3.10.1/python-3.10.1-embed-amd64.zip";
420+
SHA256 = "502670dcdff0083847abf6a33f30be666594e7e5201cd6fccd4a523b577403de";
421+
};
418422
ARM64 = @{
419423
URL = "https://www.nuget.org/api/v2/package/pythonarm64/3.10.1";
420424
SHA256 = "16becfccedf1269ff0b8695a13c64fac2102a524d66cecf69a8f9229a43b10d3";
421425
};
422-
};
426+
ARM64_Embedded = @{
427+
URL = "https://www.python.org/ftp/python/3.10.1/python-3.10.1-embed-arm64.zip";
428+
SHA256 = "1f9e215fe4e8f22a8e8fba1859efb1426437044fb3103ce85794630e3b511bc2";
429+
};
430+
}
423431
}
424432

425433
$PythonModules = @{
@@ -743,6 +751,10 @@ function Get-PythonPath([Hashtable] $Platform) {
743751
return [IO.Path]::Combine("$BinaryCache\", "Python$($Platform.Architecture.CMakeName)-$PythonVersion")
744752
}
745753

754+
function Get-EmbeddedPythonPath([Hashtable] $Platform) {
755+
return [IO.Path]::Combine("$BinaryCache\", "EmbeddedPython$($Platform.Architecture.CMakeName)-$PythonVersion")
756+
}
757+
746758
function Get-PythonExecutable {
747759
return [IO.Path]::Combine((Get-PythonPath $BuildPlatform), "tools", "python.exe")
748760
}
@@ -751,6 +763,10 @@ function Get-PythonScriptsPath {
751763
return [IO.Path]::Combine((Get-PythonPath $BuildPlatform), "tools", "Scripts")
752764
}
753765

766+
function Get-EmbeddedPythonInstallDir() {
767+
return [IO.Path]::Combine("$ImageRoot\", "Program Files", "Swift", "Python-$PythonVersion")
768+
}
769+
754770
function Get-Syft {
755771
return $KnownSyft[$SyftVersion][$BuildArchName]
756772
}
@@ -1119,6 +1135,10 @@ function Invoke-VsDevShell([Hashtable] $Platform) {
11191135
}
11201136
}
11211137

1138+
function Get-PythonLibName() {
1139+
return "python{0}{1}" -f ([System.Version]$PythonVersion).Major, ([System.Version]$PythonVersion).Minor
1140+
}
1141+
11221142
function Get-Dependencies {
11231143
Record-OperationTime $BuildPlatform "Get-Dependencies" {
11241144
Write-Host "[$([DateTime]::Now.ToString("yyyy-MM-dd HH:mm:ss"))] Get-Dependencies ..." -ForegroundColor Cyan
@@ -1277,19 +1297,31 @@ function Get-Dependencies {
12771297
New-Item -ItemType Directory -ErrorAction Ignore $BinaryCache\toolchains | Out-Null
12781298
Extract-Toolchain "$PinnedToolchain.exe" $BinaryCache $PinnedToolchain.TrimStart("swift-").TrimEnd("-a-windows10")
12791299

1280-
function Get-KnownPython([string] $ArchName) {
1300+
function Get-KnownPython([string] $ArchName, [bool] $EmbeddedPython = $false) {
12811301
if (-not $KnownPythons.ContainsKey($PythonVersion)) {
12821302
throw "Unknown python version: $PythonVersion"
12831303
}
1284-
return $KnownPythons[$PythonVersion].$ArchName
1304+
$Key = $(if ($EmbeddedPython) { "${ArchName}_Embedded" } else { $ArchName })
1305+
return $KnownPythons[$PythonVersion][$Key]
12851306
}
12861307

1287-
function Install-Python([string] $ArchName) {
1288-
$Python = Get-KnownPython $ArchName
1289-
DownloadAndVerify $Python.URL "$BinaryCache\Python$ArchName-$PythonVersion.zip" $Python.SHA256
1308+
function Install-Python([string] $ArchName, [bool] $EmbeddedPython = $false) {
1309+
$Python = Get-KnownPython $ArchName $EmbeddedPython
1310+
$FileName = $(if ($EmbeddedPython) { "EmbeddedPython$ArchName-$PythonVersion" } else { "Python$ArchName-$PythonVersion" })
1311+
DownloadAndVerify $Python.URL "$BinaryCache\$FileName.zip" $Python.SHA256
12901312
if (-not $ToBatch) {
1291-
Expand-ZipFile Python$ArchName-$PythonVersion.zip "$BinaryCache" Python$ArchName-$PythonVersion
1313+
Expand-ZipFile "$FileName.zip" "$BinaryCache" "$FileName"
12921314
}
1315+
if (-not $EmbeddedPython) {
1316+
return
1317+
}
1318+
$PythonPTHPath = "$BinaryCache/$FileName/$(Get-PythonLibName)._pth"
1319+
$PythonPTHContent = [System.IO.File]::ReadAllText($PythonPTHPath).Replace("#import site","import site")
1320+
[System.IO.File]::WriteAllText($PythonPTHPath, $PythonPTHContent)
1321+
$GetPipURL = "https://bootstrap.pypa.io/get-pip.py"
1322+
$GetPipPath = "$BinaryCache/$FileName/get-pip.py"
1323+
$WebClient.DownloadFile($GetPipURL, $GetPipPath)
1324+
& "$BinaryCache/$FileName/python.exe" $GetPipPath
12931325
}
12941326

12951327
function Install-PIPIfNeeded() {
@@ -1339,8 +1371,10 @@ function Get-Dependencies {
13391371
}
13401372

13411373
Install-Python $HostArchName
1374+
Install-Python $HostArchName $true
13421375
if ($IsCrossCompiling) {
13431376
Install-Python $BuildArchName
1377+
Install-Python $BuildArchName $true
13441378
}
13451379

13461380
# Ensure Python modules that are required as host build tools
@@ -2154,7 +2188,7 @@ function Build-CDispatch([Hashtable] $Platform, [switch] $Static = $false) {
21542188
function Get-CompilersDefines([Hashtable] $Platform, [string] $Variant, [switch] $Test) {
21552189
$BuildTools = [IO.Path]::Combine((Get-ProjectBinaryCache $BuildPlatform BuildTools), "bin")
21562190
$PythonRoot = [IO.Path]::Combine((Get-PythonPath $Platform), "tools")
2157-
$PythonLibName = "python{0}{1}" -f ([System.Version]$PythonVersion).Major, ([System.Version]$PythonVersion).Minor
2191+
$PythonLibName = Get-PythonLibName
21582192

21592193
$TestDefines = if ($Test) {
21602194
@{
@@ -2197,6 +2231,7 @@ function Get-CompilersDefines([Hashtable] $Platform, [string] $Variant, [switch]
21972231
LLDB_PYTHON_EXE_RELATIVE_PATH = "python.exe";
21982232
LLDB_PYTHON_EXT_SUFFIX = ".pyd";
21992233
LLDB_PYTHON_RELATIVE_PATH = "lib/site-packages";
2234+
LLDB_PYTHON_DLL_RELATIVE_PATH = "../../../../Python-$PythonVersion";
22002235
LLDB_TABLEGEN = (Join-Path -Path $BuildTools -ChildPath "lldb-tblgen.exe");
22012236
LLDB_TEST_MAKE = "$BinaryCache\GnuWin32Make-4.4.1\bin\make.exe";
22022237
LLVM_CONFIG_PATH = (Join-Path -Path $BuildTools -ChildPath "llvm-config.exe");
@@ -2221,6 +2256,7 @@ function Get-CompilersDefines([Hashtable] $Platform, [string] $Variant, [switch]
22212256
Python3_INCLUDE_DIR = "$PythonRoot\include";
22222257
Python3_LIBRARY = "$PythonRoot\libs\$PythonLibName.lib";
22232258
Python3_ROOT_DIR = $PythonRoot;
2259+
Python3_VERSION = $PythonVersion;
22242260
SWIFT_TOOLCHAIN_VERSION = "${ToolchainIdentifier}";
22252261
SWIFT_BUILD_SWIFT_SYNTAX = "YES";
22262262
SWIFT_CLANG_LOCATION = (Get-PinnedToolchainToolsDir);
@@ -3904,6 +3940,12 @@ function Install-HostToolchain() {
39043940
Copy-Item -Force `
39053941
-Path $SwiftDriver `
39063942
-Destination "$($HostPlatform.ToolchainInstallRoot)\usr\bin\swiftc.exe"
3943+
3944+
# Copy embeddable Python
3945+
New-Item -Type Directory -Path "$(Get-EmbeddedPythonInstallDir)" -ErrorAction Ignore | Out-Null
3946+
Copy-Item -Force -Recurse `
3947+
-Path "$(Get-EmbeddedPythonPath $HostPlatform)\*" `
3948+
-Destination "$(Get-EmbeddedPythonInstallDir)"
39073949
}
39083950

39093951
function Build-Inspect([Hashtable] $Platform) {
@@ -3977,6 +4019,7 @@ function Build-Installer([Hashtable] $Platform) {
39774019
INCLUDE_SWIFT_DOCC = $INCLUDE_SWIFT_DOCC;
39784020
SWIFT_DOCC_BUILD = "$(Get-ProjectBinaryCache $HostPlatform DocC)\release";
39794021
SWIFT_DOCC_RENDER_ARTIFACT_ROOT = "${SourceCache}\swift-docc-render-artifact";
4022+
PythonVersion = $PythonVersion
39804023
}
39814024

39824025
Invoke-IsolatingEnvVars {

0 commit comments

Comments
 (0)