Fix null-terminated array parameters in signals
[vala-lang.git] / vapigen / valavapigen.vala
blob5345b63b359a4d468f876917c1b3770b3c63255c
1 /* valavapigen.vala
3 * Copyright (C) 2006-2010 Jürg Billeter
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 class Vala.VAPIGen : Object {
26 static string directory;
27 static bool version;
28 static bool quiet_mode;
29 static bool disable_warnings;
30 [CCode (array_length = false, array_null_terminated = true)]
31 static string[] sources;
32 [CCode (array_length = false, array_null_terminated = true)]
33 static string[] vapi_directories;
34 [CCode (array_length = false, array_null_terminated = true)]
35 static string[] gir_directories;
36 static string library;
37 [CCode (array_length = false, array_null_terminated = true)]
38 static string[] packages;
39 CodeContext context;
41 const OptionEntry[] options = {
42 { "vapidir", 0, 0, OptionArg.FILENAME_ARRAY, ref vapi_directories, "Look for package bindings in DIRECTORY", "DIRECTORY..." },
43 { "girdir", 0, 0, OptionArg.FILENAME_ARRAY, ref gir_directories, "Look for GIR bindings in DIRECTORY", "DIRECTORY..." },
44 { "pkg", 0, 0, OptionArg.STRING_ARRAY, ref packages, "Include binding for PACKAGE", "PACKAGE..." },
45 { "library", 0, 0, OptionArg.STRING, ref library, "Library name", "NAME" },
46 { "directory", 'd', 0, OptionArg.FILENAME, ref directory, "Output directory", "DIRECTORY" },
47 { "disable-warnings", 0, 0, OptionArg.NONE, ref disable_warnings, "Disable warnings", null },
48 { "version", 0, 0, OptionArg.NONE, ref version, "Display version number", null },
49 { "quiet", 'q', 0, OptionArg.NONE, ref quiet_mode, "Do not print messages to the console", null },
50 { "", 0, 0, OptionArg.FILENAME_ARRAY, ref sources, null, "FILE..." },
51 { null }
54 private int quit () {
55 if (context.report.get_errors () == 0) {
56 if (!quiet_mode) {
57 stdout.printf ("Generation succeeded - %d warning(s)\n", context.report.get_warnings ());
59 return 0;
60 } else {
61 if (!quiet_mode) {
62 stdout.printf ("Generation failed: %d error(s), %d warning(s)\n", context.report.get_errors (), context.report.get_warnings ());
64 return 1;
68 private int run () {
69 context = new CodeContext ();
70 context.profile = Profile.GOBJECT;
71 context.vapi_directories = vapi_directories;
72 context.gir_directories = gir_directories;
73 context.report.enable_warnings = !disable_warnings;
74 context.report.set_verbose_errors (!quiet_mode);
75 CodeContext.push (context);
77 /* default package */
78 context.add_external_package ("glib-2.0");
79 context.add_external_package ("gobject-2.0");
81 if (context.report.get_errors () > 0) {
82 return quit ();
85 /* load packages from .deps file */
86 foreach (string source in sources) {
87 if (!source.has_suffix (".gi")) {
88 continue;
91 var depsfile = source.substring (0, source.length - "gi".length) + "deps";
92 context.add_packages_from_file (depsfile);
95 if (context.report.get_errors () > 0) {
96 return quit ();
99 // depsfile for gir case
100 if (library != null) {
101 var depsfile = library + ".deps";
102 context.add_packages_from_file (depsfile);
103 } else {
104 Report.error (null, "--library option must be specified");
107 if (context.report.get_errors () > 0) {
108 return quit ();
111 if (packages != null) {
112 foreach (string package in packages) {
113 context.add_external_package (package);
115 packages = null;
118 if (context.report.get_errors () > 0) {
119 return quit ();
122 foreach (string source in sources) {
123 if (FileUtils.test (source, FileTest.EXISTS)) {
124 context.add_source_file (new SourceFile (context, SourceFileType.PACKAGE, source));
125 } else {
126 Report.error (null, "%s not found".printf (source));
130 if (context.report.get_errors () > 0) {
131 return quit ();
134 var parser = new Parser ();
135 parser.parse (context);
137 if (context.report.get_errors () > 0) {
138 return quit ();
141 var girparser = new GirParser ();
142 girparser.parse (context);
144 if (context.report.get_errors () > 0) {
145 return quit ();
148 var gidlparser = new GIdlParser ();
149 gidlparser.parse (context);
151 if (context.report.get_errors () > 0) {
152 return quit ();
155 context.check ();
157 if (context.report.get_errors () > 0) {
158 return quit ();
161 // interface writer ignores external packages
162 foreach (SourceFile file in context.get_source_files ()) {
163 if (file.filename.has_suffix (".vapi")) {
164 continue;
166 if (file.filename in sources) {
167 file.file_type = SourceFileType.SOURCE;
168 } else if (file.filename.has_suffix (".metadata")) {
169 string gir_filename = "%s.gir".printf (file.filename.substring (0, file.filename.length - ".metadata".length));
170 if (gir_filename in sources) {
171 file.file_type = SourceFileType.SOURCE;
176 var interface_writer = new CodeWriter ();
177 var vapi_filename = "%s.vapi".printf (library);
178 if (directory != null) {
179 vapi_filename = Path.build_filename (directory, vapi_filename);
182 interface_writer.write_file (context, vapi_filename);
184 library = null;
186 return quit ();
189 static int main (string[] args) {
190 try {
191 var opt_context = new OptionContext ("- Vala API Generator");
192 opt_context.set_help_enabled (true);
193 opt_context.add_main_entries (options, null);
194 opt_context.parse (ref args);
195 } catch (OptionError e) {
196 stdout.printf ("%s\n", e.message);
197 stdout.printf ("Run '%s --help' to see a full list of available command line options.\n", args[0]);
198 return 1;
201 if (version) {
202 stdout.printf ("Vala API Generator %s\n", Config.BUILD_VERSION);
203 return 0;
206 if (sources == null) {
207 stderr.printf ("No source file specified.\n");
208 return 1;
211 var vapigen = new VAPIGen ();
212 return vapigen.run ();