change run to use meson/ninja and then exec. - remove libvala code from application...
[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", GLib.FileTest.EXISTS)) {
65                                 return 0; //assume it's been set up.
66                         }
67                         string[] args = { "/usr/bin/meson" ,"setup","build", "--prefix=/" };            
68
69                         this.spawn = new Spawn(this.project.path , args);
70                         this.spawn.output_line.connect(( str) => {
71                                 this.onOutput(str);
72                         });
73                         var res = yield this.spawn.run_async();
74                         return res;
75                 }
76                         
77                 async int runNinja() 
78                 {
79                         if (!GLib.FileUtils.test(this.project.path + "/build", GLib.FileTest.EXISTS)) {
80                                 GLib.debug("build is missing");
81                                 return -1; //assume it's been set up.
82                         }
83                         string[] args = { "/usr/bin/ninja"};            
84
85                         this.spawn = new Spawn(this.project.path + "/build" , args);
86                         this.spawn.output_line.connect(( str) => {
87                                 this.onOutput(str);
88                         });
89                          var res = yield this.spawn.run_async();
90                         return res;
91                         
92                 }       
93                 public void cancel() {
94                         if (this.spawn != null && this.spawn.pid > 0) {
95                                 Posix.kill(this.spawn.pid, 9);
96                         }
97                         this.spawn = null;
98                 
99                 }       
100                          
101                  
102                 /*
103                   
104                 public int totalErrors(string type, JsRender.JsRender? file=null) 
105                 {
106                         var ar = this.errorByType.get(type);
107                         if (ar == null) {
108                                 GLib.debug("by type has no eroros %s", type);
109                                 return 0;
110                         }
111                         
112                         
113                         var ret =0;
114                         
115                         for(var i =0 ;i< ar.get_n_items();i++) {
116                                 var ce = (CompileError) ar.get_item(i);
117                                 if (file == null) {
118                                         ret += (int)ce.lines.get_n_items();
119                                         GLib.debug("got lines type has no eroros %s", type);
120                                         continue;
121                                 }
122                                 
123                                 
124                                 if (ce.file.path == file.path) {
125                                         ret += (int)ce.lines.get_n_items();
126                                 }
127                         }
128                         return ret;
129                 }
130                 */
131                 
132                 
133                   
134                 public void killChildren(int pid)
135                 {
136                         if (pid < 1) {
137                                 return;
138                         }
139                         var cn = "/proc/%d/task/%d/children".printf(pid,pid);
140                         if (!FileUtils.test(cn, GLib.FileTest.EXISTS)) {
141                                 GLib.debug("%s doesnt exist - killing %d", cn, pid);
142                                  Posix.kill(pid, 9);
143                                 return;
144                         }
145                         string cpids = "";
146                         try {
147                                 FileUtils.get_contents(cn, out cpids);
148                         
149
150                                 if (cpids.length > 0) {
151                                         this.killChildren(int.parse(cpids));
152                                 }
153
154                         } catch (GLib.FileError e) {
155                                 // skip
156                         }
157                         GLib.debug("killing %d", pid);  
158                         //Posix.kill(pid, 9);
159                 }
160                 
161                 public int terminal_pid = 0;
162                 public bool execResult()
163                 {
164                                 
165                         this.killChildren(this.terminal_pid);
166                         this.terminal_pid = 0;
167                           
168                         var exe = this.target;
169                         var pr = (Project.Gtk) this.project;
170                         var cg =  pr.compilegroups.get(exe);
171                         
172                         var exbin = pr.path + "/build/" + exe;
173                         if (!GLib.FileUtils.test(exbin, GLib.FileTest.EXISTS)) {
174                                 GLib.debug("Missing output file: %s\n",exbin);
175                                 return false;
176                         }
177                         var gdb_cfg = pr.path + "/build/.gdb-script";
178                         if (!GLib.FileUtils.test(gdb_cfg, GLib.FileTest.EXISTS)) {
179                                 pr.writeFile("build/.gdb-script", "set debuginfod enabled off\nr");
180                         }
181                          
182                         
183                         string[] args = "/usr/bin/gnome-terminal --disable-factory --wait -- /usr/bin/gdb -x".split(" ");
184
185                         args+= gdb_cfg;
186  
187                         args += exbin;
188                         if (cg.execute_args.length > 0) {
189                                 args+= "--args";
190                                 var aa = cg.execute_args.split(" ");
191                                 for (var i =0; i < aa.length; i++) {
192                                         args += aa[i];
193                                 }
194                         }
195
196                   
197                     
198                     // should be home directory...
199                     
200                     
201                     try {
202                     
203                         var exec = new Spawn(pr.path , args);
204                         exec.env = GLib.Environ.get();
205                          
206                         exec.detach = true;
207                                 exec.run(); 
208
209                                 this.terminal_pid = exec.pid;
210                                 GLib.debug("Child PID = %d", this.terminal_pid);
211                                 
212                     } catch(GLib.Error e) {
213                                 GLib.debug("Failed to spawn: %s", e.message);
214                                 return false;
215                         }
216                         return true;
217                         
218                 }
219                 
220         } 
221                 
222                 
223                 
224 }
225
226