diff options
author | Alexandru Croitor <alexandru.croitor@qt.io> | 2017-10-23 16:43:49 +0200 |
---|---|---|
committer | Alexandru Croitor <alexandru.croitor@qt.io> | 2017-11-15 15:46:58 +0000 |
commit | 554a82039fb5165bf2e1922551df54dd9fbf3d2c (patch) | |
tree | 0a4bf50e99e83a4c611870ad09bfe25cba4084c9 | |
parent | 7d58c8b4f6c6692ca0c856f676c05f3a84d40c43 (diff) |
Fix rpath handling on macOS
This change ultimately allows running python scripts that use PySide2
without setting DYLD_LIBRARY_PATH / DYLD_FRAMEWORK_PATH. It is
achieved by embedding a @loader_path LC_RPATH into all PySide shared
libraries, so that they can load each other if they have dependencies.
Also an additional LC_RPATH is embedded to point to the Qt libraries
directory which was used for building PySide2.
A new option "--rpath='your_value'" is available to allow manually
specifying the rpath value to be embedded into the libraries.
Change-Id: Id783196e908877692312b1d40fef4ad0b09f3e68
Task-number: PYSIDE-558
Reviewed-by: Christian Tismer <tismer@stackless.com>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
-rw-r--r-- | setup.py | 15 | ||||
-rw-r--r-- | utils.py | 94 |
2 files changed, 77 insertions, 32 deletions
@@ -49,6 +49,10 @@ to build and install into your current Python installation. On Linux you can use option --standalone, to embed Qt libraries to PySide2 distribution +You can use option --rpath="your_value" to specify what rpath values should be embedded into the +PySide2 modules and shared libraries. This overrides the automatically generated values when the +option is not specified. + You can use option --only-package, if you want to create more binary packages (bdist_wheel, bdist_egg, ...) without rebuilding entire PySide2 every time: # First time we create bdist_wheel with full PySide2 build @@ -176,7 +180,7 @@ from utils import update_env_path from utils import init_msvc_env from utils import regenerate_qt_resources from utils import filter_match -from utils import osx_localize_libpaths +from utils import osx_fix_rpaths_for_library # guess a close folder name for extensions def get_extension_folder(ext): @@ -233,6 +237,7 @@ OPTION_REUSE_BUILD = has_option("reuse-build") OPTION_SKIP_CMAKE = has_option("skip-cmake") OPTION_SKIP_MAKE_INSTALL = has_option("skip-make-install") OPTION_SKIP_PACKAGING = has_option("skip-packaging") +OPTION_RPATH_VALUES = option_value("rpath") if OPTION_QT_VERSION is None: OPTION_QT_VERSION = "5" @@ -1226,7 +1231,13 @@ class pyside_build(_build): pyside_libs = [lib for lib in os.listdir(package_path) if filter_match( lib, ["*.so", "*.dylib"])] def rpath_cmd(srcpath): - osx_localize_libpaths(srcpath, pyside_libs, None) + final_rpath = '' + # Command line rpath option takes precedence over automatically added one. + if OPTION_RPATH_VALUES: + final_rpath = OPTION_RPATH_VALUES + else: + final_rpath = self.qtinfo.libs_dir + osx_fix_rpaths_for_library(srcpath, final_rpath) else: raise RuntimeError('Not configured for platform ' + @@ -549,7 +549,7 @@ def osx_get_install_names(libpath): OSX_RPATH_RE = re.compile(r"path (.+) \(offset \d+\)") def osx_get_rpaths(libpath): - """ Get rpaths from library `libpath` using ``otool`` + """ Get rpath load commands from library `libpath` using ``otool`` Parameters ---------- @@ -583,40 +583,74 @@ def osx_get_rpaths(libpath): return rpaths -def osx_localize_libpaths(libpath, local_libs, enc_path=None): - """ Set rpaths and install names to load local dynamic libs at run time +def osx_fix_rpaths_for_library(library_path, qt_lib_dir): + """ Adds required rpath load commands to given library. - Use ``install_name_tool`` to set relative install names in `libpath` (as - named in `local_libs` to be relative to `enc_path`. The default for - `enc_path` is the directory containing `libpath`. + This is a necessary post-installation step, to allow loading PySide modules without setting + DYLD_LIBRARY_PATH or DYLD_FRAMEWORK_PATH. + The CMake rpath commands which are added at build time are used only for testing (make check), + and they are stripped once the equivalent of make install is executed (except for shiboken, + which currently uses CMAKE_INSTALL_RPATH_USE_LINK_PATH, which might be necessary to remove in + the future). Parameters ---------- - libpath : str - path to library for which to set install names and rpaths - local_libs : sequence of str - library (install) names that should be considered relative paths - enc_path : str, optional - path that does or will contain the `libpath` library, and to which the - `local_libs` are relative. Defaults to current directory containing - `libpath`. + library_path : str + path to library for which to set rpaths. + qt_lib_dir : str + rpath to installed Qt lib directory. """ - if enc_path is None: - enc_path = os.path.abspath(os.path.dirname(libpath)) - install_names = osx_get_install_names(libpath) - need_rpath = False + + install_names = osx_get_install_names(library_path) + existing_rpath_commands = osx_get_rpaths(library_path) + + needs_loader_path = False for install_name in install_names: + # Absolute path, skip it. if install_name[0] == '/': - # absolute path, nothing to do continue - # we possibly need to add an rpath command. - # note that a @rpath may be there already, but no rpath command. - # this happens when Qt is not linked (with qt5 this is the default) - need_rpath = True - if install_name[0] != '@': - # we need to change a relative path to @rpath - back_tick('install_name_tool -change {ina} @rpath/{ina} {lipa}'.format( - ina=install_name, lipa=libpath )) - if need_rpath and enc_path not in osx_get_rpaths(libpath): - back_tick('install_name_tool -add_rpath {epa} {lipa}'.format( - epa=enc_path, lipa=libpath )) + + # If there are dynamic library install names that contain @rpath tokens, we will + # provide an rpath load command with the value of "@loader_path". This will allow loading + # dependent libraries from within the same directory as 'library_path'. + if install_name[0] == '@': + needs_loader_path = True + break + + if needs_loader_path and "@loader_path" not in existing_rpath_commands: + back_tick('install_name_tool -add_rpath {rpath} {library_path}'.format( + rpath="@loader_path", library_path=library_path)) + + # If the library depends on a Qt library, add an rpath load comment pointing to the Qt lib + # directory. + osx_add_qt_rpath(library_path, qt_lib_dir, existing_rpath_commands, install_names) + +def osx_add_qt_rpath(library_path, qt_lib_dir, + existing_rpath_commands = [], library_dependencies = []): + """ Adds an rpath load command to the Qt lib directory if necessary + + Checks if library pointed to by 'library_path' has Qt dependencies, and adds an rpath load + command that points to the Qt lib directory (qt_lib_dir). + """ + if not existing_rpath_commands: + existing_rpath_commands = osx_get_rpaths(library_path) + + # Return early if qt rpath is already present. + if qt_lib_dir in existing_rpath_commands: + return + + # Check if any library dependencies are Qt libraries (hacky). + if not library_dependencies: + library_dependencies = osx_get_install_names(library_path) + + needs_qt_rpath = False + for library in library_dependencies: + if 'Qt' in library: + needs_qt_rpath = True + break + + if needs_qt_rpath: + back_tick('install_name_tool -add_rpath {rpath} {library_path}'.format( + rpath=qt_lib_dir, library_path=library_path)) + + |