]> git.tdb.fi Git - builder.git/blob - source/builder.cpp
Pass a tool hint to VirtualFileSystem::find_header
[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 "builtintools.h"
15 #include "clangtools.h"
16 #include "datatool.h"
17 #include "gnutools.h"
18 #include "installedfile.h"
19 #include "package.h"
20 #include "sharedlibrary.h"
21 #include "sourcepackage.h"
22 #include "task.h"
23 #include "virtualtarget.h"
24
25 using namespace std;
26 using namespace Msp;
27
28 Builder::Builder():
29         package_manager(*this),
30         native_arch(*this, string()),
31         current_arch(0),
32         build_type(0),
33         vfs(*this),
34         build_graph(*this),
35         logger(&default_logger),
36         tempdir("temp"),
37         top_loader(0)
38 {
39         set_architecture(string());
40 }
41
42 Builder::~Builder()
43 {
44         if(current_arch!=&native_arch)
45                 delete current_arch;
46 }
47
48 void Builder::set_architecture(const string &name)
49 {
50         if(name.empty())
51         {
52                 current_arch = &native_arch;
53                 prefix = FS::get_home_dir()/"local";
54         }
55         else
56         {
57                 current_arch = new Architecture(*this, name);
58                 prefix = FS::get_home_dir()/"local"/current_arch->get_name();
59         }
60 }
61
62 void Builder::set_build_type(const string &name)
63 {
64         build_type = &get_item(build_types, name);
65 }
66
67 void Builder::set_prefix(const FS::Path &p)
68 {
69         prefix = p;
70 }
71
72 void Builder::set_temp_directory(const FS::Path &p)
73 {
74         tempdir = p;
75 }
76
77 void Builder::add_default_tools()
78 {
79         if(current_arch->get_system()=="darwin")
80                 toolchain.add_toolchain(new ClangTools(*this, *current_arch));
81         toolchain.add_toolchain(new GnuTools(*this, *current_arch));
82         toolchain.add_toolchain(new BuiltinTools(*this));
83         toolchain.add_tool(new DataTool(*this));
84 }
85
86 void Builder::set_logger(const Logger *l)
87 {
88         logger = (l ? l : &default_logger);
89 }
90
91 list<string> Builder::collect_problems() const
92 {
93         list<string> problems;
94         set<const Package *> broken_packages;
95         set<const Component *> broken_components;
96         set<const Tool *> broken_tools;
97
98         const BuildGraph::TargetMap &targets = build_graph.get_targets();
99         for(BuildGraph::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
100                 if(i->second->is_broken())
101                 {
102                         const list<string> &tgt_problems = i->second->get_problems();
103                         for(list<string>::const_iterator j=tgt_problems.begin(); j!=tgt_problems.end(); ++j)
104                                 problems.push_back(format("%s: %s", i->second->get_name(), *j));
105
106                         const Package *package = i->second->get_package();
107                         if(package && !package->get_problems().empty())
108                                 broken_packages.insert(package);
109
110                         const Component *component = i->second->get_component();
111                         if(component && !component->get_problems().empty())
112                                 broken_components.insert(component);
113
114                         const Tool *tool = i->second->get_tool();
115                         if(tool && !tool->get_problems().empty())
116                                 broken_tools.insert(tool);
117                 }
118
119         // TODO Sort components after their packages, and targets last
120         for(set<const Package *>::const_iterator i=broken_packages.begin(); i!=broken_packages.end(); ++i)
121         {
122                 const list<string> &pkg_problems = (*i)->get_problems();
123                 for(list<string>::const_iterator j=pkg_problems.begin(); j!=pkg_problems.end(); ++j)
124                         problems.push_back(format("%s: %s", (*i)->get_name(), *j));
125         }
126
127         for(set<const Component *>::const_iterator i=broken_components.begin(); i!=broken_components.end(); ++i)
128         {
129                 const list<string> &comp_problems = (*i)->get_problems();
130                 for(list<string>::const_iterator j=comp_problems.begin(); j!=comp_problems.end(); ++j)
131                         problems.push_back(format("%s/%s: %s", (*i)->get_package().get_name(), (*i)->get_name(), *j));
132         }
133
134         for(set<const Tool *>::const_iterator i=broken_tools.begin(); i!=broken_tools.end(); ++i)
135         {
136                 const list<string> &tool_problems = (*i)->get_problems();
137                 for(list<string>::const_iterator j=tool_problems.begin(); j!=tool_problems.end(); ++j)
138                         problems.push_back(format("%s: %s", (*i)->get_tag(), *j));
139         }
140
141         return problems;
142 }
143
144 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
145 {
146         IO::BufferedFile in(fn.str());
147
148         get_logger().log("files", format("Reading %s", fn));
149
150         DataFile::Parser parser(in, fn.str());
151         Loader loader(*this, opts, all);
152         loader.load(parser);
153 }
154
155 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
156 {
157         unsigned total = build_graph.count_rebuild_targets();
158
159         if(!total)
160         {
161                 get_logger().log("summary", "Already up to date");
162                 return 0;
163         }
164         get_logger().log("summary", format("Will build %d target%s", total, (total!=1 ? "s" : "")));
165
166         vector<Task *> tasks;
167
168         unsigned count = 0;
169
170         bool fail = false;
171         bool finish = false;
172         bool starved = false;
173
174         while(!finish)
175         {
176                 if(tasks.size()<jobs && !fail && !starved)
177                 {
178                         Target *tgt = build_graph.get_buildable_target();
179                         if(tgt)
180                         {
181                                 if(tgt->get_tool())
182                                         get_logger().log("tasks", format("%-4s  %s", tgt->get_tool()->get_tag(), tgt->get_name()));
183                                 Task *task = tgt->build();
184                                 if(task)
185                                 {
186                                         get_logger().log("commands", format("%s", task->get_command()));
187                                         if(dry_run)
188                                         {
189                                                 task->signal_finished.emit(true);
190                                                 delete task;
191                                         }
192                                         else
193                                         {
194                                                 task->start();
195                                                 tasks.push_back(task);
196                                         }
197                                 }
198
199                                 if(show_progress)
200                                         IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
201                         }
202                         else if(tasks.empty())
203                                 finish = true;
204                         else
205                                 starved = true;
206                 }
207                 else
208                         Time::sleep(10*Time::msec);
209
210                 for(unsigned i=0; i<tasks.size();)
211                 {
212                         Task::Status status;
213                         if(jobs==1 || (tasks.size()==1 && starved))
214                                 status = tasks[i]->wait();
215                         else
216                                 status = tasks[i]->check();
217
218                         if(status!=Task::RUNNING)
219                         {
220                                 ++count;
221
222                                 delete tasks[i];
223                                 tasks.erase(tasks.begin()+i);
224                                 if(status==Task::ERROR)
225                                         fail = true;
226                                 if(tasks.empty() && fail)
227                                         finish = true;
228                                 starved = false;
229                         }
230                         else
231                                 ++i;
232                 }
233         }
234
235         if(show_progress)
236                 IO::print("\033[K");
237         if(fail)
238                 get_logger().log("summary", "Build failed");
239         else if(show_progress)
240                 get_logger().log("summary", "Build complete");
241
242         if(!dry_run)
243         {
244                 const PackageManager::PackageMap &packages = package_manager.get_packages();
245                 for(PackageManager::PackageMap::const_iterator i=packages.begin(); i!=packages.end(); ++i)
246                         i->second->save_caches();
247         }
248
249         return fail;
250 }
251
252 int Builder::clean(bool all, bool dry_run)
253 {
254         // Cleaning doesn't care about ordering, so a simpler method can be used
255
256         set<Target *> clean_tgts;
257         list<Target *> queue;
258         queue.push_back(&build_graph.get_goals());
259
260         while(!queue.empty())
261         {
262                 Target *tgt = queue.front();
263                 queue.erase(queue.begin());
264
265                 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
266                         clean_tgts.insert(tgt);
267
268                 const Target::Dependencies &deps = tgt->get_dependencies();
269                 for(list<Target *>::const_iterator i=deps.begin(); i!=deps.end(); ++i)
270                         if(!clean_tgts.count(*i))
271                                 queue.push_back(*i);
272         }
273
274         for(set<Target *>::iterator i=clean_tgts.begin(); i!=clean_tgts.end(); ++i)
275         {
276                 get_logger().log("tasks", format("RM    %s", (*i)->get_name()));
277                 if(!dry_run)
278                         (*i)->clean();
279         }
280
281         return 0;
282 }
283
284
285 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
286         DataFile::ObjectLoader<Builder>(b),
287         options(o),
288         conf_all(a)
289 {
290         add("architecture", &Loader::architecture);
291         add("binary_package", &Loader::binpkg);
292         add("build_type", &Loader::build_type);
293         add("package", &Loader::package);
294
295         if(!obj.top_loader)
296                 obj.top_loader = this;
297         else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
298                 options = obj.top_loader->options;
299 }
300
301 Builder::Loader::~Loader()
302 {
303         if(obj.top_loader==this)
304                 obj.top_loader = 0;
305 }
306
307 void Builder::Loader::architecture(const string &n)
308 {
309         if(obj.current_arch->match_name(n))
310                 load_sub(*obj.current_arch);
311 }
312
313 void Builder::Loader::binpkg(const string &n)
314 {
315         BinaryPackage *pkg = new BinaryPackage(obj, n);
316         load_sub(*pkg);
317 }
318
319 void Builder::Loader::build_type(const string &n)
320 {
321         BuildType btype(n);
322         load_sub(btype);
323         BuildTypeMap::iterator i = obj.build_types.insert(BuildTypeMap::value_type(n, btype)).first;
324         if(!obj.build_type)
325                 obj.build_type = &i->second;
326 }
327
328 void Builder::Loader::package(const string &n)
329 {
330         SourcePackage *pkg = new SourcePackage(obj, n, get_source());
331
332         if(options)
333                 load_sub(*pkg, *options);
334         else
335                 load_sub(*pkg);
336
337         if(obj.build_type)
338                 pkg->set_build_type(*obj.build_type);
339 }