pre release fixes
[roobuilder] / src / Palete / ValaCompileRequest.vala
1
2
3
4 namespace Palete {
5          
6                 
7         public class ValaCompileRequest  : Object
8         {
9                 Project.Gtk project;
10                 string target;
11                 Spawn? spawn = null;
12                 
13                 public signal void onOutput(string str);
14         
15                 public Gee.HashMap<string,GLib.ListStore>? errorByType = null;
16                 public Gee.HashMap<string,GLib.ListStore>? errorByFile  = null;
17                         
18         
19                 public ValaCompileRequest (
20                         Project.Gtk project,
21                         string target
22                          
23                 ) {
24                         this.project =   project;
25                         this.target = target;
26                 }
27                  
28                  
29                 
30                   
31                 
32                 public async bool run()
33                 {
34                         //this.queue = queue;
35
36                         if ( this.target == "") {
37                                 GLib.debug("missing target");
38                                 
39
40                                 return false;
41                         }
42                         BuilderApplication.showSpinner("spinner", "running meson");
43
44                         var res = yield  this.runMeson();
45         
46                         if (0 != res) {
47                                 GLib.debug("Failed to run Meson");
48                                 BuilderApplication.showSpinner("");
49                                 return false;
50                         }
51                         BuilderApplication.showSpinner("spinner", "running ninja");
52                         res = yield this.runNinja();
53                         if (0 != res) {
54                                 GLib.debug("Failed to run ninja");
55                                 return false;
56                         }
57                         
58                         BuilderApplication.showSpinner("");
59                         return this.execResult();
60                           
61                 }
62                 
63                 async int runMeson() {
64                         if (GLib.FileUtils.test(this.project.path + "/build/meson-info", GLib.FileTest.EXISTS)) {
65                                 return 0; //assume it's been set up.
66                         }
67                         var exe = GLib.Environment.find_program_in_path( "meson");
68                         string[] args = { exe ,"setup", "build", "--prefix=/usr" };             
69                         GLib.debug("running meson");
70                         this.spawn = new Spawn(this.project.path , args);
71                         this.spawn.output_line.connect(( str) => {
72                                 this.onOutput(str);
73                         });
74                         var res = yield this.spawn.run_async();
75                         return res;
76                 }
77                         
78                 async int runNinja() 
79                 {
80                         if (!GLib.FileUtils.test(this.project.path + "/build", GLib.FileTest.EXISTS)) {
81                                 GLib.debug("build is missing");
82                                 return -1; //assume it's been set up.
83                         }
84                         var exe = GLib.Environment.find_program_in_path( "ninja");
85                         string[] args = { exe };                
86
87                         this.spawn = new Spawn(this.project.path + "/build" , args);
88                         this.spawn.output_line.connect(( str) => {
89                                 this.onOutput(str);
90                         });
91                          var res = yield this.spawn.run_async();
92                         return res;
93                         
94                 }       
95                 public void cancel() {
96                         if (this.spawn != null && this.spawn.pid > 0) {
97                                 Posix.kill(this.spawn.pid, 9);
98                         }
99                         this.spawn = null;
100                 
101                 }       
102                          
103                  
104                 /*
105                   
106                 public int totalErrors(string type, JsRender.JsRender? file=null) 
107                 {
108                         var ar = this.errorByType.get(type);
109                         if (ar == null) {
110                                 GLib.debug("by type has no eroros %s", type);
111                                 return 0;
112                         }
113                         
114                         
115                         var ret =0;
116                         
117                         for(var i =0 ;i< ar.get_n_items();i++) {
118                                 var ce = (CompileError) ar.get_item(i);
119                                 if (file == null) {
120                                         ret += (int)ce.lines.get_n_items();
121                                         GLib.debug("got lines type has no eroros %s", type);
122                                         continue;
123                                 }
124                                 
125                                 
126                                 if (ce.file.path == file.path) {
127                                         ret += (int)ce.lines.get_n_items();
128                                 }
129                         }
130                         return ret;
131                 }
132                 */
133                 
134                 
135                   
136                 public void killChildren(int pid)
137                 {
138                         if (pid < 1) {
139                                 return;
140                         }
141                         var cn = "/proc/%d/task/%d/children".printf(pid,pid);
142                         if (!FileUtils.test(cn, GLib.FileTest.EXISTS)) {
143                                 GLib.debug("%s doesnt exist - killing %d", cn, pid);
144                                  Posix.kill(pid, 9);
145                                 return;
146                         }
147                         string cpids = "";
148                         try {
149                                 FileUtils.get_contents(cn, out cpids);
150                         
151
152                                 if (cpids.length > 0) {
153                                         this.killChildren(int.parse(cpids));
154                                 }
155
156                         } catch (GLib.FileError e) {
157                                 // skip
158                         }
159                         GLib.debug("killing %d", pid);  
160                         //Posix.kill(pid, 9);
161                 }
162                 
163                 public int terminal_pid = 0;
164                 public bool execResult()
165                 {
166                                 
167                         this.killChildren(this.terminal_pid);
168                         this.terminal_pid = 0;
169                           
170                         var exe = this.target;
171                         var pr = (Project.Gtk) this.project;
172                         var cg =  pr.compilegroups.get(exe);
173                         
174                         var exbin = pr.path + "/build/" + exe;
175                         if (!GLib.FileUtils.test(exbin, GLib.FileTest.EXISTS)) {
176                                 GLib.debug("Missing output file: %s\n",exbin);
177                                 return false;
178                         }
179                         var gdb_cfg = pr.path + "/build/.gdb-script";
180                         if (!GLib.FileUtils.test(gdb_cfg, GLib.FileTest.EXISTS)) {
181                                 pr.writeFile("build/.gdb-script", "set debuginfod enabled off\nr");
182                         }
183                         var gdb = GLib.Environment.find_program_in_path( "gdb"); 
184                         var term = GLib.Environment.find_program_in_path( "gnome-terminal"); 
185                         
186                         string[] args = @"$term --disable-factory --wait -- $gdb -x".split(" ");
187
188                         args+= gdb_cfg;
189  
190                         args += exbin;
191                         if (cg.execute_args.length > 0) {
192                                 args+= "--args";
193                                 var aa = cg.execute_args.split(" ");
194                                 for (var i =0; i < aa.length; i++) {
195                                         args += aa[i];
196                                 }
197                         }
198
199                   
200                     
201                     // should be home directory...
202                     
203                     
204                     try {
205                     
206                         var exec = new Spawn(pr.path , args);
207                         exec.env = GLib.Environ.get();
208                          
209                         exec.detach = true;
210                                 exec.run(); 
211
212                                 this.terminal_pid = exec.pid;
213                                 GLib.debug("Child PID = %d", this.terminal_pid);
214                                 
215                     } catch(GLib.Error e) {
216                                 GLib.debug("Failed to spawn: %s", e.message);
217                                 return false;
218                         }
219                         return true;
220                         
221                 }
222                 
223         } 
224                 
225                 
226                 
227 }
228
229