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());
85 void Builder::set_architecture(const string &name)
87 if(current_arch!=&native_arch)
91 current_arch = &native_arch;
93 current_arch = new Architecture(*this, name);
99 vector<string> Builder::get_build_types() const
102 keys.reserve(build_types.size());
103 for(const auto &kvp: build_types)
104 keys.push_back(kvp.first);
108 const BuildType &Builder::get_build_type() const
111 throw invalid_state("no build type");
115 void Builder::set_build_type(const string &name)
117 build_type = &get_item(build_types, name);
120 void Builder::set_prefix(const FS::Path &p)
126 void Builder::set_temp_directory(const FS::Path &p)
131 void Builder::update_auto_prefix()
133 if(current_arch->is_native())
134 prefix = FS::get_home_dir()/"local";
136 prefix = FS::get_home_dir()/"local"/current_arch->get_name();
139 void Builder::add_default_tools()
141 for(const LoadedPlugin &p: plugins)
142 p.plugin->add_tools(toolchain, *current_arch);
144 auto i = find_if(toolchain.get_toolchains(), [](const Toolchain *tc){ return (tc->has_tool("CC") || tc->has_tool("CXX")); });
145 if(i!=toolchain.get_toolchains().end())
147 current_arch->refine((*i)->get_name());
149 update_auto_prefix();
153 void Builder::set_logger(const Logger *l)
155 logger = (l ? l : &default_logger);
158 vector<string> Builder::collect_problems() const
160 vector<string> target_problems;
161 vector<string> problems;
162 vector<const Package *> broken_packages;
163 vector<const Component *> broken_components;
164 vector<const Tool *> broken_tools;
166 for(const auto &kvp: build_graph.get_targets())
167 if(kvp.second->is_broken())
169 const Package *package = kvp.second->get_package();
170 if(package && package->is_broken())
171 collect_broken_packages(*package, broken_packages);
173 const Component *component = kvp.second->get_component();
174 if(component && component->is_broken() && !any_equals(broken_components, component))
176 broken_components.push_back(component);
177 collect_broken_packages(component->get_package(), broken_packages);
178 for(const Package *r: component->get_required_packages())
180 collect_broken_packages(*r, broken_packages);
183 const Tool *tool = kvp.second->get_tool();
184 if(tool && tool->is_broken() && !any_equals(broken_tools, tool))
186 broken_tools.push_back(tool);
187 for(const string &p: tool->get_problems())
188 problems.push_back(format("%s: %s", tool->get_tag(), p));
191 for(const string &p: kvp.second->get_problems())
192 target_problems.push_back(format("%s: %s", kvp.second->get_name(), p));
195 for(const Package *p: broken_packages)
197 for(const string &b: p->get_problems())
198 problems.push_back(format("%s: %s", p->get_name(), b));
200 for(const Component *c: broken_components)
201 if(&c->get_package()==p)
203 for(const string &b: c->get_problems())
204 problems.push_back(format("%s/%s: %s", p->get_name(), c->get_name(), b));
208 problems.insert(problems.end(), make_move_iterator(target_problems.begin()), make_move_iterator(target_problems.end()));
213 void Builder::collect_broken_packages(const Package &pkg, vector<const Package *> &broken) const
215 if(any_equals(broken, &pkg))
218 broken.push_back(&pkg);
220 for(const Package *r: pkg.get_required_packages())
222 collect_broken_packages(*r, broken);
225 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
227 IO::BufferedFile in(fn.str());
229 get_logger().log("files", "Reading %s", fn);
231 DataFile::Parser parser(in, fn.str());
232 Loader loader(*this, opts, all);
236 void Builder::save_caches()
238 for(const auto &kvp: package_manager.get_packages())
239 kvp.second->save_caches();
242 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
244 unsigned total = build_graph.count_rebuild_targets();
248 get_logger().log("summary", "Already up to date");
251 get_logger().log("summary", "Will build %d target%s", total, (total!=1 ? "s" : ""));
253 vector<Task *> tasks;
259 bool starved = false;
263 if(tasks.size()<jobs && !fail && !starved)
265 Target *tgt = build_graph.get_buildable_target();
272 get_logger().log("tasks", "%-4s %s", tgt->get_tool()->get_tag(), tgt->get_name());
274 Task *task = tgt->build();
277 get_logger().log("commands", "%s", task->get_command());
280 task->signal_finished.emit(true);
286 tasks.push_back(task);
291 IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
293 else if(tasks.empty())
299 Time::sleep(10*Time::msec);
301 for(unsigned i=0; i<tasks.size();)
304 if(jobs==1 || (tasks.size()==1 && starved))
305 status = tasks[i]->wait();
307 status = tasks[i]->check();
309 if(status!=Task::RUNNING)
314 tasks.erase(tasks.begin()+i);
315 if(status==Task::ERROR)
317 if(tasks.empty() && fail)
329 get_logger().log("summary", "Build failed");
330 else if(show_progress)
331 get_logger().log("summary", "Build complete");
336 int Builder::clean(bool all, bool dry_run)
338 // Cleaning doesn't care about ordering, so a simpler method can be used
340 set<Target *> clean_tgts;
341 deque<Target *> queue;
342 queue.push_back(&build_graph.get_goals());
344 while(!queue.empty())
346 Target *tgt = queue.front();
349 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
350 clean_tgts.insert(tgt);
352 for(Target *t: tgt->get_dependencies())
353 if(!clean_tgts.count(t))
357 for(Target *t: clean_tgts)
359 get_logger().log("tasks", "RM %s", t->get_name());
368 Builder::LoadedPlugin::LoadedPlugin(LoadedPlugin &&other):
369 path(move(other.path)),
370 module(other.module),
377 Builder::LoadedPlugin::~LoadedPlugin()
384 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
385 DataFile::ObjectLoader<Builder>(b),
389 add("architecture", &Loader::architecture);
390 add("binary_package", &Loader::binpkg);
391 add("build_type", &Loader::build_type);
392 add("package", &Loader::package);
395 obj.top_loader = this;
396 else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
397 options = obj.top_loader->options;
400 Builder::Loader::~Loader()
402 if(obj.top_loader==this)
406 void Builder::Loader::architecture(const string &n)
408 if(obj.current_arch->match_name(n))
409 load_sub(*obj.current_arch);
412 void Builder::Loader::binpkg(const string &n)
414 BinaryPackage *pkg = new BinaryPackage(obj, n);
418 void Builder::Loader::build_type(const string &n)
422 auto i = obj.build_types.insert({ n, btype }).first;
424 obj.build_type = &i->second;
427 void Builder::Loader::package(const string &n)
429 SourcePackage *pkg = new SourcePackage(obj, n, get_source());
431 load_sub(*pkg, options);
434 pkg->set_build_type(*obj.build_type);