[gtk/builder-precompile: 3/4] buildertool: Add a precompile command




commit 319aa558a52f07602c732ded8385ef9708fe6b16
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Jan 29 15:50:40 2020 +0100

    buildertool: Add a precompile command
    
    This writes out the precompiled format of .ui
    files that we are using internally.

 docs/reference/gtk/gtk4-builder-tool.rst |   8 +++
 tools/gtk-builder-tool-precompile.c      | 106 +++++++++++++++++++++++++++++++
 tools/gtk-builder-tool.c                 |   2 +
 tools/gtk-builder-tool.h                 |   9 +--
 tools/meson.build                        |   3 +-
 5 files changed, 123 insertions(+), 5 deletions(-)
---
diff --git a/docs/reference/gtk/gtk4-builder-tool.rst b/docs/reference/gtk/gtk4-builder-tool.rst
index 80998a9d96..1d1b87bbfc 100644
--- a/docs/reference/gtk/gtk4-builder-tool.rst
+++ b/docs/reference/gtk/gtk4-builder-tool.rst
@@ -16,6 +16,7 @@ SYNOPSIS
 |   **gtk4-builder-tool** enumerate <FILE>
 |   **gtk4-builder-tool** simplify [OPTIONS...] <FILE>
 |   **gtk4-builder-tool** preview [OPTIONS...] <FILE>
+|   **gtk4-builder-tool** precompile [OPTIONS...] <FILE>
 
 DESCRIPTION
 -----------
@@ -83,3 +84,10 @@ to do manual fixups  after the initial conversion.
 ``--3to4``
 
   Transform a GTK 3 UI definition file to the equivalent GTK 4 definitions.
+
+Precompilation
+^^^^^^^^^^^^^^
+
+The ``precompile`` command creates a more compact, and faster to load compiled
+form of the ui file that is understood by GtkBuilder. The output is written
+to a file with the extension ``.precompiled``.
diff --git a/tools/gtk-builder-tool-precompile.c b/tools/gtk-builder-tool-precompile.c
new file mode 100644
index 0000000000..5a1a68c0a4
--- /dev/null
+++ b/tools/gtk-builder-tool-precompile.c
@@ -0,0 +1,106 @@
+/*  Copyright 2020 Red Hat, Inc.
+ *
+ * GTK+ 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.
+ *
+ * GLib 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 GTK+; see the file COPYING.  If not,
+ * see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Matthias Clasen
+ */
+
+#define GTK_COMPILATION
+#include "../gtk/gtkbuilderprecompile.c"
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include <glib/gi18n.h>
+#include <glib/gprintf.h>
+#include <glib/gstdio.h>
+#include <gtk/gtk.h>
+
+#include "gtk-builder-tool.h"
+
+static void
+precompile_file (const char *filename)
+{
+  char *data;
+  gsize len;
+  GError *error = NULL;
+  char *outfile;
+  GBytes *bytes;
+
+  if (!g_file_get_contents (filename, &data, &len, &error))
+    {
+      g_warning ("Failed to load '%s': %s", filename, error->message);
+      exit (1);
+    }
+
+  bytes = _gtk_buildable_parser_precompile (data, len, &error);
+  if (bytes == NULL)
+    {
+      g_warning ("Failed to precompile '%s': %s", filename, error->message);
+      exit (1);
+    }
+
+  outfile = g_strconcat (filename, ".precompiled", NULL);
+  if (!g_file_set_contents (outfile, g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes), &error))
+  if (bytes == NULL)
+    {
+      g_warning ("Failed to write precompiled data to '%s': %s", outfile, error->message);
+      exit (1);
+    }
+
+  g_print ("Wrote %ld bytes to %s.\n", g_bytes_get_size (bytes), outfile);
+
+  g_free (outfile);
+  g_bytes_unref (bytes);
+}
+
+void
+do_precompile (int          *argc,
+               const char ***argv)
+{
+  GOptionContext *context;
+  char **filenames = NULL;
+  const GOptionEntry entries[] = {
+    { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, NULL },
+    { NULL, }
+  };
+  GError *error = NULL;
+  int i;
+
+  context = g_option_context_new (NULL);
+  g_option_context_set_help_enabled (context, FALSE);
+  g_option_context_add_main_entries (context, entries, NULL);
+
+  if (!g_option_context_parse (context, argc, (char ***)argv, &error))
+    {
+      g_printerr ("%s\n", error->message);
+      g_error_free (error);
+      exit (1);
+    }
+
+  g_option_context_free (context);
+
+  if (filenames == NULL)
+    {
+      g_printerr ("No .ui file specified\n");
+      exit (1);
+    }
+
+  for (i = 0; filenames[i]; i++)
+    precompile_file (filenames[i]);
+
+  g_strfreev (filenames);
+}
diff --git a/tools/gtk-builder-tool.c b/tools/gtk-builder-tool.c
index bc89207049..16b034b11d 100644
--- a/tools/gtk-builder-tool.c
+++ b/tools/gtk-builder-tool.c
@@ -134,6 +134,8 @@ main (int argc, const char *argv[])
     do_enumerate (&argc, &argv);
   else if (strcmp (argv[0], "preview") == 0)
     do_preview (&argc, &argv);
+  else if (strcmp (argv[0], "precompile") == 0)
+    do_precompile (&argc, &argv);
   else
     usage ();
 
diff --git a/tools/gtk-builder-tool.h b/tools/gtk-builder-tool.h
index 3d895d83bb..710fe19e6c 100644
--- a/tools/gtk-builder-tool.h
+++ b/tools/gtk-builder-tool.h
@@ -2,9 +2,10 @@
 #ifndef __GTK_BUILDER_TOOL_H__
 #define __GTK_BUILDER_TOOL_H__
 
-void do_simplify  (int *argc, const char ***argv);
-void do_validate  (int *argc, const char ***argv);
-void do_enumerate (int *argc, const char ***argv);
-void do_preview   (int *argc, const char ***argv);
+void do_simplify   (int *argc, const char ***argv);
+void do_validate   (int *argc, const char ***argv);
+void do_enumerate  (int *argc, const char ***argv);
+void do_preview    (int *argc, const char ***argv);
+void do_precompile (int *argc, const char ***argv);
 
 #endif
diff --git a/tools/meson.build b/tools/meson.build
index 1811b6969e..59676704c2 100644
--- a/tools/meson.build
+++ b/tools/meson.build
@@ -28,7 +28,8 @@ gtk_tools = [
                          'gtk-builder-tool-simplify.c',
                          'gtk-builder-tool-validate.c',
                          'gtk-builder-tool-enumerate.c',
-                         'gtk-builder-tool-preview.c'], [libgtk_dep] ],
+                         'gtk-builder-tool-preview.c',
+                         'gtk-builder-tool-precompile.c'], [libgtk_dep] ],
   ['gtk4-update-icon-cache', ['updateiconcache.c'] + extra_update_icon_cache_objs, [ libgtk_static_dep ] ],
   ['gtk4-encode-symbolic-svg', ['encodesymbolic.c'], [ libgtk_static_dep ] ],
 ]


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