gimarshallingtests: Add string_ to boxed structure
[platform/upstream/gobject-introspection.git] / giscanner / shlibs.py
1 #!/usr/bin/env python
2 # -*- Mode: Python -*-
3 # GObject-Introspection - a framework for introspecting GObject libraries
4 # Copyright (C) 2009 Red Hat, Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20 #
21
22 import os
23 import platform
24 import re
25 import subprocess
26
27 from .utils import get_libtool_command, extract_libtool_shlib
28
29 # For .la files, the situation is easy.
30 def _resolve_libtool(options, binary, libraries):
31     shlibs = []
32     for library in libraries:
33         shlib = extract_libtool_shlib(library)
34         if shlib:
35             shlibs.append(shlib)
36
37     return shlibs
38
39 # Assume ldd output is something vaguely like
40 #
41 #  libpangoft2-1.0.so.0 => /usr/lib/libpangoft2-1.0.so.0 (0x006c1000)
42 #
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
46 #
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.
49 #
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))
53
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().
57 #
58 # On OS X this will need a straightforward alternate implementation
59 # in terms of otool.
60 #
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.
66 #
67 def _resolve_non_libtool(options, binary, libraries):
68     if not libraries:
69         return []
70
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)
81         else:
82             binary.args[0] = old_argdir
83
84     if os.name == 'nt':
85         shlibs = []
86
87         for library in libraries:
88             shlibs.append(library + '.dll')
89     else:
90         args = []
91         libtool = get_libtool_command(options)
92         if libtool:
93             args.extend(libtool)
94             args.append('--mode=execute')
95         platform_system = platform.system()
96         if platform_system == 'Darwin':
97             args.extend(['otool', '-L', binary.args[0]])
98         else:
99             args.extend(['ldd', binary.args[0]])
100         proc = subprocess.Popen(args, stdout=subprocess.PIPE)
101         patterns = {}
102         for library in libraries:
103             patterns[library] = _ldd_library_pattern(library)
104
105         shlibs = []
106         for line in proc.stdout:
107             for library, pattern in patterns.iteritems():
108                 m = pattern.search(line)
109                 if m:
110                     del patterns[library]
111                     shlibs.append(m.group(1))
112                     break
113
114         if len(patterns) > 0:
115             raise SystemExit(
116                 "ERROR: can't resolve libraries to shared libraries: " +
117                 ", ".join(patterns.keys()))
118
119     return shlibs
120
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.
126 #
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)
130
131     return (_resolve_libtool(options, binary, libtool) +
132             _resolve_non_libtool(options, binary, non_libtool))