Debian Codegraph @

glib/girepository/tools/compiler.c

raw
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 * GObject introspection: Typelib compiler
3 *
4 * Copyright (C) 2005 Matthias Clasen
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22 #include <errno.h>
23 #include <locale.h>
24 #include <string.h>
25
26 #include <glib.h>
27 #include <glib/gstdio.h>
28 #include <gio/gio.h>
29
30 #ifdef G_OS_WIN32
31 #include <io.h>
32 #include <fcntl.h>
33 #endif
34
35 #include "girmodule.h"
36 #include "girnode.h"
37 #include "girparser.h"
38 #include "gitypelib-internal.h"
39
40 gchar **includedirs = NULL;
41 gchar **input = NULL;
42 gchar *output = NULL;
43 gchar *mname = NULL;
44 gchar **shlibs = NULL;
45 gboolean include_cwd = FALSE;
46 gboolean debug = FALSE;
47 gboolean verbose = FALSE;
48 gboolean show_version = FALSE;
49
50 static gboolean
51 write_out_typelib (gchar *prefix,
52 GITypelib *typelib)
53 {
54 FILE *file;
55 gsize written;
56 GFile *file_obj;
57 gchar *filename;
58 GFile *tmp_file_obj;
59 gchar *tmp_filename;
60 GError *error = NULL;
61 gboolean success = FALSE;
62
63 if (output == NULL)
64 {
65 file = stdout;
66 file_obj = NULL;
67 filename = NULL;
68 tmp_filename = NULL;
69 tmp_file_obj = NULL;
70 #ifdef G_OS_WIN32
71 setmode (fileno (file), _O_BINARY);
72 #endif
73 }
74 else
75 {
76 if (prefix)
77 filename = g_strdup_printf ("%s-%s", prefix, output);
78 else
79 filename = g_strdup (output);
80 file_obj = g_file_new_for_path (filename);
81 tmp_filename = g_strdup_printf ("%s.tmp", filename);
82 tmp_file_obj = g_file_new_for_path (tmp_filename);
83 file = g_fopen (tmp_filename, "wb");
84
85 if (file == NULL)
86 {
87 g_fprintf (stderr, "failed to open '%s': %s\n",
88 tmp_filename, g_strerror (errno));
89 goto out;
90 }
91 }
92
93 written = fwrite (typelib->data, 1, typelib->len, file);
94 if (written < typelib->len) {
95 g_fprintf (stderr, "ERROR: Could not write the whole output: %s",
96 strerror(errno));
97 goto out;
98 }
99
100 if (output != NULL)
101 fclose (file);
102 if (tmp_filename != NULL)
103 {
104 if (!g_file_move (tmp_file_obj, file_obj, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error))
105 {
106 g_fprintf (stderr, "ERROR: failed to rename %s to %s: %s", tmp_filename, filename, error->message);
107 g_clear_error (&error);
108 goto out;
109 }
110 }
111 success = TRUE;
112 out:
113 g_clear_object (&file_obj);
114 g_clear_object (&tmp_file_obj);
115 g_free (filename);
116 g_free (tmp_filename);
117
118 return success;
119 }
120
121 GLogLevelFlags logged_levels;
122
123 static void log_handler (const gchar *log_domain,
124 GLogLevelFlags log_level,
125 const gchar *message,
126 gpointer user_data)
127 {
128
129 if (log_level & logged_levels)
130 g_log_default_handler (log_domain, log_level, message, user_data);
131 }
132
133 static GOptionEntry options[] =
134 {
135 { "includedir", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &includedirs, "include directories in GIR search path", NULL },
136 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" },
137 { "module", 'm', 0, G_OPTION_ARG_STRING, &mname, "module to compile", "NAME" },
138 { "shared-library", 'l', 0, G_OPTION_ARG_FILENAME_ARRAY, &shlibs, "shared library", "FILE" },
139 { "debug", 0, 0, G_OPTION_ARG_NONE, &debug, "show debug messages", NULL },
140 { "verbose", 0, 0, G_OPTION_ARG_NONE, &verbose, "show verbose messages", NULL },
141 { "version", 0, 0, G_OPTION_ARG_NONE, &show_version, "show program's version number and exit", NULL },
142 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
143 { NULL, }
144 };
145
146 int
147 main (int argc, char ** argv)
148 {
149 GOptionContext *context;
150 GError *error = NULL;
151 GIrParser *parser;
152 GIrModule *module;
153 gint i;
154 g_typelib_check_sanity ();
155
156 setlocale (LC_ALL, "");
157
158 context = g_option_context_new ("");
159 g_option_context_add_main_entries (context, options, NULL);
160 g_option_context_parse (context, &argc, &argv, &error);
161 g_option_context_free (context);
162
163 if (error)
164 {
165 g_fprintf (stderr, "error parsing arguments: %s\n", error->message);
166
167 g_error_free (error);
168
169 return 1;
170 }
171
172 logged_levels = G_LOG_LEVEL_MASK & ~(G_LOG_LEVEL_MESSAGE|G_LOG_LEVEL_DEBUG);
173 if (debug)
174 logged_levels = logged_levels | G_LOG_LEVEL_DEBUG;
175 if (verbose)
176 logged_levels = logged_levels | G_LOG_LEVEL_MESSAGE;
177 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
178
179 g_log_set_default_handler (log_handler, NULL);
180
181 if (show_version)
182 {
183 g_printf ("g-ir-compiler %u.%u.%u\n",
184 GI_MAJOR_VERSION, GI_MINOR_VERSION, GI_MICRO_VERSION);
185 return 0;
186 }
187
188 if (!input)
189 {
190 g_fprintf (stderr, "no input files\n");
191
192 return 1;
193 }
194
195 g_debug ("[parsing] start, %d includes",
196 includedirs ? g_strv_length (includedirs) : 0);
197
198 if (includedirs != NULL)
199 for (i = 0; includedirs[i]; i++)
200 g_irepository_prepend_search_path (includedirs[i]);
201
202 parser = _g_ir_parser_new ();
203
204 _g_ir_parser_set_includes (parser, (const char*const*) includedirs);
205
206 module = _g_ir_parser_parse_file (parser, input[0], &error);
207 if (module == NULL)
208 {
209 g_fprintf (stderr, "error parsing file %s: %s\n",
210 input[0], error->message);
211
212 return 1;
213 }
214
215 g_debug ("[parsing] done");
216
217 g_debug ("[building] start");
218
219 {
220 GITypelib *typelib;
221
222 if (shlibs)
223 {
224 if (module->shared_library)
225 g_free (module->shared_library);
226 module->shared_library = g_strjoinv (",", shlibs);
227 }
228
229 g_debug ("[building] module %s", module->name);
230
231 typelib = _g_ir_module_build_typelib (module);
232 if (typelib == NULL)
233 g_error ("Failed to build typelib for module '%s'\n", module->name);
234 if (!g_typelib_validate (typelib, &error))
235 g_error ("Invalid typelib for module '%s': %s",
236 module->name, error->message);
237
238 if (!write_out_typelib (NULL, typelib))
239 return 1;
240 g_typelib_free (typelib);
241 typelib = NULL;
242 }
243
244 g_debug ("[building] done");
245
246 #if 0
247 /* No point */
248 _g_ir_parser_free (parser);
249 #endif
250
251 return 0;
252 }