Add an --add-init-section argument
[gnome.gobject-introspection] / giscanner / dumper.py
index 622b252..430f81e 100644 (file)
@@ -24,12 +24,13 @@ import subprocess
 import tempfile
 
 from .glibtransformer import IntrospectionBinary
+from .utils import get_libtool_command
 
 # bugzilla.gnome.org/558436
 # Compile a binary program which is then linked to a library
 # we want to introspect, in order to call its get_type functions.
 
-_PROGRAM_TEMPLATE = '''/* This file is generated, do not edit */
+_PROGRAM_TEMPLATE = """/* This file is generated, do not edit */
 #include <glib.h>
 #include <girepository.h>
 #include <string.h>
@@ -45,22 +46,24 @@ main(int argc, char **argv)
   GOptionContext *context;
   GError *error = NULL;
 
+  if (!g_thread_supported ()) g_thread_init (NULL);
   g_type_init ();
-  g_thread_init (NULL);
+
+  %(init_sections)s
 
   context = g_option_context_new ("");
   g_option_context_add_main_entries (context, entries, "girepository-1.0");
   g_option_context_add_group (context, g_irepository_get_option_group ());
   if (!g_option_context_parse (context, &argc, &argv, &error))
     {
-      g_printerr ("introspect failed (%d,%d): %s\\n",
+      g_printerr ("introspect failed (%%d,%%d): %%s\\n",
                   error->domain, error->code,
                   error->message);
       return 1;
     }
   return 0;
 }
-'''
+"""
 
 
 class CompilerError(Exception):
@@ -81,7 +84,7 @@ class DumpCompiler(object):
         self._tmpdir = tempfile.mkdtemp('', 'tmp-introspect', dir=os.getcwd())
 
         self._compiler_cmd = os.environ.get('CC', 'gcc')
-        self._linker_cmd = os.environ.get('LD', self._compiler_cmd)
+        self._linker_cmd = os.environ.get('CC', self._compiler_cmd)
         self._pkgconfig_cmd = os.environ.get('PKG_CONFIG', 'pkg-config')
 
         self._uninst_srcdir = os.environ.get(
@@ -93,11 +96,12 @@ class DumpCompiler(object):
     # Public API
 
     def run(self):
-        print '  GEN   ' + (self._options.output and
-                            self._options.output or '<stdout>')
+       tpl_args = {}
+       tpl_args['init_sections'] = "\n".join(self._options.init_sections)
+
         c_path = self._generate_tempfile('.c')
         f = open(c_path, 'w')
-        f.write(_PROGRAM_TEMPLATE)
+        f.write(_PROGRAM_TEMPLATE % tpl_args)
 
         # We need to reference our get_type functions to make sure they are
         # pulled in at the linking stage if the library is a static library
@@ -146,37 +150,22 @@ class DumpCompiler(object):
             stdout=subprocess.PIPE)
         return proc.communicate()[0].split()
 
-    def _use_libtool_infection(self):
-        libtool_infection = not self._options.nolibtool
-        if not libtool_infection:
-            return None
-
-        libtool_path = self._options.libtool_path
-        if libtool_path:
-            # Automake by default sets:
-            # LIBTOOL = $(SHELL) $(top_builddir)/libtool
-            # To be strictly correct we would have to parse shell.  For now
-            # we simply split().
-            return libtool_path.split(' ')
-
-        try:
-            subprocess.check_call(['libtool', '--version'])
-        except subprocess.CalledProcessError, e:
-            # If libtool's not installed, assume we don't need it
-            return None
-
-        return ['libtool']
-
     def _compile(self, output, *sources):
         # Not strictly speaking correct, but easier than parsing shell
         args = self._compiler_cmd.split()
-        if self._compiler_cmd == 'gcc':
+       # Do not add -Wall when using init code as we do not include any
+       # header of the library being introspected
+        if self._compiler_cmd == 'gcc' and not self._options.init_sections:
             args.append('-Wall')
         pkgconfig_flags = self._run_pkgconfig('--cflags')
         if self._uninst_srcdir:
             args.append('-I' + os.path.join(self._uninst_srcdir,
                                                'girepository'))
         args.extend(pkgconfig_flags)
+        cflags = os.environ.get('CFLAGS')
+        if (cflags):
+            for iflag in cflags.split():
+                args.append(iflag)
         for include in self._options.cpp_includes:
             args.append('-I' + include)
         args.extend(['-c', '-o', output])
@@ -189,7 +178,7 @@ class DumpCompiler(object):
 
     def _link(self, output, *sources):
         args = []
-        libtool = self._use_libtool_infection()
+        libtool = get_libtool_command(self._options)
         if libtool:
             args.extend(libtool)
             args.append('--mode=link')
@@ -198,6 +187,11 @@ class DumpCompiler(object):
 
         args.extend([self._linker_cmd, '-o', output])
 
+        cflags = os.environ.get('CFLAGS')
+        if (cflags):
+            for iflag in cflags.split():
+                args.append(iflag)
+
         # Make sure to list the library to be introspected first since it's
         # likely to be uninstalled yet and we want the uninstalled RPATHs have
         # priority (or we might run with installed library that is older)
@@ -213,7 +207,10 @@ class DumpCompiler(object):
             if (uninst_builddir and
                 self._options.libraries[0] == 'girepository-1.0'):
                 continue
-            args.append('-l' + library)
+            if library.endswith(".la"): # explicitly specified libtool library
+                args.append(library)
+            else:
+                args.append('-l' + library)
 
         # hack for building gobject-introspection itself
         if uninst_builddir: