libgda-4.0, gedit-2.20: Fix gedit typo and GdaXaTransactionId.data
[vala-lang.git] / vapigen / valavapigen.vala
blob3247aa37d6bea386df2dcfa7f1a9dbf8f26c1446
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 [CCode (array_length = false, array_null_terminated = true)]
37 static string[] metadata_directories;
38 static string library;
39 [CCode (array_length = false, array_null_terminated = true)]
40 static string[] packages;
41 CodeContext context;
43 const OptionEntry[] options = {
44 { "vapidir", 0, 0, OptionArg.FILENAME_ARRAY, ref vapi_directories, "Look for package bindings in DIRECTORY", "DIRECTORY..." },
45 { "girdir", 0, 0, OptionArg.FILENAME_ARRAY, ref gir_directories, "Look for GIR bindings in DIRECTORY", "DIRECTORY..." },
46 { "metadatadir", 0, 0, OptionArg.FILENAME_ARRAY, ref metadata_directories, "Look for GIR .metadata files in DIRECTORY", "DIRECTORY..." },
47 { "pkg", 0, 0, OptionArg.STRING_ARRAY, ref packages, "Include binding for PACKAGE", "PACKAGE..." },
48 { "library", 0, 0, OptionArg.STRING, ref library, "Library name", "NAME" },
49 { "directory", 'd', 0, OptionArg.FILENAME, ref directory, "Output directory", "DIRECTORY" },
50 { "disable-warnings", 0, 0, OptionArg.NONE, ref disable_warnings, "Disable warnings", null },
51 { "version", 0, 0, OptionArg.NONE, ref version, "Display version number", null },
52 { "quiet", 'q', 0, OptionArg.NONE, ref quiet_mode, "Do not print messages to the console", null },
53 { "", 0, 0, OptionArg.FILENAME_ARRAY, ref sources, null, "FILE..." },
54 { null }
57 private int quit () {
58 if (context.report.get_errors () == 0) {
59 if (!quiet_mode) {
60 stdout.printf ("Generation succeeded - %d warning(s)\n", context.report.get_warnings ());
62 return 0;
63 } else {
64 if (!quiet_mode) {
65 stdout.printf ("Generation failed: %d error(s), %d warning(s)\n", context.report.get_errors (), context.report.get_warnings ());
67 return 1;
71 private int run () {
72 context = new CodeContext ();
73 context.profile = Profile.GOBJECT;
74 context.vapi_directories = vapi_directories;
75 context.gir_directories = gir_directories;
76 context.metadata_directories = metadata_directories;
77 context.report.enable_warnings = !disable_warnings;
78 context.report.set_verbose_errors (!quiet_mode);
79 CodeContext.push (context);
81 /* default package */
82 context.add_external_package ("glib-2.0");
83 context.add_external_package ("gobject-2.0");
85 if (context.report.get_errors () > 0) {
86 return quit ();
89 /* load packages from .deps file */
90 foreach (string source in sources) {
91 if (!source.has_suffix (".gi")) {
92 continue;
95 var depsfile = source.substring (0, source.length - "gi".length) + "deps";
96 context.add_packages_from_file (depsfile);
99 if (context.report.get_errors () > 0) {
100 return quit ();
103 // depsfile for gir case
104 if (library != null) {
105 var depsfile = library + ".deps";
106 context.add_packages_from_file (depsfile);
107 } else {
108 Report.error (null, "--library option must be specified");
111 if (context.report.get_errors () > 0) {
112 return quit ();
115 if (packages != null) {
116 foreach (string package in packages) {
117 context.add_external_package (package);
119 packages = null;
122 if (context.report.get_errors () > 0) {
123 return quit ();
126 foreach (string source in sources) {
127 if (FileUtils.test (source, FileTest.EXISTS)) {
128 context.add_source_file (new SourceFile (context, SourceFileType.PACKAGE, source));
129 } else {
130 Report.error (null, "%s not found".printf (source));
134 if (context.report.get_errors () > 0) {
135 return quit ();
138 var parser = new Parser ();
139 parser.parse (context);
141 if (context.report.get_errors () > 0) {
142 return quit ();
145 var girparser = new GirParser ();
146 girparser.parse (context);
148 if (context.report.get_errors () > 0) {
149 return quit ();
152 var gidlparser = new GIdlParser ();
153 gidlparser.parse (context);
155 if (context.report.get_errors () > 0) {
156 return quit ();
159 context.check ();
161 if (context.report.get_errors () > 0) {
162 return quit ();
165 // interface writer ignores external packages
166 foreach (SourceFile file in context.get_source_files ()) {
167 if (file.filename.has_suffix (".vapi")) {
168 continue;
170 if (file.filename in sources) {
171 file.file_type = SourceFileType.SOURCE;
172 if (file.filename.has_suffix (".gir")) {
173 // mark relative metadata as source
174 string? metadata_filename = context.get_metadata_path (file.filename);
175 if (metadata_filename != null) {
176 foreach (SourceFile metadata_file in context.get_source_files ()) {
177 if (metadata_file.filename == metadata_filename) {
178 metadata_file.file_type = SourceFileType.SOURCE;
186 var interface_writer = new CodeWriter ();
187 var vapi_filename = "%s.vapi".printf (library);
188 if (directory != null) {
189 vapi_filename = Path.build_filename (directory, vapi_filename);
192 interface_writer.write_file (context, vapi_filename);
194 library = null;
196 return quit ();
199 static int main (string[] args) {
200 try {
201 var opt_context = new OptionContext ("- Vala API Generator");
202 opt_context.set_help_enabled (true);
203 opt_context.add_main_entries (options, null);
204 opt_context.parse (ref args);
205 } catch (OptionError e) {
206 stdout.printf ("%s\n", e.message);
207 stdout.printf ("Run '%s --help' to see a full list of available command line options.\n", args[0]);
208 return 1;
211 if (version) {
212 stdout.printf ("Vala API Generator %s\n", Config.BUILD_VERSION);
213 return 0;
216 if (sources == null) {
217 stderr.printf ("No source file specified.\n");
218 return 1;
221 var vapigen = new VAPIGen ();
222 return vapigen.run ();