2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
5 * this only deals with the compiling (when builder is run with different args..)
13 public class ValaSourceReport : Vala.Report {
15 public string filepath;
17 public string tmpname;
19 //public Gee.ArrayList<ValaSourceNotice> notices;
20 public Json.Object result;
22 //public Gee.HashMap<int,string> line_errors;
24 public void compile_notice(string type, string filename, int line, string message) {
26 if (!this.result.has_member(type+"-TOTAL")) {
27 this.result.set_int_member(type+"-TOTAL", 1);
29 this.result.set_int_member(type+"-TOTAL",
30 this.result.get_int_member(type+"-TOTAL") +1
35 if (!this.result.has_member(type)) {
36 this.result.set_object_member(type, new Json.Object());
38 var t = this.result.get_object_member(type);
39 if (!t.has_member(filename)) {
40 t.set_object_member(filename, new Json.Object());
42 var tt = t.get_object_member(filename);
43 if (!tt.has_member(line.to_string())) {
44 tt.set_array_member(line.to_string(), new Json.Array());
46 var tl = tt.get_array_member(line.to_string());
47 tl.add_string_element(message);
53 public ValaSourceReport(string filepath, string tmpname)
56 this.filepath = filepath;
57 this.tmpname = tmpname;
58 this.result = new Json.Object();
59 this.result.set_boolean_member("success", true);
60 this.result.set_string_member("message", "");
64 //this.line_errors = new Gee.HashMap<int,string> ();
65 //this.notices = new Gee.ArrayList<ValaSourceNotice>();
68 public override void warn (Vala.SourceReference? source, string message) {
72 //stderr.printf ("My error: %s\n", message);
75 if (source.file.filename != this.tmpname) {
76 this.compile_notice("WARN", source.file.filename , source.begin.line, message);
79 this.compile_notice("WARN", this.filepath, source.begin.line, message);
82 public override void depr (Vala.SourceReference? source, string message) {
86 //stderr.printf ("My error: %s\n", message);
89 if (source.file.filename != this.tmpname) {
90 this.compile_notice("DEPR", source.file.filename, source.begin.line, message);
93 this.compile_notice("DEPR", this.filepath, source.begin.line, message);
97 public override void err (Vala.SourceReference? source, string message) {
101 //stderr.printf ("My error: %s\n", message);
103 if (source.file.filename != this.tmpname) {
104 this.compile_notice("ERR", source.file.filename, source.begin.line, message);
105 GLib.debug ("Other file: Got error error: %d: %s\n", source.begin.line, message);
110 this.compile_notice("ERR", this.filepath, source.begin.line, message);
111 GLib.debug ("Test file: Got error error: %d: %s\n", source.begin.line, message);
116 var iter = this.line_errors.map_iterator();
117 while (iter.next()) {
118 print ("%d : %s\n\n", iter.get_key(), iter.get_value());
125 public class ValaSourceCompiler : Object {
127 public static void jerr(string str)
129 var ret = new Json.Object();
130 ret.set_boolean_member("success", false);
131 ret.set_string_member("message", str);
133 var generator = new Json.Generator ();
134 var root = new Json.Node(Json.NodeType.OBJECT);
135 root.init_object(ret);
136 generator.set_root (root);
138 generator.pretty = true;
139 generator.indent = 4;
142 print("%s\n", generator.to_data (null));
143 GLib.Process.exit(Posix.EXIT_FAILURE);
147 public static void buildApplication()
149 //print("build based on Application settings\n");
151 if (BuilderApplication.opt_compile_target == null) {
152 jerr("missing compile target --target");
155 Project.Project.loadAll();
156 var proj = Project.Project.getProjectByHash(BuilderApplication.opt_compile_project);
159 jerr("could not load test project %s".printf( BuilderApplication.opt_compile_project));
162 if (proj.xtype != "Gtk") {
163 jerr("%s is not a Gtk Project".printf( BuilderApplication.opt_compile_project));
165 var gproj = (Project.Gtk)proj;
168 if (!gproj.compilegroups.has_key(BuilderApplication.opt_compile_target)) {
169 jerr("missing compile target %s".printf(BuilderApplication.opt_compile_target));
172 if (BuilderApplication.opt_compile_skip != null) {
173 skip_file = BuilderApplication.opt_compile_skip;
176 if (BuilderApplication.opt_compile_add != null) {
177 add_file = BuilderApplication.opt_compile_add;
181 var vs = new ValaSourceCompiler(gproj, add_file, BuilderApplication.opt_compile_target, skip_file);
182 if (BuilderApplication.opt_compile_output != null) {
183 vs.output = BuilderApplication.opt_compile_output;
190 Vala.CodeContext context;
191 ValaSourceReport report;
193 public string build_module;
194 public string filepath;
195 public string original_filepath;
196 public int line_offset = 0;
197 public string output;
199 // file.project , file.path, file.build_module, ""
200 public ValaSourceCompiler(Project.Gtk project, string filepath, string build_module, string original_filepath) {
203 this.filepath = filepath;
204 this.build_module = build_module;
205 this.original_filepath = original_filepath;
206 this.project = project;
210 public void dumpCode(string str)
212 var ls = str.split("\n");
213 for (var i=0;i < ls.length; i++) {
214 print("%d : %s\n", i+1, ls[i]);
220 public void compile( )
223 var valac = "valac " ;
225 context = new Vala.CodeContext ();
226 Vala.CodeContext.push (context);
228 context.experimental = false;
229 context.experimental_non_null = false;
241 for (int i = 2; i <= ver; i += 2) {
242 context.add_define ("VALA_0_%d".printf (i));
248 var vapidirs = this.project.vapidirs();
249 // order is important ...
250 vapidirs += Path.get_dirname (context.get_vapi_path("gee-1.0")) ; //usr/share/vala/vapi
251 vapidirs += Path.get_dirname (context.get_vapi_path("glib-2.0")) ; // usr/share/vala-XXX/vapi
253 for(var i =0 ; i < vapidirs.length; i++) {
254 valac += " --vapidir=" + vapidirs[i];
258 context.vapi_directories = vapidirs;
259 context.report.enable_warnings = true;
260 context.metadata_directories = { };
261 context.gir_directories = {};
262 context.thread = true;
265 this.report = new ValaSourceReport(this.original_filepath, this.filepath);
266 context.report = this.report;
269 context.basedir = "/tmp"; //Posix.realpath (".");
271 context.directory = context.basedir;
274 // add default packages:
275 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
276 context.profile = Vala.Profile.GOBJECT;
278 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
279 context.root.add_using_directive (ns_ref);
281 var source_file = new Vala.SourceFile (
283 Vala.SourceFileType.SOURCE,
286 source_file.add_using_directive (ns_ref);
287 context.add_source_file (source_file);
289 // add all the files (except the current one) - this.file.path
290 var pr = this.project;
291 if (this.build_module.length > 0) {
292 var cg = pr.compilegroups.get(this.build_module);
293 for (var i = 0; i < cg.sources.size; i++) {
294 var path = pr.resolve_path(
295 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
297 if (!FileUtils.test(path, FileTest.EXISTS)) {
300 // skip thie original
301 if (path == this.original_filepath) {
305 if (FileUtils.test(path, FileTest.IS_DIR)) {
308 //print("Add source file %s\n", path);
312 if (Regex.match_simple("\\.c$", path)) {
313 context.add_c_source_file(path);
318 var xsf = new Vala.SourceFile (
320 Vala.SourceFileType.SOURCE,
323 xsf.add_using_directive (ns_ref);
324 context.add_source_file(xsf);
328 // default.. packages..
329 context.add_external_package ("glib-2.0");
330 context.add_external_package ("gobject-2.0");
331 // user defined ones..
333 var dcg = pr.compilegroups.get("_default_");
334 for (var i = 0; i < dcg.packages.size; i++) {
335 valac += " --pkg " + dcg.packages.get(i);
336 if (!this.has_vapi(context.vapi_directories, dcg.packages.get(i))) {
337 GLib.debug("Skip vapi '%s' - does not exist", dcg.packages.get(i));
341 context.add_external_package (dcg.packages.get(i));
344 //Vala.Config.PACKAGE_SUFFIX.substring (1)
346 // add the modules...
348 context.output = this.output == "" ? "/tmp/testbuild" : this.output;
349 valac += " -o " +context.output;
350 GLib.debug("%s", valac);
355 //add_documented_files (context, settings.source_files);
357 Vala.Parser parser = new Vala.Parser ();
358 parser.parse (context);
359 //gir_parser.parse (context);
360 if (context.report.get_errors () > 0) {
361 Vala.CodeContext.pop ();
362 GLib.debug("parse got errors");
363 //((ValaSourceReport)context.report).dump();
364 this.report.result.set_boolean_member("success", false);
365 this.report.result.set_string_member("message", "Parse failed");
375 if (context.report.get_errors () > 0) {
376 Vala.CodeContext.pop ();
377 GLib.debug("check got errors");
378 //((ValaSourceReport)context.report).dump();
379 this.report.result.set_boolean_member("success", false);
380 this.report.result.set_string_member("message", "Check failed");
386 if (this.output == "") {
387 Vala.CodeContext.pop ();
392 context.codegen = new Vala.GDBusServerModule ();
396 context.codegen.emit (context);
398 var ccompiler = new Vala.CCodeCompiler ();
399 var cc_command = Environment.get_variable ("CC");
400 var pkg_config_command = Environment.get_variable ("PKG_CONFIG");
402 ccompiler.compile (context, cc_command, new string[] { }, pkg_config_command);
404 ccompiler.compile (context, cc_command, new string[] { });
408 Vala.CodeContext.pop ();
412 public bool has_vapi(string[] dirs, string vapi)
414 for(var i =0 ; i < dirs.length; i++) {
415 GLib.debug("check VAPI - %s", dirs[i] + "/" + vapi + ".vapi");
416 if (!FileUtils.test( dirs[i] + "/" + vapi + ".vapi", FileTest.EXISTS)) {
425 public void outputResult()
427 var generator = new Json.Generator ();
428 generator.indent = 1;
429 generator.pretty = true;
430 var node = new Json.Node(Json.NodeType.OBJECT);
431 node.set_object(this.report.result);
433 generator.set_root(node);
435 generator.pretty = true;
436 generator.indent = 4;
439 print("%s\n", generator.to_data (null));
440 GLib.Process.exit(Posix.EXIT_SUCCESS);
447 public Gee.HashMap<int,string> checkString(string contents)
450 var valac = "valac " ;
452 context = new Vala.CodeContext ();
453 Vala.CodeContext.push (context);
455 context.experimental = false;
456 context.experimental_non_null = false;
468 for (int i = 2; i <= ver; i += 2) {
469 context.add_define ("VALA_0_%d".printf (i));
478 var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
479 // what's the current version of vala???
482 vapidirs += Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
484 for(var i =0 ; i < vapidirs.length; i++) {
485 valac += " --vapidir=" + vapidirs[i];
489 // or context.get_vapi_path("glib-2.0"); // should return path..
490 context.vapi_directories = vapidirs;
491 context.report.enable_warnings = true;
492 context.metadata_directories = { };
493 context.gir_directories = {};
494 context.thread = true;
497 this.report = new ValaSourceReport(this.file.path);
498 context.report = this.report;
501 context.basedir = "/tmp"; //Posix.realpath (".");
503 context.directory = context.basedir;
506 // add default packages:
507 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
508 context.profile = Vala.Profile.GOBJECT;
510 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
511 context.root.add_using_directive (ns_ref);
513 var source_file = new Vala.SourceFile (
515 Vala.SourceFileType.SOURCE,
516 "~~~~~testfile.vala",
519 source_file.add_using_directive (ns_ref);
520 context.add_source_file (source_file);
522 // add all the files (except the current one) - this.file.path
523 var pr = ((Project.Gtk)this.file.project);
524 if (this.file.build_module.length > 0) {
525 var cg = pr.compilegroups.get(this.file.build_module);
526 for (var i = 0; i < cg.sources.size; i++) {
527 var path = pr.resolve_path(
528 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
530 if (!FileUtils.test(path, FileTest.EXISTS)) {
534 if (path == this.file.path.replace(".bjs", ".vala")) {
538 if (FileUtils.test(path, FileTest.IS_DIR)) {
541 //print("Add source file %s\n", path);
545 if (Regex.match_simple("\\.c$", path)) {
546 context.add_c_source_file(path);
551 var xsf = new Vala.SourceFile (
553 Vala.SourceFileType.SOURCE,
556 xsf.add_using_directive (ns_ref);
557 context.add_source_file(xsf);
561 // default.. packages..
562 context.add_external_package ("glib-2.0");
563 context.add_external_package ("gobject-2.0");
564 // user defined ones..
566 var dcg = pr.compilegroups.get("_default_");
567 for (var i = 0; i < dcg.packages.size; i++) {
568 valac += " --pkg " + dcg.packages.get(i);
569 context.add_external_package (dcg.packages.get(i));
572 //Vala.Config.PACKAGE_SUFFIX.substring (1)
574 // add the modules...
578 //context.add_external_package ("libvala-0.24");
580 this.report.compile_notice("START", "", 0, "");
583 //add_documented_files (context, settings.source_files);
585 Vala.Parser parser = new Vala.Parser ();
586 parser.parse (context);
587 //gir_parser.parse (context);
588 if (context.report.get_errors () > 0) {
589 print("parse got errors");
590 ((ValaSourceReport)context.report).dump();
592 Vala.CodeContext.pop ();
593 this.report.compile_notice("END", "", 0, "");
594 return this.report.line_errors;
601 if (context.report.get_errors () > 0) {
602 print("check got errors");
603 ((ValaSourceReport)context.report).dump();
604 Vala.CodeContext.pop ();
605 this.report.compile_notice("END", "", 0, "");
606 return this.report.line_errors;
610 //context.codegen = new Vala.GDBusServerModule ();
613 context.output = "/tmp/testbuild";
614 valac += " -o " +context.output;
615 //context.codegen.emit (context);
617 //(new Vala.CodeNode()).get_error_types().clear();
618 //(new Vala.NullType()).get_type_arguments().clear();
619 (new Vala.NullType(null)).get_type_arguments().clear();
621 this.report.compile_notice("END", "", 0, "");
622 print("%s\n", valac);
624 return this.report.line_errors;
633 int main (string[] args) {
635 var a = new ValaSource(file);
636 a.create_valac_tree();