]> git.tdb.fi Git - builder.git/blob - source/builder.cpp
Remove deprecated features
[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 list<string> Builder::collect_problems() const
102 {
103         list<string> problems;
104         set<const Package *> broken_packages;
105         set<const Component *> broken_components;
106         set<const Tool *> broken_tools;
107
108         const BuildGraph::TargetMap &targets = build_graph.get_targets();
109         for(BuildGraph::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
110                 if(i->second->is_broken())
111                 {
112                         const list<string> &tgt_problems = i->second->get_problems();
113                         for(list<string>::const_iterator j=tgt_problems.begin(); j!=tgt_problems.end(); ++j)
114                                 problems.push_back(format("%s: %s", i->second->get_name(), *j));
115
116                         const Package *package = i->second->get_package();
117                         if(package && !package->get_problems().empty())
118                                 broken_packages.insert(package);
119
120                         const Component *component = i->second->get_component();
121                         if(component && !component->get_problems().empty())
122                                 broken_components.insert(component);
123
124                         const Tool *tool = i->second->get_tool();
125                         if(tool && !tool->get_problems().empty())
126                                 broken_tools.insert(tool);
127                 }
128
129         // TODO Sort components after their packages, and targets last
130         for(set<const Package *>::const_iterator i=broken_packages.begin(); i!=broken_packages.end(); ++i)
131         {
132                 const list<string> &pkg_problems = (*i)->get_problems();
133                 for(list<string>::const_iterator j=pkg_problems.begin(); j!=pkg_problems.end(); ++j)
134                         problems.push_back(format("%s: %s", (*i)->get_name(), *j));
135         }
136
137         for(set<const Component *>::const_iterator i=broken_components.begin(); i!=broken_components.end(); ++i)
138         {
139                 const list<string> &comp_problems = (*i)->get_problems();
140                 for(list<string>::const_iterator j=comp_problems.begin(); j!=comp_problems.end(); ++j)
141                         problems.push_back(format("%s/%s: %s", (*i)->get_package().get_name(), (*i)->get_name(), *j));
142         }
143
144         for(set<const Tool *>::const_iterator i=broken_tools.begin(); i!=broken_tools.end(); ++i)
145         {
146                 const list<string> &tool_problems = (*i)->get_problems();
147                 for(list<string>::const_iterator j=tool_problems.begin(); j!=tool_problems.end(); ++j)
148                         problems.push_back(format("%s: %s", (*i)->get_tag(), *j));
149         }
150
151         return problems;
152 }
153
154 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
155 {
156         IO::BufferedFile in(fn.str());
157
158         get_logger().log("files", format("Reading %s", fn));
159
160         DataFile::Parser parser(in, fn.str());
161         Loader loader(*this, opts, all);
162         loader.load(parser);
163 }
164
165 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
166 {
167         unsigned total = build_graph.count_rebuild_targets();
168
169         if(!total)
170         {
171                 get_logger().log("summary", "Already up to date");
172                 return 0;
173         }
174         get_logger().log("summary", format("Will build %d target%s", total, (total!=1 ? "s" : "")));
175
176         vector<Task *> tasks;
177
178         unsigned count = 0;
179
180         bool fail = false;
181         bool finish = false;
182         bool starved = false;
183
184         while(!finish)
185         {
186                 if(tasks.size()<jobs && !fail && !starved)
187                 {
188                         Target *tgt = build_graph.get_buildable_target();
189                         if(tgt)
190                         {
191                                 if(tgt->get_tool())
192                                         get_logger().log("tasks", format("%-4s  %s", tgt->get_tool()->get_tag(), tgt->get_name()));
193                                 Task *task = tgt->build();
194                                 if(task)
195                                 {
196                                         get_logger().log("commands", format("%s", task->get_command()));
197                                         if(dry_run)
198                                         {
199                                                 task->signal_finished.emit(true);
200                                                 delete task;
201                                         }
202                                         else
203                                         {
204                                                 task->start();
205                                                 tasks.push_back(task);
206                                         }
207                                 }
208
209                                 if(show_progress)
210                                         IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
211                         }
212                         else if(tasks.empty())
213                                 finish = true;
214                         else
215                                 starved = true;
216                 }
217                 else
218                         Time::sleep(10*Time::msec);
219
220                 for(unsigned i=0; i<tasks.size();)
221                 {
222                         Task::Status status;
223                         if(jobs==1 || (tasks.size()==1 && starved))
224                                 status = tasks[i]->wait();
225                         else
226                                 status = tasks[i]->check();
227
228                         if(status!=Task::RUNNING)
229                         {
230                                 ++count;
231
232                                 delete tasks[i];
233                                 tasks.erase(tasks.begin()+i);
234                                 if(status==Task::ERROR)
235                                         fail = true;
236                                 if(tasks.empty() && fail)
237                                         finish = true;
238                                 starved = false;
239                         }
240                         else
241                                 ++i;
242                 }
243         }
244
245         if(show_progress)
246                 IO::print("\033[K");
247         if(fail)
248                 get_logger().log("summary", "Build failed");
249         else if(show_progress)
250                 get_logger().log("summary", "Build complete");
251
252         if(!dry_run)
253         {
254                 const PackageManager::PackageMap &packages = package_manager.get_packages();
255                 for(PackageManager::PackageMap::const_iterator i=packages.begin(); i!=packages.end(); ++i)
256                         i->second->save_caches();
257         }
258
259         return fail;
260 }
261
262 int Builder::clean(bool all, bool dry_run)
263 {
264         // Cleaning doesn't care about ordering, so a simpler method can be used
265
266         set<Target *> clean_tgts;
267         list<Target *> queue;
268         queue.push_back(build_graph.get_target("cmdline"));
269
270         while(!queue.empty())
271         {
272                 Target *tgt = queue.front();
273                 queue.erase(queue.begin());
274
275                 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
276                         clean_tgts.insert(tgt);
277
278                 const Target::Dependencies &deps = tgt->get_dependencies();
279                 for(list<Target *>::const_iterator i=deps.begin(); i!=deps.end(); ++i)
280                         if(!clean_tgts.count(*i))
281                                 queue.push_back(*i);
282         }
283
284         for(set<Target *>::iterator i=clean_tgts.begin(); i!=clean_tgts.end(); ++i)
285         {
286                 get_logger().log("tasks", format("RM    %s", (*i)->get_name()));
287                 if(!dry_run)
288                         (*i)->clean();
289         }
290
291         return 0;
292 }
293
294
295 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
296         DataFile::ObjectLoader<Builder>(b),
297         options(o),
298         conf_all(a)
299 {
300         add("architecture", &Loader::architecture);
301         add("binary_package", &Loader::binpkg);
302         add("build_type", &Loader::build_type);
303         add("package", &Loader::package);
304
305         if(!obj.top_loader)
306                 obj.top_loader = this;
307         else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
308                 options = obj.top_loader->options;
309 }
310
311 Builder::Loader::~Loader()
312 {
313         if(obj.top_loader==this)
314                 obj.top_loader = 0;
315 }
316
317 void Builder::Loader::architecture(const string &n)
318 {
319         if(obj.current_arch->match_name(n))
320                 load_sub(*obj.current_arch);
321 }
322
323 void Builder::Loader::binpkg(const string &n)
324 {
325         BinaryPackage *pkg = new BinaryPackage(obj, n);
326         load_sub(*pkg);
327 }
328
329 void Builder::Loader::build_type(const string &n)
330 {
331         BuildType btype(n);
332         load_sub(btype);
333         BuildTypeMap::iterator i = obj.build_types.insert(BuildTypeMap::value_type(n, btype)).first;
334         if(!obj.build_type)
335                 obj.build_type = &i->second;
336 }
337
338 void Builder::Loader::package(const string &n)
339 {
340         SourcePackage *pkg = new SourcePackage(obj, n, get_source());
341
342         if(options)
343                 load_sub(*pkg, *options);
344         else
345                 load_sub(*pkg);
346
347         if(obj.build_type)
348                 pkg->set_build_type(*obj.build_type);
349 }