3 #include <msp/core/algorithm.h>
4 #include <msp/core/except.h>
5 #include <msp/core/maputils.h>
6 #include <msp/datafile/parser.h>
7 #include <msp/fs/dir.h>
8 #include <msp/fs/stat.h>
9 #include <msp/fs/utils.h>
10 #include <msp/io/buffered.h>
11 #include <msp/io/file.h>
12 #include <msp/io/print.h>
13 #include <msp/strings/format.h>
14 #include <msp/strings/utils.h>
15 #include <msp/time/timedelta.h>
16 #include <msp/time/utils.h>
17 #include "binarypackage.h"
19 #include "installedfile.h"
22 #include "sharedlibrary.h"
25 #include "virtualtarget.h"
31 package_manager(*this),
32 native_arch(*this, string()),
35 logger(&default_logger)
37 set_architecture(string());
42 if(current_arch!=&native_arch)
46 void Builder::load_plugins()
48 using CreateFunc = Plugin *(Builder &);
50 FS::Path plugins_dir = FS::get_sys_lib_dir();
51 if(!FS::exists(plugins_dir))
54 logger->log("files", "Traversing %s", plugins_dir);
55 vector<LoadedPlugin> unordered_plugins;
56 for(const string &f: list_filtered(plugins_dir, "\\.dlm$"))
59 plugin.path = plugins_dir/f;
63 plugin.module = new Module(plugin.path.str());
65 catch(const exception &exc)
67 logger->log("plugins", "Failed to load plugin %s: %s", f, exc.what());
73 CreateFunc *create_func = reinterpret_cast<CreateFunc *>(plugin.module->get_symbol("create_plugin"));
74 plugin.plugin = create_func(*this);
77 logger->log("plugins", "Loaded plugin %s", f);
78 unordered_plugins.emplace_back(move(plugin));
82 logger->log("plugins", "Plugin %s refused to initialize", f);
84 catch(const exception &exc)
86 logger->log("plugins", "Failed to initialize plugin %s: %s", f, exc.what());
90 add_plugins(unordered_plugins);
93 void Builder::add_plugins(vector<LoadedPlugin> &unordered_plugins)
95 auto have_plugin = [this](const string &r){
96 return any_of(plugins.begin(), plugins.end(), [&r](const LoadedPlugin &p){ return FS::basepart(FS::basename(p.path))==r; });
99 while(!unordered_plugins.empty())
101 bool any_added = false;
102 for(auto i=unordered_plugins.begin(); i!=unordered_plugins.end(); )
104 const vector<string> &required = i->plugin->get_required_plugins();
105 if(all_of(required.begin(), required.end(), have_plugin))
107 plugins.push_back(move(*i));
108 i = unordered_plugins.erase(i);
119 for(const LoadedPlugin &p: unordered_plugins)
121 vector<string> missing;
122 for(const string &r: p.plugin->get_required_plugins())
124 missing.push_back(r);
125 logger->log("plugins", "Missing required plugins for plugin %s: %s",
126 FS::basename(p.path), join(missing.begin(), missing.end()));
130 void Builder::set_architecture(const string &name)
132 if(current_arch!=&native_arch)
136 current_arch = &native_arch;
138 current_arch = new Architecture(*this, name);
141 update_auto_prefix();
144 vector<string> Builder::get_build_types() const
147 keys.reserve(build_types.size());
148 for(const auto &kvp: build_types)
149 keys.push_back(kvp.first);
153 const BuildType &Builder::get_build_type() const
156 throw invalid_state("no build type");
160 void Builder::set_build_type(const string &name)
162 build_type = &get_item(build_types, name);
165 void Builder::set_prefix(const FS::Path &p)
171 void Builder::set_temp_directory(const FS::Path &p)
176 void Builder::update_auto_prefix()
178 if(current_arch->is_native())
179 prefix = FS::get_home_dir()/"local";
181 prefix = FS::get_home_dir()/"local"/current_arch->get_name();
184 void Builder::add_default_tools()
186 for(const LoadedPlugin &p: plugins)
187 p.plugin->add_tools(toolchain, *current_arch);
189 auto i = find_if(toolchain.get_toolchains(), [](const Toolchain *tc){ return (tc->has_tool("CC") || tc->has_tool("CXX")); });
190 if(i!=toolchain.get_toolchains().end())
192 current_arch->refine((*i)->get_name());
194 update_auto_prefix();
198 void Builder::set_logger(const Logger *l)
200 logger = (l ? l : &default_logger);
203 vector<string> Builder::collect_problems() const
205 vector<string> target_problems;
206 vector<string> problems;
207 vector<const Package *> broken_packages;
208 vector<const Component *> broken_components;
209 vector<const Tool *> broken_tools;
211 for(const auto &kvp: build_graph.get_targets())
212 if(kvp.second->is_broken())
214 const Package *package = kvp.second->get_package();
215 if(package && package->is_broken())
216 collect_broken_packages(*package, broken_packages);
218 const Component *component = kvp.second->get_component();
219 if(component && component->is_broken() && !any_equals(broken_components, component))
221 broken_components.push_back(component);
222 collect_broken_packages(component->get_package(), broken_packages);
223 for(const Package *r: component->get_required_packages())
225 collect_broken_packages(*r, broken_packages);
228 const Tool *tool = kvp.second->get_tool();
229 if(tool && tool->is_broken() && !any_equals(broken_tools, tool))
231 broken_tools.push_back(tool);
232 for(const string &p: tool->get_problems())
233 problems.push_back(format("%s: %s", tool->get_tag(), p));
236 for(const string &p: kvp.second->get_problems())
237 target_problems.push_back(format("%s: %s", kvp.second->get_name(), p));
240 for(const Package *p: broken_packages)
242 for(const string &b: p->get_problems())
243 problems.push_back(format("%s: %s", p->get_name(), b));
245 for(const Component *c: broken_components)
246 if(&c->get_package()==p)
248 for(const string &b: c->get_problems())
249 problems.push_back(format("%s/%s: %s", p->get_name(), c->get_name(), b));
253 problems.insert(problems.end(), make_move_iterator(target_problems.begin()), make_move_iterator(target_problems.end()));
258 void Builder::collect_broken_packages(const Package &pkg, vector<const Package *> &broken) const
260 if(any_equals(broken, &pkg))
263 broken.push_back(&pkg);
265 for(const Package *r: pkg.get_required_packages())
267 collect_broken_packages(*r, broken);
270 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
272 IO::BufferedFile in(fn.str());
274 get_logger().log("files", "Reading %s", fn);
276 DataFile::Parser parser(in, fn.str());
277 Loader loader(*this, opts, all);
281 void Builder::save_caches()
283 for(const auto &kvp: package_manager.get_packages())
284 kvp.second->save_caches();
287 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
289 unsigned total = build_graph.count_rebuild_targets();
290 Time::TimeStamp start_time = Time::now();
294 get_logger().log("summary", "Already up to date");
297 get_logger().log("summary", "Will build %d target%s", total, (total!=1 ? "s" : ""));
299 vector<Task *> tasks;
302 Time::TimeDelta sum_time;
306 bool starved = false;
310 if(tasks.size()<jobs && !fail && !starved)
312 Target *tgt = build_graph.get_buildable_target();
319 get_logger().log("tasks", "%-4s %s", tgt->get_tool()->get_tag(), tgt->get_name());
321 Task *task = tgt->build();
324 get_logger().log("commands", "%s", task->get_command());
327 task->signal_finished.emit(true);
333 tasks.push_back(task);
338 IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
340 else if(tasks.empty())
346 Time::sleep(10*Time::msec);
348 for(unsigned i=0; i<tasks.size();)
351 if(jobs==1 || (tasks.size()==1 && starved))
352 status = tasks[i]->wait();
354 status = tasks[i]->check();
356 if(status!=Task::RUNNING)
360 const vector<const FileTarget *> &targets = tasks[i]->get_targets();
363 sum_time += tasks[i]->get_duration();
364 get_logger().log("timings", "%s built in %s", targets.front()->get_name(), tasks[i]->get_duration());
368 tasks.erase(tasks.begin()+i);
369 if(status==Task::ERROR)
371 if(tasks.empty() && fail)
383 get_logger().log("summary", "Build failed");
384 else if(show_progress)
385 get_logger().log("summary", "Build complete");
387 Time::TimeStamp end_time = Time::now();
388 get_logger().log("timings", "Build took %s, with a total %s spent on tasks", end_time-start_time, sum_time);
393 int Builder::clean(bool all, bool dry_run)
395 // Cleaning doesn't care about ordering, so a simpler method can be used
397 set<Target *> clean_tgts;
398 deque<Target *> queue;
399 queue.push_back(&build_graph.get_goals());
401 while(!queue.empty())
403 Target *tgt = queue.front();
406 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
407 clean_tgts.insert(tgt);
409 for(Target *t: tgt->get_dependencies())
410 if(!clean_tgts.count(t))
414 for(Target *t: clean_tgts)
416 get_logger().log("tasks", "RM %s", t->get_name());
425 Builder::LoadedPlugin::LoadedPlugin(LoadedPlugin &&other):
426 path(move(other.path)),
427 module(other.module),
434 Builder::LoadedPlugin &Builder::LoadedPlugin::operator=(LoadedPlugin &&other)
438 path = move(other.path);
439 module = other.module;
440 plugin = other.plugin;
446 Builder::LoadedPlugin::~LoadedPlugin()
453 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
454 DataFile::ObjectLoader<Builder>(b),
458 add("architecture", &Loader::architecture);
459 add("binary_package", &Loader::binpkg);
460 add("build_type", &Loader::build_type);
461 add("package", &Loader::package);
464 obj.top_loader = this;
465 else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
466 options = obj.top_loader->options;
469 Builder::Loader::~Loader()
471 if(obj.top_loader==this)
475 void Builder::Loader::architecture(const string &n)
477 if(obj.current_arch->match_name(n))
478 load_sub(*obj.current_arch);
481 void Builder::Loader::binpkg(const string &n)
483 BinaryPackage *pkg = new BinaryPackage(obj, n);
487 void Builder::Loader::build_type(const string &n)
491 auto i = obj.build_types.insert({ n, btype }).first;
493 obj.build_type = &i->second;
496 void Builder::Loader::package(const string &n)
498 SourcePackage *pkg = new SourcePackage(obj, n, get_source());
500 load_sub(*pkg, options);
503 pkg->set_build_type(*obj.build_type);