Skip to content

Commit a83efe5

Browse files
[windows] add Embeddable Python to build.ps1
1 parent 761b88f commit a83efe5

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

utils/build.ps1

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,11 +413,19 @@ $KnownPythons = @{
413413
URL = "https://www.nuget.org/api/v2/package/python/3.10.1";
414414
SHA256 = "987a0e446d68900f58297bc47dc7a235ee4640a49dace58bc9f573797d3a8b33";
415415
};
416+
AMD64_Embedded = @{
417+
URL = "https://www.python.org/ftp/python/3.10.1/python-3.10.1-embed-amd64.zip";
418+
SHA256 = "502670dcdff0083847abf6a33f30be666594e7e5201cd6fccd4a523b577403de";
419+
};
416420
ARM64 = @{
417421
URL = "https://www.nuget.org/api/v2/package/pythonarm64/3.10.1";
418422
SHA256 = "16becfccedf1269ff0b8695a13c64fac2102a524d66cecf69a8f9229a43b10d3";
419423
};
420-
};
424+
ARM64_Embedded = @{
425+
URL = "https://www.python.org/ftp/python/3.10.1/python-3.10.1-embed-arm64.zip";
426+
SHA256 = "1f9e215fe4e8f22a8e8fba1859efb1426437044fb3103ce85794630e3b511bc2";
427+
};
428+
}
421429
}
422430

