Skip to content

Commit

Permalink
Portable/Relocatable Python Linking
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswanthikolla committed May 19, 2024
1 parent 521be20 commit e36e941
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions builders/ubuntu-python-builder.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class UbuntuPythonBuilder : NixPythonBuilder {

$pythonBinariesLocation = $this.GetFullPythonToolcacheLocation()

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

Write-Host "The passed configure options are: "
Write-Host $configureString
Write-Host "LDFLAGS: $env:LDFLAGS"

Execute-Command -Command $configureString
}
Expand Down
30 changes: 30 additions & 0 deletions tests/python-tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ BeforeAll {

return 0
}

function moveAssets([string] $source, [string] $destination) {
$parentDestDir = Split-Path -Path $destination -Parent
New-Item -Path $parentDestDir -ItemType Directory -Force
Move-Item -Path $source -Destination $parentDestDir -Force
}
}

Describe "Tests" {
Expand Down Expand Up @@ -88,6 +94,30 @@ Describe "Tests" {
It "Check if shared libraries are linked correctly" {
"bash ./sources/psutil-install-test.sh" | Should -ReturnZeroExitCode
}

It "Relocatable Python" {
$semversion = [semver] $Version
$pyfilename = "python$($semversion.Major).$($semversion.Minor)"
$artifactPath = Join-Path "Python" $Version | Join-Path -ChildPath $Architecture

$relocatedPython = Join-Path $HOME "relocated_python"
$relocatedPythonTool = Join-Path -Path $relocatedPython -ChildPath $artifactPath
$relocatedFullPath = Join-Path $relocatedPythonTool "bin" | Join-Path -ChildPath $pyfilename

# copy the current build to relocated_python
$toolCacheArtifact = Join-Path $env:RUNNER_TOOL_CACHE $artifactPath
moveAssets -source $toolCacheArtifact -destination $relocatedPythonTool
try {
# Verify that relocated Python works
$relocatedFullPath | Should -Exist
"$relocatedFullPath --version" | Should -ReturnZeroExitCode
"sudo $relocatedFullPath --version" | Should -ReturnZeroExitCode
}
finally {
# Revert the changes for other tests
moveAssets -source $relocatedPythonTool -destination $toolCacheArtifact
}
}
}

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

0 comments on commit e36e941

Please sign in to comment.