Skip to content

Commit 5569ff0

Browse files
committed
Try to fix SONAME...
1 parent 74b3b58 commit 5569ff0

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed

.cargo/cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[target.x86_64-unknown-linux-gnu]
2+
rustflags = ["-C", "link-args=-Wl,-export-dynamic"]
3+
4+
[target.aarch64-unknown-linux-gnu]
5+
rustflags = ["-C", "link-args=-Wl,-export-dynamic"]

.github/workflows/CI.yml

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ jobs:
142142
git config --global url."ssh://git@github.com-http-handler/platformatic/http-handler.git".insteadOf "ssh://git@github.com/platformatic/http-handler.git"
143143
git config --global url."ssh://git@github.com-http-rewriter/platformatic/http-rewriter.git".insteadOf "ssh://git@github.com/platformatic/http-rewriter.git"
144144
145+
# Set Python-related environment variables for the build
146+
export PYO3_PYTHON=$(which python3)
147+
export PYTHON_SYS_EXECUTABLE=$(which python3)
148+
145149
${{ matrix.settings.build }}
146150
- name: Build
147151
run: ${{ matrix.settings.build }}
@@ -326,15 +330,94 @@ jobs:
326330
uses: addnab/docker-run-action@v3
327331
with:
328332
image: ${{ steps.docker.outputs.IMAGE }}
329-
options: -v ${{ steps.docker.outputs.PNPM_STORE_PATH }}:${{ steps.docker.outputs.PNPM_STORE_PATH }} -v ${{ github.workspace }}:${{ github.workspace }} -w ${{ github.workspace }} --platform ${{ steps.docker.outputs.PLATFORM }}
333+
options: -v ${{ steps.docker.outputs.PNPM_STORE_PATH }}:${{ steps.docker.outputs.PNPM_STORE_PATH }} -v ${{ github.workspace }}:${{ github.workspace }} -w ${{ github.workspace }} --platform ${{ steps.docker.outputs.PLATFORM }} -e CI=true -e GITHUB_ACTIONS=true
330334
run: |
331335
# Install Python 3.9+ (any version will work with abi3-py39)
332336
apt-get update -y
333-
apt-get install -y python3 python3-dev
337+
apt-get install -y python3 python3-dev patchelf
338+
339+
echo "=== Starting test setup ==="
340+
echo "Current directory: $(pwd)"
341+
echo "Python version: $(python3 --version)"
342+
echo "Patchelf version: $(patchelf --version)"
343+
echo "CI environment: CI=$CI, GITHUB_ACTIONS=$GITHUB_ACTIONS"
344+
345+
# Check what .node files exist
346+
echo "=== Available .node files ==="
347+
ls -la *.node || echo "No .node files found"
348+
349+
# Check what Python version the binary was built with and patch SONAME if needed
350+
echo "=== Checking .node file Python dependencies ==="
351+
for file in *.node; do
352+
if [ -f "$file" ]; then
353+
case "$file" in
354+
*linux*)
355+
echo "Checking $file..."
356+
echo "Python dependencies before patching:"
357+
ldd "$file" 2>/dev/null | grep python || echo "No Python dependencies found"
358+
359+
# Check if we need to patch SONAME
360+
current_python_lib=$(ldd "$file" 2>/dev/null | grep "libpython" | head -1 | awk '{print $1}')
361+
if [ -n "$current_python_lib" ]; then
362+
echo "Current Python library: $current_python_lib"
363+
364+
# Find the actual Python library on the system
365+
system_python_lib=$(find /usr/lib* -name "libpython3*.so.*" -type f 2>/dev/null | head -1)
366+
if [ -n "$system_python_lib" ]; then
367+
system_python_soname=$(basename "$system_python_lib")
368+
echo "System Python library: $system_python_soname"
369+
370+
# Only patch if they're different
371+
if [ "$current_python_lib" != "$system_python_soname" ]; then
372+
echo "Patching SONAME from $current_python_lib to $system_python_soname"
373+
patchelf --replace-needed "$current_python_lib" "$system_python_soname" "$file"
374+
echo "SONAME patching completed"
375+
else
376+
echo "SONAME already matches system Python"
377+
fi
378+
else
379+
echo "Warning: Could not find system Python library"
380+
fi
381+
else
382+
echo "No Python library dependency found"
383+
fi
384+
385+
echo "Python dependencies after patching:"
386+
ldd "$file" 2>/dev/null | grep python || echo "No Python dependencies found"
387+
echo "---"
388+
;;
389+
*)
390+
echo "Skipping non-Linux file: $file"
391+
;;
392+
esac
393+
fi
394+
done
334395
335396
# Install pnpm and run tests
397+
echo "=== Installing pnpm ==="
336398
corepack disable
337399
npm i -gf pnpm
400+
401+
echo "=== Running pnpm install ==="
402+
# Should be non-interactive in CI environment
403+
pnpm install --prefer-offline
404+
405+
echo "=== Setting up Python library path ==="
406+
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH
407+
echo "LD_LIBRARY_PATH: $LD_LIBRARY_PATH"
408+
409+
# Find and preload the Python library
410+
echo "=== Finding Python library to preload ==="
411+
PYTHON_LIB=$(find /usr/lib* -name "libpython3*.so.*" -type f 2>/dev/null | head -1)
412+
if [ -n "$PYTHON_LIB" ]; then
413+
echo "Found Python library: $PYTHON_LIB"
414+
export LD_PRELOAD=$PYTHON_LIB
415+
echo "LD_PRELOAD: $LD_PRELOAD"
416+
else
417+
echo "Warning: Could not find Python library to preload"
418+
fi
419+
420+
echo "=== Running tests ==="
338421
pnpm test
339422
340423
publish:

build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@ extern crate napi_build;
44
fn main() {
55
#[cfg(feature = "napi-support")]
66
napi_build::setup();
7+
8+
// For Linux targets, ensure we link Python properly
9+
#[cfg(target_os = "linux")]
10+
{
11+
println!("cargo:rustc-link-arg=-Wl,-export-dynamic");
12+
println!("cargo:rustc-link-arg=-Wl,--no-as-needed");
13+
}
714
}

src/asgi/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ impl Asgi {
8383
let module_name = CString::new(target.file.clone()).map_err(HandlerError::StringCovertError)?;
8484

8585
Python::with_gil(|py| -> PyResult<PyObject> {
86+
// Ensure sys.path includes the docroot
87+
let sys = py.import("sys")?;
88+
let path = sys.getattr("path")?;
89+
path.call_method1("insert", (0, docroot.to_string_lossy()))?;
90+
91+
// Import asyncio to ensure it's loaded before we need it
92+
py.import("asyncio")?;
93+
8694
let module = PyModule::from_code(py, &code, &file_name, &module_name)?;
8795
Ok(module.getattr(&target.function)?.unbind())
8896
})

0 commit comments

Comments
 (0)