[glib/ebassi/compile-resource-split] Do not generate C resources for all possible toolchains




commit ecf38ed5810f4b7fa8a658eaa3f0b3c53bc71d24
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Sat Sep 18 13:45:57 2021 +0100

    Do not generate C resources for all possible toolchains
    
    The resources data is generated for both GCC and MSVC toolchains, even
    though we know beforehand which toolchain we're going to compile it for.
    By dropping the data duplication we make the generated resources file
    faster to compile, especially when dealing with large embedded data,
    instead of relying on the C pre-processor to walk the whole file and
    discard the branch we're not using.

 docs/reference/gio/glib-compile-resources.xml | 10 +++++
 gio/glib-compile-resources.c                  | 64 +++++++++++++++++++--------
 2 files changed, 55 insertions(+), 19 deletions(-)
---
diff --git a/docs/reference/gio/glib-compile-resources.xml b/docs/reference/gio/glib-compile-resources.xml
index a85f6003c..7ab36f5a9 100644
--- a/docs/reference/gio/glib-compile-resources.xml
+++ b/docs/reference/gio/glib-compile-resources.xml
@@ -200,6 +200,16 @@ be used with <literal>make</literal>.
 </para></listitem>
 </varlistentry>
 
+<varlistentry>
+<term><option>--compiler=<replaceable>NAME</replaceable></option></term>
+<listitem><para>
+Generate code that is going to target the given compiler <replaceable>NAME</replaceable>.
+The current two compiler modes are "gcc", for all GCC-compatible toolchains; and "msvc",
+for the Microsoft Visual C Compiler. If this option isn't set, then the default will be
+taken from the <envar>CC</envar> environment variable.
+</para></listitem>
+</varlistentry>
+
 </variablelist>
 </refsect1>
 
diff --git a/gio/glib-compile-resources.c b/gio/glib-compile-resources.c
index ac95801f4..fd63d9b80 100644
--- a/gio/glib-compile-resources.c
+++ b/gio/glib-compile-resources.c
@@ -710,6 +710,11 @@ escape_makefile_string (const char *string)
   return g_string_free (str, FALSE);
 }
 
+typedef enum {
+  COMPILER_GCC,
+  COMPILER_MSVC
+} CompilerType;
+
 int
 main (int argc, char **argv)
 {
@@ -732,6 +737,8 @@ main (int argc, char **argv)
   char *c_name = NULL;
   char *c_name_no_underscores;
   const char *linkage = "extern";
+  const char *compiler = NULL;
+  CompilerType compiler_type = COMPILER_GCC;
   GOptionContext *context;
   GOptionEntry entries[] = {
     { "version", 0, 0, G_OPTION_ARG_NONE, &show_version_and_exit, N_("Show program version and exit"), NULL 
},
@@ -747,6 +754,7 @@ main (int argc, char **argv)
     { "internal", 0, 0, G_OPTION_ARG_NONE, &internal, N_("Don’t export functions; declare them 
G_GNUC_INTERNAL"), NULL },
     { "external-data", 0, 0, G_OPTION_ARG_NONE, &external_data, N_("Don’t embed resource data in the C file; 
assume it's linked externally instead"), NULL },
     { "c-name", 0, 0, G_OPTION_ARG_STRING, &c_name, N_("C identifier name used for the generated source 
code"), NULL },
+    { "compiler", 'C', 0, G_OPTION_ARG_STRING, &compiler, N_("The target C compiler (default: the CC 
environment variable)"), NULL },
     G_OPTION_ENTRY_NULL
   };
 
@@ -802,6 +810,18 @@ main (int argc, char **argv)
   if (internal)
     linkage = "G_GNUC_INTERNAL";
 
+  if (compiler == NULL)
+    {
+      compiler = g_getenv ("CC");
+      if (compiler == NULL || *compiler == '\0')
+        compiler = "gcc";
+    }
+
+  if (g_strcmp0 (compiler, "msvc") == 0)
+    compiler_type = COMPILER_MSVC;
+  else
+    compiler_type = COMPILER_GCC;
+
   srcfile = argv[1];
 
   xmllint = g_strdup (g_getenv ("XMLLINT"));
@@ -1105,27 +1125,31 @@ main (int argc, char **argv)
         }
       else
         {
-          /* For Visual Studio builds: Avoid surpassing the 65535-character limit for a string, GitLab issue 
#1580 */
-          g_fprintf (file, "#ifdef _MSC_VER\n");
-          g_fprintf (file,
-                     "static const SECTION union { const guint8 data[%"G_GSIZE_FORMAT"]; const double 
alignment; void * const ptr;}  %s_resource_data = { {\n",
-                     data_size + 1 /* nul terminator */, c_name);
-
-          for (i = 0; i < data_size; i++)
+          if (compiler_type == COMPILER_MSVC)
             {
-              if (i % 16 == 0)
-                g_fprintf (file, "  ");
-              g_fprintf (file, "0%3.3o", (int)data[i]);
-              if (i != data_size - 1)
-                g_fprintf (file, ", ");
-              if (i % 16 == 15 || i == data_size - 1)
-                g_fprintf (file, "\n");
-            }
+              /* For Visual Studio builds: Avoid surpassing the 65535-character limit for a string, GitLab 
issue #1580 */
+              g_fprintf (file, "#ifdef _MSC_VER\n");
+              g_fprintf (file,
+                         "static const SECTION union { const guint8 data[%"G_GSIZE_FORMAT"]; const double 
alignment; void * const ptr;}  %s_resource_data = { {\n",
+                         data_size + 1 /* nul terminator */, c_name);
+
+              for (i = 0; i < data_size; i++)
+                {
+                  if (i % 16 == 0)
+                    g_fprintf (file, "  ");
+                  g_fprintf (file, "0%3.3o", (int)data[i]);
+                  if (i != data_size - 1)
+                    g_fprintf (file, ", ");
+                  if (i % 16 == 15 || i == data_size - 1)
+                     g_fprintf (file, "\n");
+                }
 
-          g_fprintf (file, "} };\n");
+             g_fprintf (file, "} };\n");
+
+             /* For other compilers, use the long string approach */
+             g_fprintf (file, "#else /* _MSC_VER */\n");
+           }
 
-          /* For other compilers, use the long string approach */
-          g_fprintf (file, "#else /* _MSC_VER */\n");
           g_fprintf (file,
                      "static const SECTION union { const guint8 data[%"G_GSIZE_FORMAT"]; const double 
alignment; void * const ptr;}  %s_resource_data = {\n  \"",
                      data_size + 1 /* nul terminator */, c_name);
@@ -1138,7 +1162,9 @@ main (int argc, char **argv)
             }
 
           g_fprintf (file, "\" };\n");
-          g_fprintf (file, "#endif /* !_MSC_VER */\n");
+
+          if (compiler_type == COMPILER_MSVC)
+            g_fprintf (file, "#endif /* !_MSC_VER */\n");
         }
 
       g_fprintf (file,


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