Merge branch 'ewlsh/gir-compiler' into 'main'

girepository: Update gir-compiler and use it to compile GIRs

See merge request GNOME/glib!3853
This commit is contained in:
Philip Withnall 2024-01-31 11:37:35 +00:00
commit 180e4867c8
5 changed files with 122 additions and 87 deletions

View File

@ -1,41 +1,44 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
* GObject introspection: Typelib compiler
/* GObject introspection: Typelib compiler
* *
*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright (C) 2005 Matthias Clasen * Copyright (C) 2005 Matthias Clasen
* Copyright (C) 2024 GNOME Foundation
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either * License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version. * version 2.1 of the License, or (at your option) any later version.
* *
* This library is distributed in the hope that it will be useful, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General
* License along with this library; if not, write to the * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/ */
#include <errno.h> #include <errno.h>
#include <locale.h> #include <locale.h>
#include <string.h> #include <string.h>
#include <gio/gio.h>
#include <girepository.h>
#include <glib.h> #include <glib.h>
#include <glib/gstdio.h> #include <glib/gstdio.h>
#include <gio/gio.h>
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
#include <io.h>
#include <fcntl.h> #include <fcntl.h>
#include <io.h>
#endif #endif
#include "girmodule.h" #include "girmodule-private.h"
#include "girnode.h" #include "girnode-private.h"
#include "girparser.h" #include "girparser-private.h"
#include "gitypelib-internal.h"
gchar **includedirs = NULL; gchar **includedirs = NULL;
gchar **input = NULL; gchar **input = NULL;
@ -48,8 +51,8 @@ gboolean verbose = FALSE;
gboolean show_version = FALSE; gboolean show_version = FALSE;
static gboolean static gboolean
write_out_typelib (gchar *prefix, write_out_typelib (gchar *prefix,
GITypelib *typelib) GITypelib *typelib)
{ {
FILE *file; FILE *file;
gsize written; gsize written;
@ -74,28 +77,33 @@ write_out_typelib (gchar *prefix,
else else
{ {
if (prefix) if (prefix)
filename = g_strdup_printf ("%s-%s", prefix, output); {
filename = g_strdup_printf ("%s-%s", prefix, output);
}
else else
filename = g_strdup (output); {
filename = g_strdup (output);
}
file_obj = g_file_new_for_path (filename); file_obj = g_file_new_for_path (filename);
tmp_filename = g_strdup_printf ("%s.tmp", filename); tmp_filename = g_strdup_printf ("%s.tmp", filename);
tmp_file_obj = g_file_new_for_path (tmp_filename); tmp_file_obj = g_file_new_for_path (tmp_filename);
file = g_fopen (tmp_filename, "wb"); file = g_fopen (tmp_filename, "wb");
if (file == NULL) if (file == NULL)
{ {
g_fprintf (stderr, "failed to open '%s': %s\n", g_fprintf (stderr, "failed to open '%s': %s\n",
tmp_filename, g_strerror (errno)); tmp_filename, g_strerror (errno));
goto out; goto out;
} }
} }
written = fwrite (typelib->data, 1, typelib->len, file); written = fwrite (typelib->data, 1, typelib->len, file);
if (written < typelib->len) { if (written < typelib->len)
g_fprintf (stderr, "ERROR: Could not write the whole output: %s", {
strerror(errno)); g_fprintf (stderr, "ERROR: Could not write the whole output: %s",
goto out; strerror (errno));
} goto out;
}
if (output != NULL) if (output != NULL)
fclose (file); fclose (file);
@ -103,9 +111,9 @@ write_out_typelib (gchar *prefix,
{ {
if (!g_file_move (tmp_file_obj, file_obj, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error)) if (!g_file_move (tmp_file_obj, file_obj, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error))
{ {
g_fprintf (stderr, "ERROR: failed to rename %s to %s: %s", tmp_filename, filename, error->message); g_fprintf (stderr, "ERROR: failed to rename %s to %s: %s", tmp_filename, filename, error->message);
g_clear_error (&error); g_clear_error (&error);
goto out; goto out;
} }
} }
success = TRUE; success = TRUE;
@ -120,38 +128,35 @@ out:
GLogLevelFlags logged_levels; GLogLevelFlags logged_levels;
static void log_handler (const gchar *log_domain, static void
GLogLevelFlags log_level, log_handler (const gchar *log_domain,
const gchar *message, GLogLevelFlags log_level,
gpointer user_data) const gchar *message,
gpointer user_data)
{ {
if (log_level & logged_levels) if (log_level & logged_levels)
g_log_default_handler (log_domain, log_level, message, user_data); g_log_default_handler (log_domain, log_level, message, user_data);
} }
static GOptionEntry options[] = static GOptionEntry options[] = {
{ { "includedir", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &includedirs, "include directories in GIR search path", NULL },
{ "includedir", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &includedirs, "include directories in GIR search path", NULL }, { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" },
{ "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" }, { "module", 'm', 0, G_OPTION_ARG_STRING, &mname, "module to compile", "NAME" },
{ "module", 'm', 0, G_OPTION_ARG_STRING, &mname, "module to compile", "NAME" }, { "shared-library", 'l', 0, G_OPTION_ARG_FILENAME_ARRAY, &shlibs, "shared library", "FILE" },
{ "shared-library", 'l', 0, G_OPTION_ARG_FILENAME_ARRAY, &shlibs, "shared library", "FILE" }, { "debug", 0, 0, G_OPTION_ARG_NONE, &debug, "show debug messages", NULL },
{ "debug", 0, 0, G_OPTION_ARG_NONE, &debug, "show debug messages", NULL }, { "verbose", 0, 0, G_OPTION_ARG_NONE, &verbose, "show verbose messages", NULL },
{ "verbose", 0, 0, G_OPTION_ARG_NONE, &verbose, "show verbose messages", NULL },
{ "version", 0, 0, G_OPTION_ARG_NONE, &show_version, "show program's version number and exit", 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 }, { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
{ NULL, } G_OPTION_ENTRY_NULL
}; };
int int
main (int argc, char ** argv) main (int argc, char **argv)
{ {
GOptionContext *context; GOptionContext *context;
GError *error = NULL; GError *error = NULL;
GIrParser *parser; GIIrParser *parser;
GIrModule *module; GIIrModule *module;
gint i;
g_typelib_check_sanity ();
setlocale (LC_ALL, ""); setlocale (LC_ALL, "");
@ -169,7 +174,7 @@ main (int argc, char ** argv)
return 1; return 1;
} }
logged_levels = G_LOG_LEVEL_MASK & ~(G_LOG_LEVEL_MESSAGE|G_LOG_LEVEL_DEBUG); logged_levels = G_LOG_LEVEL_MASK & ~(G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_DEBUG);
if (debug) if (debug)
logged_levels = logged_levels | G_LOG_LEVEL_DEBUG; logged_levels = logged_levels | G_LOG_LEVEL_DEBUG;
if (verbose) if (verbose)
@ -180,35 +185,31 @@ main (int argc, char ** argv)
if (show_version) if (show_version)
{ {
g_printf ("g-ir-compiler %u.%u.%u\n", g_printf ("gi-compile-repository %u.%u.%u\n",
GI_MAJOR_VERSION, GI_MINOR_VERSION, GI_MICRO_VERSION); GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
return 0; return 0;
} }
if (!input) if (!input)
{ {
g_fprintf (stderr, "no input files\n"); g_fprintf (stderr, "no input files\n");
return 1; return 1;
} }
g_debug ("[parsing] start, %d includes", g_debug ("[parsing] start, %d includes",
includedirs ? g_strv_length (includedirs) : 0); includedirs ? g_strv_length (includedirs) : 0);
if (includedirs != NULL) parser = gi_ir_parser_new ();
for (i = 0; includedirs[i]; i++)
g_irepository_prepend_search_path (includedirs[i]);
parser = _g_ir_parser_new (); gi_ir_parser_set_includes (parser, (const char *const *) includedirs);
_g_ir_parser_set_includes (parser, (const char*const*) includedirs); module = gi_ir_parser_parse_file (parser, input[0], &error);
if (module == NULL)
module = _g_ir_parser_parse_file (parser, input[0], &error);
if (module == NULL)
{ {
g_fprintf (stderr, "error parsing file %s: %s\n", g_fprintf (stderr, "error parsing file %s: %s\n",
input[0], error->message); input[0], error->message);
return 1; return 1;
} }
@ -217,36 +218,36 @@ main (int argc, char ** argv)
g_debug ("[building] start"); g_debug ("[building] start");
{ {
GITypelib *typelib; GITypelib *typelib;
if (shlibs) if (shlibs)
{ {
if (module->shared_library) if (module->shared_library)
g_free (module->shared_library); g_free (module->shared_library);
module->shared_library = g_strjoinv (",", shlibs); module->shared_library = g_strjoinv (",", shlibs);
} }
g_debug ("[building] module %s", module->name); g_debug ("[building] module %s", module->name);
typelib = _g_ir_module_build_typelib (module); typelib = gi_ir_module_build_typelib (module);
if (typelib == NULL) if (typelib == NULL)
g_error ("Failed to build typelib for module '%s'\n", module->name); g_error ("Failed to build typelib for module '%s'\n", module->name);
if (!g_typelib_validate (typelib, &error)) if (!gi_typelib_validate (typelib, &error))
g_error ("Invalid typelib for module '%s': %s", g_error ("Invalid typelib for module '%s': %s",
module->name, error->message); module->name, error->message);
if (!write_out_typelib (NULL, typelib)) if (!write_out_typelib (NULL, typelib))
return 1; return 1;
g_typelib_free (typelib); gi_typelib_free (typelib);
typelib = NULL; typelib = NULL;
} }
g_debug ("[building] done"); g_debug ("[building] done");
#if 0 #if 0
/* No point */ /* No point */
_g_ir_parser_free (parser); gi_ir_parser_free (parser);
#endif #endif
return 0; return 0;
} }

View File

@ -0,0 +1,29 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# SPDX-FileCopyrightText: 2024 GNOME Foundation
custom_c_args = [
'-DG_LOG_DOMAIN="GLib-GirCompiler"',
]
if cc.get_id() != 'msvc'
custom_c_args = cc.get_supported_arguments([
'-Wno-old-style-definition',
'-Wno-cast-align',
'-Wno-unused-parameter',
'-Wno-duplicated-branches',
])
endif
gicompilerepository = executable('gi-compile-repository', 'compiler.c',
dependencies: [
libgirepository_dep,
libgirepository_internals_dep,
libgio_dep,
],
install: true,
c_args: custom_c_args,
)
# Replace the default g-ir-compiler target with the version we
# just built.
meson.override_find_program('g-ir-compiler', gicompilerepository)

View File

@ -255,7 +255,7 @@ gi_field_info_get_field (GIFieldInfo *field_info,
break; break;
case GI_TYPE_TAG_ARRAY: case GI_TYPE_TAG_ARRAY:
/* We don't check the array type and that it is fixed-size, /* We don't check the array type and that it is fixed-size,
we trust g-ir-compiler to do the right thing */ we trust gi-compile-repository to do the right thing */
value->v_pointer = G_STRUCT_MEMBER_P (mem, offset); value->v_pointer = G_STRUCT_MEMBER_P (mem, offset);
result = TRUE; result = TRUE;
break; break;

View File

@ -241,3 +241,5 @@ endif
if build_tests if build_tests
subdir('tests') subdir('tests')
endif endif
subdir('compiler')

View File

@ -1,3 +1,6 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# SPDX-FileCopyrightText: 2024 GNOME Foundation
girepository_tests = { girepository_tests = {
'cmph-bdz': { 'cmph-bdz': {
'dependencies': [cmph_dep], 'dependencies': [cmph_dep],