Make constructor-based resource registration malloc free
[platform/upstream/glib.git] / gio / glib-compile-resources.c
1 /*
2  * Copyright © 2011 Red Hat, Inc
3  *
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 of the licence, or (at your option) any later version.
8  *
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.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * Author: Alexander Larsson <alexl@redhat.com>
20  */
21
22 #include "config.h"
23
24 #include <glib.h>
25 #include <gstdio.h>
26 #include <gi18n.h>
27 #include <gioenums.h>
28
29 #include <string.h>
30 #include <stdio.h>
31 #include <locale.h>
32 #include <errno.h>
33 #ifdef HAVE_SYS_WAIT_H
34 #include <sys/wait.h>
35 #endif
36
37 #include <gio/gmemoryoutputstream.h>
38 #include <gio/gzlibcompressor.h>
39 #include <gio/gconverteroutputstream.h>
40
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44
45 #include <glib.h>
46 #include "gvdb/gvdb-builder.h"
47
48 #include "gconstructor_as_data.h"
49
50 typedef struct
51 {
52   char *filename;
53   char *content;
54   gsize content_size;
55   gsize size;
56   guint32 flags;
57 } FileData;
58
59 typedef struct
60 {
61   GHashTable *table; /* resource path -> FileData */
62
63   gboolean collect_data;
64
65   /* per gresource */
66   char *prefix;
67
68   /* per file */
69   char *alias;
70   gboolean compressed;
71   char *preproc_options;
72
73   GString *string;  /* non-NULL when accepting text */
74 } ParseState;
75
76 static gchar *sourcedir = NULL;
77 static gchar *xmllint = NULL;
78
79 static void
80 file_data_free (FileData *data)
81 {
82   g_free (data->filename);
83   g_free (data->content);
84   g_free (data);
85 }
86
87 static void
88 start_element (GMarkupParseContext  *context,
89                const gchar          *element_name,
90                const gchar         **attribute_names,
91                const gchar         **attribute_values,
92                gpointer              user_data,
93                GError              **error)
94 {
95   ParseState *state = user_data;
96   const GSList *element_stack;
97   const gchar *container;
98
99   element_stack = g_markup_parse_context_get_element_stack (context);
100   container = element_stack->next ? element_stack->next->data : NULL;
101
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)
111
112   if (container == NULL)
113     {
114       if (strcmp (element_name, "gresources") == 0)
115         return;
116     }
117   else if (strcmp (container, "gresources") == 0)
118     {
119       if (strcmp (element_name, "gresource") == 0)
120         {
121           COLLECT (OPTIONAL | STRDUP,
122                    "prefix", &state->prefix);
123           return;
124         }
125     }
126   else if (strcmp (container, "gresource") == 0)
127     {
128       if (strcmp (element_name, "file") == 0)
129         {
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 ("");
134           return;
135         }
136     }
137
138   if (container)
139     g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
140                  _("Element <%s> not allowed inside <%s>"),
141                  element_name, container);
142   else
143     g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
144                  _("Element <%s> not allowed at toplevel"), element_name);
145
146 }
147
148 static GvdbItem *
149 get_parent (GHashTable *table,
150             gchar      *key,
151             gint        length)
152 {
153   GvdbItem *grandparent, *parent;
154
155   if (length == 1)
156     return NULL;
157
158   while (key[--length - 1] != '/');
159   key[length] = '\0';
160
161   parent = g_hash_table_lookup (table, key);
162
163   if (parent == NULL)
164     {
165       parent = gvdb_hash_table_insert (table, key);
166
167       grandparent = get_parent (table, key, length);
168
169       if (grandparent != NULL)
170         gvdb_item_set_parent (parent, grandparent);
171     }
172
173   return parent;
174 }
175
176 static void
177 end_element (GMarkupParseContext  *context,
178              const gchar          *element_name,
179              gpointer              user_data,
180              GError              **error)
181 {
182   ParseState *state = user_data;
183   GError *my_error = NULL;
184
185   if (strcmp (element_name, "gresource") == 0)
186     {
187       g_free (state->prefix);
188       state->prefix = NULL;
189     }
190
191   else if (strcmp (element_name, "file") == 0)
192     {
193       gchar *file, *real_file;
194       gchar *key;
195       FileData *data;
196       char *tmp_file = NULL;
197
198       file = state->string->str;
199       key = file;
200       if (state->alias)
201         key = state->alias;
202
203       if (state->prefix)
204         key = g_build_path ("/", "/", state->prefix, key, NULL);
205       else
206         key = g_build_path ("/", "/", key, NULL);
207
208       if (g_hash_table_lookup (state->table, key) != NULL)
209         {
210           g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
211                        _("File %s appears multiple times in the resource"),
212                        key);
213           return;
214         }
215
216       data = g_new0 (FileData, 1);
217
218       if (sourcedir != NULL)
219         real_file = g_build_filename (sourcedir, file, NULL);
220       else
221         real_file = g_strdup (file);
222
223       data->filename = g_strdup (real_file);
224       if (!state->collect_data)
225         goto done;
226
227       if (state->preproc_options)
228         {
229           gchar **options;
230           guint i;
231           gboolean xml_stripblanks = FALSE;
232
233           options = g_strsplit (state->preproc_options, ",", -1);
234
235           for (i = 0; options[i]; i++)
236             {
237               if (!strcmp (options[i], "xml-stripblanks"))
238                 xml_stripblanks = TRUE;
239               else
240                 {
241                   g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
242                                _("Unknown proprocessing options \"%s\""), options[i]);
243                   g_strfreev (options);
244                   goto cleanup;
245                 }
246             }
247           g_strfreev (options);
248
249           if (xml_stripblanks && xmllint != NULL)
250             {
251               gchar *argv[8];
252               int status, fd, argc;
253
254               tmp_file = g_strdup ("resource-XXXXXXXX");
255               if ((fd = g_mkstemp (tmp_file)) == -1)
256                 {
257                   int errsv = errno;
258
259                   g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
260                                _("Failed to create temp file: %s"),
261                               g_strerror (errsv));
262                   g_free (tmp_file);
263                   tmp_file = NULL;
264                   goto cleanup;
265                 }
266               close (fd);
267
268               argc = 0;
269               argv[argc++] = (gchar *) xmllint;
270               argv[argc++] = "--nonet";
271               argv[argc++] = "--noent";
272               argv[argc++] = "--noblanks";
273               argv[argc++] = "--output";
274               argv[argc++] = tmp_file;
275               argv[argc++] = real_file;
276               argv[argc++] = NULL;
277               g_assert (argc <= G_N_ELEMENTS (argv));
278
279               if (!g_spawn_sync (NULL /* cwd */, argv, NULL /* envv */,
280                                  G_SPAWN_STDOUT_TO_DEV_NULL |
281                                  G_SPAWN_STDERR_TO_DEV_NULL,
282                                  NULL, NULL, NULL, NULL, &status, &my_error))
283                 {
284                   g_propagate_error (error, my_error);
285                   goto cleanup;
286                 }
287 #ifdef HAVE_SYS_WAIT_H
288               if (!WIFEXITED (status) || WEXITSTATUS (status) != 0)
289                 {
290                   g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
291                                       _("Error processing input file with xmllint"));
292                   goto cleanup;
293                 }
294 #endif
295
296               g_free (real_file);
297               real_file = g_strdup (tmp_file);
298             }
299         }
300
301       if (!g_file_get_contents (real_file, &data->content, &data->size, &my_error))
302         {
303           g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
304                        _("Error reading file %s: %s"),
305                        real_file, my_error->message);
306           g_clear_error (&my_error);
307           goto cleanup;
308         }
309       /* Include zero termination in content_size for uncompressed files (but not in size) */
310       data->content_size = data->size + 1;
311
312       if (state->compressed)
313         {
314           GOutputStream *out = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
315           GZlibCompressor *compressor =
316             g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB, 9);
317           GOutputStream *out2 = g_converter_output_stream_new (out, G_CONVERTER (compressor));
318
319           if (!g_output_stream_write_all (out2, data->content, data->size,
320                                           NULL, NULL, NULL) ||
321               !g_output_stream_close (out2, NULL, NULL))
322             {
323               g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
324                            _("Error compressing file %s"),
325                            real_file);
326               goto cleanup;
327             }
328
329           g_free (data->content);
330           data->content_size = g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (out));
331           data->content = g_memory_output_stream_steal_data (G_MEMORY_OUTPUT_STREAM (out));
332
333           g_object_unref (compressor);
334           g_object_unref (out);
335           g_object_unref (out2);
336
337           data->flags |= G_RESOURCE_FLAGS_COMPRESSED;
338         }
339
340     done:
341
342       g_hash_table_insert (state->table, key, data);
343
344     cleanup:
345       /* Cleanup */
346
347       g_free (state->alias);
348       state->alias = NULL;
349       g_string_free (state->string, TRUE);
350       state->string = NULL;
351       g_free (state->preproc_options);
352       state->preproc_options = NULL;
353
354       g_free (real_file);
355       if (tmp_file)
356         {
357           unlink (tmp_file);
358           g_free (tmp_file);
359         }
360     }
361 }
362
363 static void
364 text (GMarkupParseContext  *context,
365       const gchar          *text,
366       gsize                 text_len,
367       gpointer              user_data,
368       GError              **error)
369 {
370   ParseState *state = user_data;
371   gsize i;
372
373   for (i = 0; i < text_len; i++)
374     if (!g_ascii_isspace (text[i]))
375       {
376         if (state->string)
377           g_string_append_len (state->string, text, text_len);
378
379         else
380           g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
381                        _("text may not appear inside <%s>"),
382                        g_markup_parse_context_get_element (context));
383
384         break;
385       }
386 }
387
388 static GHashTable *
389 parse_resource_file (const gchar *filename,
390                      gboolean collect_data)
391 {
392   GMarkupParser parser = { start_element, end_element, text };
393   ParseState state = { 0, };
394   GMarkupParseContext *context;
395   GError *error = NULL;
396   gchar *contents;
397   GHashTable *table = NULL;
398   gsize size;
399
400   if (!g_file_get_contents (filename, &contents, &size, &error))
401     {
402       g_printerr ("%s\n", error->message);
403       g_clear_error (&error);
404       return NULL;
405     }
406
407   state.table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)file_data_free);
408   state.collect_data = collect_data;
409
410   context = g_markup_parse_context_new (&parser,
411                                         G_MARKUP_TREAT_CDATA_AS_TEXT |
412                                         G_MARKUP_PREFIX_ERROR_POSITION,
413                                         &state, NULL);
414
415   if (!g_markup_parse_context_parse (context, contents, size, &error) ||
416       !g_markup_parse_context_end_parse (context, &error))
417     {
418       g_printerr ("%s: %s.\n", filename, error->message);
419       g_clear_error (&error);
420     }
421   else if (collect_data)
422     {
423       GHashTableIter iter;
424       const char *key;
425       char *mykey;
426       gsize key_len;
427       FileData *data;
428       GVariant *v_data;
429       GVariantBuilder builder;
430       GvdbItem *item;
431
432       table = gvdb_hash_table_new (NULL, NULL);
433
434       g_hash_table_iter_init (&iter, state.table);
435       while (g_hash_table_iter_next (&iter, (gpointer *)&key, (gpointer *)&data))
436         {
437           key_len = strlen (key);
438           mykey = g_strdup (key);
439
440           item = gvdb_hash_table_insert (table, key);
441           gvdb_item_set_parent (item,
442                                 get_parent (table, mykey, key_len));
443
444           g_free (mykey);
445
446           g_variant_builder_init (&builder, G_VARIANT_TYPE ("(uuay)"));
447
448           g_variant_builder_add (&builder, "u", data->size); /* Size */
449           g_variant_builder_add (&builder, "u", data->flags); /* Flags */
450
451           v_data = g_variant_new_from_data (G_VARIANT_TYPE("ay"),
452                                             data->content, data->content_size, TRUE,
453                                             g_free, data->content);
454           g_variant_builder_add_value (&builder, v_data);
455           data->content = NULL; /* Take ownership */
456
457           gvdb_item_set_value (item,
458                                g_variant_builder_end (&builder));
459         }
460     }
461   else
462     {
463       table = g_hash_table_ref (state.table);
464     }
465
466   g_hash_table_unref (state.table);
467   g_markup_parse_context_free (context);
468   g_free (contents);
469
470   return table;
471 }
472
473 static gboolean
474 write_to_file (GHashTable   *table,
475                const gchar  *filename,
476                GError      **error)
477 {
478   gboolean success;
479
480   success = gvdb_table_write_contents (table, filename,
481                                        G_BYTE_ORDER != G_LITTLE_ENDIAN,
482                                        error);
483
484   return success;
485 }
486
487 int
488 main (int argc, char **argv)
489 {
490   GError *error;
491   GHashTable *table;
492   gchar *srcfile;
493   gchar *target = NULL;
494   gchar *binary_target = NULL;
495   gboolean generate_automatic = FALSE;
496   gboolean generate_source = FALSE;
497   gboolean generate_header = FALSE;
498   gboolean manual_register = FALSE;
499   gboolean generate_dependencies = FALSE;
500   char *c_name = NULL;
501   char *c_name_no_underscores;
502   GOptionContext *context;
503   GOptionEntry entries[] = {
504     { "target", 0, 0, G_OPTION_ARG_FILENAME, &target, N_("name of the output file"), N_("FILE") },
505     { "sourcedir", 0, 0, G_OPTION_ARG_FILENAME, &sourcedir, N_("The directory where files are to be read from (default to current directory)"), N_("DIRECTORY") },
506     { "generate", 0, 0, G_OPTION_ARG_NONE, &generate_automatic, N_("Generate output in the format selected for by the target filename extension"), NULL },
507     { "generate-header", 0, 0, G_OPTION_ARG_NONE, &generate_header, N_("Generate source header"), NULL },
508     { "generate-source", 0, 0, G_OPTION_ARG_NONE, &generate_source, N_("Generate sourcecode used to link in the resource file into your code"), NULL },
509     { "generate-dependencies", 0, 0, G_OPTION_ARG_NONE, &generate_dependencies, N_("Generate dependency list"), NULL },
510     { "manual-register", 0, 0, G_OPTION_ARG_NONE, &manual_register, N_("Don't automatically create and register resource"), NULL },
511     { "c-name", 0, 0, G_OPTION_ARG_STRING, &c_name, N_("C identifier name used for the generated source code"), NULL },
512     { NULL }
513   };
514
515 #ifdef G_OS_WIN32
516   extern gchar *_glib_get_locale_dir (void);
517   gchar *tmp;
518 #endif
519
520   setlocale (LC_ALL, "");
521   textdomain (GETTEXT_PACKAGE);
522
523 #ifdef G_OS_WIN32
524   tmp = _glib_get_locale_dir ();
525   bindtextdomain (GETTEXT_PACKAGE, tmp);
526   g_free (tmp);
527 #else
528   bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
529 #endif
530
531 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
532   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
533 #endif
534
535   g_type_init ();
536
537   context = g_option_context_new (N_("FILE"));
538   g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
539   g_option_context_set_summary (context,
540     N_("Compile a resource specification into a resource file.\n"
541        "Resource specification files have the extension .gresource.xml,\n"
542        "and the resource file have the extension called .gresource."));
543   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
544
545   error = NULL;
546   if (!g_option_context_parse (context, &argc, &argv, &error))
547     {
548       g_printerr ("%s\n", error->message);
549       return 1;
550     }
551
552   g_option_context_free (context);
553
554   if (argc != 2)
555     {
556       g_printerr (_("You should give exactly one file name\n"));
557       return 1;
558     }
559
560   srcfile = argv[1];
561
562   if (sourcedir == NULL)
563     sourcedir = "";
564
565   xmllint = g_strdup (g_getenv ("XMLLINT"));
566   if (xmllint == NULL)
567     xmllint = g_find_program_in_path ("xmllint");
568   if (xmllint == NULL)
569     g_printerr ("XMLLINT not set and xmllint not found in path; skipping xml preprocessing.\n");
570
571   if (target == NULL)
572     {
573       char *dirname = g_path_get_dirname (srcfile);
574       char *base = g_path_get_basename (srcfile);
575       char *target_basename;
576       if (g_str_has_suffix (base, ".xml"))
577         base[strlen(base) - strlen (".xml")] = 0;
578
579       if (generate_source)
580         {
581           if (g_str_has_suffix (base, ".gresource"))
582             base[strlen(base) - strlen (".gresource")] = 0;
583           target_basename = g_strconcat (base, ".c", NULL);
584         }
585       else
586         {
587           if (g_str_has_suffix (base, ".gresource"))
588             target_basename = g_strdup (base);
589           else
590             target_basename = g_strconcat (base, ".gresource", NULL);
591         }
592
593       target = g_build_filename (dirname, target_basename, NULL);
594       g_free (target_basename);
595       g_free (dirname);
596       g_free (base);
597     }
598   else if (generate_automatic)
599     {
600       if (g_str_has_suffix (target, ".c"))
601         generate_source = TRUE;
602       else if (g_str_has_suffix (target, ".h"))
603         generate_header = TRUE;
604       else if (g_str_has_suffix (target, ".gresource"))
605         ;
606     }
607
608   if ((table = parse_resource_file (srcfile, !generate_dependencies)) == NULL)
609     {
610       g_free (target);
611       return 1;
612     }
613
614   if (generate_dependencies)
615     {
616       GHashTableIter iter;
617       gpointer key, data;
618       FileData *file_data;
619
620       g_hash_table_iter_init (&iter, table);
621       while (g_hash_table_iter_next (&iter, &key, &data))
622         {
623           file_data = data;
624           g_print ("%s\n",file_data->filename);
625         }
626     }
627   else if (generate_source || generate_header)
628     {
629       if (generate_source)
630         {
631           int fd = g_file_open_tmp (NULL, &binary_target, NULL);
632           if (fd == -1)
633             {
634               g_printerr ("Can't open temp file\n");
635               return 1;
636             }
637           close (fd);
638         }
639
640       if (c_name == NULL)
641         {
642           char *base = g_path_get_basename (srcfile);
643           GString *s;
644           char *dot;
645           int i;
646
647           /* Remove extensions */
648           dot = strchr (base, '.');
649           if (dot)
650             *dot = 0;
651
652           s = g_string_new ("");
653
654           for (i = 0; base[i] != 0; i++)
655             {
656               const char *first = G_CSET_A_2_Z G_CSET_a_2_z "_";
657               const char *rest = G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "_";
658               if (strchr ((i == 0) ? first : rest, base[i]) != NULL)
659                 g_string_append_c (s, base[i]);
660               else if (base[i] == '-')
661                 g_string_append_c (s, '_');
662
663             }
664
665           c_name = g_string_free (s, FALSE);
666         }
667     }
668   else
669     binary_target = g_strdup (target);
670
671   c_name_no_underscores = c_name;
672   while (c_name_no_underscores && *c_name_no_underscores == '_')
673     c_name_no_underscores++;
674
675   if (binary_target != NULL &&
676       !write_to_file (table, binary_target, &error))
677     {
678       g_printerr ("%s\n", error->message);
679       g_free (target);
680       return 1;
681     }
682
683   if (generate_header)
684     {
685       FILE *file;
686
687       file = fopen (target, "w");
688       if (file == NULL)
689         {
690           g_printerr ("can't write to file %s", target);
691           return 1;
692         }
693
694       fprintf (file,
695                "#ifndef __RESOURCE_%s_H__\n"
696                "#define __RESOURCE_%s_H__\n"
697                "\n"
698                "#include <gio/gio.h>\n"
699                "\n"
700                "extern GResource *%s_get_resource (void);\n",
701                c_name, c_name, c_name);
702
703       if (manual_register)
704         fprintf (file,
705                  "\n"
706                  "extern void %s_register_resource (void);\n"
707                  "extern void %s_unregister_resource (void);\n"
708                  "\n",
709                  c_name, c_name);
710
711       fprintf (file,
712                "#endif\n");
713
714       fclose (file);
715     }
716   else if (generate_source)
717     {
718       FILE *file;
719       guint8 *data;
720       gsize data_size;
721       gsize i;
722
723       if (!g_file_get_contents (binary_target, (char **)&data,
724                                 &data_size, NULL))
725         {
726           g_printerr ("can't read back temporary file");
727           return 1;
728         }
729       g_unlink (binary_target);
730
731       file = fopen (target, "w");
732       if (file == NULL)
733         {
734           g_printerr ("can't write to file %s", target);
735           return 1;
736         }
737
738       fprintf (file,
739                "#include <gio/gio.h>\n"
740                "\n"
741                "#if defined (__ELF__) && ( __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 6))\n"
742                "# define SECTION __attribute__ ((section (\".gresource.%s\"), aligned (8)))\n"
743                "#else\n"
744                "# define SECTION\n"
745                "#endif\n"
746                "\n"
747                "static const SECTION union { const guint8 data[%"G_GSIZE_FORMAT"]; const double alignment; void * const ptr;}  %s_resource_data = { {\n",
748                c_name_no_underscores, data_size, c_name);
749
750       for (i = 0; i < data_size; i++) {
751         if (i % 8 == 0)
752           fprintf (file, "  ");
753         fprintf (file, "0x%2.2x", (int)data[i]);
754         if (i != data_size - 1)
755           fprintf (file, ", ");
756         if ((i % 8 == 7) || (i == data_size - 1))
757           fprintf (file, "\n");
758       }
759
760       fprintf (file, "} };\n");
761
762       fprintf (file,
763                "\n"
764                "static GStaticResource static_resource = { %s_resource_data.data, sizeof (%s_resource_data.data) };\n"
765                "extern GResource *%s_get_resource (void);\n"
766                "GResource *%s_get_resource (void)\n"
767                "{\n"
768                "  return g_static_resource_get_resource (&static_resource);\n"
769                "}\n",
770                c_name, c_name, c_name, c_name);
771
772
773       if (manual_register)
774         {
775           fprintf (file,
776                    "\n"
777                    "extern void %s_unregister_resource (void);\n"
778                    "void %s_unregister_resource (void)\n"
779                    "{\n"
780                    "  g_static_resource_fini (&static_resource);\n"
781                    "}\n"
782                    "\n"
783                    "extern void %s_register_resource (void);\n"
784                    "void %s_register_resource (void)\n"
785                    "{\n"
786                    "  g_static_resource_init (&static_resource);\n"
787                    "}\n",
788                    c_name, c_name, c_name, c_name);
789         }
790       else
791         {
792           fprintf (file, "%s", gconstructor_code);
793           fprintf (file,
794                    "\n"
795                    "#ifdef G_HAS_CONSTRUCTORS\n"
796                    "\n"
797                    "#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA\n"
798                    "#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(resource_constructor)\n"
799                    "#endif\n"
800                    "G_DEFINE_CONSTRUCTOR(resource_constructor)\n"
801                    "#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA\n"
802                    "#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(resource_destructor)\n"
803                    "#endif\n"
804                    "G_DEFINE_DESTRUCTOR(resource_destructor)\n"
805                    "\n"
806                    "#else\n"
807                    "#warning \"Constructor not supported on this compiler, linking in resources will not work\"\n"
808                    "#endif\n"
809                    "\n"
810                    "static void resource_constructor (void)\n"
811                    "{\n"
812                    "  g_static_resource_init (&static_resource);\n"
813                    "}\n"
814                    "\n"
815                    "static void resource_destructor (void)\n"
816                    "{\n"
817                    "  g_static_resource_fini (&static_resource);\n"
818                    "}\n");
819         }
820
821       fclose (file);
822
823       g_free (data);
824     }
825
826   g_free (binary_target);
827   g_free (target);
828   g_hash_table_destroy (table);
829   g_free (xmllint);
830
831   return 0;
832 }