3 # GObject-Introspection - a framework for introspecting GObject libraries
4 # Copyright (C) 2009 Red Hat, Inc.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 from .utils import get_libtool_command, extract_libtool_shlib
29 # For .la files, the situation is easy.
30 def _resolve_libtool(options, binary, libraries):
32 for library in libraries:
33 shlib = extract_libtool_shlib(library)
39 # Assume ldd output is something vaguely like
41 # libpangoft2-1.0.so.0 => /usr/lib/libpangoft2-1.0.so.0 (0x006c1000)
43 # We say that if something in the output looks like libpangoft2<blah>
44 # then the *first* such in the output is the soname. We require <blah>
45 # to start with [^A-Za-z0-9_-] to avoid problems with libpango vs libpangoft2
47 # The negative lookbehind at the start is to avoid problems if someone
48 # is crazy enough to name a library liblib<foo> when lib<foo> exists.
50 def _ldd_library_pattern(library_name):
51 return re.compile("(?<![A-Za-z0-9_-])(lib*%s[^A-Za-z0-9_-][^\s\(\)]*)"
52 % re.escape(library_name))
54 # This is a what we do for non-la files. We assume that we are on an
55 # ELF-like system where ldd exists and the soname extracted with ldd is
56 # a filename that can be opened with dlopen().
58 # On OS X this will need a straightforward alternate implementation
61 # Windows is more difficult, since there isn't always a straightforward
62 # translation between library name (.lib) and the name of the .dll, so
63 # extracting the dll names from the compiled app may not be sufficient.
64 # We might need to hunt down the .lib in the compile-time path and
65 # use that to figure out the name of the DLL.
67 def _resolve_non_libtool(options, binary, libraries):
71 if os.name == 'OpenBSD':
72 # Hack for OpenBSD when using the ports' libtool which uses slightly
73 # different directories to store the libraries in. So rewite binary.args[0]
74 # by inserting '.libs/'.
75 old_argdir = binary.args[0]
76 new_libsdir = os.path.join(os.path.dirname(binary.args[0]), '.libs/')
77 new_lib = new_libsdir + os.path.basename(binary.args[0])
78 if os.path.exists(new_lib):
79 binary.args[0] = new_lib
80 os.putenv('LD_LIBRARY_PATH', new_libsdir)
82 binary.args[0] = old_argdir
87 for library in libraries:
88 shlibs.append(library + '.dll')
91 libtool = get_libtool_command(options)
94 args.append('--mode=execute')
95 platform_system = platform.system()
96 if platform_system == 'Darwin':
97 args.extend(['otool', '-L', binary.args[0]])
99 args.extend(['ldd', binary.args[0]])
100 proc = subprocess.Popen(args, stdout=subprocess.PIPE)
102 for library in libraries:
103 patterns[library] = _ldd_library_pattern(library)
106 for line in proc.stdout:
107 for library, pattern in patterns.iteritems():
108 m = pattern.search(line)
110 del patterns[library]
111 shlibs.append(m.group(1))
114 if len(patterns) > 0:
116 "ERROR: can't resolve libraries to shared libraries: " +
117 ", ".join(patterns.keys()))
121 # We want to resolve a set of library names (the <foo> of -l<foo>)
122 # against a library to find the shared library name. The shared
123 # library name is suppose to be what you pass to dlopen() (or
124 # equivalent). And we want to do this using the libraries that 'binary'
125 # is linking against.
127 def resolve_shlibs(options, binary, libraries):
128 libtool = filter(lambda x: x.endswith(".la"), libraries)
129 non_libtool = filter(lambda x: not x.endswith(".la"), libraries)
131 return (_resolve_libtool(options, binary, libtool) +
132 _resolve_non_libtool(options, binary, non_libtool))