Add an --add-init-section argument
authorDamien Lespiau <damien.lespiau@intel.com>
Wed, 23 Dec 2009 23:15:21 +0000 (00:15 +0100)
committerColin Walters <walters@verbum.org>
Tue, 5 Jan 2010 18:50:55 +0000 (13:50 -0500)
One might need to call some init functions before being able to call
get_type() to create the types of a library.

--add-init-section let the user insert some initialization code in the
introspection program.

https://bugzilla.gnome.org/show_bug.cgi?id=605778

giscanner/dumper.py
giscanner/scannermain.py

index b11768c..430f81e 100644 (file)
@@ -30,7 +30,7 @@ from .utils import get_libtool_command
 # 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>
@@ -49,19 +49,21 @@ main(int argc, char **argv)
   if (!g_thread_supported ()) g_thread_init (NULL);
   g_type_init ();
 
+  %(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):
@@ -94,9 +96,12 @@ class DumpCompiler(object):
     # Public API
 
     def run(self):
+       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
@@ -148,7 +153,9 @@ class DumpCompiler(object):
     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:
index dfe1743..cc70fde 100644 (file)
@@ -75,6 +75,9 @@ def _get_option_parser():
     parser.add_option("", "--strip-prefix",
                       action="store", dest="strip_prefix", default=None,
                       help="remove this prefix from objects and functions")
+    parser.add_option("", "--add-init-section",
+                      action="append", dest="init_sections", default=[],
+            help="add extra initialization code in the introspection program")
     parser.add_option("-o", "--output",
                       action="store", dest="output",
                       help="output to writeout, defaults to stdout")