423431
$PythonModules = @{
@@ -741,6 +749,10 @@ function Get-PythonPath([Hashtable] $Platform) {
741749
return [IO.Path]::Combine("$BinaryCache\", "Python$($Platform.Architecture.CMakeName)-$PythonVersion")
742750
}
743751

752+
function Get-EmbeddedPythonPath([Hashtable] $Platform) {
753+
return [IO.Path]::Combine("$BinaryCache\", "EmbeddedPython$($Platform.Architecture.CMakeName)-$PythonVersion")
754+
}
755+
744756
function Get-PythonExecutable {
745757
return [IO.Path]::Combine((Get-PythonPath $BuildPlatform), "tools", "python.exe")
746758
}
@@ -749,6 +761,10 @@ function Get-PythonScriptsPath {
749761
return [IO.Path]::Combine((Get-PythonPath $BuildPlatform), "tools", "Scripts")
750762
}
751763

764+
function Get-EmbeddedPythonInstallDir() {
765+
return [IO.Path]::Combine("$ImageRoot\", "Program Files", "Swift", "Python-$PythonVersion")
766+
}
767+
752768
function Get-Syft {
753769
return $KnownSyft[$SyftVersion][$BuildArchName]
754770
}
@@ -1250,11 +1266,33 @@ function Get-Dependencies {
12501266
return $KnownPythons[$PythonVersion].$ArchName
12511267
}
12521268

1269+
function Get-KnownEmbeddedPython([string] $ArchName) {
1270+
if (-not $KnownPythons.ContainsKey($PythonVersion)) {
1271+
throw "Unknown python version: $PythonVersion"
1272+
}
1273+
if (-not $KnownPythons[$PythonVersion].ContainsKey("${ArchName}_Embedded")) {
1274+
return $null
1275+
}
1276+
return $KnownPythons[$PythonVersion]["${ArchName}_Embedded"]
1277+
}
1278+
12531279
function Install-Python([string] $ArchName) {
12541280
$Python = Get-KnownPython $ArchName
12551281
DownloadAndVerify $Python.URL "$BinaryCache\Python$ArchName-$PythonVersion.zip" $Python.SHA256
12561282
if (-not $ToBatch) {
1257-
Expand-ZipFile Python$ArchName-$PythonVersion.zip "$BinaryCache" Python$ArchName-$PythonVersion
1283+
Expand-ZipFile "Python$ArchName-$PythonVersion.zip" "$BinaryCache" "Python$ArchName-$PythonVersion"
1284+
}
1285+
}
1286+
1287+
function Install-EmbeddedPython([string] $ArchName) {
1288+
$Python = Get-KnownEmbeddedPython $ArchName
1289+
if ($null -eq $Python) {
1290+
Write-Output "Python $PythonVersion does not have an embeddable version."
1291+
return
1292+
}
1293+
DownloadAndVerify $Python.URL "$BinaryCache\EmbeddedPython$ArchName-$PythonVersion.zip" $Python.SHA256
1294+
if (-not $ToBatch) {
1295+
Expand-ZipFile "EmbeddedPython$ArchName-$PythonVersion.zip" "$BinaryCache" "EmbeddedPython$ArchName-$PythonVersion"
12581296
}
12591297
}
12601298

@@ -1305,6 +1343,7 @@ function Get-Dependencies {
13051343
}
13061344

13071345
Install-Python $HostArchName
1346+
Install-EmbeddedPython $HostArchName
13081347
if ($IsCrossCompiling) {
13091348
Install-Python $BuildArchName
13101349
}
@@ -2163,6 +2202,7 @@ function Get-CompilersDefines([Hashtable] $Platform, [string] $Variant, [switch]
21632202
LLDB_PYTHON_EXE_RELATIVE_PATH = "python.exe";
21642203
LLDB_PYTHON_EXT_SUFFIX = ".pyd";
21652204
LLDB_PYTHON_RELATIVE_PATH = "lib/site-packages";
2205+
LLDB_PYTHON_DLL_RELATIVE_PATH = "../../../../Python-$PythonVersion";
21662206
LLDB_TABLEGEN = (Join-Path -Path $BuildTools -ChildPath "lldb-tblgen.exe");
21672207
LLDB_TEST_MAKE = "$BinaryCache\GnuWin32Make-4.4.1\bin\make.exe";
21682208
LLVM_CONFIG_PATH = (Join-Path -Path $BuildTools -ChildPath "llvm-config.exe");
@@ -2187,6 +2227,7 @@ function Get-CompilersDefines([Hashtable] $Platform, [string] $Variant, [switch]
21872227
Python3_INCLUDE_DIR = "$PythonRoot\include";
21882228
Python3_LIBRARY = "$PythonRoot\libs\$PythonLibName.lib";
21892229
Python3_ROOT_DIR = $PythonRoot;
2230+
Python3_VERSION = $PythonVersion;
21902231
SWIFT_TOOLCHAIN_VERSION = "${ToolchainIdentifier}";
21912232
SWIFT_BUILD_SWIFT_SYNTAX = "YES";
21922233
SWIFT_CLANG_LOCATION = (Get-PinnedToolchainToolsDir);
@@ -3831,6 +3872,12 @@ function Install-HostToolchain() {
38313872
Copy-Item -Force `
38323873
-Path $SwiftDriver `
38333874
-Destination "$($HostPlatform.ToolchainInstallRoot)\usr\bin\swiftc.exe"
3875+
3876+
# Copy embeddable Python
3877+
New-Item -Type Directory -Path "$(Get-EmbeddedPythonInstallDir)" -ErrorAction Ignore | Out-Null
3878+
Copy-Item -Force -Recurse `
3879+
-Path "$(Get-EmbeddedPythonPath $HostPlatform)\*" `
3880+
-Destination "$(Get-EmbeddedPythonInstallDir)"
38343881
}
38353882

38363883
function Build-Inspect([Hashtable] $Platform) {
@@ -3903,6 +3950,7 @@ function Build-Installer([Hashtable] $Platform) {
39033950
INCLUDE_SWIFT_DOCC = $INCLUDE_SWIFT_DOCC;
39043951
SWIFT_DOCC_BUILD = "$(Get-ProjectBinaryCache $HostPlatform DocC)\release";
39053952
SWIFT_DOCC_RENDER_ARTIFACT_ROOT = "${SourceCache}\swift-docc-render-artifact";
3953+
PythonVersion = $PythonVersion
39063954
}
39073955

39083956
Invoke-IsolatingEnvVars {

0 commit comments

Comments
 (0)