glib/girepository/tools/compiler.c
Johan Dahlin 04a5cdf05d Merge in the gir-compiler branch. Thanks to Philip and Colin for their
2008-08-08  Johan Dahlin  <johan@gnome.org>

    * girepository/gtypelib.c (validate_header):
    * girepository/gtypelib.h:
    * giscanner/ast.py:
    * giscanner/girwriter.py:
    * giscanner/sourcescanner.c (gi_source_symbol_ref),
    (gi_source_symbol_unref):
    * tests/array.test:
    * tests/boxed.test:
    * tests/constant.test:
    * tests/enum.test:
    * tests/errors.test:
    * tests/function.test:
    * tests/gobject.test:
    * tests/interface.test:
    * tests/invoke/Makefile.am:
    * tests/invoke/testfns.xml:
    * tests/object.test:
    * tests/parser/Makefile.am:
    * tests/roundtrips.sh:
    * tests/struct.test:
    * tests/types.test:
    * tests/union.test:
    * tests/xref1.test:
    * tests/xref2.test:
    * tools/Makefile.am:
    * tools/compiler.c (main):
    * tools/generate.c (write_callable_info), (write_function_info),
    (write_repository):
    * tools/gidlmodule.c:
    * tools/gidlmodule.h:
    * tools/gidlnode.c:
    * tools/gidlnode.h:
    * tools/gidlparser.c:
    * tools/gidlparser.h:
    * tools/gidlwriter.c:
    * tools/gidlwriter.h:
    * tools/scanner.c (create_node_from_gtype),
    (create_node_from_ctype), (g_igenerator_process_properties),
    (g_igenerator_process_signals), (g_igenerator_create_object),
    (g_igenerator_create_interface), (g_igenerator_create_boxed),
    (g_igenerator_create_enum), (g_igenerator_create_flags),
    (g_igenerator_process_function_symbol),
    (g_igenerator_process_unregistered_struct_typedef),
    (g_igenerator_process_struct_typedef),
    (g_igenerator_process_union_typedef),
    (g_igenerator_process_enum_typedef),
    (g_igenerator_process_function_typedef),
    (g_igenerator_process_constant), (g_igenerator_process_symbols),
    (g_igenerator_add_module), (g_igenerator_add_include_idl):
    Merge in the gir-compiler branch.
    Thanks to Philip and Colin for their help.


svn path=/trunk/; revision=325
2024-01-15 22:44:41 -08:00

266 lines
6.6 KiB
C

