projects/guix·blob python-team · refs

173 lines · 7.0 KiB

1Names of libraries included in typelib files are opened by dlopen.
2Here we add the full path.
3
4This patch was provided by Luca Bruno <[email protected]>,
5for 'gobject-introspection' 1.40.0 in Nix.
6
7It has since been updated to work with newer versions of
8gobject-introspection.
9
10diff --git a/giscanner/scannermain.py b/giscanner/scannermain.py
11index 1d39ab84..e12ed24e 100644
12--- a/giscanner/scannermain.py
13+++ b/giscanner/scannermain.py
14@@ -95,6 +95,39 @@ def get_windows_option_group(parser):
15 return group
16
17
18+def _get_default_fallback_libpath():
19+ # Newer multiple-output-optimized stdenv has an environment variable
20+ # $outputLib which in turn specifies another variable which then is used as
21+ # the destination for the library contents (${!outputLib}/lib).
22+ store_path = os.environ.get(os.environ.get("outputLib")) if "outputLib" in os.environ else None
23+ if store_path is None:
24+ outputs = os.environ.get("outputs", "out").split()
25+ if "lib" in outputs:
26+ # For multiple output derivations let's try whether there is a $lib
27+ # environment variable and use that as the base store path.
28+ store_path = os.environ.get("lib")
29+ elif "out" in outputs:
30+ # Otherwise we have a single output derivation, so the libraries
31+ # most certainly will end up in "$out/lib".
32+ store_path = os.environ.get("out")
33+
34+ if store_path is not None:
35+ # Even if we have a $lib as output, there still should be a $lib/lib
36+ # directory.
37+ return os.path.join(store_path, 'lib')
38+ else:
39+ # If we haven't found a possible scenario, let's return an empty string
40+ # so that the shared library won't be prepended with a path.
41+ #
42+ # Note that this doesn't mean that all hope is lost, because after all
43+ # we can still use --fallback-library-path to set one.
44+ #
45+ # Also, we're not returning None, because that would make it very
46+ # difficult to disable adding fallback paths altogether using something
47+ # like: --fallback-library-path=""
48+ return ""
49+
50+
51 def _get_option_parser():
52 parser = optparse.OptionParser('%prog [options] sources',
53 version='%prog ' + giscanner.__version__)
54@@ -225,6 +258,10 @@ match the namespace prefix.""")
55 help=("name of the documentation format used in the project, "
56 "should be one of gi-docgen, gtk-doc-docbook, "
57 "gtk-doc-markdown or hotdoc"))
58+ parser.add_option("", "--fallback-library-path",
59+ action="store", dest="fallback_libpath",
60+ default=_get_default_fallback_libpath(),
61+ help="Path to prepend to unknown shared libraries")
62
63 group = get_preprocessor_option_group(parser)
64 parser.add_option_group(group)
65diff --git a/giscanner/shlibs.py b/giscanner/shlibs.py
66index 9f8ab5df..8aa37c99 100644
67--- a/giscanner/shlibs.py
68+++ b/giscanner/shlibs.py
69@@ -57,6 +57,14 @@ def _ldd_library_pattern(library_name):
70 $""" % re.escape(library_name), re.VERBOSE)
71
72
73+def _ldd_library_guix_pattern(library_name):
74+ store_dir = re.escape(
75+ os.environ.get("NIX_STORE", default="/gnu/store")
76+ )
77+ pattern = r'(%s(?:/[^/]*)+lib%s[^A-Za-z0-9_-][^\s\(\)]*)'
78+ return re.compile(pattern % (store_dir, re.escape(library_name)))
79+
80+
81 # This is a what we do for non-la files. We assume that we are on an
82 # ELF-like system where ldd exists and the soname extracted with ldd is
83 # a filename that can be opened with dlopen().
84@@ -108,7 +116,8 @@ def _resolve_non_libtool(options, binary, libraries):
85 output = output.decode("utf-8", "replace")
86
87 shlibs = resolve_from_ldd_output(libraries, output)
88- return list(map(sanitize_shlib_path, shlibs))
89+ fallback_libpath = options.fallback_libpath or "";
90+ return list(map(lambda p: os.path.join(fallback_libpath, p), map(sanitize_shlib_path, shlibs)))
91
92
93 def sanitize_shlib_path(lib):
94@@ -117,19 +126,18 @@ def sanitize_shlib_path(lib):
95 # In case we get relative paths on macOS (like @rpath) then we fall
96 # back to the basename as well:
97 # https://gitlab.gnome.org/GNOME/gobject-introspection/issues/222
98- if sys.platform == "darwin":
99- if not os.path.isabs(lib):
100- return os.path.basename(lib)
101- return lib
102- else:
103+
104+ # Always use absolute paths if available
105+ if not os.path.isabs(lib):
106 return os.path.basename(lib)
107+ return lib
108
109
110 def resolve_from_ldd_output(libraries, output):
111 patterns = {}
112 for library in libraries:
113 if not os.path.isfile(library):
114- patterns[library] = _ldd_library_pattern(library)
115+ patterns[library] = (_ldd_library_pattern(library), _ldd_library_guix_pattern(library))
116 if len(patterns) == 0:
117 return []
118
119@@ -141,8 +149,12 @@ def resolve_from_ldd_output(libraries, output):
120 if line.endswith(':'):
121 continue
122 for word in line.split():
123- for library, pattern in patterns.items():
124- m = pattern.match(word)
125+ for library, (pattern, guix_pattern) in patterns.items():
126+ store_dir = os.environ.get("NIX_STORE", default="/gnu/store")
127+ if line.find(store_dir) != -1:
128+ m = guix_pattern.match(word)
129+ else:
130+ m = pattern.match(word)
131 if m:
132 del patterns[library]
133 shlibs.append(m.group())
134diff --git a/giscanner/utils.py b/giscanner/utils.py
135index 31c7ea48..630002a8 100644
136--- a/giscanner/utils.py
137+++ b/giscanner/utils.py
138@@ -114,16 +114,11 @@ def extract_libtool_shlib(la_file):
139 if dlname is None:
140 return None
141
142- # Darwin uses absolute paths where possible; since the libtool files never
143- # contain absolute paths, use the libdir field
144- if platform.system() == 'Darwin':
145- dlbasename = os.path.basename(dlname)
146- libdir = _extract_libdir_field(la_file)
147- if libdir is None:
148- return dlbasename
149- return libdir + '/' + dlbasename
150- # Older libtools had a path rather than the raw dlname
151- return os.path.basename(dlname)
152+ dlbasename = os.path.basename(dlname)
153+ libdir = _extract_libdir_field(la_file)
154+ if libdir is None:
155+ return dlbasename
156+ return libdir + '/' + dlbasename
157
158
159 # Returns arguments for invoking libtool, if applicable, otherwise None
160diff --git a/tests/scanner/test_shlibs.py b/tests/scanner/test_shlibs.py
161index a8337c60..7f123103 100644
162--- a/tests/scanner/test_shlibs.py
163+++ b/tests/scanner/test_shlibs.py
164@@ -40,7 +40,8 @@ class TestLddParser(unittest.TestCase):
165
166 self.assertEqual(
167 sanitize_shlib_path('/foo/bar'),
168- '/foo/bar' if sys.platform == 'darwin' else 'bar')
169+ # Always use an absolute filename for Guix
170+ '/foo/bar')
171
172 def test_unresolved_library(self):
173 output = ''