2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008 Colin Walters
4 # Copyright (C) 2008 Johan Dahlin
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2 of the License, or (at your option) any later version.
11 # This library 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 GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the
18 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 # Boston, MA 02111-1307, USA.
26 from .glibtransformer import IntrospectionBinary
28 # bugzilla.gnome.org/558436
29 # Compile a binary program which is then linked to a library
30 # we want to introspect, in order to call its get_type functions.
32 _PROGRAM_TEMPLATE = '''/* This file is generated, do not edit */
34 #include <girepository.h>
37 static GOptionEntry entries[] =
43 main(int argc, char **argv)
45 GOptionContext *context;
51 context = g_option_context_new ("");
52 g_option_context_add_main_entries (context, entries, "girepository-1.0");
53 g_option_context_add_group (context, g_irepository_get_option_group ());
54 if (!g_option_context_parse (context, &argc, &argv, &error))
56 g_printerr ("introspect failed (%d,%d): %s\\n",
57 error->domain, error->code,
66 class CompilerError(Exception):
70 class LinkerError(Exception):
74 class DumpCompiler(object):
76 def __init__(self, options, get_type_functions):
77 self._options = options
78 self._get_type_functions = get_type_functions
79 self._tmpdir = tempfile.mkdtemp('', 'tmp-introspect')
81 self._compiler_cmd = os.environ.get('CC', 'gcc')
82 self._linker_cmd = os.environ.get('LD', self._compiler_cmd)
83 self._pkgconfig_cmd = os.environ.get('PKG_CONFIG', 'pkg-config')
85 self._uninst_srcdir = os.environ.get(
86 'UNINSTALLED_INTROSPECTION_SRCDIR')
87 self._packages = ['gio-2.0 gthread-2.0']
88 if not self._uninst_srcdir:
89 self._packages.append('gobject-introspection-1.0')
94 print ' GEN ' + (self._options.output and
95 self._options.output or '<stdout>')
96 c_path = self._generate_tempfile('.c')
98 f.write(_PROGRAM_TEMPLATE)
100 # We need to reference our get_type functions to make sure they are
101 # pulled in at the linking stage if the library is a static library
102 # rather than a shared library.
103 if len(self._get_type_functions) > 0:
104 for func in self._get_type_functions:
105 f.write("extern GType " + func + "(void);\n")
106 f.write("GType (*GI_GET_TYPE_FUNCS_[])(void) = {\n")
108 for func in self._get_type_functions:
117 o_path = self._generate_tempfile('.o')
118 bin_path = self._generate_tempfile()
121 self._compile(o_path, c_path)
122 except CompilerError, e:
123 raise SystemExit('ERROR: ' + str(e))
126 self._link(bin_path, o_path)
127 except LinkerError, e:
128 raise SystemExit('ERROR: ' + str(e))
132 return IntrospectionBinary([bin_path], self._tmpdir)
136 def _generate_tempfile(self, suffix=''):
137 tmpl = '%s-%s%s' % (self._options.namespace_name,
138 self._options.namespace_version, suffix)
139 return os.path.join(self._tmpdir, tmpl)
141 def _run_pkgconfig(self, flag):
142 proc = subprocess.Popen(
143 [self._pkgconfig_cmd, flag] + self._packages,
144 stdout=subprocess.PIPE)
145 return proc.communicate()[0].split()
147 def _use_libtool_infection(self):
148 libtool_infection = not self._options.nolibtool
149 if not libtool_infection:
152 libtool_path = self._options.libtool_path
154 # Automake by default sets:
155 # LIBTOOL = $(SHELL) $(top_builddir)/libtool
156 # To be strictly correct we would have to parse shell. For now
158 return libtool_path.split(' ')
161 subprocess.check_call(['libtool', '--version'])
162 except subprocess.CalledProcessError, e:
163 # If libtool's not installed, assume we don't need it
168 def _compile(self, output, *sources):
169 # Not strictly speaking correct, but easier than parsing shell
170 args = self._compiler_cmd.split()
171 if self._compiler_cmd == 'gcc':
173 pkgconfig_flags = self._run_pkgconfig('--cflags')
174 if self._uninst_srcdir:
175 args.append('-I' + os.path.join(self._uninst_srcdir,
177 args.extend(pkgconfig_flags)
178 for include in self._options.cpp_includes:
179 args.append('-I' + include)
180 args.extend(['-c', '-o', output])
181 for source in sources:
182 if not os.path.exists(source):
184 "Could not find c source file: %s" % (source, ))
185 args.extend(list(sources))
186 subprocess.check_call(args)
188 def _link(self, output, *sources):
190 libtool = self._use_libtool_infection()
193 args.append('--mode=link')
194 args.append('--tag=CC')
195 args.append('--silent')
197 args.extend([self._linker_cmd, '-o', output])
199 # Make sure to list the library to be introspected first since it's
200 # likely to be uninstalled yet and we want the uninstalled RPATHs have
201 # priority (or we might run with installed library that is older)
203 # Search the current directory first
206 uninst_builddir = os.environ.get('UNINSTALLED_INTROSPECTION_BUILDDIR')
207 # hack for building GIRepository.gir, skip -lgirepository-1.0 since
208 # libgirepository-1.0.la is not in current directory and we refer to it
209 # explicitly below anyway
210 for library in self._options.libraries:
211 if (uninst_builddir and
212 self._options.libraries[0] == 'girepository-1.0'):
214 args.append('-l' + library)
216 # hack for building gobject-introspection itself
218 path = os.path.join(uninst_builddir, 'girepository',
219 'libgirepository-1.0.la')
222 args.extend(self._run_pkgconfig('--libs'))
223 for source in sources:
224 if not os.path.exists(source):
226 "Could not find object file: %s" % (source, ))
227 args.extend(list(sources))
229 subprocess.check_call(args)
232 def compile_introspection_binary(options, get_type_functions):
233 dc = DumpCompiler(options, get_type_functions)