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
20 * Jürg Billeter <j@bitron.ch>
25 class Vala
.VAPIGen
: Object
{
26 static string directory
;
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
;
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..." },
55 if (context
.report
.get_errors () == 0) {
57 stdout
.printf ("Generation succeeded - %d warning(s)\n", context
.report
.get_warnings ());
62 stdout
.printf ("Generation failed: %d error(s), %d warning(s)\n", context
.report
.get_errors (), context
.report
.get_warnings ());
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
);
78 context
.add_external_package ("glib-2.0");
79 context
.add_external_package ("gobject-2.0");
81 if (context
.report
.get_errors () > 0) {
85 /* load packages from .deps file */
86 foreach (string source
in sources
) {
87 if (!source
.has_suffix (".gi")) {
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) {
99 // depsfile for gir case
100 if (library
!= null) {
101 var depsfile
= library
+ ".deps";
102 context
.add_packages_from_file (depsfile
);
104 Report
.error (null, "--library option must be specified");
107 if (context
.report
.get_errors () > 0) {
111 if (packages
!= null) {
112 foreach (string package
in packages
) {
113 context
.add_external_package (package
);
118 if (context
.report
.get_errors () > 0) {
122 foreach (string source
in sources
) {
123 if (FileUtils
.test (source
, FileTest
.EXISTS
)) {
124 context
.add_source_file (new
SourceFile (context
, SourceFileType
.PACKAGE
, source
));
126 Report
.error (null, "%s not found".printf (source
));
130 if (context
.report
.get_errors () > 0) {
134 var parser
= new
Parser ();
135 parser
.parse (context
);
137 if (context
.report
.get_errors () > 0) {
141 var girparser
= new
GirParser ();
142 girparser
.parse (context
);
144 if (context
.report
.get_errors () > 0) {
148 var gidlparser
= new
GIdlParser ();
149 gidlparser
.parse (context
);
151 if (context
.report
.get_errors () > 0) {
157 if (context
.report
.get_errors () > 0) {
161 // interface writer ignores external packages
162 foreach (SourceFile file
in context
.get_source_files ()) {
163 if (file
.filename
.has_suffix (".vapi")) {
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
);
189 static int main (string[] args
) {
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]);
202 stdout
.printf ("Vala API Generator %s\n", Config
.BUILD_VERSION
);
206 if (sources
== null) {
207 stderr
.printf ("No source file specified.\n");
211 var vapigen
= new
VAPIGen ();
212 return vapigen
.run ();