libgda-4.0, gedit-2.20: Fix gedit typo and GdaXaTransactionId.data
[vala-lang.git] / compiler / valacompiler.vala
blobeab72c4a4ecb15039da83e1e0d7b378f18d7707d
1 /* valacompiler.vala
3 * Copyright (C) 2006-2010 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
20 * Author:
21 * Jürg Billeter <j@bitron.ch>
24 using GLib;
26 class Vala.Compiler {
27 static string basedir;
28 static string directory;
29 static bool version;
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 vapi_filename;
39 static string library;
40 static string gir;
41 [CCode (array_length = false, array_null_terminated = true)]
42 static string[] packages;
43 [CCode (array_length = false, array_null_terminated = true)]
44 static string[] fast_vapis;
45 static string target_glib;
47 static bool ccode_only;
48 static string header_filename;
49 static bool use_header;
50 static string internal_header_filename;
51 static string internal_vapi_filename;
52 static string fast_vapi_filename;
53 static string symbols_filename;
54 static string includedir;
55 static bool compile_only;
56 static string output;
57 static bool debug;
58 static bool thread;
59 static bool mem_profiler;
60 static bool disable_assert;
61 static bool enable_checking;
62 static bool deprecated;
63 static bool experimental;
64 static bool experimental_non_null;
65 static bool disable_warnings;
66 static string cc_command;
67 [CCode (array_length = false, array_null_terminated = true)]
68 static string[] cc_options;
69 static string dump_tree;
70 static bool save_temps;
71 [CCode (array_length = false, array_null_terminated = true)]
72 static string[] defines;
73 static bool quiet_mode;
74 static bool verbose_mode;
75 static string profile;
76 static bool nostdpkg;
77 static bool enable_version_header;
78 static bool disable_version_header;
79 static bool fatal_warnings;
80 static string dependencies;
82 static string entry_point;
84 static bool run_output;
86 private CodeContext context;
88 const OptionEntry[] options = {
89 { "vapidir", 0, 0, OptionArg.FILENAME_ARRAY, ref vapi_directories, "Look for package bindings in DIRECTORY", "DIRECTORY..." },
90 { "girdir", 0, 0, OptionArg.FILENAME_ARRAY, ref gir_directories, "Look for .gir files in DIRECTORY", "DIRECTORY..." },
91 { "metadatadir", 0, 0, OptionArg.FILENAME_ARRAY, ref metadata_directories, "Look for GIR .metadata files in DIRECTORY", "DIRECTORY..." },
92 { "pkg", 0, 0, OptionArg.STRING_ARRAY, ref packages, "Include binding for PACKAGE", "PACKAGE..." },
93 { "vapi", 0, 0, OptionArg.FILENAME, ref vapi_filename, "Output VAPI file name", "FILE" },
94 { "library", 0, 0, OptionArg.STRING, ref library, "Library name", "NAME" },
95 { "gir", 0, 0, OptionArg.STRING, ref gir, "GObject-Introspection repository file name", "NAME-VERSION.gir" },
96 { "basedir", 'b', 0, OptionArg.FILENAME, ref basedir, "Base source directory", "DIRECTORY" },
97 { "directory", 'd', 0, OptionArg.FILENAME, ref directory, "Output directory", "DIRECTORY" },
98 { "version", 0, 0, OptionArg.NONE, ref version, "Display version number", null },
99 { "ccode", 'C', 0, OptionArg.NONE, ref ccode_only, "Output C code", null },
100 { "header", 'H', 0, OptionArg.FILENAME, ref header_filename, "Output C header file", "FILE" },
101 { "use-header", 0, 0, OptionArg.NONE, ref use_header, "Use C header file", null },
102 { "includedir", 0, 0, OptionArg.FILENAME, ref includedir, "Directory used to include the C header file", "DIRECTORY" },
103 { "internal-header", 'h', 0, OptionArg.FILENAME, ref internal_header_filename, "Output internal C header file", "FILE" },
104 { "internal-vapi", 0, 0, OptionArg.FILENAME, ref internal_vapi_filename, "Output vapi with internal api", "FILE" },
105 { "fast-vapi", 0, 0, OptionArg.STRING, ref fast_vapi_filename, "Output vapi without performing symbol resolution", null },
106 { "use-fast-vapi", 0, 0, OptionArg.STRING_ARRAY, ref fast_vapis, "Use --fast-vapi output during this compile", null },
107 { "deps", 0, 0, OptionArg.STRING, ref dependencies, "Write make-style dependency information to this file", null },
108 { "symbols", 0, 0, OptionArg.FILENAME, ref symbols_filename, "Output symbols file", "FILE" },
109 { "compile", 'c', 0, OptionArg.NONE, ref compile_only, "Compile but do not link", null },
110 { "output", 'o', 0, OptionArg.FILENAME, ref output, "Place output in file FILE", "FILE" },
111 { "debug", 'g', 0, OptionArg.NONE, ref debug, "Produce debug information", null },
112 { "thread", 0, 0, OptionArg.NONE, ref thread, "Enable multithreading support", null },
113 { "enable-mem-profiler", 0, 0, OptionArg.NONE, ref mem_profiler, "Enable GLib memory profiler", null },
114 { "define", 'D', 0, OptionArg.STRING_ARRAY, ref defines, "Define SYMBOL", "SYMBOL..." },
115 { "main", 0, 0, OptionArg.STRING, ref entry_point, "Use SYMBOL as entry point", "SYMBOL..." },
116 { "nostdpkg", 0, 0, OptionArg.NONE, ref nostdpkg, "Do not include standard packages", null },
117 { "disable-assert", 0, 0, OptionArg.NONE, ref disable_assert, "Disable assertions", null },
118 { "enable-checking", 0, 0, OptionArg.NONE, ref enable_checking, "Enable additional run-time checks", null },
119 { "enable-deprecated", 0, 0, OptionArg.NONE, ref deprecated, "Enable deprecated features", null },
120 { "enable-experimental", 0, 0, OptionArg.NONE, ref experimental, "Enable experimental features", null },
121 { "disable-warnings", 0, 0, OptionArg.NONE, ref disable_warnings, "Disable warnings", null },
122 { "fatal-warnings", 0, 0, OptionArg.NONE, ref fatal_warnings, "Treat warnings as fatal", null },
123 { "enable-experimental-non-null", 0, 0, OptionArg.NONE, ref experimental_non_null, "Enable experimental enhancements for non-null types", null },
124 { "cc", 0, 0, OptionArg.STRING, ref cc_command, "Use COMMAND as C compiler command", "COMMAND" },
125 { "Xcc", 'X', 0, OptionArg.STRING_ARRAY, ref cc_options, "Pass OPTION to the C compiler", "OPTION..." },
126 { "dump-tree", 0, 0, OptionArg.FILENAME, ref dump_tree, "Write code tree to FILE", "FILE" },
127 { "save-temps", 0, 0, OptionArg.NONE, ref save_temps, "Keep temporary files", null },
128 { "profile", 0, 0, OptionArg.STRING, ref profile, "Use the given profile instead of the default", "PROFILE" },
129 { "quiet", 'q', 0, OptionArg.NONE, ref quiet_mode, "Do not print messages to the console", null },
130 { "verbose", 'v', 0, OptionArg.NONE, ref verbose_mode, "Print additional messages to the console", null },
131 { "target-glib", 0, 0, OptionArg.STRING, ref target_glib, "Target version of glib for code generation", "MAJOR.MINOR" },
132 { "enable-version-header", 0, 0, OptionArg.NONE, ref enable_version_header, "Write vala build version in generated files", null },
133 { "disable-version-header", 0, 0, OptionArg.NONE, ref disable_version_header, "Do not write vala build version in generated files", null },
134 { "", 0, 0, OptionArg.FILENAME_ARRAY, ref sources, null, "FILE..." },
135 { null }
138 private int quit () {
139 if (context.report.get_errors () == 0 && context.report.get_warnings () == 0) {
140 return 0;
142 if (context.report.get_errors () == 0 && (!fatal_warnings || context.report.get_warnings () == 0)) {
143 if (!quiet_mode) {
144 stdout.printf ("Compilation succeeded - %d warning(s)\n", context.report.get_warnings ());
146 return 0;
147 } else {
148 if (!quiet_mode) {
149 stdout.printf ("Compilation failed: %d error(s), %d warning(s)\n", context.report.get_errors (), context.report.get_warnings ());
151 return 1;
155 private int run () {
156 context = new CodeContext ();
157 CodeContext.push (context);
159 // default to build executable
160 if (!ccode_only && !compile_only && output == null) {
161 // strip extension if there is one
162 // else we use the default output file of the C compiler
163 if (sources[0].last_index_of_char ('.') != -1) {
164 int dot = sources[0].last_index_of_char ('.');
165 output = Path.get_basename (sources[0].substring (0, dot));
169 context.assert = !disable_assert;
170 context.checking = enable_checking;
171 context.deprecated = deprecated;
172 context.experimental = experimental;
173 context.experimental_non_null = experimental_non_null;
174 context.report.enable_warnings = !disable_warnings;
175 context.report.set_verbose_errors (!quiet_mode);
176 context.verbose_mode = verbose_mode;
177 context.version_header = !disable_version_header;
179 context.ccode_only = ccode_only;
180 context.compile_only = compile_only;
181 context.header_filename = header_filename;
182 if (header_filename == null && use_header) {
183 Report.error (null, "--use-header may only be used in combination with --header");
185 context.use_header = use_header;
186 context.internal_header_filename = internal_header_filename;
187 context.symbols_filename = symbols_filename;
188 context.includedir = includedir;
189 context.output = output;
190 if (basedir == null) {
191 context.basedir = CodeContext.realpath (".");
192 } else {
193 context.basedir = CodeContext.realpath (basedir);
195 if (directory != null) {
196 context.directory = CodeContext.realpath (directory);
197 } else {
198 context.directory = context.basedir;
200 context.vapi_directories = vapi_directories;
201 context.gir_directories = gir_directories;
202 context.metadata_directories = metadata_directories;
203 context.debug = debug;
204 context.thread = thread;
205 context.mem_profiler = mem_profiler;
206 context.save_temps = save_temps;
207 if (profile == "posix") {
208 context.profile = Profile.POSIX;
209 context.add_define ("POSIX");
210 } else if (profile == "gobject-2.0" || profile == "gobject" || profile == null) {
211 // default profile
212 context.profile = Profile.GOBJECT;
213 context.add_define ("GOBJECT");
214 } else if (profile == "dova") {
215 context.profile = Profile.DOVA;
216 context.add_define ("DOVA");
217 } else {
218 Report.error (null, "Unknown profile %s".printf (profile));
220 nostdpkg |= fast_vapi_filename != null;
221 context.nostdpkg = nostdpkg;
223 context.entry_point_name = entry_point;
225 context.run_output = run_output;
227 if (defines != null) {
228 foreach (string define in defines) {
229 context.add_define (define);
233 for (int i = 2; i <= 14; i += 2) {
234 context.add_define ("VALA_0_%d".printf (i));
237 if (context.profile == Profile.POSIX) {
238 if (!nostdpkg) {
239 /* default package */
240 context.add_external_package ("posix");
242 } else if (context.profile == Profile.GOBJECT) {
243 int glib_major = 2;
244 int glib_minor = 16;
245 if (target_glib != null && target_glib.scanf ("%d.%d", out glib_major, out glib_minor) != 2) {
246 Report.error (null, "Invalid format for --target-glib");
249 context.target_glib_major = glib_major;
250 context.target_glib_minor = glib_minor;
251 if (context.target_glib_major != 2) {
252 Report.error (null, "This version of valac only supports GLib 2");
255 for (int i = 16; i <= glib_minor; i += 2) {
256 context.add_define ("GLIB_2_%d".printf (i));
259 if (!nostdpkg) {
260 /* default packages */
261 context.add_external_package ("glib-2.0");
262 context.add_external_package ("gobject-2.0");
264 } else if (context.profile == Profile.DOVA) {
265 if (!nostdpkg) {
266 /* default package */
267 context.add_external_package ("dova-core-0.1");
271 if (packages != null) {
272 foreach (string package in packages) {
273 context.add_external_package (package);
275 packages = null;
278 if (fast_vapis != null) {
279 foreach (string vapi in fast_vapis) {
280 var rpath = CodeContext.realpath (vapi);
281 var source_file = new SourceFile (context, SourceFileType.FAST, rpath);
282 context.add_source_file (source_file);
286 if (context.report.get_errors () > 0 || (fatal_warnings && context.report.get_warnings () > 0)) {
287 return quit ();
290 if (context.profile == Profile.GOBJECT) {
291 context.codegen = new GDBusServerModule ();
292 } else if (context.profile == Profile.DOVA) {
293 context.codegen = new DovaErrorModule ();
294 } else {
295 context.codegen = new CCodeDelegateModule ();
298 bool has_c_files = false;
300 foreach (string source in sources) {
301 if (context.add_source_filename (source, run_output)) {
302 if (source.has_suffix (".c")) {
303 has_c_files = true;
307 sources = null;
309 if (context.report.get_errors () > 0 || (fatal_warnings && context.report.get_warnings () > 0)) {
310 return quit ();
313 var parser = new Parser ();
314 parser.parse (context);
316 var genie_parser = new Genie.Parser ();
317 genie_parser.parse (context);
319 var gir_parser = new GirParser ();
320 gir_parser.parse (context);
322 if (context.report.get_errors () > 0 || (fatal_warnings && context.report.get_warnings () > 0)) {
323 return quit ();
326 if (fast_vapi_filename != null) {
327 var interface_writer = new CodeWriter (CodeWriterType.FAST);
328 interface_writer.write_file (context, fast_vapi_filename);
329 return quit ();
332 context.check ();
334 if (context.report.get_errors () > 0 || (fatal_warnings && context.report.get_warnings () > 0)) {
335 return quit ();
338 if (!ccode_only && !compile_only && library == null) {
339 // building program, require entry point
340 if (!has_c_files && context.entry_point == null) {
341 Report.error (null, "program does not contain a static `main' method");
345 if (dump_tree != null) {
346 var code_writer = new CodeWriter (CodeWriterType.DUMP);
347 code_writer.write_file (context, dump_tree);
350 if (context.report.get_errors () > 0 || (fatal_warnings && context.report.get_warnings () > 0)) {
351 return quit ();
354 context.codegen.emit (context);
356 if (context.report.get_errors () > 0 || (fatal_warnings && context.report.get_warnings () > 0)) {
357 return quit ();
360 if (vapi_filename == null && library != null) {
361 // keep backward compatibility with --library option
362 vapi_filename = "%s.vapi".printf (library);
365 if (library != null) {
366 if (gir != null) {
367 if (context.profile == Profile.GOBJECT) {
368 long gir_len = gir.length;
369 int last_hyphen = gir.last_index_of_char ('-');
371 if (last_hyphen == -1 || !gir.has_suffix (".gir")) {
372 Report.error (null, "GIR file name `%s' is not well-formed, expected NAME-VERSION.gir".printf (gir));
373 } else {
374 string gir_namespace = gir.substring (0, last_hyphen);
375 string gir_version = gir.substring (last_hyphen + 1, gir_len - last_hyphen - 5);
376 gir_version.canon ("0123456789.", '?');
377 if (gir_namespace == "" || gir_version == "" || !gir_version[0].isdigit () || gir_version.contains ("?")) {
378 Report.error (null, "GIR file name `%s' is not well-formed, expected NAME-VERSION.gir".printf (gir));
379 } else {
380 var gir_writer = new GIRWriter ();
382 // put .gir file in current directory unless -d has been explicitly specified
383 string gir_directory = ".";
384 if (directory != null) {
385 gir_directory = context.directory;
388 gir_writer.write_file (context, gir_directory, gir_namespace, gir_version, library);
393 gir = null;
396 library = null;
399 // The GIRWriter places the gir_namespace and gir_version into the top namespace, so write the vapi after that stage
400 if (vapi_filename != null) {
401 var interface_writer = new CodeWriter ();
403 // put .vapi file in current directory unless -d has been explicitly specified
404 if (directory != null && !Path.is_absolute (vapi_filename)) {
405 vapi_filename = "%s%c%s".printf (context.directory, Path.DIR_SEPARATOR, vapi_filename);
408 interface_writer.write_file (context, vapi_filename);
411 if (internal_vapi_filename != null) {
412 if (internal_header_filename == null ||
413 header_filename == null) {
414 Report.error (null, "--internal-vapi may only be used in combination with --header and --internal-header");
415 return quit();
418 var interface_writer = new CodeWriter (CodeWriterType.INTERNAL);
419 interface_writer.set_cheader_override(header_filename, internal_header_filename);
420 string vapi_filename = internal_vapi_filename;
422 // put .vapi file in current directory unless -d has been explicitly specified
423 if (directory != null && !Path.is_absolute (vapi_filename)) {
424 vapi_filename = "%s%c%s".printf (context.directory, Path.DIR_SEPARATOR, vapi_filename);
427 interface_writer.write_file (context, vapi_filename);
429 internal_vapi_filename = null;
432 if (dependencies != null) {
433 context.write_dependencies (dependencies);
436 if (!ccode_only) {
437 var ccompiler = new CCodeCompiler ();
438 if (cc_command == null && Environment.get_variable ("CC") != null) {
439 cc_command = Environment.get_variable ("CC");
441 if (cc_options == null) {
442 ccompiler.compile (context, cc_command, new string[] { });
443 } else {
444 ccompiler.compile (context, cc_command, cc_options);
448 return quit ();
451 static int run_source (string[] args) {
452 int i = 1;
453 if (args[i] != null && args[i].has_prefix ("-")) {
454 try {
455 string[] compile_args;
456 Shell.parse_argv ("valac " + args[1], out compile_args);
458 var opt_context = new OptionContext ("- Vala");
459 opt_context.set_help_enabled (true);
460 opt_context.add_main_entries (options, null);
461 unowned string[] temp_args = compile_args;
462 opt_context.parse (ref temp_args);
463 } catch (ShellError e) {
464 stdout.printf ("%s\n", e.message);
465 return 1;
466 } catch (OptionError e) {
467 stdout.printf ("%s\n", e.message);
468 stdout.printf ("Run '%s --help' to see a full list of available command line options.\n", args[0]);
469 return 1;
472 i++;
475 if (args[i] == null) {
476 stderr.printf ("No source file specified.\n");
477 return 1;
480 sources = { args[i] };
481 output = "%s/%s.XXXXXX".printf (Environment.get_tmp_dir (), Path.get_basename (args[i]));
482 int outputfd = FileUtils.mkstemp (output);
483 if (outputfd < 0) {
484 return 1;
487 run_output = true;
488 disable_warnings = true;
489 quiet_mode = true;
491 var compiler = new Compiler ();
492 int ret = compiler.run ();
493 if (ret != 0) {
494 return ret;
497 FileUtils.close (outputfd);
498 if (FileUtils.chmod (output, 0700) != 0) {
499 FileUtils.unlink (output);
500 return 1;
503 string[] target_args = { output };
504 while (i < args.length) {
505 target_args += args[i];
506 i++;
509 try {
510 Pid pid;
511 var loop = new MainLoop ();
512 int child_status = 0;
514 Process.spawn_async (null, target_args, null, SpawnFlags.CHILD_INHERITS_STDIN | SpawnFlags.DO_NOT_REAP_CHILD | SpawnFlags.FILE_AND_ARGV_ZERO, null, out pid);
516 FileUtils.unlink (output);
517 ChildWatch.add (pid, (pid, status) => {
518 child_status = (status & 0xff00) >> 8;
519 loop.quit ();
522 loop.run ();
524 return child_status;
525 } catch (SpawnError e) {
526 stdout.printf ("%s\n", e.message);
527 return 1;
531 static int main (string[] args) {
532 // initialize locale
533 Intl.setlocale (LocaleCategory.ALL, "");
535 if (Path.get_basename (args[0]) == "vala" || Path.get_basename (args[0]) == "vala" + Config.PACKAGE_SUFFIX) {
536 return run_source (args);
539 try {
540 var opt_context = new OptionContext ("- Vala Compiler");
541 opt_context.set_help_enabled (true);
542 opt_context.add_main_entries (options, null);
543 opt_context.parse (ref args);
544 } catch (OptionError e) {
545 stdout.printf ("%s\n", e.message);
546 stdout.printf ("Run '%s --help' to see a full list of available command line options.\n", args[0]);
547 return 1;
550 if (version) {
551 stdout.printf ("Vala %s\n", Config.BUILD_VERSION);
552 return 0;
555 if (sources == null && fast_vapis == null) {
556 stderr.printf ("No source file specified.\n");
557 return 1;
560 var compiler = new Compiler ();
561 return compiler.run ();