[gtk/compile-ui-files: 1/2] buildertool: Add a compile command




commit 11b9b1759abc0468e437580a42c5acea5290c81f
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Sep 18 11:34:04 2021 -0400

    buildertool: Add a compile command
    
    This converts the ui file into our internal, precompiled,
    format. The widget-factory.ui file shrinks from 200k to 45k.
    But sadly, the loading time only shrinks from 240ms to 235.

 tools/gtk-builder-tool-compile.c | 110 +++++++++++++++++++++++++++++++++++++++
 tools/gtk-builder-tool.c         |   4 ++
 tools/gtk-builder-tool.h         |   1 +
 tools/meson.build                |   3 +-
 4 files changed, 117 insertions(+), 1 deletion(-)
---
diff --git a/tools/gtk-builder-tool-compile.c b/tools/gtk-builder-tool-compile.c
new file mode 100644
index 0000000000..dd1018411d
--- /dev/null
+++ b/tools/gtk-builder-tool-compile.c
@@ -0,0 +1,110 @@
+/*  Copyright 2015 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
+ */
+
+#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 "gtkbuilderprivate.h"
+#include "gtk-builder-tool.h"
+
+
+static void
+compile_file (const char *input,
+              const char *output)
+{
+  GtkBuilder *builder;
+  char *text;
+  gsize len;
+  GError *error = NULL;
+  GBytes *bytes;
+
+  if (!g_file_get_contents (input, &text, &len, &error))
+    {
+      g_printerr ("%s\n", error->message);
+      exit (1);
+    }
+
+  builder = gtk_builder_new ();
+  bytes = _gtk_buildable_parser_precompile (text, len, &error);
+  if (!bytes)
+    {
+      g_printerr ("%s\n", error->message);
+      exit (1);
+    }
+
+  if (!g_file_set_contents (output,
+                            g_bytes_get_data (bytes, NULL),
+                            g_bytes_get_size (bytes),
+                            &error))
+    {
+      g_printerr ("%s\n", error->message);
+      exit (1);
+    }
+
+  g_bytes_unref (bytes);
+  g_object_unref (builder);
+  g_free (text);
+}
+
+void
+do_compile (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;
+
+  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);
+    }
+
+  if (g_strv_length (filenames) != 2)
+    {
+      g_printerr ("Need to specify an output file\n");
+      exit (1);
+    }
+
+  compile_file (filenames[0], filenames[1]);
+
+  g_strfreev (filenames);
+}
diff --git a/tools/gtk-builder-tool.c b/tools/gtk-builder-tool.c
index bc89207049..919f5fd1ac 100644
--- a/tools/gtk-builder-tool.c
+++ b/tools/gtk-builder-tool.c
@@ -39,6 +39,8 @@ usage (void)
              "  simplify     Simplify the file\n"
              "  enumerate    List all named objects\n"
              "  preview      Preview the file\n"
+             "  compile      Compile the file\n"
+             "               into a compact format\n"
              "\n"
              "Simplify Options:\n"
              "  --replace    Replace the file\n"
@@ -134,6 +136,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], "compile") == 0)
+    do_compile (&argc, &argv);
   else
     usage ();
 
diff --git a/tools/gtk-builder-tool.h b/tools/gtk-builder-tool.h
index 3d895d83bb..9e6c619104 100644
--- a/tools/gtk-builder-tool.h
+++ b/tools/gtk-builder-tool.h
@@ -6,5 +6,6 @@ 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_compile   (int *argc, const char ***argv);
 
 #endif
diff --git a/tools/meson.build b/tools/meson.build
index 1811b6969e..2b9d0087a5 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-compile.c',
+                         'gtk-builder-tool-preview.c'], [libgtk_static_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]