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 /utils.py | |
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>
Diffstat (limited to 'utils.py')
-rw-r--r-- | utils.py | 94 |
1 files changed, 64 insertions, 30 deletions
@@ -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)) + + |