Imported Upstream version 1.49.1
[platform/upstream/gobject-introspection.git] / tools / g-ir-inspect.c
1 /* GObject introspection: typelib inspector
2  *
3  * Copyright (C) 2011-2016 Dominique Leuenberger <dimstar@opensuse.org>
4  * Copyright © 2016 Igor Gnatenko <ignatenko@redhat.com>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include <glib.h>
23 #include <girepository.h>
24 #include <stdlib.h>
25
26 static void
27 print_shlibs (const gchar *namespace)
28 {
29   guint i = 0;
30
31   /* Finding the shared library we depend on (if any) */
32   const gchar *shlibs = g_irepository_get_shared_library (NULL, namespace);
33   if (shlibs && shlibs[0] != '\0')
34     {
35       /* shlibs is a comma-separated list of libraries */
36       GStrv libs = g_strsplit (shlibs, ",", -1);
37       for (i = 0; libs[i]; i++)
38         g_print ("shlib: %s\n", libs[i]);
39       g_strfreev (libs);
40     }
41 }
42
43 static void
44 print_typelibs (const gchar *namespace)
45 {
46   guint i = 0;
47
48   /* Finding all the typelib-based Requires */
49   GStrv deps = g_irepository_get_dependencies (NULL, namespace);
50   if (deps)
51     {
52       for (i = 0; deps[i]; i++)
53         g_print ("typelib: %s\n", deps[i]);
54       g_strfreev (deps);
55     }
56 }
57
58 gint
59 main (gint   argc,
60       gchar *argv[])
61 {
62   gint status = EXIT_SUCCESS;
63
64   GError *error = NULL;
65   GITypelib *typelib = NULL;
66
67   gchar *version = NULL;
68   gboolean opt_shlibs = FALSE;
69   gboolean opt_typelibs = FALSE;
70   GStrv namespaces = NULL;
71   const gchar *namespace = NULL;
72   const GOptionEntry options[] = {
73     { "version", 0, 0, G_OPTION_ARG_STRING, &version, "Version", "VERSION" },
74     { "print-shlibs", 0, 0, G_OPTION_ARG_NONE, &opt_shlibs, "List the shared libraries the typelib requires" },
75     { "print-typelibs", 0, 0, G_OPTION_ARG_NONE, &opt_typelibs, "List other typelibs the inspected typelib requires" },
76     { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &namespaces, "The typelib to inspect", "NAMESPACE" },
77     { NULL },
78   };
79
80   g_autoptr(GOptionContext) context = g_option_context_new ("- Inspect GI typelib");
81   g_option_context_add_main_entries (context, options, NULL);
82   if (!g_option_context_parse (context, &argc, &argv, &error))
83     {
84       status = EXIT_FAILURE;
85       g_printerr ("Failed to parse command line options: %s\n", error->message);
86       goto out;
87     }
88
89   if (!namespaces)
90     {
91       status = EXIT_FAILURE;
92       g_printerr ("Please specify at least one namespace\n");
93       goto out;
94     }
95
96   if (g_strv_length (namespaces) > 1)
97     {
98       status = EXIT_FAILURE;
99       g_printerr ("Please specify only one namespace\n");
100       goto out;
101     }
102   namespace = namespaces[0];
103
104   if (!opt_shlibs && !opt_typelibs)
105     {
106       status = EXIT_FAILURE;
107       g_printerr ("Please specify --print-shlibs, --print-typelibs or both.\n");
108       goto out;
109     }
110
111   typelib = g_irepository_require (NULL, namespace, version, 0, &error);
112   if (!typelib)
113     {
114       status = EXIT_FAILURE;
115       g_printerr ("Failed to load typelib: %s\n", error->message);
116       goto out;
117     }
118
119   if (opt_shlibs)
120     print_shlibs (namespace);
121   if (opt_typelibs)
122     print_typelibs (namespace);
123
124 out:
125   if (error)
126     g_error_free (error);
127   if (typelib)
128     g_typelib_free (typelib);
129   g_strfreev (namespaces);
130   g_free (version);
131
132   return status;
133 }