2 * Copyright © 2011 Red Hat, Inc
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Alexander Larsson <alexl@redhat.com>
38 #include <gio/gmemoryoutputstream.h>
39 #include <gio/gzlibcompressor.h>
40 #include <gio/gconverteroutputstream.h>
43 #include "gvdb/gvdb-builder.h"
45 #include "gconstructor_as_data.h"
46 #include "glib/glib-private.h"
59 GHashTable *table; /* resource path -> FileData */
61 gboolean collect_data;
69 char *preproc_options;
71 GString *string; /* non-NULL when accepting text */
74 static gchar **sourcedirs = NULL;
75 static gchar *xmllint = NULL;
76 static gchar *jsonformat = NULL;
77 static gchar *gdk_pixbuf_pixdata = NULL;
80 file_data_free (FileData *data)
82 g_free (data->filename);
83 g_free (data->content);
88 start_element (GMarkupParseContext *context,
89 const gchar *element_name,
90 const gchar **attribute_names,
91 const gchar **attribute_values,
95 ParseState *state = user_data;
96 const GSList *element_stack;
97 const gchar *container;
99 element_stack = g_markup_parse_context_get_element_stack (context);
100 container = element_stack->next ? element_stack->next->data : NULL;
102 #define COLLECT(first, ...) \
103 g_markup_collect_attributes (element_name, \
104 attribute_names, attribute_values, error, \
105 first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
106 #define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
107 #define STRDUP G_MARKUP_COLLECT_STRDUP
108 #define STRING G_MARKUP_COLLECT_STRING
109 #define BOOL G_MARKUP_COLLECT_BOOLEAN
110 #define NO_ATTRS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
112 if (container == NULL)
114 if (strcmp (element_name, "gresources") == 0)
117 else if (strcmp (container, "gresources") == 0)
119 if (strcmp (element_name, "gresource") == 0)
121 COLLECT (OPTIONAL | STRDUP,
122 "prefix", &state->prefix);
126 else if (strcmp (container, "gresource") == 0)
128 if (strcmp (element_name, "file") == 0)
130 COLLECT (OPTIONAL | STRDUP, "alias", &state->alias,
131 OPTIONAL | BOOL, "compressed", &state->compressed,
132 OPTIONAL | STRDUP, "preprocess", &state->preproc_options);
133 state->string = g_string_new ("");
139 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
140 _("Element <%s> not allowed inside <%s>"),
141 element_name, container);
143 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
144 _("Element <%s> not allowed at toplevel"), element_name);
149 get_parent (GHashTable *table,
153 GvdbItem *grandparent, *parent;
158 while (key[--length - 1] != '/');
161 parent = g_hash_table_lookup (table, key);
165 parent = gvdb_hash_table_insert (table, key);
167 grandparent = get_parent (table, key, length);
169 if (grandparent != NULL)
170 gvdb_item_set_parent (parent, grandparent);
177 find_file (const gchar *filename)
183 if (g_path_is_absolute (filename))
184 return g_strdup (filename);
186 /* search all the sourcedirs for the correct files in order */
187 for (i = 0; sourcedirs[i] != NULL; i++)
189 real_file = g_build_path ("/", sourcedirs[i], filename, NULL);
190 exists = g_file_test (real_file, G_FILE_TEST_EXISTS);
199 end_element (GMarkupParseContext *context,
200 const gchar *element_name,
204 ParseState *state = user_data;
205 GError *my_error = NULL;
207 if (strcmp (element_name, "gresource") == 0)
209 g_free (state->prefix);
210 state->prefix = NULL;
213 else if (strcmp (element_name, "file") == 0)
216 gchar *real_file = NULL;
218 FileData *data = NULL;
219 char *tmp_file = NULL;
221 file = state->string->str;
227 key = g_build_path ("/", "/", state->prefix, key, NULL);
229 key = g_build_path ("/", "/", key, NULL);
231 if (g_hash_table_lookup (state->table, key) != NULL)
233 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
234 _("File %s appears multiple times in the resource"),
239 if (sourcedirs != NULL)
241 real_file = find_file (file);
242 if (real_file == NULL && state->collect_data)
244 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
245 _("Failed to locate “%s” in any source directory"), file);
252 exists = g_file_test (file, G_FILE_TEST_EXISTS);
253 if (!exists && state->collect_data)
255 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
256 _("Failed to locate “%s” in current directory"), file);
261 if (real_file == NULL)
262 real_file = g_strdup (file);
264 data = g_new0 (FileData, 1);
265 data->filename = g_strdup (real_file);
266 if (!state->collect_data)
269 if (state->preproc_options)
273 gboolean xml_stripblanks = FALSE;
274 gboolean json_stripblanks = FALSE;
275 gboolean to_pixdata = FALSE;
277 options = g_strsplit (state->preproc_options, ",", -1);
279 for (i = 0; options[i]; i++)
281 if (!strcmp (options[i], "xml-stripblanks"))
282 xml_stripblanks = TRUE;
283 else if (!strcmp (options[i], "to-pixdata"))
285 else if (!strcmp (options[i], "json-stripblanks"))
286 json_stripblanks = TRUE;
289 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
290 _("Unknown processing option “%s”"), options[i]);
291 g_strfreev (options);
295 g_strfreev (options);
299 /* This is not fatal: pretty-printed XML is still valid XML */
302 static gboolean xmllint_warned = FALSE;
306 /* Translators: the first %s is a gresource XML attribute,
307 * the second %s is an environment variable, and the third
308 * %s is a command line tool
310 char *warn = g_strdup_printf (_("%s preprocessing requested, but %s is not set, and %s is not in PATH"),
314 g_printerr ("%s\n", warn);
318 xmllint_warned = TRUE;
326 fd = g_file_open_tmp ("resource-XXXXXXXX", &tmp_file, error);
332 proc = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_SILENCE, error,
333 xmllint, "--nonet", "--noblanks", "--output", tmp_file, real_file, NULL);
340 if (!g_subprocess_wait_check (proc, NULL, error))
342 g_object_unref (proc);
346 g_object_unref (proc);
348 real_file = g_strdup (tmp_file);
352 if (json_stripblanks)
354 /* As above, this is not fatal: pretty-printed JSON is still
357 if (jsonformat == NULL)
359 static gboolean jsonformat_warned = FALSE;
361 if (!jsonformat_warned)
363 /* Translators: the first %s is a gresource XML attribute,
364 * the second %s is an environment variable, and the third
365 * %s is a command line tool
367 char *warn = g_strdup_printf (_("%s preprocessing requested, but %s is not set, and %s is not in PATH"),
371 g_printerr ("%s\n", warn);
375 jsonformat_warned = TRUE;
383 fd = g_file_open_tmp ("resource-XXXXXXXX", &tmp_file, error);
389 proc = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_SILENCE, error,
390 jsonformat, "--output", tmp_file, real_file, NULL);
397 if (!g_subprocess_wait_check (proc, NULL, error))
399 g_object_unref (proc);
403 g_object_unref (proc);
405 real_file = g_strdup (tmp_file);
414 /* This is a fatal error: if to-pixdata is used it means that
415 * the code loading the GResource expects a specific data format
417 if (gdk_pixbuf_pixdata == NULL)
419 /* Translators: the first %s is a gresource XML attribute,
420 * the second %s is an environment variable, and the third
421 * %s is a command line tool
423 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
424 _("%s preprocessing requested, but %s is not set, and %s is not in PATH"),
426 "GDK_PIXBUF_PIXDATA",
427 "gdk-pixbuf-pixdata");
431 fd = g_file_open_tmp ("resource-XXXXXXXX", &tmp_file, error);
437 proc = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_SILENCE, error,
438 gdk_pixbuf_pixdata, real_file, tmp_file, NULL);
442 if (!g_subprocess_wait_check (proc, NULL, error))
444 g_object_unref (proc);
448 g_object_unref (proc);
450 real_file = g_strdup (tmp_file);
454 if (!g_file_get_contents (real_file, &data->content, &data->size, &my_error))
456 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
457 _("Error reading file %s: %s"),
458 real_file, my_error->message);
459 g_clear_error (&my_error);
462 /* Include zero termination in content_size for uncompressed files (but not in size) */
463 data->content_size = data->size + 1;
465 if (state->compressed)
467 GOutputStream *out = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
468 GZlibCompressor *compressor =
469 g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB, 9);
470 GOutputStream *out2 = g_converter_output_stream_new (out, G_CONVERTER (compressor));
472 if (!g_output_stream_write_all (out2, data->content, data->size,
474 !g_output_stream_close (out2, NULL, NULL))
476 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
477 _("Error compressing file %s"),
479 g_object_unref (compressor);
480 g_object_unref (out);
481 g_object_unref (out2);
485 g_free (data->content);
486 data->content_size = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (out));
487 data->content = g_memory_output_stream_steal_data (G_MEMORY_OUTPUT_STREAM (out));
489 g_object_unref (compressor);
490 g_object_unref (out);
491 g_object_unref (out2);
493 data->flags |= G_RESOURCE_FLAGS_COMPRESSED;
497 g_hash_table_insert (state->table, key, data);
503 g_free (state->alias);
505 g_string_free (state->string, TRUE);
506 state->string = NULL;
507 g_free (state->preproc_options);
508 state->preproc_options = NULL;
519 file_data_free (data);
524 text (GMarkupParseContext *context,
530 ParseState *state = user_data;
533 for (i = 0; i < text_len; i++)
534 if (!g_ascii_isspace (text[i]))
537 g_string_append_len (state->string, text, text_len);
540 g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
541 _("text may not appear inside <%s>"),
542 g_markup_parse_context_get_element (context));
549 parse_resource_file (const gchar *filename,
550 gboolean collect_data,
553 GMarkupParser parser = { start_element, end_element, text };
554 ParseState state = { 0, };
555 GMarkupParseContext *context;
556 GError *error = NULL;
558 GHashTable *table = NULL;
561 if (!g_file_get_contents (filename, &contents, &size, &error))
563 g_printerr ("%s\n", error->message);
564 g_clear_error (&error);
568 state.collect_data = collect_data;
569 state.table = g_hash_table_ref (files);
571 context = g_markup_parse_context_new (&parser,
572 G_MARKUP_TREAT_CDATA_AS_TEXT |
573 G_MARKUP_PREFIX_ERROR_POSITION,
576 if (!g_markup_parse_context_parse (context, contents, size, &error) ||
577 !g_markup_parse_context_end_parse (context, &error))
579 g_printerr ("%s: %s.\n", filename, error->message);
580 g_clear_error (&error);
590 GVariantBuilder builder;
593 table = gvdb_hash_table_new (NULL, NULL);
595 g_hash_table_iter_init (&iter, state.table);
596 while (g_hash_table_iter_next (&iter, (gpointer *)&key, (gpointer *)&data))
598 key_len = strlen (key);
599 mykey = g_strdup (key);
601 item = gvdb_hash_table_insert (table, key);
602 gvdb_item_set_parent (item,
603 get_parent (table, mykey, key_len));
607 g_variant_builder_init (&builder, G_VARIANT_TYPE ("(uuay)"));
609 g_variant_builder_add (&builder, "u", data->size); /* Size */
610 g_variant_builder_add (&builder, "u", data->flags); /* Flags */
612 v_data = g_variant_new_from_data (G_VARIANT_TYPE("ay"),
613 data->content, data->content_size, TRUE,
614 g_free, data->content);
615 g_variant_builder_add_value (&builder, v_data);
616 data->content = NULL; /* Take ownership */
618 gvdb_item_set_value (item,
619 g_variant_builder_end (&builder));
623 g_hash_table_unref (state.table);
624 g_markup_parse_context_free (context);
631 write_to_file (GHashTable *table,
632 const gchar *filename,
637 success = gvdb_table_write_contents (table, filename,
638 G_BYTE_ORDER != G_LITTLE_ENDIAN,
645 extension_in_set (const char *str,
649 const char *ext, *value;
652 ext = strrchr (str, '.');
657 va_start (list, str);
658 while ((value = va_arg (list, const char *)) != NULL)
660 if (g_ascii_strcasecmp (ext, value) != 0)
672 * We must escape any characters that `make` finds significant.
673 * This is largely a duplicate of the logic in gcc's `mkdeps.c:munge()`.
676 escape_makefile_string (const char *string)
681 str = g_string_sized_new (strlen (string) + 1);
682 for (p = string; *p != '\0'; ++p)
688 /* GNU make uses a weird quoting scheme for white space.
689 A space or tab preceded by 2N+1 backslashes represents
690 N backslashes followed by space; a space or tab
691 preceded by 2N backslashes represents N backslashes at
692 the end of a file name; and backslashes in other
693 contexts should not be doubled. */
694 for (q = p - 1; string <= q && *q == '\\'; q--)
695 g_string_append_c (str, '\\');
696 g_string_append_c (str, '\\');
700 g_string_append_c (str, '$');
704 g_string_append_c (str, '\\');
707 g_string_append_c (str, *p);
710 return g_string_free (str, FALSE);
714 main (int argc, char **argv)
720 gboolean show_version_and_exit = FALSE;
721 gchar *target = NULL;
722 gchar *binary_target = NULL;
723 gboolean generate_automatic = FALSE;
724 gboolean generate_source = FALSE;
725 gboolean generate_header = FALSE;
726 gboolean manual_register = FALSE;
727 gboolean internal = FALSE;
728 gboolean external_data = FALSE;
729 gboolean generate_dependencies = FALSE;
730 gboolean generate_phony_targets = FALSE;
731 char *dependency_file = NULL;
733 char *c_name_no_underscores;
734 const char *linkage = "extern";
735 GOptionContext *context;
736 GOptionEntry entries[] = {
737 { "version", 0, 0, G_OPTION_ARG_NONE, &show_version_and_exit, N_("Show program version and exit"), NULL },
738 { "target", 0, 0, G_OPTION_ARG_FILENAME, &target, N_("Name of the output file"), N_("FILE") },
739 { "sourcedir", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &sourcedirs, N_("The directories to load files referenced in FILE from (default: current directory)"), N_("DIRECTORY") },
740 { "generate", 0, 0, G_OPTION_ARG_NONE, &generate_automatic, N_("Generate output in the format selected for by the target filename extension"), NULL },
741 { "generate-header", 0, 0, G_OPTION_ARG_NONE, &generate_header, N_("Generate source header"), NULL },
742 { "generate-source", 0, 0, G_OPTION_ARG_NONE, &generate_source, N_("Generate source code used to link in the resource file into your code"), NULL },
743 { "generate-dependencies", 0, 0, G_OPTION_ARG_NONE, &generate_dependencies, N_("Generate dependency list"), NULL },
744 { "dependency-file", 0, 0, G_OPTION_ARG_FILENAME, &dependency_file, N_("Name of the dependency file to generate"), N_("FILE") },
745 { "generate-phony-targets", 0, 0, G_OPTION_ARG_NONE, &generate_phony_targets, N_("Include phony targets in the generated dependency file"), NULL },
746 { "manual-register", 0, 0, G_OPTION_ARG_NONE, &manual_register, N_("Don’t automatically create and register resource"), NULL },
747 { "internal", 0, 0, G_OPTION_ARG_NONE, &internal, N_("Don’t export functions; declare them G_GNUC_INTERNAL"), NULL },
748 { "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 },
749 { "c-name", 0, 0, G_OPTION_ARG_STRING, &c_name, N_("C identifier name used for the generated source code"), NULL },
757 setlocale (LC_ALL, GLIB_DEFAULT_LOCALE);
758 textdomain (GETTEXT_PACKAGE);
761 tmp = _glib_get_locale_dir ();
762 bindtextdomain (GETTEXT_PACKAGE, tmp);
765 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
768 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
769 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
772 context = g_option_context_new (N_("FILE"));
773 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
774 g_option_context_set_summary (context,
775 N_("Compile a resource specification into a resource file.\n"
776 "Resource specification files have the extension .gresource.xml,\n"
777 "and the resource file have the extension called .gresource."));
778 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
781 if (!g_option_context_parse (context, &argc, &argv, &error))
783 g_printerr ("%s\n", error->message);
787 g_option_context_free (context);
789 if (show_version_and_exit)
791 g_print (PACKAGE_VERSION "\n");
797 g_printerr (_("You should give exactly one file name\n"));
803 linkage = "G_GNUC_INTERNAL";
807 xmllint = g_strdup (g_getenv ("XMLLINT"));
809 xmllint = g_find_program_in_path ("xmllint");
811 jsonformat = g_strdup (g_getenv ("JSON_GLIB_FORMAT"));
812 if (jsonformat == NULL)
813 jsonformat = g_find_program_in_path ("json-glib-format");
815 gdk_pixbuf_pixdata = g_strdup (g_getenv ("GDK_PIXBUF_PIXDATA"));
816 if (gdk_pixbuf_pixdata == NULL)
817 gdk_pixbuf_pixdata = g_find_program_in_path ("gdk-pixbuf-pixdata");
821 char *dirname = g_path_get_dirname (srcfile);
822 char *base = g_path_get_basename (srcfile);
823 char *target_basename;
824 if (g_str_has_suffix (base, ".xml"))
825 base[strlen(base) - strlen (".xml")] = 0;
829 if (g_str_has_suffix (base, ".gresource"))
830 base[strlen(base) - strlen (".gresource")] = 0;
831 target_basename = g_strconcat (base, ".c", NULL);
833 else if (generate_header)
835 if (g_str_has_suffix (base, ".gresource"))
836 base[strlen(base) - strlen (".gresource")] = 0;
837 target_basename = g_strconcat (base, ".h", NULL);
841 if (g_str_has_suffix (base, ".gresource"))
842 target_basename = g_strdup (base);
844 target_basename = g_strconcat (base, ".gresource", NULL);
847 target = g_build_filename (dirname, target_basename, NULL);
848 g_free (target_basename);
852 else if (generate_automatic)
854 if (extension_in_set (target, "c", "cc", "cpp", "cxx", "c++", NULL))
855 generate_source = TRUE;
856 else if (extension_in_set (target, "h", "hh", "hpp", "hxx", "h++", NULL))
857 generate_header = TRUE;
858 else if (extension_in_set (target, "gresource", NULL))
862 files = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)file_data_free);
864 if ((table = parse_resource_file (srcfile, !generate_dependencies, files)) == NULL)
868 g_hash_table_unref (files);
872 /* This can be used in the same invocation
873 as other generate commands */
874 if (dependency_file != NULL)
876 /* Generate a .d file that describes the dependencies for
877 * build tools, gcc -M -MF style */
884 g_hash_table_iter_init (&iter, files);
886 dep_string = g_string_new (NULL);
887 escaped = escape_makefile_string (srcfile);
888 g_string_printf (dep_string, "%s:", escaped);
891 /* First rule: foo.xml: resource1 resource2.. */
892 while (g_hash_table_iter_next (&iter, &key, &data))
895 if (!g_str_equal (file_data->filename, srcfile))
897 escaped = escape_makefile_string (file_data->filename);
898 g_string_append_printf (dep_string, " %s", escaped);
903 g_string_append (dep_string, "\n");
905 /* Optionally include phony targets as it silences `make` but
906 * isn't supported on `ninja` at the moment. See also: `gcc -MP`
908 if (generate_phony_targets)
910 g_string_append (dep_string, "\n");
912 /* One rule for every resource: resourceN: */
913 g_hash_table_iter_init (&iter, files);
914 while (g_hash_table_iter_next (&iter, &key, &data))
917 if (!g_str_equal (file_data->filename, srcfile))
919 escaped = escape_makefile_string (file_data->filename);
920 g_string_append_printf (dep_string, "%s:\n\n", escaped);
926 if (g_str_equal (dependency_file, "-"))
928 g_print ("%s\n", dep_string->str);
932 if (!g_file_set_contents (dependency_file, dep_string->str, dep_string->len, &error))
934 g_printerr ("Error writing dependency file: %s\n", error->message);
935 g_string_free (dep_string, TRUE);
936 g_free (dependency_file);
937 g_error_free (error);
938 g_hash_table_unref (files);
943 g_string_free (dep_string, TRUE);
944 g_free (dependency_file);
947 if (generate_dependencies)
953 g_hash_table_iter_init (&iter, files);
955 /* Generate list of files for direct use as dependencies in a Makefile */
956 while (g_hash_table_iter_next (&iter, &key, &data))
959 g_print ("%s\n", file_data->filename);
962 else if (generate_source || generate_header)
966 int fd = g_file_open_tmp (NULL, &binary_target, NULL);
969 g_printerr ("Can't open temp file\n");
971 g_hash_table_unref (files);
979 char *base = g_path_get_basename (srcfile);
984 /* Remove extensions */
985 dot = strchr (base, '.');
989 s = g_string_new ("");
991 for (i = 0; base[i] != 0; i++)
993 const char *first = G_CSET_A_2_Z G_CSET_a_2_z "_";
994 const char *rest = G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "_";
995 if (strchr ((s->len == 0) ? first : rest, base[i]) != NULL)
996 g_string_append_c (s, base[i]);
997 else if (base[i] == '-')
998 g_string_append_c (s, '_');
1003 c_name = g_string_free (s, FALSE);
1007 binary_target = g_strdup (target);
1009 c_name_no_underscores = c_name;
1010 while (c_name_no_underscores && *c_name_no_underscores == '_')
1011 c_name_no_underscores++;
1013 if (binary_target != NULL &&
1014 !write_to_file (table, binary_target, &error))
1016 g_printerr ("%s\n", error->message);
1019 g_hash_table_unref (files);
1023 if (generate_header)
1027 file = fopen (target, "w");
1030 g_printerr ("can't write to file %s", target);
1032 g_hash_table_unref (files);
1037 "#ifndef __RESOURCE_%s_H__\n"
1038 "#define __RESOURCE_%s_H__\n"
1040 "#include <gio/gio.h>\n"
1042 "%s GResource *%s_get_resource (void);\n",
1043 c_name, c_name, linkage, c_name);
1045 if (manual_register)
1048 "%s void %s_register_resource (void);\n"
1049 "%s void %s_unregister_resource (void);\n"
1051 linkage, c_name, linkage, c_name);
1058 else if (generate_source)
1064 const char *export = "G_MODULE_EXPORT";
1066 if (!g_file_get_contents (binary_target, (char **)&data,
1069 g_printerr ("can't read back temporary file");
1071 g_hash_table_unref (files);
1074 g_unlink (binary_target);
1076 file = fopen (target, "w");
1079 g_printerr ("can't write to file %s", target);
1081 g_hash_table_unref (files);
1086 export = "G_GNUC_INTERNAL";
1089 "#include <gio/gio.h>\n"
1091 "#if defined (__ELF__) && ( __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 6))\n"
1092 "# define SECTION __attribute__ ((section (\".gresource.%s\"), aligned (8)))\n"
1094 "# define SECTION\n"
1097 c_name_no_underscores);
1102 "extern const SECTION union { const guint8 data[%"G_GSIZE_FORMAT"]; const double alignment; void * const ptr;} %s_resource_data;"
1108 /* For Visual Studio builds: Avoid surpassing the 65535-character limit for a string, GitLab issue #1580 */
1109 g_fprintf (file, "#ifdef _MSC_VER\n");
1111 "static const SECTION union { const guint8 data[%"G_GSIZE_FORMAT"]; const double alignment; void * const ptr;} %s_resource_data = { {\n",
1112 data_size + 1 /* nul terminator */, c_name);
1114 for (i = 0; i < data_size; i++)
1117 g_fprintf (file, " ");
1118 g_fprintf (file, "0%3.3o", (int)data[i]);
1119 if (i != data_size - 1)
1120 g_fprintf (file, ", ");
1121 if (i % 16 == 15 || i == data_size - 1)
1122 g_fprintf (file, "\n");
1125 g_fprintf (file, "} };\n");
1127 /* For other compilers, use the long string approach */
1128 g_fprintf (file, "#else /* _MSC_VER */\n");
1130 "static const SECTION union { const guint8 data[%"G_GSIZE_FORMAT"]; const double alignment; void * const ptr;} %s_resource_data = {\n \"",
1131 data_size + 1 /* nul terminator */, c_name);
1133 for (i = 0; i < data_size; i++)
1135 g_fprintf (file, "\\%3.3o", (int)data[i]);
1137 g_fprintf (file, "\"\n \"");
1140 g_fprintf (file, "\" };\n");
1141 g_fprintf (file, "#endif /* !_MSC_VER */\n");
1146 "static GStaticResource static_resource = { %s_resource_data.data, sizeof (%s_resource_data.data)%s, NULL, NULL, NULL };\n"
1149 "GResource *%s_get_resource (void);\n"
1150 "GResource *%s_get_resource (void)\n"
1152 " return g_static_resource_get_resource (&static_resource);\n"
1154 c_name, c_name, (external_data ? "" : " - 1 /* nul terminator */"),
1155 export, c_name, c_name);
1158 if (manual_register)
1163 "void %s_unregister_resource (void);\n"
1164 "void %s_unregister_resource (void)\n"
1166 " g_static_resource_fini (&static_resource);\n"
1170 "void %s_register_resource (void);\n"
1171 "void %s_register_resource (void)\n"
1173 " g_static_resource_init (&static_resource);\n"
1175 export, c_name, c_name,
1176 export, c_name, c_name);
1180 g_fprintf (file, "%s", gconstructor_code);
1183 "#ifdef G_HAS_CONSTRUCTORS\n"
1185 "#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA\n"
1186 "#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(resource_constructor)\n"
1188 "G_DEFINE_CONSTRUCTOR(resource_constructor)\n"
1189 "#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA\n"
1190 "#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(resource_destructor)\n"
1192 "G_DEFINE_DESTRUCTOR(resource_destructor)\n"
1195 "#warning \"Constructor not supported on this compiler, linking in resources will not work\"\n"
1198 "static void resource_constructor (void)\n"
1200 " g_static_resource_init (&static_resource);\n"
1203 "static void resource_destructor (void)\n"
1205 " g_static_resource_fini (&static_resource);\n"
1214 g_free (binary_target);
1216 g_hash_table_destroy (table);
1218 g_free (jsonformat);
1220 g_hash_table_unref (files);