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;
242 for (int i = 2; i <= ver; i += 2) {
243 context.add_define ("VALA_0_%d".printf (i));
249 var vapidirs = this.project.vapidirs();
250 // order is important ...
251 vapidirs += Path.get_dirname (context.get_vapi_path("gee-1.0")) ; //usr/share/vala/vapi
252 vapidirs += Path.get_dirname (context.get_vapi_path("glib-2.0")) ; // usr/share/vala-XXX/vapi
254 for(var i =0 ; i < vapidirs.length; i++) {
255 valac += " --vapidir=" + vapidirs[i];
259 context.vapi_directories = vapidirs;
260 context.report.enable_warnings = true;
261 context.metadata_directories = { };
262 context.gir_directories = {};
263 context.thread = true;
264 valac += " --thread ";
266 // we should parse the compilegroup to find out the flags..
267 context.debug = true;
270 this.report = new ValaSourceReport(this.original_filepath, this.filepath);
271 context.report = this.report;
273 valac += " -b " + GLib.Environment.get_home_dir() + " ";
274 context.basedir = GLib.Environment.get_home_dir(); //Posix.realpath (".");
276 context.directory = context.basedir;
279 // add default packages:
280 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
281 context.profile = Vala.Profile.GOBJECT;
283 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
284 context.root.add_using_directive (ns_ref);
286 var source_file = new Vala.SourceFile (
288 Vala.SourceFileType.SOURCE,
291 source_file.add_using_directive (ns_ref);
292 context.add_source_file (source_file);
294 // add all the files (except the current one) - this.file.path
295 var pr = this.project;
298 if (this.build_module.length > 0) {
299 var cg = pr.compilegroups.get(this.build_module);
300 if (this.output.length < 1 && cg.target_bin.length > 0) {
301 this.output = cg.target_bin;
305 for (var i = 0; i < cg.sources.size; i++) {
306 var path = pr.resolve_path(
307 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
309 if (!FileUtils.test(path, FileTest.EXISTS)) {
312 // skip thie original
313 if (path == this.original_filepath) {
317 if (FileUtils.test(path, FileTest.IS_DIR)) {
320 //print("Add source file %s\n", path);
324 if (Regex.match_simple("\\.c$", relpath)) {
325 context.add_c_source_file(path);
330 var xsf = new Vala.SourceFile (
332 Vala.SourceFileType.SOURCE,
335 xsf.add_using_directive (ns_ref);
336 context.add_source_file(xsf);
341 print("%s\n", valac);
342 // default.. packages..
343 context.add_external_package ("glib-2.0");
344 context.add_external_package ("gobject-2.0");
345 // user defined ones..
347 var dcg = pr.compilegroups.get("_default_");
348 for (var i = 0; i < dcg.packages.size; i++) {
350 var pkg = dcg.packages.get(i);
351 // do not add libvala versions except the one that matches the one we are compiled against..
352 if (Regex.match_simple("^libvala", pkg) && pkg != ("libvala-0." + ver.to_string())) {
355 valac += " --pkg " + dcg.packages.get(i);
356 if (!this.has_vapi(context.vapi_directories, dcg.packages.get(i))) {
357 GLib.debug("Skip vapi '%s' - does not exist", dcg.packages.get(i));
361 context.add_external_package (dcg.packages.get(i));
364 //Vala.Config.PACKAGE_SUFFIX.substring (1)
366 // add the modules...
368 context.output = this.output == "" ? "/tmp/testrun" : this.output;
369 valac += " -o " + context.output;
370 GLib.debug("%s", valac);
372 context.target_glib_major = 2;
373 context.target_glib_minor = 32;
374 valac += " --target-glib=2.32 ";
376 //add_documented_files (context, settings.source_files);
378 Vala.Parser parser = new Vala.Parser ();
379 parser.parse (context);
380 //gir_parser.parse (context);
381 if (context.report.get_errors () > 0) {
382 Vala.CodeContext.pop ();
383 GLib.debug("parse got errors");
384 //((ValaSourceReport)context.report).dump();
385 this.report.result.set_boolean_member("success", false);
386 this.report.result.set_string_member("message", "Parse failed");
396 if (context.report.get_errors () > 0) {
397 Vala.CodeContext.pop ();
398 GLib.debug("check got errors");
399 //((ValaSourceReport)context.report).dump();
400 this.report.result.set_boolean_member("success", false);
401 this.report.result.set_string_member("message", "Check failed");
407 if (this.output == "") {
408 Vala.CodeContext.pop ();
413 context.codegen = new Vala.GDBusServerModule ();
416 context.codegen.emit (context);
418 var ccompiler = new Vala.CCodeCompiler ();
419 var cc_command = Environment.get_variable ("CC");
422 string [] cc_options = { "-lm" };
425 #if VALA_0_28 || VALA_0_30
426 var pkg_config_command = Environment.get_variable ("PKG_CONFIG");
427 ccompiler.compile (context, cc_command, cc_options, pkg_config_command);
429 ccompiler.compile (context, cc_command, cc_options);
432 print("%s\n", valac);
433 Vala.CodeContext.pop ();
434 GLib.Process.exit(Posix.EXIT_SUCCESS);
435 //this.outputResult();
438 public bool has_vapi(string[] dirs, string vapi)
440 for(var i =0 ; i < dirs.length; i++) {
441 GLib.debug("check VAPI - %s", dirs[i] + "/" + vapi + ".vapi");
442 if (!FileUtils.test( dirs[i] + "/" + vapi + ".vapi", FileTest.EXISTS)) {
451 public void outputResult()
453 var generator = new Json.Generator ();
454 generator.indent = 1;
455 generator.pretty = true;
456 var node = new Json.Node(Json.NodeType.OBJECT);
457 node.set_object(this.report.result);
459 generator.set_root(node);
461 generator.pretty = true;
462 generator.indent = 4;
465 print("%s\n", generator.to_data (null));
466 GLib.Process.exit(Posix.EXIT_SUCCESS);
474 int main (string[] args) {
476 var a = new ValaSource(file);
477 a.create_valac_tree();