3 * Copyright (C) 2006-2009 Jürg Billeter
4 * Copyright (C) 1996-2002, 2004, 2005, 2006 Free Software Foundation, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Jürg Billeter <j@bitron.ch>
27 static string basedir
;
28 static string directory
;
30 [CCode (array_length
= false, array_null_terminated
= true)]
32 static string[] sources
;
33 [CCode (array_length
= false, array_null_terminated
= true)]
35 static string[] vapi_directories
;
36 static string library
;
37 [CCode (array_length
= false, array_null_terminated
= true)]
39 static string[] packages
;
40 static string target_glib
;
42 static bool ccode_only
;
43 static string header_filename
;
44 static bool compile_only
;
48 static bool disable_assert
;
49 static bool enable_checking
;
50 static bool deprecated
;
51 static bool experimental
;
52 static bool non_null_experimental
;
53 static bool disable_dbus_transformation
;
54 static string cc_command
;
55 [CCode (array_length
= false, array_null_terminated
= true)]
57 static string[] cc_options
;
58 static string dump_tree
;
59 static bool save_temps
;
60 static bool quiet_mode
;
62 private CodeContext context
;
64 const OptionEntry
[] options
= {
65 { "vapidir", 0, 0, OptionArg
.FILENAME_ARRAY
, ref vapi_directories
, "Look for package bindings in DIRECTORY", "DIRECTORY..." },
66 { "pkg", 0, 0, OptionArg
.STRING_ARRAY
, ref packages
, "Include binding for PACKAGE", "PACKAGE..." },
67 { "library", 0, 0, OptionArg
.STRING
, ref library
, "Library name", "NAME" },
68 { "basedir", 'b', 0, OptionArg
.FILENAME
, ref basedir
, "Base source directory", "DIRECTORY" },
69 { "directory", 'd', 0, OptionArg
.FILENAME
, ref directory
, "Output directory", "DIRECTORY" },
70 { "version", 0, 0, OptionArg
.NONE
, ref version
, "Display version number", null },
71 { "ccode", 'C', 0, OptionArg
.NONE
, ref ccode_only
, "Output C code", null },
72 { "header", 'H', 0, OptionArg
.FILENAME
, ref header_filename
, "Output C header file", "FILE" },
73 { "compile", 'c', 0, OptionArg
.NONE
, ref compile_only
, "Compile but do not link", null },
74 { "output", 'o', 0, OptionArg
.FILENAME
, ref output
, "Place output in file FILE", "FILE" },
75 { "debug", 'g', 0, OptionArg
.NONE
, ref debug
, "Produce debug information", null },
76 { "thread", 0, 0, OptionArg
.NONE
, ref thread
, "Enable multithreading support", null },
77 { "disable-assert", 0, 0, OptionArg
.NONE
, ref disable_assert
, "Disable assertions", null },
78 { "enable-checking", 0, 0, OptionArg
.NONE
, ref enable_checking
, "Enable additional run-time checks", null },
79 { "enable-deprecated", 0, 0, OptionArg
.NONE
, ref deprecated
, "Enable deprecated features", null },
80 { "enable-experimental", 0, 0, OptionArg
.NONE
, ref experimental
, "Enable experimental features", null },
81 { "enable-non-null-experimental", 0, 0, OptionArg
.NONE
, ref non_null_experimental
, "Enable experimental enhancements for non-null types", null },
82 { "disable-dbus-transformation", 0, 0, OptionArg
.NONE
, ref disable_dbus_transformation
, "Disable transformation of D-Bus member names", null },
83 { "cc", 0, 0, OptionArg
.STRING
, ref cc_command
, "Use COMMAND as C compiler command", "COMMAND" },
84 { "Xcc", 'X', 0, OptionArg
.STRING_ARRAY
, ref cc_options
, "Pass OPTION to the C compiler", "OPTION..." },
85 { "dump-tree", 0, 0, OptionArg
.FILENAME
, ref dump_tree
, "Write code tree to FILE", "FILE" },
86 { "save-temps", 0, 0, OptionArg
.NONE
, ref save_temps
, "Keep temporary files", null },
87 { "quiet", 'q', 0, OptionArg
.NONE
, ref quiet_mode
, "Do not print messages to the console", null },
88 { "target-glib", 0, 0, OptionArg
.STRING
, ref target_glib
, "Target version of glib for code generation", "MAJOR.MINOR" },
89 { "", 0, 0, OptionArg
.FILENAME_ARRAY
, ref sources
, null, "FILE..." },
94 if (context
.report
.get_errors () == 0 && context
.report
.get_warnings () == 0) {
97 if (context
.report
.get_errors () == 0) {
99 stdout
.printf ("Compilation succeeded - %d warning(s)\n", context
.report
.get_warnings ());
104 stdout
.printf ("Compilation failed: %d error(s), %d warning(s)\n", context
.report
.get_errors (), context
.report
.get_warnings ());
110 private bool add_package (CodeContext context
, string pkg
) {
111 if (context
.has_package (pkg
)) {
112 // ignore multiple occurences of the same package
116 var package_path
= context
.get_package_path (pkg
, vapi_directories
);
118 if (package_path
== null) {
122 context
.add_package (pkg
);
124 context
.add_source_file (new
SourceFile (context
, package_path
, true));
126 var deps_filename
= Path
.build_filename (Path
.get_dirname (package_path
), "%s.deps".printf (pkg
));
127 if (FileUtils
.test (deps_filename
, FileTest
.EXISTS
)) {
131 FileUtils
.get_contents (deps_filename
, out deps_content
, out deps_len
);
132 foreach (string dep
in deps_content
.split ("\n")) {
134 if (!add_package (context
, dep
)) {
135 Report
.error (null, "%s, dependency of %s, not found in specified Vala API directories".printf (dep
, pkg
));
139 } catch (FileError e
) {
140 Report
.error (null, "Unable to read dependency file: %s".printf (e
.message
));
148 context
= new
CodeContext ();
149 CodeContext
.push (context
);
151 // default to build executable
152 if (!ccode_only
&& !compile_only
&& output
== null) {
153 // strip extension if there is one
154 // else we use the default output file of the C compiler
155 if (sources
[0].rchr (-1, '.') != null) {
156 long dot
= sources
[0].pointer_to_offset (sources
[0].rchr (-1, '.'));
157 output
= Path
.get_basename (sources
[0].substring (0, dot
));
161 context
.library
= library
;
162 context
.assert
= !disable_assert
;
163 context
.checking
= enable_checking
;
164 context
.deprecated
= deprecated
;
165 context
.experimental
= experimental
;
166 context
.non_null_experimental
= non_null_experimental
;
167 context
.dbus_transformation
= !disable_dbus_transformation
;
168 context
.report
.set_verbose_errors (!quiet_mode
);
170 context
.ccode_only
= ccode_only
;
171 context
.compile_only
= compile_only
;
172 context
.header_filename
= header_filename
;
173 context
.output
= output
;
174 if (basedir
!= null) {
175 context
.basedir
= realpath (basedir
);
177 if (directory
!= null) {
178 context
.directory
= realpath (directory
);
180 context
.directory
= context
.basedir
;
182 context
.debug
= debug
;
183 context
.thread
= thread
;
184 context
.save_temps
= save_temps
;
188 if (target_glib
!= null && target_glib
.scanf ("%d.%d", out glib_major
, out glib_minor
) != 2) {
189 Report
.error (null, "Invalid format for --target-glib");
192 context
.target_glib_major
= glib_major
;
193 context
.target_glib_minor
= glib_minor
;
194 if (context
.target_glib_major
!= 2) {
195 Report
.error (null, "This version of valac only supports GLib 2");
198 context
.codegen
= new
CCodeGenerator ();
200 /* default packages */
201 if (!add_package (context
, "glib-2.0")) {
202 Report
.error (null, "glib-2.0 not found in specified Vala API directories");
204 if (!add_package (context
, "gobject-2.0")) {
205 Report
.error (null, "gobject-2.0 not found in specified Vala API directories");
208 if (packages
!= null) {
209 foreach (string package
in packages
) {
210 if (!add_package (context
, package
)) {
211 Report
.error (null, "%s not found in specified Vala API directories".printf (package
));
217 if (context
.report
.get_errors () > 0) {
221 foreach (string source
in sources
) {
222 if (FileUtils
.test (source
, FileTest
.EXISTS
)) {
223 var rpath
= realpath (source
);
224 if (source
.has_suffix (".vala") || source
.has_suffix (".gs")) {
225 var source_file
= new
SourceFile (context
, rpath
);
227 // import the GLib namespace by default (namespace of backend-specific standard library)
228 source_file
.add_using_directive (new
UsingDirective (new
UnresolvedSymbol (null, "GLib", null)));
230 context
.add_source_file (source_file
);
231 } else if (source
.has_suffix (".vapi")) {
232 context
.add_source_file (new
SourceFile (context
, rpath
, true));
233 } else if (source
.has_suffix (".c")) {
234 context
.add_c_source_file (rpath
);
236 Report
.error (null, "%s is not a supported source file type. Only .vala, .vapi, .gs, and .c files are supported.".printf (source
));
239 Report
.error (null, "%s not found".printf (source
));
244 if (context
.report
.get_errors () > 0) {
248 var parser
= new
Parser ();
249 parser
.parse (context
);
251 var genie_parser
= new Genie
.Parser ();
252 genie_parser
.parse (context
);
254 if (context
.report
.get_errors () > 0) {
258 var resolver
= new
SymbolResolver ();
259 resolver
.resolve (context
);
261 if (context
.report
.get_errors () > 0) {
265 var analyzer
= new
SemanticAnalyzer ();
266 analyzer
.analyze (context
);
268 if (dump_tree
!= null) {
269 var code_writer
= new
CodeWriter (true);
270 code_writer
.write_file (context
, dump_tree
);
273 if (context
.report
.get_errors () > 0) {
277 var flow_analyzer
= new
FlowAnalyzer ();
278 flow_analyzer
.analyze (context
);
280 if (context
.report
.get_errors () > 0) {
284 if (context
.non_null_experimental
) {
285 var null_checker
= new
NullChecker ();
286 null_checker
.check (context
);
288 if (context
.report
.get_errors () > 0) {
293 context
.codegen
.emit (context
);
295 if (context
.report
.get_errors () > 0) {
299 if (library
!= null) {
300 var interface_writer
= new
CodeWriter ();
301 string vapi_filename
= "%s.vapi".printf (library
);
303 // put .vapi file in current directory unless -d has been explicitly specified
304 if (directory
!= null && !Path
.is_absolute (vapi_filename
)) {
305 vapi_filename
= "%s%c%s".printf (context
.directory
, Path
.DIR_SEPARATOR
, vapi_filename
);
308 interface_writer
.write_file (context
, vapi_filename
);
311 var gir_writer
= new
GIRWriter ();
312 string gir_filename
= "%s.gir".printf (library
);
314 // put .gir file in current directory unless -d has been explicitly specified
315 if (directory
!= null && !Path
.is_absolute (gir_filename
)) {
316 gir_filename
= "%s%c%s".printf (context
.directory
, Path
.DIR_SEPARATOR
, gir_filename
);
319 gir_writer
.write_file (context
, gir_filename
);
326 var ccompiler
= new
CCodeCompiler ();
327 if (cc_command
== null && Environment
.get_variable ("CC") != null) {
328 cc_command
= Environment
.get_variable ("CC");
330 if (cc_options
== null) {
331 ccompiler
.compile (context
, cc_command
, new
string[] { });
333 ccompiler
.compile (context
, cc_command
, cc_options
);
340 private static bool ends_with_dir_separator (string s
) {
341 return Path
.is_dir_separator (s
.offset (s
.len () - 1).get_char ());
344 /* ported from glibc */
345 private static string realpath (string name
) {
348 // start of path component
350 // end of path component
353 if (!Path
.is_absolute (name
)) {
355 rpath
= Environment
.get_current_dir ();
359 // set start after root
360 start
= end
= Path
.skip_root (name
);
363 rpath
= name
.substring (0, name
.pointer_to_offset (start
));
366 long root_len
= rpath
.pointer_to_offset (Path
.skip_root (rpath
));
368 for (; start
.get_char () != 0; start
= end
) {
369 // skip sequence of multiple path-separators
370 while (Path
.is_dir_separator (start
.get_char ())) {
371 start
= start
.next_char ();
374 // find end of path component
376 for (end
= start
; end
.get_char () != 0 && !Path
.is_dir_separator (end
.get_char ()); end
= end
.next_char ()) {
382 } else if (len
== 1 && start
.get_char () == '.') {
384 } else if (len
== 2 && start
.has_prefix ("..")) {
385 // back up to previous component, ignore if at root already
386 if (rpath
.len () > root_len
) {
388 rpath
= rpath
.substring (0, rpath
.len () - 1);
389 } while (!ends_with_dir_separator (rpath
));
392 if (!ends_with_dir_separator (rpath
)) {
393 rpath
+= Path
.DIR_SEPARATOR_S
;
396 rpath
+= start
.substring (0, len
);
400 if (rpath
.len () > root_len
&& ends_with_dir_separator (rpath
)) {
401 rpath
= rpath
.substring (0, rpath
.len () - 1);
404 if (Path
.DIR_SEPARATOR
!= '/') {
405 // don't use backslashes internally,
406 // to avoid problems in #include directives
407 string[] components
= rpath
.split ("\\");
408 rpath
= string.joinv ("/", components
);
414 static int main (string[] args
) {
416 var opt_context
= new
OptionContext ("- Vala Compiler");
417 opt_context
.set_help_enabled (true);
418 opt_context
.add_main_entries (options
, null);
419 opt_context
.parse (ref args
);
420 } catch (OptionError e
) {
421 stdout
.printf ("%s\n", e
.message
);
422 stdout
.printf ("Run '%s --help' to see a full list of available command line options.\n", args
[0]);
427 stdout
.printf ("Vala %s\n", Config
.PACKAGE_VERSION
);
431 if (sources
== null) {
432 stderr
.printf ("No source file specified.\n");
436 var compiler
= new
Compiler ();
437 return compiler
.run ();