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/utils.h>
9 #include <msp/io/buffered.h>
10 #include <msp/io/file.h>
11 #include <msp/io/print.h>
12 #include <msp/strings/format.h>
13 #include <msp/strings/utils.h>
14 #include <msp/time/timedelta.h>
15 #include <msp/time/utils.h>
16 #include "binarypackage.h"
18 #include "installedfile.h"
21 #include "sharedlibrary.h"
24 #include "virtualtarget.h"
30 package_manager(*this),
31 native_arch(*this, string()),
34 logger(&default_logger)
36 set_architecture(string());
41 if(current_arch!=&native_arch)
45 void Builder::load_plugins()
47 using CreateFunc = Plugin *(Builder &);
49 FS::Path plugins_dir = FS::get_sys_lib_dir();
50 logger->log("files", "Traversing %s", plugins_dir);
51 vector<LoadedPlugin> unordered_plugins;
52 for(const string &f: list_filtered(plugins_dir, "\\.dlm$"))
55 plugin.path = plugins_dir/f;
59 plugin.module = new Module(plugin.path.str());
61 catch(const exception &exc)
63 logger->log("plugins", "Failed to load plugin %s: %s", f, exc.what());
69 CreateFunc *create_func = reinterpret_cast<CreateFunc *>(plugin.module->get_symbol("create_plugin"));
70 plugin.plugin = create_func(*this);
73 logger->log("plugins", "Loaded plugin %s", f);
74 unordered_plugins.emplace_back(move(plugin));
78 logger->log("plugins", "Plugin %s refused to initialize", f);
80 catch(const exception &exc)
82 logger->log("plugins", "Failed to initialize plugin %s: %s", f, exc.what());
86 auto have_plugin = [this](const string &r){
87 return any_of(plugins.begin(), plugins.end(), [&r](const LoadedPlugin &p){ return FS::basepart(FS::basename(p.path))==r; });
90 while(!unordered_plugins.empty())
92 bool any_added = false;
93 for(auto i=unordered_plugins.begin(); i!=unordered_plugins.end(); )
95 const vector<string> &required = i->plugin->get_required_plugins();
96 if(all_of(required.begin(), required.end(), have_plugin))
98 plugins.push_back(move(*i));
99 i = unordered_plugins.erase(i);
110 for(const LoadedPlugin &p: unordered_plugins)
112 vector<string> missing;
113 for(const string &r: p.plugin->get_required_plugins())
115 missing.push_back(r);
116 logger->log("plugins", "Missing required plugins for plugin %s: %s",
117 FS::basename(p.path), join(missing.begin(), missing.end()));
121 void Builder::set_architecture(const string &name)
123 if(current_arch!=&native_arch)
127 current_arch = &native_arch;
129 current_arch = new Architecture(*this, name);
132 update_auto_prefix();
135 vector<string> Builder::get_build_types() const
138 keys.reserve(build_types.size());
139 for(const auto &kvp: build_types)
140 keys.push_back(kvp.first);
144 const BuildType &Builder::get_build_type() const
147 throw invalid_state("no build type");
151 void Builder::set_build_type(const string &name)
153 build_type = &get_item(build_types, name);
156 void Builder::set_prefix(const FS::Path &p)
162 void Builder::set_temp_directory(const FS::Path &p)
167 void Builder::update_auto_prefix()
169 if(current_arch->is_native())
170 prefix = FS::get_home_dir()/"local";
172 prefix = FS::get_home_dir()/"local"/current_arch->get_name();
175 void Builder::add_default_tools()
177 for(const LoadedPlugin &p: plugins)
178 p.plugin->add_tools(toolchain, *current_arch);
180 auto i = find_if(toolchain.get_toolchains(), [](const Toolchain *tc){ return (tc->has_tool("CC") || tc->has_tool("CXX")); });
181 if(i!=toolchain.get_toolchains().end())
183 current_arch->refine((*i)->get_name());
185 update_auto_prefix();
189 void Builder::set_logger(const Logger *l)
191 logger = (l ? l : &default_logger);
194 vector<string> Builder::collect_problems() const
196 vector<string> target_problems;
197 vector<string> problems;
198 vector<const Package *> broken_packages;
199 vector<const Component *> broken_components;
200 vector<const Tool *> broken_tools;
202 for(const auto &kvp: build_graph.get_targets())
203 if(kvp.second->is_broken())
205 const Package *package = kvp.second->get_package();
206 if(package && package->is_broken())
207 collect_broken_packages(*package, broken_packages);
209 const Component *component = kvp.second->get_component();
210 if(component && component->is_broken() && !any_equals(broken_components, component))
212 broken_components.push_back(component);
213 collect_broken_packages(component->get_package(), broken_packages);
214 for(const Package *r: component->get_required_packages())
216 collect_broken_packages(*r, broken_packages);
219 const Tool *tool = kvp.second->get_tool();
220 if(tool && tool->is_broken() && !any_equals(broken_tools, tool))
222 broken_tools.push_back(tool);
223 for(const string &p: tool->get_problems())
224 problems.push_back(format("%s: %s", tool->get_tag(), p));
227 for(const string &p: kvp.second->get_problems())
228 target_problems.push_back(format("%s: %s", kvp.second->get_name(), p));
231 for(const Package *p: broken_packages)
233 for(const string &b: p->get_problems())
234 problems.push_back(format("%s: %s", p->get_name(), b));
236 for(const Component *c: broken_components)
237 if(&c->get_package()==p)
239 for(const string &b: c->get_problems())
240 problems.push_back(format("%s/%s: %s", p->get_name(), c->get_name(), b));
244 problems.insert(problems.end(), make_move_iterator(target_problems.begin()), make_move_iterator(target_problems.end()));
249 void Builder::collect_broken_packages(const Package &pkg, vector<const Package *> &broken) const
251 if(any_equals(broken, &pkg))
254 broken.push_back(&pkg);
256 for(const Package *r: pkg.get_required_packages())
258 collect_broken_packages(*r, broken);
261 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
263 IO::BufferedFile in(fn.str());
265 get_logger().log("files", "Reading %s", fn);
267 DataFile::Parser parser(in, fn.str());
268 Loader loader(*this, opts, all);
272 void Builder::save_caches()
274 for(const auto &kvp: package_manager.get_packages())
275 kvp.second->save_caches();
278 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
280 unsigned total = build_graph.count_rebuild_targets();
284 get_logger().log("summary", "Already up to date");
287 get_logger().log("summary", "Will build %d target%s", total, (total!=1 ? "s" : ""));
289 vector<Task *> tasks;
295 bool starved = false;
299 if(tasks.size()<jobs && !fail && !starved)
301 Target *tgt = build_graph.get_buildable_target();
308 get_logger().log("tasks", "%-4s %s", tgt->get_tool()->get_tag(), tgt->get_name());
310 Task *task = tgt->build();
313 get_logger().log("commands", "%s", task->get_command());
316 task->signal_finished.emit(true);
322 tasks.push_back(task);
327 IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
329 else if(tasks.empty())
335 Time::sleep(10*Time::msec);
337 for(unsigned i=0; i<tasks.size();)
340 if(jobs==1 || (tasks.size()==1 && starved))
341 status = tasks[i]->wait();
343 status = tasks[i]->check();
345 if(status!=Task::RUNNING)
350 tasks.erase(tasks.begin()+i);
351 if(status==Task::ERROR)
353 if(tasks.empty() && fail)
365 get_logger().log("summary", "Build failed");
366 else if(show_progress)
367 get_logger().log("summary", "Build complete");
372 int Builder::clean(bool all, bool dry_run)
374 // Cleaning doesn't care about ordering, so a simpler method can be used
376 set<Target *> clean_tgts;
377 deque<Target *> queue;
378 queue.push_back(&build_graph.get_goals());
380 while(!queue.empty())
382 Target *tgt = queue.front();
385 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
386 clean_tgts.insert(tgt);
388 for(Target *t: tgt->get_dependencies())
389 if(!clean_tgts.count(t))
393 for(Target *t: clean_tgts)
395 get_logger().log("tasks", "RM %s", t->get_name());
404 Builder::LoadedPlugin::LoadedPlugin(LoadedPlugin &&other):
405 path(move(other.path)),
406 module(other.module),
413 Builder::LoadedPlugin &Builder::LoadedPlugin::operator=(LoadedPlugin &&other)
417 path = move(other.path);
418 module = other.module;
419 plugin = other.plugin;
425 Builder::LoadedPlugin::~LoadedPlugin()
432 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
433 DataFile::ObjectLoader<Builder>(b),
437 add("architecture", &Loader::architecture);
438 add("binary_package", &Loader::binpkg);
439 add("build_type", &Loader::build_type);
440 add("package", &Loader::package);
443 obj.top_loader = this;
444 else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
445 options = obj.top_loader->options;
448 Builder::Loader::~Loader()
450 if(obj.top_loader==this)
454 void Builder::Loader::architecture(const string &n)
456 if(obj.current_arch->match_name(n))
457 load_sub(*obj.current_arch);
460 void Builder::Loader::binpkg(const string &n)
462 BinaryPackage *pkg = new BinaryPackage(obj, n);
466 void Builder::Loader::build_type(const string &n)
470 auto i = obj.build_types.insert({ n, btype }).first;
472 obj.build_type = &i->second;
475 void Builder::Loader::package(const string &n)
477 SourcePackage *pkg = new SourcePackage(obj, n, get_source());
479 load_sub(*pkg, options);
482 pkg->set_build_type(*obj.build_type);