Merge branch '3155-import-generate-and-inspect' into 'main'

Import source for g-ir-generate and g-ir-inspect from gobject-introspection.git

Closes #55

See merge request GNOME/glib!3907
This commit is contained in:
Philip Withnall 2024-02-08 15:20:50 +00:00
commit 0fa7daad12
2 changed files with 270 additions and 0 deletions

View File

@ -0,0 +1,140 @@
/* GObject introspection: typelib inspector
*
* Copyright (C) 2011-2016 Dominique Leuenberger <dimstar@opensuse.org>
* Copyright © 2016 Igor Gnatenko <ignatenko@redhat.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* 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>
#include <locale.h>
static void
print_shlibs (const gchar *namespace)
{
guint i = 0;
/* 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 */
GStrv libs = g_strsplit (shlibs, ",", -1);
for (i = 0; libs[i]; i++)
g_print ("shlib: %s\n", libs[i]);
g_strfreev (libs);
}
}
static void
print_typelibs (const gchar *namespace)
{
guint i = 0;
/* Finding all the typelib-based Requires */
GStrv deps = g_irepository_get_dependencies (NULL, namespace);
if (deps)
{
for (i = 0; deps[i]; i++)
g_print ("typelib: %s\n", deps[i]);
g_strfreev (deps);
}
}
gint
main (gint argc,
gchar *argv[])
{
gint status = EXIT_SUCCESS;
GError *error = NULL;
GITypelib *typelib = NULL;
gchar *version = NULL;
gboolean opt_shlibs = FALSE;
gboolean opt_typelibs = FALSE;
GStrv namespaces = NULL;
const gchar *namespace = NULL;
const GOptionEntry options[] = {
{ "version", 0, 0, G_OPTION_ARG_STRING, &version, "Typelib version to inspect", "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 },
};
GOptionContext *context = NULL;
setlocale (LC_ALL, "");
context = g_option_context_new ("- Inspect GI typelib");
g_option_context_add_main_entries (context, options, NULL);
if (!g_option_context_parse (context, &argc, &argv, &error))
{
status = EXIT_FAILURE;
g_printerr ("Failed to parse command line options: %s\n", error->message);
goto out;
}
if (!namespaces)
{
status = EXIT_FAILURE;
g_printerr ("Please specify at least one namespace\n");
goto out;
}
if (g_strv_length (namespaces) > 1)
{
status = EXIT_FAILURE;
g_printerr ("Please specify only one namespace\n");
goto out;
}
namespace = namespaces[0];
if (!opt_shlibs && !opt_typelibs)
{
status = EXIT_FAILURE;
g_printerr ("Please specify --print-shlibs, --print-typelibs or both.\n");
goto out;
}
typelib = g_irepository_require (NULL, namespace, version, 0, &error);
if (!typelib)
{
status = EXIT_FAILURE;
g_printerr ("Failed to load typelib: %s\n", error->message);
goto out;
}
if (opt_shlibs)
print_shlibs (namespace);
if (opt_typelibs)
print_typelibs (namespace);
out:
g_option_context_free (context);
if (error)
g_error_free (error);
if (typelib)
g_typelib_free (typelib);
g_strfreev (namespaces);
g_free (version);
return status;
}

View File

@ -0,0 +1,130 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
* GObject introspection: IDL generator
*
* Copyright (C) 2005 Matthias Clasen
* Copyright (C) 2008,2009 Red Hat, Inc.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* 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 <locale.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <gmodule.h>
#include "girwriter.h"
#include "girepository.h"
#include "gitypelib-internal.h"
int
main (int argc, char *argv[])
{
gboolean shlib = FALSE;
gchar *output = NULL;
gchar **includedirs = NULL;
gboolean show_all = FALSE;
gchar **input = NULL;
GOptionContext *context;
GError *error = NULL;
gboolean needs_prefix;
gboolean show_version = FALSE;
gint i;
GOptionEntry options[] =
{
{ "shlib", 0, 0, G_OPTION_ARG_NONE, &shlib, "handle typelib embedded in shlib", NULL },
{ "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" },
{ "includedir", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &includedirs, "include directories in GIR search path", NULL },
{ "all", 0, 0, G_OPTION_ARG_NONE, &show_all, "show all available information", NULL, },
{ "version", 0, 0, G_OPTION_ARG_NONE, &show_version, "show program's version number and exit", NULL },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
{ NULL, }
};
g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
g_typelib_check_sanity ();
setlocale (LC_ALL, "");
context = g_option_context_new ("");
g_option_context_add_main_entries (context, options, NULL);
if (!g_option_context_parse (context, &argc, &argv, &error))
{
g_fprintf (stderr, "failed to parse: %s\n", error->message);
g_error_free (error);
return 1;
}
if (show_version)
{
g_printf ("g-ir-generate %u.%u.%u\n",
GI_MAJOR_VERSION, GI_MINOR_VERSION, GI_MICRO_VERSION);
return 0;
}
if (!input)
{
g_fprintf (stderr, "no input files\n");
return 1;
}
if (includedirs != NULL)
for (i = 0; includedirs[i]; i++)
g_irepository_prepend_search_path (includedirs[i]);
for (i = 0; input[i]; i++)
{
const char *namespace;
GMappedFile *mfile;
GITypelib *typelib;
error = NULL;
mfile = g_mapped_file_new (input[i], FALSE, &error);
if (!mfile)
g_error ("failed to read '%s': %s", input[i], error->message);
if (input[i + 1] && output)
needs_prefix = TRUE;
else
needs_prefix = FALSE;
typelib = g_typelib_new_from_mapped_file (mfile, &error);
if (!typelib)
g_error ("failed to create typelib '%s': %s", input[i], error->message);
namespace = g_irepository_load_typelib (g_irepository_get_default (), typelib, 0,
&error);
if (namespace == NULL)
g_error ("failed to load typelib: %s", error->message);
gir_writer_write (output, namespace, needs_prefix, show_all);
/* when writing to stdout, stop after the first module */
if (input[i + 1] && !output)
{
g_fprintf (stderr, "warning, %d modules omitted\n",
g_strv_length (input) - 1);
break;
}
}
return 0;
}