1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 * GObject introspection: Typelib compiler
4 * Copyright (C) 2005 Matthias Clasen
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.
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.
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.
26 #include <glib/gstdio.h>
34 #include "girmodule.h"
36 #include "girparser.h"
37 #include "gitypelib-internal.h"
39 gboolean no_init = FALSE;
40 gchar **includedirs = NULL;
45 gboolean include_cwd = FALSE;
46 gboolean debug = FALSE;
47 gboolean verbose = FALSE;
50 write_out_typelib (gchar *prefix,
60 gboolean success = FALSE;
70 setmode (fileno (file), _O_BINARY);
76 filename = g_strdup_printf ("%s-%s", prefix, output);
78 filename = g_strdup (output);
79 file_obj = g_file_new_for_path (filename);
80 tmp_filename = g_strdup_printf ("%s.tmp", filename);
81 tmp_file_obj = g_file_new_for_path (tmp_filename);
82 file = g_fopen (tmp_filename, "wb");
86 g_fprintf (stderr, "failed to open '%s': %s\n",
87 tmp_filename, g_strerror (errno));
92 written = fwrite (typelib->data, 1, typelib->len, file);
93 if (written < typelib->len) {
94 g_fprintf (stderr, "ERROR: Could not write the whole output: %s",
101 if (tmp_filename != NULL)
103 if (!g_file_move (tmp_file_obj, file_obj, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error))
105 g_fprintf (stderr, "ERROR: failed to rename %s to %s: %s", tmp_filename, filename, error->message);
106 g_clear_error (&error);
113 g_free (tmp_filename);
118 GLogLevelFlags logged_levels;
120 static void log_handler (const gchar *log_domain,
121 GLogLevelFlags log_level,
122 const gchar *message,
126 if (log_level & logged_levels)
127 g_log_default_handler (log_domain, log_level, message, user_data);
130 static GOptionEntry options[] =
132 { "no-init", 0, 0, G_OPTION_ARG_NONE, &no_init, "do not create _init() function", NULL },
133 { "includedir", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &includedirs, "include directories in GIR search path", NULL },
134 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output, "output file", "FILE" },
135 { "module", 'm', 0, G_OPTION_ARG_STRING, &mname, "module to compile", "NAME" },
136 { "shared-library", 'l', 0, G_OPTION_ARG_FILENAME, &shlib, "shared library", "FILE" },
137 { "debug", 0, 0, G_OPTION_ARG_NONE, &debug, "show debug messages", NULL },
138 { "verbose", 0, 0, G_OPTION_ARG_NONE, &verbose, "show verbose messages", NULL },
139 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &input, NULL, NULL },
144 main (int argc, char ** argv)
146 GOptionContext *context;
147 GError *error = NULL;
151 g_typelib_check_sanity ();
153 context = g_option_context_new ("");
154 g_option_context_add_main_entries (context, options, NULL);
155 g_option_context_parse (context, &argc, &argv, &error);
156 g_option_context_free (context);
158 logged_levels = G_LOG_LEVEL_MASK & ~(G_LOG_LEVEL_MESSAGE|G_LOG_LEVEL_DEBUG);
160 logged_levels = logged_levels | G_LOG_LEVEL_DEBUG;
162 logged_levels = logged_levels | G_LOG_LEVEL_MESSAGE;
163 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
165 g_log_set_default_handler (log_handler, NULL);
169 g_fprintf (stderr, "no input files\n");
174 g_debug ("[parsing] start, %d includes",
175 includedirs ? g_strv_length (includedirs) : 0);
179 if (includedirs != NULL)
180 for (i = 0; includedirs[i]; i++)
181 g_irepository_prepend_search_path (includedirs[i]);
183 parser = _g_ir_parser_new ();
185 _g_ir_parser_set_includes (parser, (const char*const*) includedirs);
187 module = _g_ir_parser_parse_file (parser, input[0], &error);
190 g_fprintf (stderr, "error parsing file %s: %s\n",
191 input[0], error->message);
196 g_debug ("[parsing] done");
198 g_debug ("[building] start");
205 if (module->shared_library)
206 g_free (module->shared_library);
207 module->shared_library = g_strdup (shlib);
210 g_debug ("[building] module %s", module->name);
212 typelib = _g_ir_module_build_typelib (module);
214 g_error ("Failed to build typelib for module '%s'\n", module->name);
215 if (!g_typelib_validate (typelib, &error))
216 g_error ("Invalid typelib for module '%s': %s",
217 module->name, error->message);
219 if (!write_out_typelib (NULL, typelib))
221 g_typelib_free (typelib);
225 g_debug ("[building] done");
229 _g_ir_parser_free (parser);