Skip to content

Commit 4b3833f

Browse files
[windows] add Embeddable Python to build.ps1
1 parent 2149731 commit 4b3833f

File tree

1 file changed

+60
-7
lines changed

1 file changed

+60
-7
lines changed

utils/build.ps1

Lines changed: 60 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,42 @@ function Get-Dependencies {
12521272
Write-Success "syft $SyftVersion"
12531273
}
12541274

1255-
function Get-KnownPython([string] $ArchName) {
1275+
if ($SkipBuild -and $SkipPackaging) { return }
1276+
1277+
$Stopwatch = [Diagnostics.Stopwatch]::StartNew()
1278+
if ($ToBatch) {
1279+
Write-Host -ForegroundColor Cyan "[$([DateTime]::Now.ToString("yyyy-MM-dd HH:mm:ss"))] Get-Dependencies..."
1280+
}
1281+
1282+
DownloadAndVerify $WiX.URL "$BinaryCache\WiX-$($WiX.Version).zip" $WiX.SHA256
1283+
Expand-ZipFile WiX-$($WiX.Version).zip $BinaryCache WiX-$($WiX.Version)
1284+
1285+
function Get-KnownPython([string] $ArchName, [bool] $EmbeddedPython = $false) {
12561286
if (-not $KnownPythons.ContainsKey($PythonVersion)) {
12571287
throw "Unknown python version: $PythonVersion"
12581288
}
1259-
return $KnownPythons[$PythonVersion].$ArchName
1289+
$Key = $(if ($EmbeddedPython) { "${ArchName}_Embedded" } else { $ArchName })
1290+
return $KnownPythons[$PythonVersion][$Key]
12601291
}
12611292

1262-
function Install-Python([string] $ArchName) {
1263-
$Python = Get-KnownPython $ArchName
1264-
DownloadAndVerify $Python.URL "$BinaryCache\Python$ArchName-$PythonVersion.zip" $Python.SHA256
1293+
function Install-Python([string] $ArchName, [bool] $EmbeddedPython = $false) {
1294+
$Python = Get-KnownPython $ArchName $EmbeddedPython
1295+
$FileName = $(if ($EmbeddedPython) { "EmbeddedPython$ArchName-$PythonVersion" } else { "Python$ArchName-$PythonVersion" })
1296+
DownloadAndVerify $Python.URL "$BinaryCache\$FileName.zip" $Python.SHA256
12651297
if (-not $ToBatch) {
1266-
Expand-ZipFile Python$ArchName-$PythonVersion.zip "$BinaryCache" Python$ArchName-$PythonVersion
1298+
Expand-ZipFile "$FileName.zip" "$BinaryCache" "$FileName"
12671299
Write-Success "$ArchName Python $PythonVersion"
12681300
}
1301+
if (-not $EmbeddedPython) {
1302+
return
1303+
}
1304+
$PythonPTHPath = "$BinaryCache/$FileName/$(Get-PythonLibName)._pth"
1305+
$PythonPTHContent = [System.IO.File]::ReadAllText($PythonPTHPath).Replace("#import site","import site")
1306+
[System.IO.File]::WriteAllText($PythonPTHPath, $PythonPTHContent)
1307+
$GetPipURL = "https://bootstrap.pypa.io/get-pip.py"
1308+
$GetPipPath = "$BinaryCache/$FileName/get-pip.py"
1309+
$WebClient.DownloadFile($GetPipURL, $GetPipPath)
1310+
& "$BinaryCache/$FileName/python.exe" $GetPipPath
12691311
}
12701312

12711313
function Install-PIPIfNeeded {
@@ -1318,8 +1360,10 @@ function Get-Dependencies {
13181360

13191361
# Ensure Python modules that are required as host build tools
13201362
Install-Python $HostArchName
1363+
Install-Python $HostArchName $true
13211364
if ($IsCrossCompiling) {
13221365
Install-Python $BuildArchName
1366+
Install-Python $BuildArchName $true
13231367
}
13241368
Install-PythonModules
13251369

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

21742218
$TestDefines = if ($Test) {
21752219
@{
@@ -2212,6 +2256,7 @@ function Get-CompilersDefines([Hashtable] $Platform, [string] $Variant, [switch]
22122256
LLDB_PYTHON_EXE_RELATIVE_PATH = "python.exe";
22132257
LLDB_PYTHON_EXT_SUFFIX = ".pyd";
22142258
LLDB_PYTHON_RELATIVE_PATH = "lib/site-packages";
2259+
LLDB_PYTHON_DLL_RELATIVE_PATH = "../../../../Python-$PythonVersion";
22152260
LLDB_TABLEGEN = (Join-Path -Path $BuildTools -ChildPath "lldb-tblgen.exe");
22162261
LLDB_TEST_MAKE = "$BinaryCache\GnuWin32Make-4.4.1\bin\make.exe";
22172262
LLVM_CONFIG_PATH = (Join-Path -Path $BuildTools -ChildPath "llvm-config.exe");
@@ -2236,6 +2281,7 @@ function Get-CompilersDefines([Hashtable] $Platform, [string] $Variant, [switch]
22362281
Python3_INCLUDE_DIR = "$PythonRoot\include";
22372282
Python3_LIBRARY = "$PythonRoot\libs\$PythonLibName.lib";
22382283
Python3_ROOT_DIR = $PythonRoot;
2284+
Python3_VERSION = $PythonVersion;
22392285
SWIFT_TOOLCHAIN_VERSION = "${ToolchainIdentifier}";
22402286
SWIFT_BUILD_SWIFT_SYNTAX = "YES";
22412287
SWIFT_CLANG_LOCATION = (Get-PinnedToolchainToolsDir);
@@ -3938,6 +3984,12 @@ function Install-HostToolchain() {
39383984
Copy-Item -Force `
39393985
-Path $SwiftDriver `
39403986
-Destination "$($HostPlatform.ToolchainInstallRoot)\usr\bin\swiftc.exe"
3987+
3988+
# Copy embeddable Python
3989+
New-Item -Type Directory -Path "$(Get-EmbeddedPythonInstallDir)" -ErrorAction Ignore | Out-Null
3990+
Copy-Item -Force -Recurse `
3991+
-Path "$(Get-EmbeddedPythonPath $HostPlatform)\*" `
3992+
-Destination "$(Get-EmbeddedPythonInstallDir)"
39413993
}
39423994

39433995
function Build-Inspect([Hashtable] $Platform) {
@@ -4011,6 +4063,7 @@ function Build-Installer([Hashtable] $Platform) {
40114063
INCLUDE_SWIFT_DOCC = $INCLUDE_SWIFT_DOCC;
40124064
SWIFT_DOCC_BUILD = "$(Get-ProjectBinaryCache $HostPlatform DocC)\release";
40134065
SWIFT_DOCC_RENDER_ARTIFACT_ROOT = "${SourceCache}\swift-docc-render-artifact";
4066+
PythonVersion = $PythonVersion
40144067
}
40154068

40164069
Invoke-IsolatingEnvVars {

0 commit comments

Comments
 (0)