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/time/timedelta.h>
14 #include <msp/time/utils.h>
15 #include "binarypackage.h"
17 #include "installedfile.h"
20 #include "sharedlibrary.h"
23 #include "virtualtarget.h"
29 package_manager(*this),
30 native_arch(*this, string()),
33 logger(&default_logger)
35 set_architecture(string());
40 if(current_arch!=&native_arch)
44 void Builder::load_plugins()
46 using CreateFunc = Plugin *(Builder &);
48 FS::Path plugins_dir = FS::get_sys_lib_dir();
49 logger->log("files", "Traversing %s", plugins_dir);
50 for(const string &f: list_filtered(plugins_dir, "\\.dlm$"))
53 plugin.path = plugins_dir/f;
57 plugin.module = new Module(plugin.path.str());
59 catch(const exception &exc)
61 logger->log("plugins", "Failed to load plugin %s: %s", f, exc.what());
67 CreateFunc *create_func = reinterpret_cast<CreateFunc *>(plugin.module->get_symbol("create_plugin"));
68 plugin.plugin = create_func(*this);
71 logger->log("plugins", "Loaded plugin %s", f);
72 plugins.emplace_back(move(plugin));
76 logger->log("plugins", "Plugin %s refused to initialize", f);
78 catch(const exception &exc)
80 logger->log("plugins", "Failed to initialize plugin %s: %s", f, exc.what());
87 void Builder::set_architecture(const string &name)
89 if(current_arch!=&native_arch)
93 current_arch = &native_arch;
95 current_arch = new Architecture(*this, name);
101 vector<string> Builder::get_build_types() const
104 keys.reserve(build_types.size());
105 for(const auto &kvp: build_types)
106 keys.push_back(kvp.first);
110 const BuildType &Builder::get_build_type() const
113 throw invalid_state("no build type");
117 void Builder::set_build_type(const string &name)
119 build_type = &get_item(build_types, name);
122 void Builder::set_prefix(const FS::Path &p)
128 void Builder::set_temp_directory(const FS::Path &p)
133 void Builder::update_auto_prefix()
135 if(current_arch->is_native())
136 prefix = FS::get_home_dir()/"local";
138 prefix = FS::get_home_dir()/"local"/current_arch->get_name();
141 void Builder::add_default_tools()
143 for(const LoadedPlugin &p: plugins)
144 p.plugin->add_tools(toolchain, *current_arch);
146 auto i = find_if(toolchain.get_toolchains(), [](const Toolchain *tc){ return (tc->has_tool("CC") || tc->has_tool("CXX")); });
147 if(i!=toolchain.get_toolchains().end())
149 current_arch->refine((*i)->get_name());
151 update_auto_prefix();
155 void Builder::set_logger(const Logger *l)
157 logger = (l ? l : &default_logger);
160 vector<string> Builder::collect_problems() const
162 vector<string> target_problems;
163 vector<string> problems;
164 vector<const Package *> broken_packages;
165 vector<const Component *> broken_components;
166 vector<const Tool *> broken_tools;
168 for(const auto &kvp: build_graph.get_targets())
169 if(kvp.second->is_broken())
171 const Package *package = kvp.second->get_package();
172 if(package && package->is_broken())
173 collect_broken_packages(*package, broken_packages);
175 const Component *component = kvp.second->get_component();
176 if(component && component->is_broken() && !any_equals(broken_components, component))
178 broken_components.push_back(component);
179 collect_broken_packages(component->get_package(), broken_packages);
180 for(const Package *r: component->get_required_packages())
182 collect_broken_packages(*r, broken_packages);
185 const Tool *tool = kvp.second->get_tool();
186 if(tool && tool->is_broken() && !any_equals(broken_tools, tool))
188 broken_tools.push_back(tool);
189 for(const string &p: tool->get_problems())
190 problems.push_back(format("%s: %s", tool->get_tag(), p));
193 for(const string &p: kvp.second->get_problems())
194 target_problems.push_back(format("%s: %s", kvp.second->get_name(), p));
197 for(const Package *p: broken_packages)
199 for(const string &b: p->get_problems())
200 problems.push_back(format("%s: %s", p->get_name(), b));
202 for(const Component *c: broken_components)
203 if(&c->get_package()==p)
205 for(const string &b: c->get_problems())
206 problems.push_back(format("%s/%s: %s", p->get_name(), c->get_name(), b));
210 problems.insert(problems.end(), make_move_iterator(target_problems.begin()), make_move_iterator(target_problems.end()));
215 void Builder::collect_broken_packages(const Package &pkg, vector<const Package *> &broken) const
217 if(any_equals(broken, &pkg))
220 broken.push_back(&pkg);
222 for(const Package *r: pkg.get_required_packages())
224 collect_broken_packages(*r, broken);
227 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
229 IO::BufferedFile in(fn.str());
231 get_logger().log("files", "Reading %s", fn);
233 DataFile::Parser parser(in, fn.str());
234 Loader loader(*this, opts, all);
238 void Builder::save_caches()
240 for(const auto &kvp: package_manager.get_packages())
241 kvp.second->save_caches();
244 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
246 unsigned total = build_graph.count_rebuild_targets();
250 get_logger().log("summary", "Already up to date");
253 get_logger().log("summary", "Will build %d target%s", total, (total!=1 ? "s" : ""));
255 vector<Task *> tasks;
261 bool starved = false;
265 if(tasks.size()<jobs && !fail && !starved)
267 Target *tgt = build_graph.get_buildable_target();
274 get_logger().log("tasks", "%-4s %s", tgt->get_tool()->get_tag(), tgt->get_name());
276 Task *task = tgt->build();
279 get_logger().log("commands", "%s", task->get_command());
282 task->signal_finished.emit(true);
288 tasks.push_back(task);
293 IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
295 else if(tasks.empty())
301 Time::sleep(10*Time::msec);
303 for(unsigned i=0; i<tasks.size();)
306 if(jobs==1 || (tasks.size()==1 && starved))
307 status = tasks[i]->wait();
309 status = tasks[i]->check();
311 if(status!=Task::RUNNING)
316 tasks.erase(tasks.begin()+i);
317 if(status==Task::ERROR)
319 if(tasks.empty() && fail)
331 get_logger().log("summary", "Build failed");
332 else if(show_progress)
333 get_logger().log("summary", "Build complete");
338 int Builder::clean(bool all, bool dry_run)
340 // Cleaning doesn't care about ordering, so a simpler method can be used
342 set<Target *> clean_tgts;
343 deque<Target *> queue;
344 queue.push_back(&build_graph.get_goals());
346 while(!queue.empty())
348 Target *tgt = queue.front();
351 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
352 clean_tgts.insert(tgt);
354 for(Target *t: tgt->get_dependencies())
355 if(!clean_tgts.count(t))
359 for(Target *t: clean_tgts)
361 get_logger().log("tasks", "RM %s", t->get_name());
370 Builder::LoadedPlugin::LoadedPlugin(LoadedPlugin &&other):
371 path(move(other.path)),
372 module(other.module),
379 Builder::LoadedPlugin::~LoadedPlugin()
386 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
387 DataFile::ObjectLoader<Builder>(b),
391 add("architecture", &Loader::architecture);
392 add("binary_package", &Loader::binpkg);
393 add("build_type", &Loader::build_type);
394 add("package", &Loader::package);
397 obj.top_loader = this;
398 else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
399 options = obj.top_loader->options;
402 Builder::Loader::~Loader()
404 if(obj.top_loader==this)
408 void Builder::Loader::architecture(const string &n)
410 if(obj.current_arch->match_name(n))
411 load_sub(*obj.current_arch);
414 void Builder::Loader::binpkg(const string &n)
416 BinaryPackage *pkg = new BinaryPackage(obj, n);
420 void Builder::Loader::build_type(const string &n)
424 auto i = obj.build_types.insert({ n, btype }).first;
426 obj.build_type = &i->second;
429 void Builder::Loader::package(const string &n)
431 SourcePackage *pkg = new SourcePackage(obj, n, get_source());
433 load_sub(*pkg, options);
436 pkg->set_build_type(*obj.build_type);