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 auto have_plugin = [this](const string &r){
91 return any_of(plugins.begin(), plugins.end(), [&r](const LoadedPlugin &p){ return FS::basepart(FS::basename(p.path))==r; });
94 while(!unordered_plugins.empty())
96 bool any_added = false;
97 for(auto i=unordered_plugins.begin(); i!=unordered_plugins.end(); )
99 const vector<string> &required = i->plugin->get_required_plugins();
100 if(all_of(required.begin(), required.end(), have_plugin))
102 plugins.push_back(move(*i));
103 i = unordered_plugins.erase(i);
114 for(const LoadedPlugin &p: unordered_plugins)
116 vector<string> missing;
117 for(const string &r: p.plugin->get_required_plugins())
119 missing.push_back(r);
120 logger->log("plugins", "Missing required plugins for plugin %s: %s",
121 FS::basename(p.path), join(missing.begin(), missing.end()));
125 void Builder::set_architecture(const string &name)
127 if(current_arch!=&native_arch)
131 current_arch = &native_arch;
133 current_arch = new Architecture(*this, name);
136 update_auto_prefix();
139 vector<string> Builder::get_build_types() const
142 keys.reserve(build_types.size());
143 for(const auto &kvp: build_types)
144 keys.push_back(kvp.first);
148 const BuildType &Builder::get_build_type() const
151 throw invalid_state("no build type");
155 void Builder::set_build_type(const string &name)
157 build_type = &get_item(build_types, name);
160 void Builder::set_prefix(const FS::Path &p)
166 void Builder::set_temp_directory(const FS::Path &p)
171 void Builder::update_auto_prefix()
173 if(current_arch->is_native())
174 prefix = FS::get_home_dir()/"local";
176 prefix = FS::get_home_dir()/"local"/current_arch->get_name();
179 void Builder::add_default_tools()
181 for(const LoadedPlugin &p: plugins)
182 p.plugin->add_tools(toolchain, *current_arch);
184 auto i = find_if(toolchain.get_toolchains(), [](const Toolchain *tc){ return (tc->has_tool("CC") || tc->has_tool("CXX")); });
185 if(i!=toolchain.get_toolchains().end())
187 current_arch->refine((*i)->get_name());
189 update_auto_prefix();
193 void Builder::set_logger(const Logger *l)
195 logger = (l ? l : &default_logger);
198 vector<string> Builder::collect_problems() const
200 vector<string> target_problems;
201 vector<string> problems;
202 vector<const Package *> broken_packages;
203 vector<const Component *> broken_components;
204 vector<const Tool *> broken_tools;
206 for(const auto &kvp: build_graph.get_targets())
207 if(kvp.second->is_broken())
209 const Package *package = kvp.second->get_package();
210 if(package && package->is_broken())
211 collect_broken_packages(*package, broken_packages);
213 const Component *component = kvp.second->get_component();
214 if(component && component->is_broken() && !any_equals(broken_components, component))
216 broken_components.push_back(component);
217 collect_broken_packages(component->get_package(), broken_packages);
218 for(const Package *r: component->get_required_packages())
220 collect_broken_packages(*r, broken_packages);
223 const Tool *tool = kvp.second->get_tool();
224 if(tool && tool->is_broken() && !any_equals(broken_tools, tool))
226 broken_tools.push_back(tool);
227 for(const string &p: tool->get_problems())
228 problems.push_back(format("%s: %s", tool->get_tag(), p));
231 for(const string &p: kvp.second->get_problems())
232 target_problems.push_back(format("%s: %s", kvp.second->get_name(), p));
235 for(const Package *p: broken_packages)
237 for(const string &b: p->get_problems())
238 problems.push_back(format("%s: %s", p->get_name(), b));
240 for(const Component *c: broken_components)
241 if(&c->get_package()==p)
243 for(const string &b: c->get_problems())
244 problems.push_back(format("%s/%s: %s", p->get_name(), c->get_name(), b));
248 problems.insert(problems.end(), make_move_iterator(target_problems.begin()), make_move_iterator(target_problems.end()));
253 void Builder::collect_broken_packages(const Package &pkg, vector<const Package *> &broken) const
255 if(any_equals(broken, &pkg))
258 broken.push_back(&pkg);
260 for(const Package *r: pkg.get_required_packages())
262 collect_broken_packages(*r, broken);
265 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
267 IO::BufferedFile in(fn.str());
269 get_logger().log("files", "Reading %s", fn);
271 DataFile::Parser parser(in, fn.str());
272 Loader loader(*this, opts, all);
276 void Builder::save_caches()
278 for(const auto &kvp: package_manager.get_packages())
279 kvp.second->save_caches();
282 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
284 unsigned total = build_graph.count_rebuild_targets();
288 get_logger().log("summary", "Already up to date");
291 get_logger().log("summary", "Will build %d target%s", total, (total!=1 ? "s" : ""));
293 vector<Task *> tasks;
299 bool starved = false;
303 if(tasks.size()<jobs && !fail && !starved)
305 Target *tgt = build_graph.get_buildable_target();
312 get_logger().log("tasks", "%-4s %s", tgt->get_tool()->get_tag(), tgt->get_name());
314 Task *task = tgt->build();
317 get_logger().log("commands", "%s", task->get_command());
320 task->signal_finished.emit(true);
326 tasks.push_back(task);
331 IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
333 else if(tasks.empty())
339 Time::sleep(10*Time::msec);
341 for(unsigned i=0; i<tasks.size();)
344 if(jobs==1 || (tasks.size()==1 && starved))
345 status = tasks[i]->wait();
347 status = tasks[i]->check();
349 if(status!=Task::RUNNING)
354 tasks.erase(tasks.begin()+i);
355 if(status==Task::ERROR)
357 if(tasks.empty() && fail)
369 get_logger().log("summary", "Build failed");
370 else if(show_progress)
371 get_logger().log("summary", "Build complete");
376 int Builder::clean(bool all, bool dry_run)
378 // Cleaning doesn't care about ordering, so a simpler method can be used
380 set<Target *> clean_tgts;
381 deque<Target *> queue;
382 queue.push_back(&build_graph.get_goals());
384 while(!queue.empty())
386 Target *tgt = queue.front();
389 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
390 clean_tgts.insert(tgt);
392 for(Target *t: tgt->get_dependencies())
393 if(!clean_tgts.count(t))
397 for(Target *t: clean_tgts)
399 get_logger().log("tasks", "RM %s", t->get_name());
408 Builder::LoadedPlugin::LoadedPlugin(LoadedPlugin &&other):
409 path(move(other.path)),
410 module(other.module),
417 Builder::LoadedPlugin &Builder::LoadedPlugin::operator=(LoadedPlugin &&other)
421 path = move(other.path);
422 module = other.module;
423 plugin = other.plugin;
429 Builder::LoadedPlugin::~LoadedPlugin()
436 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
437 DataFile::ObjectLoader<Builder>(b),
441 add("architecture", &Loader::architecture);
442 add("binary_package", &Loader::binpkg);
443 add("build_type", &Loader::build_type);
444 add("package", &Loader::package);
447 obj.top_loader = this;
448 else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
449 options = obj.top_loader->options;
452 Builder::Loader::~Loader()
454 if(obj.top_loader==this)
458 void Builder::Loader::architecture(const string &n)
460 if(obj.current_arch->match_name(n))
461 load_sub(*obj.current_arch);
464 void Builder::Loader::binpkg(const string &n)
466 BinaryPackage *pkg = new BinaryPackage(obj, n);
470 void Builder::Loader::build_type(const string &n)
474 auto i = obj.build_types.insert({ n, btype }).first;
476 obj.build_type = &i->second;
479 void Builder::Loader::package(const string &n)
481 SourcePackage *pkg = new SourcePackage(obj, n, get_source());
483 load_sub(*pkg, options);
486 pkg->set_build_type(*obj.build_type);