Skip to content

Commit 9020c11

Browse files
[windows] add Embeddable Python to build.ps1
1 parent 450fe43 commit 9020c11

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

utils/build.ps1

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,18 @@ $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
};
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+
};
422430
};
423431
}
424432

@@ -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
function Write-Success([string] $Description) {
@@ -1252,20 +1272,32 @@ function Get-Dependencies {
12521272
Write-Success "syft $SyftVersion"
12531273
}
12541274

1255-
function Get-KnownPython([string] $ArchName) {
1275+
function Get-KnownPython([string] $ArchName, [bool] $EmbeddedPython = $false) {
12561276
if (-not $KnownPythons.ContainsKey($PythonVersion)) {
12571277
throw "Unknown python version: $PythonVersion"
12581278
}
1259-
return $KnownPythons[$PythonVersion].$ArchName
1279+
$Key = $(if ($EmbeddedPython) { "${ArchName}_Embedded" } else { $ArchName })
1280+
return $KnownPythons[$PythonVersion][$Key]
12601281
}
12611282

1262-
function Install-Python([string] $ArchName) {
1263-
$Python = Get-KnownPython $ArchName
1264-
DownloadAndVerify $Python.URL "$BinaryCache\Python$ArchName-$PythonVersion.zip" $Python.SHA256
1283+
function Install-Python([string] $ArchName, [bool] $EmbeddedPython = $false) {
1284+
$Python = Get-KnownPython $ArchName $EmbeddedPython
1285+
$FileName = $(if ($EmbeddedPython) { "EmbeddedPython$ArchName-$PythonVersion" } else { "Python$ArchName-$PythonVersion" })
1286+
DownloadAndVerify $Python.URL "$BinaryCache\$FileName.zip" $Python.SHA256
12651287
if (-not $ToBatch) {
1266-
Expand-ZipFile Python$ArchName-$PythonVersion.zip "$BinaryCache" Python$ArchName-$PythonVersion
1288+
Expand-ZipFile "$FileName.zip" "$BinaryCache" "$FileName"
12671289
Write-Success "$ArchName Python $PythonVersion"
12681290
}
1291+
if (-not $EmbeddedPython) {
1292+
return
1293+
}
1294+
$PythonPTHPath = "$BinaryCache/$FileName/$(Get-PythonLibName)._pth"
1295+
$PythonPTHContent = [System.IO.File]::ReadAllText($PythonPTHPath).Replace("#import site","import site")
1296+
[System.IO.File]::WriteAllText($PythonPTHPath, $PythonPTHContent)
1297+
$GetPipURL = "https://bootstrap.pypa.io/get-pip.py"
1298+
$GetPipPath = "$BinaryCache/$FileName/get-pip.py"
1299+
$WebClient.DownloadFile($GetPipURL, $GetPipPath)
1300+
& "$BinaryCache/$FileName/python.exe" $GetPipPath
12691301
}
12701302

12711303
function Install-PIPIfNeeded {
@@ -1318,8 +1350,10 @@ function Get-Dependencies {
13181350

13191351
# Ensure Python modules that are required as host build tools
13201352
Install-Python $HostArchName
1353+
Install-Python $HostArchName $true
13211354
if ($IsCrossCompiling) {
13221355
Install-Python $BuildArchName
1356+
Install-Python $BuildArchName $true
13231357
}
13241358
Install-PythonModules
13251359

@@ -2169,7 +2203,7 @@ function Build-CDispatch([Hashtable] $Platform, [switch] $Static = $false) {
21692203
function Get-CompilersDefines([Hashtable] $Platform, [string] $Variant, [switch] $Test) {
21702204
$BuildTools = [IO.Path]::Combine((Get-ProjectBinaryCache $BuildPlatform BuildTools), "bin")
21712205
$PythonRoot = [IO.Path]::Combine((Get-PythonPath $Platform), "tools")
2172-
$PythonLibName = "python{0}{1}" -f ([System.Version]$PythonVersion).Major, ([System.Version]$PythonVersion).Minor
2206+
$PythonLibName = Get-PythonLibName
21732207

21742208
$TestDefines = if ($Test) {
21752209
@{
@@ -2212,6 +2246,7 @@ function Get-CompilersDefines([Hashtable] $Platform, [string] $Variant, [switch]
22122246
LLDB_PYTHON_EXE_RELATIVE_PATH = "python.exe";
22132247
LLDB_PYTHON_EXT_SUFFIX = ".pyd";
22142248
LLDB_PYTHON_RELATIVE_PATH = "lib/site-packages";
2249+
LLDB_PYTHON_DLL_RELATIVE_PATH = "../../../../Python-$PythonVersion";
22152250
LLDB_TABLEGEN = (Join-Path -Path $BuildTools -ChildPath "lldb-tblgen.exe");
22162251
LLDB_TEST_MAKE = "$BinaryCache\GnuWin32Make-4.4.1\bin\make.exe";
22172252
LLVM_CONFIG_PATH = (Join-Path -Path $BuildTools -ChildPath "llvm-config.exe");
@@ -2236,6 +2271,7 @@ function Get-CompilersDefines([Hashtable] $Platform, [string] $Variant, [switch]
22362271
Python3_INCLUDE_DIR = "$PythonRoot\include";
22372272
Python3_LIBRARY = "$PythonRoot\libs\$PythonLibName.lib";
22382273
Python3_ROOT_DIR = $PythonRoot;
2274+
Python3_VERSION = $PythonVersion;
22392275
SWIFT_TOOLCHAIN_VERSION = "${ToolchainIdentifier}";
22402276
SWIFT_BUILD_SWIFT_SYNTAX = "YES";
22412277
SWIFT_CLANG_LOCATION = (Get-PinnedToolchainToolsDir);
@@ -3938,6 +3974,12 @@ function Install-HostToolchain() {
39383974
Copy-Item -Force `
39393975
-Path $SwiftDriver `
39403976
-Destination "$($HostPlatform.ToolchainInstallRoot)\usr\bin\swiftc.exe"
3977+
3978+
# Copy embeddable Python
3979+
New-Item -Type Directory -Path "$(Get-EmbeddedPythonInstallDir)" -ErrorAction Ignore | Out-Null
3980+
Copy-Item -Force -Recurse `
3981+
-Path "$(Get-EmbeddedPythonPath $HostPlatform)\*" `
3982+
-Destination "$(Get-EmbeddedPythonInstallDir)"
39413983
}
39423984

39433985
function Build-Inspect([Hashtable] $Platform) {
@@ -4011,6 +4053,7 @@ function Build-Installer([Hashtable] $Platform) {
40114053
INCLUDE_SWIFT_DOCC = $INCLUDE_SWIFT_DOCC;
40124054
SWIFT_DOCC_BUILD = "$(Get-ProjectBinaryCache $HostPlatform DocC)\release";
40134055
SWIFT_DOCC_RENDER_ARTIFACT_ROOT = "${SourceCache}\swift-docc-render-artifact";
4056+
PythonVersion = $PythonVersion
40144057
}
40154058

40164059
Invoke-IsolatingEnvVars {

0 commit comments

Comments
 (0)