Commit fd9125f1 authored by Christoph Reiter's avatar Christoph Reiter 🐍

shlibs: fall back to basename on macOS for relative paths. Fixes #222

On macOS we ideally want to write the final absolute path of the library
into the gir and typelib. Up until now we inferred the install path from
the .la file in case we used libtool and through otool in case we weren't
using libtool hoping that the install_name was matching the install path.

meson currently sets the install_name of libraries to "@rpath/foo.dylib"
and adds rpaths to the executables in the build dir. Only during install does
it change the install_name to the absolute target path in all places.

Since we get called during build time we only get the @rpath from otool,
which then makes things fail at runtime since the executables don't have
the matching rpath set.

To make this somewhat work just fall back to the basename for relative
paths, so we dlopen "foo.dylib" and depend on things being in /usr/local/lib
or DYLD_FALLBACK_LIBRARY_PATH including the lib path (see man dlopen)
parent e695a4fe
Pipeline #70569 passed with stages
in 5 minutes and 23 seconds
......@@ -19,6 +19,7 @@
import os
import sys
import platform
import re
import subprocess
......@@ -104,13 +105,25 @@ def _resolve_non_libtool(options, binary, libraries):
if isinstance(output, bytes):
output = output.decode("utf-8", "replace")
# Use absolute paths on OS X to conform to how libraries are usually
# referenced on OS X systems, and file names everywhere else.
basename = platform.system() != 'Darwin'
return resolve_from_ldd_output(libraries, output, basename=basename)
shlibs = resolve_from_ldd_output(libraries, output)
return list(map(sanitize_shlib_path, shlibs))
def resolve_from_ldd_output(libraries, output, basename=False):
def sanitize_shlib_path(lib):
# Use absolute paths on OS X to conform to how libraries are usually
# referenced on OS X systems, and file names everywhere else.
# In case we get relative paths on macOS (like @rpath) then we fall
# back to the basename as well:
if sys.platform == "darwin":
if not os.path.isabs(lib):
return os.path.basename(lib)
return lib
return os.path.basename(lib)
def resolve_from_ldd_output(libraries, output):
patterns = {}
for library in libraries:
if not os.path.isfile(library):
......@@ -138,8 +151,6 @@ def resolve_from_ldd_output(libraries, output, basename=False):
"ERROR: can't resolve libraries to shared libraries: " +
", ".join(patterns.keys()))
if basename:
shlibs = list(map(os.path.basename, shlibs))
return shlibs
import unittest
import sys
import os
from giscanner.shlibs import resolve_from_ldd_output
from giscanner.shlibs import resolve_from_ldd_output, sanitize_shlib_path
class TestLddParser(unittest.TestCase):
......@@ -18,6 +20,28 @@ class TestLddParser(unittest.TestCase):
['', '', ''],
resolve_from_ldd_output(libraries, output))
@unittest.skipUnless( == "posix", "posix only")
def test_resolve_from_ldd_output_macos_rpath(self):
output = '''\
@rpath/libbarapp-1.0.dylib (compatibility version 0.0.0, current version 0.0.0)
/foo/libgio-2.0.0.dylib (compatibility version 5801.0.0, current version 5801.3.0)
/foo/libgmodule-2.0.0.dylib (compatibility version 5801.0.0, current version 5801.3.0)'''
libraries = ['barapp-1.0']
shlibs = resolve_from_ldd_output(libraries, output)
self.assertEqual(shlibs, ['@rpath/libbarapp-1.0.dylib'])
self.assertEqual(sanitize_shlib_path(shlibs[0]), 'libbarapp-1.0.dylib')
@unittest.skipUnless( == "posix", "posix only")
def test_sanitize_shlib_path(self):
'/foo/bar' if sys.platform == 'darwin' else 'bar')
def test_unresolved_library(self):
output = ''
libraries = ['foo']
......@@ -87,10 +111,7 @@ class TestLddParser(unittest.TestCase):
resolve_from_ldd_output(['foo'], output, basename=False))
resolve_from_ldd_output(['foo'], output, basename=True))
resolve_from_ldd_output(['foo'], output))
if __name__ == '__main__':
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment