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> problems;
163 set<const Package *> broken_packages;
164 set<const Component *> broken_components;
165 set<const Tool *> broken_tools;
167 for(const auto &kvp: build_graph.get_targets())
168 if(kvp.second->is_broken())
170 for(const string &p: kvp.second->get_problems())
171 problems.push_back(format("%s: %s", kvp.second->get_name(), p));
173 const Package *package = kvp.second->get_package();
174 if(package && !package->get_problems().empty())
175 broken_packages.insert(package);
177 const Component *component = kvp.second->get_component();
178 if(component && !component->get_problems().empty())
179 broken_components.insert(component);
181 const Tool *tool = kvp.second->get_tool();
182 if(tool && !tool->get_problems().empty())
183 broken_tools.insert(tool);
186 // TODO Sort components after their packages, and targets last
187 for(const Package *p: broken_packages)
188 for(const string &b: p->get_problems())
189 problems.push_back(format("%s: %s", p->get_name(), b));
191 for(const Component *c: broken_components)
192 for(const string &b: c->get_problems())
193 problems.push_back(format("%s/%s: %s", c->get_package().get_name(), c->get_name(), b));
195 for(const Tool *t: broken_tools)
196 for(const string &b: t->get_problems())
197 problems.push_back(format("%s: %s", t->get_tag(), b));
202 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
204 IO::BufferedFile in(fn.str());
206 get_logger().log("files", "Reading %s", fn);
208 DataFile::Parser parser(in, fn.str());
209 Loader loader(*this, opts, all);
213 void Builder::save_caches()
215 for(const auto &kvp: package_manager.get_packages())
216 kvp.second->save_caches();
219 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
221 unsigned total = build_graph.count_rebuild_targets();
225 get_logger().log("summary", "Already up to date");
228 get_logger().log("summary", "Will build %d target%s", total, (total!=1 ? "s" : ""));
230 vector<Task *> tasks;
236 bool starved = false;
240 if(tasks.size()<jobs && !fail && !starved)
242 Target *tgt = build_graph.get_buildable_target();
249 get_logger().log("tasks", "%-4s %s", tgt->get_tool()->get_tag(), tgt->get_name());
251 Task *task = tgt->build();
254 get_logger().log("commands", "%s", task->get_command());
257 task->signal_finished.emit(true);
263 tasks.push_back(task);
268 IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
270 else if(tasks.empty())
276 Time::sleep(10*Time::msec);
278 for(unsigned i=0; i<tasks.size();)
281 if(jobs==1 || (tasks.size()==1 && starved))
282 status = tasks[i]->wait();
284 status = tasks[i]->check();
286 if(status!=Task::RUNNING)
291 tasks.erase(tasks.begin()+i);
292 if(status==Task::ERROR)
294 if(tasks.empty() && fail)
306 get_logger().log("summary", "Build failed");
307 else if(show_progress)
308 get_logger().log("summary", "Build complete");
313 int Builder::clean(bool all, bool dry_run)
315 // Cleaning doesn't care about ordering, so a simpler method can be used
317 set<Target *> clean_tgts;
318 deque<Target *> queue;
319 queue.push_back(&build_graph.get_goals());
321 while(!queue.empty())
323 Target *tgt = queue.front();
326 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
327 clean_tgts.insert(tgt);
329 for(Target *t: tgt->get_dependencies())
330 if(!clean_tgts.count(t))
334 for(Target *t: clean_tgts)
336 get_logger().log("tasks", "RM %s", t->get_name());
345 Builder::LoadedPlugin::LoadedPlugin(LoadedPlugin &&other):
346 path(move(other.path)),
347 module(other.module),
354 Builder::LoadedPlugin::~LoadedPlugin()
361 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
362 DataFile::ObjectLoader<Builder>(b),
366 add("architecture", &Loader::architecture);
367 add("binary_package", &Loader::binpkg);
368 add("build_type", &Loader::build_type);
369 add("package", &Loader::package);
372 obj.top_loader = this;
373 else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
374 options = obj.top_loader->options;
377 Builder::Loader::~Loader()
379 if(obj.top_loader==this)
383 void Builder::Loader::architecture(const string &n)
385 if(obj.current_arch->match_name(n))
386 load_sub(*obj.current_arch);
389 void Builder::Loader::binpkg(const string &n)
391 BinaryPackage *pkg = new BinaryPackage(obj, n);
395 void Builder::Loader::build_type(const string &n)
399 auto i = obj.build_types.insert({ n, btype }).first;
401 obj.build_type = &i->second;
404 void Builder::Loader::package(const string &n)
406 SourcePackage *pkg = new SourcePackage(obj, n, get_source());
408 load_sub(*pkg, options);
411 pkg->set_build_type(*obj.build_type);