resources/RooUsage.txt
[app.Builder.js] / src / Palete / ValaSourceCompiler.vala
1
2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
3
4 /**
5  *  this only deals with the compiling (when builder is run with different args..)
6  * 
7  */
8
9
10 namespace Palete {
11         
12          
13         public class ValaSourceReport  : Vala.Report {
14
15                 public string filepath;
16                 
17                 public string tmpname;
18                 
19                 //public Gee.ArrayList<ValaSourceNotice> notices;
20                 public Json.Object result;
21                  
22                 //public Gee.HashMap<int,string> line_errors;
23                 
24                 public void  compile_notice(string type, string filename, int line, string message) {
25                          
26                          if (!this.result.has_member(type+"-TOTAL")) {
27                                  this.result.set_int_member(type+"-TOTAL", 1);
28                          } else {
29                                 this.result.set_int_member(type+"-TOTAL", 
30                                         this.result.get_int_member(type+"-TOTAL") +1 
31                                 );
32                          }
33                          
34                          
35                          if (!this.result.has_member(type)) {
36                                  this.result.set_object_member(type, new Json.Object());
37                          }
38                          var t = this.result.get_object_member(type);
39                          if (!t.has_member(filename)) {
40                                  t.set_object_member(filename, new Json.Object());
41                          }
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());
45                          }
46                          var tl = tt.get_array_member(line.to_string());
47                          tl.add_string_element(message);
48                          
49                 }
50                 
51                 
52          
53                 public ValaSourceReport(string filepath, string tmpname)
54                 {
55                         base();
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", "");
61                         
62                         
63                         
64                         //this.line_errors = new Gee.HashMap<int,string> ();
65                         //this.notices = new Gee.ArrayList<ValaSourceNotice>();
66                 }
67                 
68                 public override void warn (Vala.SourceReference? source, string message) {
69                          
70                         if (source == null) {
71                                 return;
72                                 //stderr.printf ("My error: %s\n", message);
73                         }
74                         
75                         if (source.file.filename != this.tmpname) {
76                                 this.compile_notice("WARN", source.file.filename , source.begin.line, message);
77                                 return;
78                         }
79                         this.compile_notice("WARN", this.filepath, source.begin.line, message);
80                         
81                 }
82                 public override void depr (Vala.SourceReference? source, string message) {
83                          
84                         if (source == null) {
85                                 return;
86                                 //stderr.printf ("My error: %s\n", message);
87                         }
88                         
89                         if (source.file.filename != this.tmpname) {
90                                 this.compile_notice("DEPR", source.file.filename, source.begin.line, message);
91                                 return;
92                         }
93                         this.compile_notice("DEPR",  this.filepath, source.begin.line, message);
94                         
95                 }
96                 
97                 public override void err (Vala.SourceReference? source, string message) {
98                         errors++;
99                         if (source == null) {
100                                 return;
101                                 //stderr.printf ("My error: %s\n", message);
102                         }
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);
106                                 return;
107                         }
108                          
109                          
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);
112                 }
113                 /*
114                 public void dump()
115                 {
116                         var iter = this.line_errors.map_iterator();
117                         while (iter.next()) {
118                                 print ("%d : %s\n\n", iter.get_key(), iter.get_value());
119                         }
120                 }
121                 */
122
123         }
124
125         public class ValaSourceCompiler : Object {
126
127                 public static void jerr(string str)
128                 {
129                         var ret = new Json.Object(); 
130                         ret.set_boolean_member("success", false);
131                         ret.set_string_member("message", str);
132                         
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);
137                          
138                         generator.pretty = true;
139                         generator.indent = 4;
140                  
141
142                         print("%s\n",  generator.to_data (null));
143                         GLib.Process.exit(Posix.EXIT_FAILURE);
144                         
145                 }
146
147                 public static void buildApplication()
148                 {
149                         //print("build based on Application settings\n");
150                         
151                         if (BuilderApplication.opt_compile_target == null) {
152                                 jerr("missing compile target --target");
153                         }
154                         
155                         Project.Project.loadAll();
156                         var proj = Project.Project.getProjectByHash(BuilderApplication.opt_compile_project);
157                         
158                         if (proj == null) {
159                                 jerr("could not load test project %s".printf( BuilderApplication.opt_compile_project));
160                         }
161                         
162                         if (proj.xtype != "Gtk") {
163                                 jerr("%s is not a Gtk Project".printf( BuilderApplication.opt_compile_project));
164                         }
165                         var gproj = (Project.Gtk)proj;
166                         
167                         
168                         if (!gproj.compilegroups.has_key(BuilderApplication.opt_compile_target)) {
169                                 jerr("missing compile target %s".printf(BuilderApplication.opt_compile_target));
170                         }
171                         var skip_file = "";
172                         if (BuilderApplication.opt_compile_skip != null) {
173                                 skip_file = BuilderApplication.opt_compile_skip;
174                         }
175                         var add_file = "";
176                         if (BuilderApplication.opt_compile_add != null) {
177                                 add_file = BuilderApplication.opt_compile_add;
178                         }
179                         
180                         
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;
184                         }
185                         vs.compile();
186                         
187                         
188                 }
189
190                 Vala.CodeContext context;
191                 ValaSourceReport report;
192                 Project.Gtk project;
193                 public string build_module;
194                 public string filepath;
195                 public string original_filepath;
196                 public int line_offset = 0;
197                 public string output;
198                 
199                 // file.project , file.path, file.build_module, ""
200                 public ValaSourceCompiler(Project.Gtk project, string filepath, string build_module, string original_filepath) {
201                         base();
202                         //this.file = file;
203                         this.filepath = filepath;
204                         this.build_module = build_module;
205                         this.original_filepath = original_filepath;
206                         this.project =  project;
207                         this.output = "";
208                         
209                 }
210                 public void dumpCode(string str) 
211                 {
212                         var ls = str.split("\n");
213                         for (var i=0;i < ls.length; i++) {
214                                 print("%d : %s\n", i+1, ls[i]);
215                         }
216                 }
217                 
218                  
219                 
220                 public void compile( )
221                 {
222                         // init context:
223                         var valac = "valac " ;
224                         
225                         context = new Vala.CodeContext ();
226                         Vala.CodeContext.push (context);
227                 
228                         context.experimental = false;
229                         context.experimental_non_null = false;
230 #if VALA_0_32
231                         var ver=32;
232 #elif VALA_0_30
233                         var ver=30;
234 #elif VALA_0_28
235                         var ver=28;
236 #elif VALA_0_26 
237                         var ver=26;
238 #elif VALA_0_24
239                         var ver=24;
240 #elif VALA_0_22 
241                         var ver=22;
242 #endif
243                         
244                         for (int i = 2; i <= ver; i += 2) {
245                                 context.add_define ("VALA_0_%d".printf (i));
246                         }
247                         
248                         
249                         
250                          
251                         var vapidirs = this.project.vapidirs();
252                         // order is important ...
253                          vapidirs +=  Path.get_dirname (context.get_vapi_path("gee-1.0")) ; //usr/share/vala/vapi 
254                          vapidirs +=  Path.get_dirname (context.get_vapi_path("glib-2.0")) ; // usr/share/vala-XXX/vapi
255                                                         
256                         for(var i =0 ; i < vapidirs.length; i++) {
257                                 valac += " --vapidir=" + vapidirs[i];
258                         }
259                                 
260                         
261                         context.vapi_directories = vapidirs;
262                         context.report.enable_warnings = true;
263                         context.metadata_directories = { };
264                         context.gir_directories = {};
265                         context.thread = true;
266                         valac += " --thread ";
267                         
268                         // we should parse the compilegroup to find out the flags..
269                         context.debug = true;
270                         valac += " -g ";
271                         
272                         this.report = new ValaSourceReport(this.original_filepath, this.filepath);
273                         context.report = this.report;
274                         
275                         valac += " -b  " + GLib.Environment.get_home_dir() + " ";
276                         context.basedir = GLib.Environment.get_home_dir(); //Posix.realpath (".");
277                 
278                         context.directory = null; //??? causes target to end up in the right place at present..
279                 
280
281                         // add default packages:
282                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
283                         context.profile = Vala.Profile.GOBJECT;
284                          
285                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
286                         context.root.add_using_directive (ns_ref);
287
288                         var source_file = new Vala.SourceFile (
289                                 context, 
290                                 Vala.SourceFileType.SOURCE, 
291                                         this.filepath 
292                         );
293                         source_file.add_using_directive (ns_ref);
294                         context.add_source_file (source_file);
295                         
296                 // add all the files (except the current one) - this.file.path
297                 var pr = this.project;
298
299                 
300                 if (this.build_module.length > 0) {
301                                 var cg =  pr.compilegroups.get(this.build_module);
302                                 if (this.output.length < 1 && cg.target_bin.length > 0) {
303                                         this.output = cg.target_bin;
304                                 }
305                                 
306
307                                 for (var i = 0; i < cg.sources.size; i++) {
308                                         var path = pr.resolve_path(
309                                                         pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
310                                                         
311                                         if (!FileUtils.test(path, FileTest.EXISTS)) {
312                                                 continue;
313                                         }       
314                         // skip thie original
315                                         if (path == this.original_filepath) {
316                                                 valac += " " + path;
317                                                 continue;
318                                         }
319                                         if (FileUtils.test(path, FileTest.IS_DIR)) {
320                                                 continue;
321                                         }
322                                         //print("Add source file %s\n", path);
323                                         
324                                         valac += " " + path;
325                                         
326                                         if (Regex.match_simple("\\.c$", path)) {
327                                                 context.add_c_source_file(path);
328                                                 continue;
329                                         }
330                                         
331                                         
332                                         var xsf = new Vala.SourceFile (
333                                                 context,
334                                                 Vala.SourceFileType.SOURCE, 
335                                                 path
336                                         );
337                                         xsf.add_using_directive (ns_ref);
338                                         context.add_source_file(xsf);
339                                         
340                                 }
341                         }
342                         
343                         // print("%s\n", valac); -
344                         // default.. packages..
345                         context.add_external_package ("glib-2.0"); 
346                         context.add_external_package ("gobject-2.0");
347                         // user defined ones..
348                         
349                 var dcg = pr.compilegroups.get("_default_");
350                 for (var i = 0; i < dcg.packages.size; i++) {
351                 
352                         var pkg = dcg.packages.get(i);
353                         // do not add libvala versions except the one that matches the one we are compiled against..
354                         if (Regex.match_simple("^libvala", pkg) && pkg != ("libvala-0." + ver.to_string())) {
355                                 continue;
356                         }
357                                 valac += " --pkg " + dcg.packages.get(i);
358                                 if (!this.has_vapi(context.vapi_directories, dcg.packages.get(i))) {
359                                         GLib.debug("Skip vapi '%s' - does not exist", dcg.packages.get(i));
360                                         continue;
361                                 }
362                                 
363                                 context.add_external_package (dcg.packages.get(i));
364                         }
365                 
366                          //Vala.Config.PACKAGE_SUFFIX.substring (1)
367                         
368                         // add the modules...
369                         
370                         context.output = this.output == "" ? "/tmp/testrun" : this.output;
371                         valac += " -o " + context.output;
372                         GLib.debug("%s", valac);
373                         
374                         context.target_glib_major = 2;
375                         context.target_glib_minor = 32;
376                         valac += " --target-glib=2.32 ";
377                 
378                         //add_documented_files (context, settings.source_files);
379                 
380                         Vala.Parser parser = new Vala.Parser ();
381                         parser.parse (context);
382                         //gir_parser.parse (context);
383                         if (context.report.get_errors () > 0) {
384                                 Vala.CodeContext.pop ();
385                                 GLib.debug("parse got errors");
386                                 //((ValaSourceReport)context.report).dump();
387                                 this.report.result.set_boolean_member("success", false);
388                                 this.report.result.set_string_member("message", "Parse failed");
389                                 
390                                 this.outputResult();
391                                 return;
392                         }
393
394
395                         
396                         // check context:
397                         context.check ();
398                         if (context.report.get_errors () > 0) {
399                                 Vala.CodeContext.pop ();
400                                 GLib.debug("check got errors");
401                                 //((ValaSourceReport)context.report).dump();
402                                 this.report.result.set_boolean_member("success", false);
403                                 this.report.result.set_string_member("message", "Check failed");
404                                 
405                                 this.outputResult();
406                                 return;
407                         }
408                         
409                         if (this.output == "") {
410                                 Vala.CodeContext.pop ();
411                                 this.outputResult();
412                                 return;
413                         }
414                          
415                         context.codegen = new Vala.GDBusServerModule ();
416                          
417                         
418                         context.codegen.emit (context);
419                         
420                         var ccompiler = new Vala.CCodeCompiler ();
421                         var cc_command = Environment.get_variable ("CC");
422                         
423                         
424                         string [] cc_options = { "-lm", "-pg" };
425                         valac += " -X -lm -X -pg";
426                         
427 #if VALA_0_28 || VALA_0_30 || VALA_0_32
428                         var pkg_config_command = Environment.get_variable ("PKG_CONFIG");
429                         ccompiler.compile (context, cc_command, cc_options, pkg_config_command);
430 #else
431                         ccompiler.compile (context, cc_command, cc_options);
432 #endif
433                 
434                         print("%s\n", valac);
435                         Vala.CodeContext.pop ();
436                         GLib.Process.exit(Posix.EXIT_SUCCESS);
437                         //this.outputResult();
438                 
439                 }
440                 public bool has_vapi(string[] dirs,  string vapi) 
441                 {
442                         for(var i =0 ; i < dirs.length; i++) {
443                                 GLib.debug("check VAPI - %s", dirs[i] + "/" + vapi + ".vapi");
444                                 if (!FileUtils.test( dirs[i] + "/" + vapi + ".vapi", FileTest.EXISTS)) {
445                                         continue;
446                                 }   
447                                 return true;
448                         }
449                         return false;
450                         
451                 }
452                 
453                 public void outputResult()
454                 {
455                         var generator = new Json.Generator ();
456                     generator.indent = 1;
457                     generator.pretty = true;
458                     var node = new Json.Node(Json.NodeType.OBJECT);
459                     node.set_object(this.report.result);
460                     
461                     generator.set_root(node);
462                          
463                         generator.pretty = true;
464                         generator.indent = 4;
465                  
466
467                         print("%s\n",  generator.to_data (null));
468                         GLib.Process.exit(Posix.EXIT_SUCCESS);
469                         
470                          
471                 }
472  
473         }
474 }
475 /*
476 int main (string[] args) {
477
478         var a = new ValaSource(file);
479         a.create_valac_tree();
480         return 0;
481 }
482 */
483
484