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
20 * Jürg Billeter <j@bitron.ch>
26 static string directory
;
29 static string[] sources
;
31 static string[] vapi_directories
;
32 static string library
;
34 static string[] packages
;
35 static bool disable_memory_management
;
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..." },
49 if (Report
.get_errors () == 0) {
50 stdout
.printf ("Generation succeeded - %d warning(s)\n", Report
.get_warnings ());
53 stdout
.printf ("Generation failed: %d error(s), %d warning(s)\n", Report
.get_errors (), Report
.get_warnings ());
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
)) {
70 var filename
= Path
.build_filename ("/usr/local/share/vala/vapi", basename
, null);
71 if (FileUtils
.test (filename
, FileTest
.EXISTS
)) {
75 filename
= Path
.build_filename ("/usr/share/vala/vapi", basename
, null);
76 if (FileUtils
.test (filename
, FileTest
.EXISTS
)) {
83 private bool add_package (string! pkg
) {
84 var package_path
= get_package_path (pkg
);
86 if (package_path
== null) {
90 context
.add_source_file (new
SourceFile (context
, package_path
, true));
96 context
= new
CodeContext ();
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
));
112 if (Report
.get_errors () > 0) {
116 foreach (string source
in sources
) {
117 if (FileUtils
.test (source
, FileTest
.EXISTS
)) {
118 context
.add_source_file (new
SourceFile (context
, source
));
120 Report
.error (null, "%s not found".printf (source
));
125 if (Report
.get_errors () > 0) {
129 var parser
= new
Parser ();
130 parser
.parse (context
);
132 if (Report
.get_errors () > 0) {
136 var gidlparser
= new
GIdlParser ();
137 gidlparser
.parse (context
);
139 if (Report
.get_errors () > 0) {
143 var builder
= new
SymbolBuilder ();
144 builder
.build (context
);
146 if (Report
.get_errors () > 0) {
150 var attributeprocessor
= new
AttributeProcessor ();
151 attributeprocessor
.process (context
);
153 if (Report
.get_errors () > 0) {
157 var resolver
= new
SymbolResolver ();
158 resolver
.resolve (context
);
160 if (Report
.get_errors () > 0) {
164 if (library
!= null) {
165 var interface_writer
= new
InterfaceWriter ();
166 interface_writer
.write_file (context
, "%s.vala".printf (library
));
174 static int main (string[] args
) {
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
);
186 if (sources
== null) {
187 stderr
.printf ("No source file specified.\n");
191 var vapigen
= new
VAPIGen ();
192 return vapigen
.run ();