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"
14 #include "builtintools.h"
15 #include "clangtools.h"
18 #include "installedfile.h"
20 #include "sharedlibrary.h"
21 #include "sourcepackage.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 void Builder::set_build_type(const string &name)
64 build_type = &get_item(build_types, name);
67 void Builder::set_prefix(const FS::Path &p)
72 void Builder::set_temp_directory(const FS::Path &p)
77 void Builder::add_default_tools()
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));
86 void Builder::set_logger(const Logger *l)
88 logger = (l ? l : &default_logger);
91 list<string> Builder::collect_problems() const
93 list<string> problems;
94 set<const Package *> broken_packages;
95 set<const Component *> broken_components;
96 set<const Tool *> broken_tools;
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())
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));
106 const Package *package = i->second->get_package();
107 if(package && !package->get_problems().empty())
108 broken_packages.insert(package);
110 const Component *component = i->second->get_component();
111 if(component && !component->get_problems().empty())
112 broken_components.insert(component);
114 const Tool *tool = i->second->get_tool();
115 if(tool && !tool->get_problems().empty())
116 broken_tools.insert(tool);
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)
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));
127 for(set<const Component *>::const_iterator i=broken_components.begin(); i!=broken_components.end(); ++i)
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));
134 for(set<const Tool *>::const_iterator i=broken_tools.begin(); i!=broken_tools.end(); ++i)
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));
144 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
146 IO::BufferedFile in(fn.str());
148 get_logger().log("files", format("Reading %s", fn));
150 DataFile::Parser parser(in, fn.str());
151 Loader loader(*this, opts, all);
155 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
157 unsigned total = build_graph.count_rebuild_targets();
161 get_logger().log("summary", "Already up to date");
164 get_logger().log("summary", format("Will build %d target%s", total, (total!=1 ? "s" : "")));
166 vector<Task *> tasks;
172 bool starved = false;
176 if(tasks.size()<jobs && !fail && !starved)
178 Target *tgt = build_graph.get_buildable_target();
182 get_logger().log("tasks", format("%-4s %s", tgt->get_tool()->get_tag(), tgt->get_name()));
183 Task *task = tgt->build();
186 get_logger().log("commands", format("%s", task->get_command()));
189 task->signal_finished.emit(true);
195 tasks.push_back(task);
200 IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
202 else if(tasks.empty())
208 Time::sleep(10*Time::msec);
210 for(unsigned i=0; i<tasks.size();)
213 if(jobs==1 || (tasks.size()==1 && starved))
214 status = tasks[i]->wait();
216 status = tasks[i]->check();
218 if(status!=Task::RUNNING)
223 tasks.erase(tasks.begin()+i);
224 if(status==Task::ERROR)
226 if(tasks.empty() && fail)
238 get_logger().log("summary", "Build failed");
239 else if(show_progress)
240 get_logger().log("summary", "Build complete");
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();
252 int Builder::clean(bool all, bool dry_run)
254 // Cleaning doesn't care about ordering, so a simpler method can be used
256 set<Target *> clean_tgts;
257 list<Target *> queue;
258 queue.push_back(&build_graph.get_goals());
260 while(!queue.empty())
262 Target *tgt = queue.front();
263 queue.erase(queue.begin());
265 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
266 clean_tgts.insert(tgt);
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))
274 for(set<Target *>::iterator i=clean_tgts.begin(); i!=clean_tgts.end(); ++i)
276 get_logger().log("tasks", format("RM %s", (*i)->get_name()));
285 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
286 DataFile::ObjectLoader<Builder>(b),
290 add("architecture", &Loader::architecture);
291 add("binary_package", &Loader::binpkg);
292 add("build_type", &Loader::build_type);
293 add("package", &Loader::package);
296 obj.top_loader = this;
297 else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
298 options = obj.top_loader->options;
301 Builder::Loader::~Loader()
303 if(obj.top_loader==this)
307 void Builder::Loader::architecture(const string &n)
309 if(obj.current_arch->match_name(n))
310 load_sub(*obj.current_arch);
313 void Builder::Loader::binpkg(const string &n)
315 BinaryPackage *pkg = new BinaryPackage(obj, n);
319 void Builder::Loader::build_type(const string &n)
323 BuildTypeMap::iterator i = obj.build_types.insert(BuildTypeMap::value_type(n, btype)).first;
325 obj.build_type = &i->second;
328 void Builder::Loader::package(const string &n)
330 SourcePackage *pkg = new SourcePackage(obj, n, get_source());
333 load_sub(*pkg, *options);
338 pkg->set_build_type(*obj.build_type);