remove obsolete ref modifier and callback keyword
[vala-lang.git] / vapigen / valavapigen.vala
blobd0d16f88b8364e11387403efc32b761ea97493c8
1 /* valavapigen.vala
3 * Copyright (C) 2006-2007 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 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 {
26 static string directory;
27 static bool version;
28 [NoArrayLength ()]
29 static string[] sources;
30 [NoArrayLength ()]
31 static string[] vapi_directories;
32 static string library;
33 [NoArrayLength ()]
34 static string[] packages;
35 static bool disable_memory_management;
36 CodeContext context;
38 const OptionEntry[] options = {
39 { "vapidir", 0, 0, OptionArg.FILENAME_ARRAY, out vapi_directories, "Look for package bindings in DIRECTORY", "DIRECTORY..." },
40 { "pkg", 0, 0, OptionArg.STRING_ARRAY, out packages, "Include binding for PACKAGE", "PACKAGE..." },
41 { "library", 0, 0, OptionArg.STRING, out library, "Library name", "NAME" },
42 { "directory", 'd', 0, OptionArg.FILENAME, out directory, "Output directory", "DIRECTORY" },
43 { "version", 0, 0, OptionArg.NONE, ref version, "Display version number", null },
44 { "", 0, 0, OptionArg.FILENAME_ARRAY, out sources, null, "FILE..." },
45 { null }
48 private int quit () {
49 if (Report.get_errors () == 0) {
50 stdout.printf ("Generation succeeded - %d warning(s)\n", Report.get_warnings ());
51 return 0;
52 } else {
53 stdout.printf ("Generation failed: %d error(s), %d warning(s)\n", Report.get_errors (), Report.get_warnings ());
54 return 1;
58 private string get_package_path (string! pkg) {
59 var basename = "%s.vala".printf (pkg);
61 if (vapi_directories != null) {
62 foreach (string vapidir in vapi_directories) {
63 var filename = Path.build_filename (vapidir, basename, null);
64 if (FileUtils.test (filename, FileTest.EXISTS)) {
65 return filename;
70 var filename = Path.build_filename ("/usr/local/share/vala/vapi", basename, null);
71 if (FileUtils.test (filename, FileTest.EXISTS)) {
72 return filename;
75 filename = Path.build_filename ("/usr/share/vala/vapi", basename, null);
76 if (FileUtils.test (filename, FileTest.EXISTS)) {
77 return filename;
80 return null;
83 private bool add_package (string! pkg) {
84 var package_path = get_package_path (pkg);
86 if (package_path == null) {
87 return false;
90 context.add_source_file (new SourceFile (context, package_path, true));
92 return true;
95 private int run () {
96 context = new CodeContext ();
98 /* default package */
99 if (!add_package ("glib-2.0")) {
100 Report.error (null, "glib-2.0 not found in specified Vala API directories");
103 if (packages != null) {
104 foreach (string package in packages) {
105 if (!add_package (package)) {
106 Report.error (null, "%s not found in specified Vala API directories".printf (package));
109 packages = null;
112 if (Report.get_errors () > 0) {
113 return quit ();
116 foreach (string source in sources) {
117 if (FileUtils.test (source, FileTest.EXISTS)) {
118 context.add_source_file (new SourceFile (context, source));
119 } else {
120 Report.error (null, "%s not found".printf (source));
123 sources = null;
125 if (Report.get_errors () > 0) {
126 return quit ();
129 var parser = new Parser ();
130 parser.parse (context);
132 if (Report.get_errors () > 0) {
133 return quit ();
136 var gidlparser = new GIdlParser ();
137 gidlparser.parse (context);
139 if (Report.get_errors () > 0) {
140 return quit ();
143 var builder = new SymbolBuilder ();
144 builder.build (context);
146 if (Report.get_errors () > 0) {
147 return quit ();
150 var attributeprocessor = new AttributeProcessor ();
151 attributeprocessor.process (context);
153 if (Report.get_errors () > 0) {
154 return quit ();
157 var resolver = new SymbolResolver ();
158 resolver.resolve (context);
160 if (Report.get_errors () > 0) {
161 return quit ();
164 if (library != null) {
165 var interface_writer = new InterfaceWriter ();
166 interface_writer.write_file (context, "%s.vala".printf (library));
168 library = null;
171 return quit ();
174 static int main (string[] args) {
175 Error err = null;
177 var opt_context = new OptionContext ("- Vala API Generator");
178 opt_context.set_help_enabled (true);
179 opt_context.add_main_entries (options, null);
180 opt_context.parse (out args, out err);
182 if (err != null) {
183 return 1;
186 if (sources == null) {
187 stderr.printf ("No source file specified.\n");
188 return 1;
191 var vapigen = new VAPIGen ();
192 return vapigen.run ();