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/timedelta.h>
11 #include <msp/time/utils.h>
12 #include "androidtools.h"
13 #include "binarypackage.h"
15 #include "builtintools.h"
17 #include "installedfile.h"
19 #include "sharedlibrary.h"
20 #include "sourcepackage.h"
21 #include "systemtools.h"
23 #include "virtualtarget.h"
29 package_manager(*this),
30 native_arch(*this, string()),
35 logger(&default_logger),
39 set_architecture(string());
44 if(current_arch!=&native_arch)
48 void Builder::set_architecture(const string &name)
52 current_arch = &native_arch;
53 prefix = FS::get_home_dir()/"local";
57 current_arch = new Architecture(*this, name);
58 prefix = FS::get_home_dir()/"local"/current_arch->get_name();
62 vector<string> Builder::get_build_types() const
65 keys.reserve(build_types.size());
66 for(BuildTypeMap::const_iterator i=build_types.begin(); i!=build_types.end(); ++i)
67 keys.push_back(i->first);
71 void Builder::set_build_type(const string &name)
73 build_type = &get_item(build_types, name);
76 void Builder::set_prefix(const FS::Path &p)
81 void Builder::set_temp_directory(const FS::Path &p)
86 void Builder::add_default_tools()
88 if(current_arch->get_system()=="android")
89 toolchain.add_toolchain(new AndroidTools(*this, *current_arch));
90 toolchain.add_toolchain(new SystemTools(*this, *current_arch));
91 toolchain.add_toolchain(new BuiltinTools(*this));
92 toolchain.add_tool(new DataTool(*this));
95 void Builder::set_logger(const Logger *l)
97 logger = (l ? l : &default_logger);
100 list<string> Builder::collect_problems() const
102 list<string> problems;
103 set<const Package *> broken_packages;
104 set<const Component *> broken_components;
105 set<const Tool *> broken_tools;
107 const BuildGraph::TargetMap &targets = build_graph.get_targets();
108 for(BuildGraph::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
109 if(i->second->is_broken())
111 const list<string> &tgt_problems = i->second->get_problems();
112 for(list<string>::const_iterator j=tgt_problems.begin(); j!=tgt_problems.end(); ++j)
113 problems.push_back(format("%s: %s", i->second->get_name(), *j));
115 const Package *package = i->second->get_package();
116 if(package && !package->get_problems().empty())
117 broken_packages.insert(package);
119 const Component *component = i->second->get_component();
120 if(component && !component->get_problems().empty())
121 broken_components.insert(component);
123 const Tool *tool = i->second->get_tool();
124 if(tool && !tool->get_problems().empty())
125 broken_tools.insert(tool);
128 // TODO Sort components after their packages, and targets last
129 for(set<const Package *>::const_iterator i=broken_packages.begin(); i!=broken_packages.end(); ++i)
131 const list<string> &pkg_problems = (*i)->get_problems();
132 for(list<string>::const_iterator j=pkg_problems.begin(); j!=pkg_problems.end(); ++j)
133 problems.push_back(format("%s: %s", (*i)->get_name(), *j));
136 for(set<const Component *>::const_iterator i=broken_components.begin(); i!=broken_components.end(); ++i)
138 const list<string> &comp_problems = (*i)->get_problems();
139 for(list<string>::const_iterator j=comp_problems.begin(); j!=comp_problems.end(); ++j)
140 problems.push_back(format("%s/%s: %s", (*i)->get_package().get_name(), (*i)->get_name(), *j));
143 for(set<const Tool *>::const_iterator i=broken_tools.begin(); i!=broken_tools.end(); ++i)
145 const list<string> &tool_problems = (*i)->get_problems();
146 for(list<string>::const_iterator j=tool_problems.begin(); j!=tool_problems.end(); ++j)
147 problems.push_back(format("%s: %s", (*i)->get_tag(), *j));
153 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
155 IO::BufferedFile in(fn.str());
157 get_logger().log("files", format("Reading %s", fn));
159 DataFile::Parser parser(in, fn.str());
160 Loader loader(*this, opts, all);
164 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
166 unsigned total = build_graph.count_rebuild_targets();
170 get_logger().log("summary", "Already up to date");
173 get_logger().log("summary", format("Will build %d target%s", total, (total!=1 ? "s" : "")));
175 vector<Task *> tasks;
181 bool starved = false;
185 if(tasks.size()<jobs && !fail && !starved)
187 Target *tgt = build_graph.get_buildable_target();
191 get_logger().log("tasks", format("%-4s %s", tgt->get_tool()->get_tag(), tgt->get_name()));
192 Task *task = tgt->build();
195 get_logger().log("commands", format("%s", task->get_command()));
198 task->signal_finished.emit(true);
204 tasks.push_back(task);
209 IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
211 else if(tasks.empty())
217 Time::sleep(10*Time::msec);
219 for(unsigned i=0; i<tasks.size();)
222 if(jobs==1 || (tasks.size()==1 && starved))
223 status = tasks[i]->wait();
225 status = tasks[i]->check();
227 if(status!=Task::RUNNING)
232 tasks.erase(tasks.begin()+i);
233 if(status==Task::ERROR)
235 if(tasks.empty() && fail)
247 get_logger().log("summary", "Build failed");
248 else if(show_progress)
249 get_logger().log("summary", "Build complete");
253 const PackageManager::PackageMap &packages = package_manager.get_packages();
254 for(PackageManager::PackageMap::const_iterator i=packages.begin(); i!=packages.end(); ++i)
255 i->second->save_caches();
261 int Builder::clean(bool all, bool dry_run)
263 // Cleaning doesn't care about ordering, so a simpler method can be used
265 set<Target *> clean_tgts;
266 list<Target *> queue;
267 queue.push_back(&build_graph.get_goals());
269 while(!queue.empty())
271 Target *tgt = queue.front();
274 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
275 clean_tgts.insert(tgt);
277 const Target::Dependencies &deps = tgt->get_dependencies();
278 for(list<Target *>::const_iterator i=deps.begin(); i!=deps.end(); ++i)
279 if(!clean_tgts.count(*i))
283 for(set<Target *>::iterator i=clean_tgts.begin(); i!=clean_tgts.end(); ++i)
285 get_logger().log("tasks", format("RM %s", (*i)->get_name()));
294 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
295 DataFile::ObjectLoader<Builder>(b),
299 add("architecture", &Loader::architecture);
300 add("binary_package", &Loader::binpkg);
301 add("build_type", &Loader::build_type);
302 add("package", &Loader::package);
305 obj.top_loader = this;
306 else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
307 options = obj.top_loader->options;
310 Builder::Loader::~Loader()
312 if(obj.top_loader==this)
316 void Builder::Loader::architecture(const string &n)
318 if(obj.current_arch->match_name(n))
319 load_sub(*obj.current_arch);
322 void Builder::Loader::binpkg(const string &n)
324 BinaryPackage *pkg = new BinaryPackage(obj, n);
328 void Builder::Loader::build_type(const string &n)
332 BuildTypeMap::iterator i = obj.build_types.insert(BuildTypeMap::value_type(n, btype)).first;
334 obj.build_type = &i->second;
337 void Builder::Loader::package(const string &n)
339 SourcePackage *pkg = new SourcePackage(obj, n, get_source());
342 load_sub(*pkg, *options);
347 pkg->set_build_type(*obj.build_type);