]> git.tdb.fi Git - builder.git/blob - source/builder.cpp
Separate the command-line interface into its own class
[builder.git] / source / builder.cpp
1 #include <set>
2 #include <msp/core/maputils.h>
3 #include <msp/datafile/parser.h>
4 #include <msp/fs/dir.h>
5 #include <msp/fs/utils.h>
6 #include <msp/io/buffered.h>
7 #include <msp/io/file.h>
8 #include <msp/io/print.h>
9 #include <msp/strings/format.h>
10 #include <msp/time/units.h>
11 #include <msp/time/utils.h>
12 #include "binarypackage.h"
13 #include "builder.h"
14 #include "copy.h"
15 #include "datatool.h"
16 #include "gnuarchiver.h"
17 #include "gnuccompiler.h"
18 #include "gnucxxcompiler.h"
19 #include "gnulinker.h"
20 #include "installedfile.h"
21 #include "mingwdlltool.h"
22 #include "package.h"
23 #include "pkgconfiggenerator.h"
24 #include "sharedlibrary.h"
25 #include "sourcepackage.h"
26 #include "tar.h"
27 #include "task.h"
28 #include "virtualtarget.h"
29
30 using namespace std;
31 using namespace Msp;
32
33 Builder::Builder():
34         package_manager(*this),
35         native_arch(*this, string()),
36         current_arch(0),
37         build_type(0),
38         vfs(*this),
39         build_graph(*this),
40         logger(&default_logger),
41         tempdir("temp"),
42         top_loader(0)
43 {
44         set_architecture(string());
45 }
46
47 Builder::~Builder()
48 {
49         if(current_arch!=&native_arch)
50                 delete current_arch;
51 }
52
53 void Builder::set_architecture(const string &name)
54 {
55         if(name.empty())
56         {
57                 current_arch = &native_arch;
58                 prefix = FS::get_home_dir()/"local";
59         }
60         else
61         {
62                 current_arch = new Architecture(*this, name);
63                 prefix = FS::get_home_dir()/"local"/current_arch->get_name();
64         }
65 }
66
67 void Builder::set_build_type(const string &name)
68 {
69         build_type = &get_item(build_types, name);
70 }
71
72 void Builder::set_prefix(const FS::Path &p)
73 {
74         prefix = p;
75 }
76
77 void Builder::set_temp_directory(const FS::Path &p)
78 {
79         tempdir = p;
80 }
81
82 void Builder::add_default_tools()
83 {
84         toolchain.add_tool(new GnuCCompiler(*this, *current_arch));
85         toolchain.add_tool(new GnuCxxCompiler(*this, *current_arch));
86         toolchain.add_tool(new GnuLinker(*this, *current_arch));
87         toolchain.add_tool(new GnuArchiver(*this, *current_arch));
88         toolchain.add_tool(new Copy(*this));
89         toolchain.add_tool(new Tar(*this));
90         toolchain.add_tool(new PkgConfigGenerator(*this));
91         if(current_arch->get_system()=="windows")
92                 toolchain.add_tool(new MingwDllTool(*this, *current_arch));
93         toolchain.add_tool(new DataTool(*this));
94 }
95
96 void Builder::set_logger(const Logger *l)
97 {
98         logger = (l ? l : &default_logger);
99 }
100
101 void Builder::problem(const string &p, const string &d)
102 {
103         problems.push_back(Problem(p, d));
104 }
105
106 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
107 {
108         IO::BufferedFile in(fn.str());
109
110         get_logger().log("files", format("Reading %s", fn));
111
112         DataFile::Parser parser(in, fn.str());
113         Loader loader(*this, opts, all);
114         loader.load(parser);
115 }
116
117 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
118 {
119         unsigned total = build_graph.count_rebuild_targets();
120
121         if(!total)
122         {
123                 get_logger().log("summary", "Already up to date");
124                 return 0;
125         }
126         get_logger().log("summary", format("Will build %d target%s", total, (total!=1 ? "s" : "")));
127
128         vector<Task *> tasks;
129
130         unsigned count = 0;
131
132         bool fail = false;
133         bool finish = false;
134         bool starved = false;
135
136         while(!finish)
137         {
138                 if(tasks.size()<jobs && !fail && !starved)
139                 {
140                         Target *tgt = build_graph.get_buildable_target();
141                         if(tgt)
142                         {
143                                 if(tgt->get_tool())
144                                         get_logger().log("tasks", format("%-4s  %s", tgt->get_tool()->get_tag(), tgt->get_name()));
145                                 Task *task = tgt->build();
146                                 if(task)
147                                 {
148                                         get_logger().log("commands", format("%s", task->get_command()));
149                                         if(dry_run)
150                                         {
151                                                 task->signal_finished.emit(true);
152                                                 delete task;
153                                         }
154                                         else
155                                         {
156                                                 task->start();
157                                                 tasks.push_back(task);
158                                         }
159                                 }
160
161                                 if(show_progress)
162                                         IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
163                         }
164                         else if(tasks.empty())
165                                 finish = true;
166                         else
167                                 starved = true;
168                 }
169                 else
170                         Time::sleep(10*Time::msec);
171
172                 for(unsigned i=0; i<tasks.size();)
173                 {
174                         Task::Status status;
175                         if(jobs==1 || (tasks.size()==1 && starved))
176                                 status = tasks[i]->wait();
177                         else
178                                 status = tasks[i]->check();
179
180                         if(status!=Task::RUNNING)
181                         {
182                                 ++count;
183
184                                 delete tasks[i];
185                                 tasks.erase(tasks.begin()+i);
186                                 if(status==Task::ERROR)
187                                         fail = true;
188                                 if(tasks.empty() && fail)
189                                         finish = true;
190                                 starved = false;
191                         }
192                         else
193                                 ++i;
194                 }
195         }
196
197         if(show_progress)
198                 IO::print("\033[K");
199         if(fail)
200                 get_logger().log("summary", "Build failed");
201         else if(show_progress)
202                 get_logger().log("summary", "Build complete");
203
204         if(!dry_run)
205         {
206                 const PackageManager::PackageMap &packages = package_manager.get_packages();
207                 for(PackageManager::PackageMap::const_iterator i=packages.begin(); i!=packages.end(); ++i)
208                         i->second->save_caches();
209         }
210
211         return fail;
212 }
213
214 int Builder::clean(bool all, bool dry_run)
215 {
216         // Cleaning doesn't care about ordering, so a simpler method can be used
217
218         set<Target *> clean_tgts;
219         list<Target *> queue;
220         queue.push_back(build_graph.get_target("cmdline"));
221
222         while(!queue.empty())
223         {
224                 Target *tgt = queue.front();
225                 queue.erase(queue.begin());
226
227                 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
228                         clean_tgts.insert(tgt);
229
230                 const Target::Dependencies &deps = tgt->get_dependencies();
231                 for(list<Target *>::const_iterator i=deps.begin(); i!=deps.end(); ++i)
232                         if(!clean_tgts.count(*i))
233                                 queue.push_back(*i);
234         }
235
236         for(set<Target *>::iterator i=clean_tgts.begin(); i!=clean_tgts.end(); ++i)
237         {
238                 get_logger().log("tasks", format("RM    %s", (*i)->get_name()));
239                 if(!dry_run)
240                         (*i)->clean();
241         }
242
243         return 0;
244 }
245
246
247 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
248         DataFile::ObjectLoader<Builder>(b),
249         options(o),
250         conf_all(a)
251 {
252         add("architecture", &Loader::architecture);
253         add("binary_package", &Loader::binpkg);
254         add("build_type", &Loader::build_type);
255         add("profile", &Loader::profile);
256         add("package", &Loader::package);
257
258         if(!obj.top_loader)
259                 obj.top_loader = this;
260         else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
261                 options = obj.top_loader->options;
262 }
263
264 Builder::Loader::~Loader()
265 {
266         if(obj.top_loader==this)
267                 obj.top_loader = 0;
268 }
269
270 void Builder::Loader::architecture(const string &n)
271 {
272         if(obj.current_arch->match_name(n))
273                 load_sub(*obj.current_arch);
274 }
275
276 void Builder::Loader::binpkg(const string &n)
277 {
278         BinaryPackage *pkg = new BinaryPackage(obj, n);
279         load_sub(*pkg);
280 }
281
282 void Builder::Loader::build_type(const string &n)
283 {
284         BuildType btype(n);
285         load_sub(btype);
286         BuildTypeMap::iterator i = obj.build_types.insert(BuildTypeMap::value_type(n, btype)).first;
287         if(!obj.build_type)
288                 obj.build_type = &i->second;
289 }
290
291 void Builder::Loader::profile(const string &)
292 {
293         IO::print("Profiles are deprecated\n");
294 }
295
296 void Builder::Loader::package(const string &n)
297 {
298         SourcePackage *pkg = new SourcePackage(obj, n, get_source());
299
300         if(options)
301                 load_sub(*pkg, *options);
302         else
303                 load_sub(*pkg);
304
305         if(obj.build_type)
306                 pkg->set_build_type(*obj.build_type);
307 }