Skip to content

Commit e36e941

Browse files
Portable/Relocatable Python Linking
1 parent 521be20 commit e36e941

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

builders/ubuntu-python-builder.psm1

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class UbuntuPythonBuilder : NixPythonBuilder {
3030

3131
$pythonBinariesLocation = $this.GetFullPythonToolcacheLocation()
3232

33-
### To build Python with SO we must pass full path to lib folder to the linker
34-
$env:LDFLAGS="-Wl,--rpath=${pythonBinariesLocation}/lib"
33+
### To build Python with SO, passing relative path W.r.t to the binary location.
34+
$env:LDFLAGS="-Wl,-rpath='`$`$ORIGIN/../lib'"
3535
$configureString = "./configure"
3636
$configureString += " --prefix=$pythonBinariesLocation"
3737
$configureString += " --enable-shared"
@@ -43,6 +43,7 @@ class UbuntuPythonBuilder : NixPythonBuilder {
4343

4444
Write-Host "The passed configure options are: "
4545
Write-Host $configureString
46+
Write-Host "LDFLAGS: $env:LDFLAGS"
4647

4748
Execute-Command -Command $configureString
4849
}

tests/python-tests.ps1

+30
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ BeforeAll {
3636

3737
return 0
3838
}
39+
40+
function moveAssets([string] $source, [string] $destination) {
41+
$parentDestDir = Split-Path -Path $destination -Parent
42+
New-Item -Path $parentDestDir -ItemType Directory -Force
43+
Move-Item -Path $source -Destination $parentDestDir -Force
44+
}
3945
}
4046

4147
Describe "Tests" {
@@ -88,6 +94,30 @@ Describe "Tests" {
8894
It "Check if shared libraries are linked correctly" {
8995
"bash ./sources/psutil-install-test.sh" | Should -ReturnZeroExitCode
9096
}
97+
98+
It "Relocatable Python" {
99+
$semversion = [semver] $Version
100+
$pyfilename = "python$($semversion.Major).$($semversion.Minor)"
101+
$artifactPath = Join-Path "Python" $Version | Join-Path -ChildPath $Architecture
102+
103+
$relocatedPython = Join-Path $HOME "relocated_python"
104+
$relocatedPythonTool = Join-Path -Path $relocatedPython -ChildPath $artifactPath
105+
$relocatedFullPath = Join-Path $relocatedPythonTool "bin" | Join-Path -ChildPath $pyfilename
106+
107+
# copy the current build to relocated_python
108+
$toolCacheArtifact = Join-Path $env:RUNNER_TOOL_CACHE $artifactPath
109+
moveAssets -source $toolCacheArtifact -destination $relocatedPythonTool
110+
try {
111+
# Verify that relocated Python works
112+
$relocatedFullPath | Should -Exist
113+
"$relocatedFullPath --version" | Should -ReturnZeroExitCode
114+
"sudo $relocatedFullPath --version" | Should -ReturnZeroExitCode
115+
}
116+
finally {
117+
# Revert the changes for other tests
118+
moveAssets -source $relocatedPythonTool -destination $toolCacheArtifact
119+
}
120+
}
91121
}
92122

93123
# Pyinstaller 3.5 does not support Python 3.8.0. Check issue https://github.com/pyinstaller/pyinstaller/issues/4311

0 commit comments

Comments
 (0)