[gobject-introspection/ignatenko/g-ir-readtypelib] g-ir-readtypelib: new tool



commit fd9ffe4c4b62cdd16a12cedad605e63227fc8031
Author: Igor Gnatenko <ignatenko redhat com>
Date:   Thu Apr 28 23:53:43 2016 +0200

    g-ir-readtypelib: new tool
    
    Various distributions (mainly RPM based so far) make use of automatic
    dependencies extracted from typelib files (they can require other typelibs
    and also shared libraries)
    
    g-ir-readtypelib inspects a typelib and prints the dependencies.
    
    Reference: https://bugzilla.gnome.org/show_bug.cgi?id=665672
    Signed-off-by: Dominique Leuenberger <dimstar opensuse org>
    Signed-off-by: Igor Gnatenko <ignatenko redhat com>

 Makefile-tools.am        |   10 ++++-
 tools/g-ir-readtypelib.c |  102 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 110 insertions(+), 2 deletions(-)
---
diff --git a/Makefile-tools.am b/Makefile-tools.am
index dbd264c..39f0717 100644
--- a/Makefile-tools.am
+++ b/Makefile-tools.am
@@ -1,4 +1,4 @@
-bin_PROGRAMS += g-ir-compiler g-ir-generate
+bin_PROGRAMS += g-ir-compiler g-ir-generate g-ir-readtypelib
 bin_SCRIPTS += g-ir-scanner g-ir-annotation-tool
 
 if BUILD_DOCTOOL
@@ -40,8 +40,14 @@ g_ir_generate_LDADD = \
        libgirepository-1.0.la          \
        $(GIREPO_LIBS)
 
+g_ir_readtypelib_SOURCES = tools/g-ir-readtypelib.c
+g_ir_readtypelib_CFLAGS = $(GIO_CFLAGS) -I$(top_srcdir)/girepository
+g_ir_readtypelib_LDADD = \
+        libgirepository-1.0.la \
+        $(GIREPO_LIBS)
+
 GCOVSOURCES =                                  \
        $(g_ir_compiler_SOURCES)                \
        $(g_ir_generate_SOURCES)
 
-CLEANFILES += g-ir-scanner g-ir-annotation-tool g-ir-doc-tool
+CLEANFILES += g-ir-scanner g-ir-annotation-tool g-ir-doc-tool g-ir-readtypelib
diff --git a/tools/g-ir-readtypelib.c b/tools/g-ir-readtypelib.c
new file mode 100644
index 0000000..52d8f66
--- /dev/null
+++ b/tools/g-ir-readtypelib.c
@@ -0,0 +1,102 @@
+/* GObject introspection: typelib inspector
+ *
+ * Copyright (C) 2011-2016 Dominique Leuenberger <dimstar opensuse org>
+ * Copyright © 2016 Igor Gnatenko <ignatenko redhat com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <glib.h>
+#include <girepository.h>
+#include <stdlib.h>
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(GITypelib, g_typelib_free)
+
+static void
+print_shlibs (const gchar *namespace)
+{
+    /* Finding the shared library we depend on (if any) */
+    const gchar *shlibs = g_irepository_get_shared_library (NULL, namespace);
+    if (shlibs && shlibs[0] != '\0') {
+        /* shlibs is a comma-separated list of libraries */
+        g_auto(GStrv) libs = g_strsplit (shlibs, ",", -1);
+        for (guint i = 0; libs[i]; i++)
+            g_print ("shlib: %s\n", libs[i]);
+    }
+}
+
+static void
+print_typelibs (const gchar *namespace)
+{
+    /* Finding all the typelib-based Requires */
+    g_auto(GStrv) deps = g_irepository_get_dependencies (NULL, namespace);
+    if (deps) {
+        for (guint i = 0; deps[i]; i++)
+            g_print ("typelib: %s\n", deps[i]);
+    }
+}
+
+gint
+main (gint   argc,
+      gchar *argv[])
+{
+    g_autoptr(GError) error = NULL;
+    g_autoptr(GITypelib) typelib = NULL;
+
+    g_autofree gchar *version = NULL;
+    gboolean opt_shlibs = FALSE;
+    gboolean opt_typelibs = FALSE;
+    g_auto(GStrv) namespaces = NULL;
+    const GOptionEntry options[] = {
+        { "version", 0, 0, G_OPTION_ARG_STRING, &version, "Version", "VERSION" },
+        { "print-shlibs", 0, 0, G_OPTION_ARG_NONE, &opt_shlibs, "List the shared libraries the typelib 
requires" },
+        { "print-typelibs", 0, 0, G_OPTION_ARG_NONE, &opt_typelibs, "List other typelibs the inspected 
typelib requires" },
+        { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &namespaces, "The typelib to inspect", 
"NAMESPACE" },
+        { NULL },
+    };
+
+    g_autoptr(GOptionContext) context = g_option_context_new ("- Read a GI namespace");
+    g_option_context_add_main_entries (context, options, NULL);
+    if (!g_option_context_parse (context, &argc, &argv, &error)) {
+        g_printerr ("Failed to parse command line options: %s\n", error->message);
+        return EXIT_FAILURE;
+    }
+
+    if (!namespaces) {
+        g_printerr ("Please specify at least one namespace\n");
+        return EXIT_FAILURE;
+    } else if (g_strv_length (namespaces) > 1) {
+        g_printerr ("Please specify only one namespace\n");
+        return EXIT_FAILURE;
+    }
+    const gchar *namespace = namespaces[0];
+
+    if (!opt_shlibs && !opt_typelibs) {
+        g_printerr ("Please specify --print-shlibs, --print-typelibs or both.\n");
+        return EXIT_FAILURE;
+    }
+
+    typelib = g_irepository_require (NULL, namespace, version, 0, &error);
+    if (!typelib) {
+        g_printerr ("Failed to load typelib: %s\n", error->message);
+        return EXIT_FAILURE;
+    }
+
+    if (opt_shlibs)   print_shlibs   (namespace);
+    if (opt_typelibs) print_typelibs (namespace);
+
+    return EXIT_SUCCESS;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]