Skip to content

Commit e7800bc

Browse files
author
renaud gaudin
committed
Rewrite runtine path lookup to libzim in wrapper for macOS
* Using fix for the `build_ext --rpath` on macOS that has an incorrect behavior See https://bugs.python.org/issue36353 * Using a new `$RPATH` var in release workflow to set `$ORIGIN` on Linux or `@loader_path/` on macOS * Manually set (using install_name_tool) the wrapper's rpath for libzim as the linker doesn't set it properly anyway. Users of the PyPi releases (wheels created in the release workflow) will thus always make use of the bundled libzim.
1 parent b725cb6 commit e7800bc

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

.github/workflows/release.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jobs:
3838
echo ::set-env name=LIBZIM_RELEASE::libzim_macos-x86_64-$LIBZIM_VERSION
3939
echo ::set-env name=LIBZIM_LIBRARY_PATH::lib/libzim.${LIBZIM_VERSION:0:1}.dylib
4040
echo ::set-env name=PLAFTORM_NAME::macosx_10.9_x86_64
41+
echo ::set-env name=RPATH::@loader_path/
4142
4243
- name: set linux environ
4344
if: matrix.os == 'ubuntu-latest'
@@ -46,6 +47,7 @@ jobs:
4647
echo ::set-env name=LIBZIM_RELEASE::libzim_linux-x86_64-$LIBZIM_VERSION
4748
echo ::set-env name=LIBZIM_LIBRARY_PATH::lib/x86_64-linux-gnu/libzim.so.$LIBZIM_VERSION
4849
echo ::set-env name=PLAFTORM_NAME::manylinux1_x86_64
50+
echo ::set-env name=RPATH::\$ORIGIN
4951
5052
- name: Cache libzim dylib & headers
5153
uses: actions/cache@master
@@ -78,15 +80,17 @@ jobs:
7880
- name: Build cython, sdist, and bdist_wheels
7981
run: |
8082
pip install --upgrade "cython>=0.29.20,<3.0" setuptools pip wheel
81-
python3 setup.py build_ext --rpath='$ORIGIN'
83+
python3 setup.py build_ext --rpath $RPATH
8284
if [[ "${{ matrix.python-version }}" == "3.6" ]]
8385
then
8486
python3 setup.py sdist
8587
fi
8688
8789
- name: add macOS libzim binary to source for wheel
8890
if: matrix.os == 'macos-latest'
89-
run: cp -p lib/libzim.${LIBZIM_VERSION:0:1}.dylib libzim
91+
run: |
92+
install_name_tool -change libzim.6.dylib @loader_path/libzim.6.dylib $(find build -name "wrapper*.so")
93+
cp -p lib/libzim.${LIBZIM_VERSION:0:1}.dylib libzim
9094
9195
- name: add Linux libzim binary to source for wheel
9296
if: matrix.os == 'ubuntu-latest'

setup.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
from setuptools import setup, Extension
3636
from Cython.Build import cythonize
37+
from Cython.Distutils.build_ext import new_build_ext as build_ext
3738

3839
GITHUB_URL = "https://github.com/openzim/python-libzim"
3940
BASE_DIR = Path(__file__).parent
@@ -42,6 +43,21 @@
4243
LIBZIM_DYLIB = 'libzim.{ext}'.format(ext='dylib' if platform.system() == 'Darwin' else 'so')
4344

4445

46+
class fixed_build_ext(build_ext):
47+
"""Workaround for rpath bug in distutils for OSX."""
48+
49+
def finalize_options(self):
50+
super().finalize_options()
51+
# Special treatment of rpath in case of OSX, to work around python
52+
# distutils bug 36353. This constructs proper rpath arguments for clang.
53+
# See https://bugs.python.org/issue36353
54+
if platform.system() == 'Darwin':
55+
for path in self.rpath:
56+
for ext in self.extensions:
57+
ext.extra_link_args.append("-Wl,-rpath," + path)
58+
self.rpath[:] = []
59+
60+
4561
# Check for the CPP Libzim library headers in expected directory
4662
if not (BASE_DIR / LIBZIM_INCLUDE_DIR / 'zim/zim.h').exists():
4763
print(
@@ -96,6 +112,7 @@ def get_long_description():
96112

97113
# Content
98114
packages=["libzim"],
115+
cmdclass={'build_ext': fixed_build_ext},
99116
ext_modules=cythonize([wrapper_extension],
100117
compiler_directives={"language_level": "3"}
101118
),

0 commit comments

Comments
 (0)