/* GObject introspection: Metadata compiler
*
* Copyright (C) 2005 Matthias Clasen
*
* 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 <errno.h>
#include <string.h>
#include <glib.h>
#include <glib/gstdio.h>
#include "girmodule.h"
#include "girnode.h"
#include "girparser.h"
#include "gtypelib.h"
gboolean raw = FALSE;
gboolean no_init = FALSE;
gchar **input = NULL;
gchar *output = NULL;
gchar *mname = NULL;
gchar *shlib = NULL;
gboolean debug = FALSE;
gboolean verbose = FALSE;
static gchar *
format_output (GTypelib *metadata)
{
GString *result;
gint i;
result = g_string_sized_new (6 * metadata->len);
g_string_append_printf (result, "#include <stdlib.h>\n");
g_string_append_printf (result, "#include <girepository.h>\n\n");
g_string_append_printf (result, "const unsigned char _G_TYPELIB[] = \n{");
for (i = 0; i < metadata->len; i++)
{
if (i > 0)
g_string_append (result, ", ");
if (i % 10 == 0)
g_string_append (result, "\n\t");
g_string_append_printf (result, "0x%.2x", metadata->data[i]);
}
g_string_append_printf (result, "\n};\n\n");
g_string_append_printf (result, "const gsize _G_TYPELIB_SIZE = %u;\n\n",
(guint)metadata->len);
if (!no_init)
{
g_string_append_printf (result,
"__attribute__((constructor)) void\n"
"register_metadata (void)\n"
"{\n"
"\tGTypelib *metadata;\n"
"\tmetadata = g_typelib_new_from_const_memory (_G_TYPELIB, _G_TYPELIB_SIZE);\n"
"\tg_irepository_register (NULL, metadata);\n"
"}\n\n");
g_string_append_printf (result,
"__attribute__((destructor)) void\n"
"unregister_metadata (void)\n"
"{\n"
"\tg_irepository_unregister (NULL, \"%s\");\n"
"}\n",
g_typelib_get_namespace (metadata));
}
return g_string_free (result, FALSE);
}
static void
write_out_metadata (gchar *prefix,
GTypelib *metadata)
{
FILE *file;
if (output == NULL)
file = stdout;
else
{
gchar *filename;
if (prefix)
filename = g_strdup_printf ("%s-%s", prefix, output);
else
filename = g_strdup (output);
file = g_fopen (filename, "w");
if (file == NULL)
{
g_fprintf (stderr, "failed to open '%s': %s\n",
filename, g_strerror (errno));
g_free (filename);
return;
}
g_free (filename);
}
if (raw)
fwrite (metadata->data, 1, metadata->len, file);
else
{
gchar *code;
code = format_output (metadata);
fputs (code, file);
g_free (code);
}
if (output != NULL)
fclose (file);
}
GLogLevelFlags logged_levels;
static void log_handler (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer user_data)
{
if (log_level & logged_levels)
g_log_default_handler (log_domain, log_level, message, user_data);
}
static GOptionEntry options[] =
{
{ "raw", 0, 0, G_OPTION_ARG_NONE, &raw, "emit raw metadata", NULL },
{ "code", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &raw, "emit C code", NULL },
{ "no-init", 0, 0, G_OPTION_ARG_NONE, &no_init, "do not create _init() function", NULL },
{ "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" },
{ "module", 'm', 0, G_OPTION_ARG_STRING, &mname, "module to compile", "NAME" },
{ "shared-library", 'l', 0, G_OPTION_ARG_FILENAME, &shlib, "shared library", "FILE" },
{ "debug", 0, 0, G_OPTION_ARG_NONE, &debug, "show debug messages", NULL },
{ "verbose", 0, 0, G_OPTION_ARG_NONE, &verbose, "show verbose messages", NULL },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
{ NULL, }
};
int
main (int argc, char ** argv)
{
GOptionContext *context;
GError *error = NULL;
GList *c, *m, *modules;
gint i;
g_typelib_check_sanity ();
context = g_option_context_new ("");
g_option_context_add_main_entries (context, options, NULL);
g_option_context_parse (context, &argc, &argv, &error);
g_option_context_free (context);
logged_levels = G_LOG_LEVEL_MASK & ~(G_LOG_LEVEL_MESSAGE|G_LOG_LEVEL_DEBUG);
if (debug)
logged_levels = logged_levels | G_LOG_LEVEL_DEBUG;
if (verbose)
logged_levels = logged_levels | G_LOG_LEVEL_MESSAGE;
g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
g_log_set_default_handler (log_handler, NULL);
if (!input)
{
g_fprintf (stderr, "no input files\n");
return 1;
}
g_debug ("[parsing] start");
modules = NULL;
for (i = 0; input[i]; i++)
{
GList *mods;
mods = g_ir_parse_file (input[i], &error);
if (mods == NULL)
{
g_fprintf (stderr, "error parsing file %s: %s\n",
input[i], error->message);
return 1;
}
modules = g_list_concat (modules, mods);
}
g_debug ("[parsing] done");
g_debug ("[building] start");
for (m = modules; m; m = m->next)
{
GIrModule *module = m->data;
gchar *prefix;
GTypelib *metadata;
if (mname && strcmp (mname, module->name) != 0)
continue;
if (shlib)
{
if (module->shared_library)
g_free (module->shared_library);
module->shared_library = g_strdup (shlib);
}
g_debug ("[building] module %s", module->name);
metadata = g_ir_module_build_metadata (module, modules);
if (metadata == NULL)
{
g_error ("Failed to build metadata for module '%s'\n", module->name);
continue;
}
if (!g_typelib_validate (metadata, &error))
g_error ("Invalid metadata for module '%s': %s",
module->name, error->message);
if (!mname && (m->next || m->prev) && output)
prefix = module->name;
else
prefix = NULL;
write_out_metadata (prefix, metadata);
g_typelib_free (metadata);
metadata = NULL;
/* when writing to stdout, stop after the first module */
if (m->next && !output && !mname)
{
g_warning ("%d modules omitted\n", g_list_length (modules) - 1);
break;
}
}
g_debug ("[building] done");
return 0;
}