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 "android/androidtools.h"
16 #include "binarypackage.h"
18 #include "datafile/datatool.h"
19 #include "installedfile.h"
22 #include "sharedlibrary.h"
23 #include "sourcepackage.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 logger->log("files", "Traversing %s", plugins_dir);
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 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());
89 void Builder::set_architecture(const string &name)
91 if(current_arch!=&native_arch)
95 current_arch = &native_arch;
97 current_arch = new Architecture(*this, name);
100 update_auto_prefix();
103 vector<string> Builder::get_build_types() const
106 keys.reserve(build_types.size());
107 for(const auto &kvp: build_types)
108 keys.push_back(kvp.first);
112 const BuildType &Builder::get_build_type() const
115 throw invalid_state("no build type");
119 void Builder::set_build_type(const string &name)
121 build_type = &get_item(build_types, name);
124 void Builder::set_prefix(const FS::Path &p)
130 void Builder::set_temp_directory(const FS::Path &p)
135 void Builder::update_auto_prefix()
137 if(current_arch->is_native())
138 prefix = FS::get_home_dir()/"local";
140 prefix = FS::get_home_dir()/"local"/current_arch->get_name();
143 void Builder::add_default_tools()
145 if(current_arch->get_system()=="android")
146 toolchain.add_toolchain(new AndroidTools(*this, *current_arch));
147 toolchain.add_tool(new DataTool(*this));
148 for(const LoadedPlugin &p: plugins)
149 p.plugin->add_tools(toolchain, *current_arch);
151 auto i = find_if(toolchain.get_toolchains(), [](const Toolchain *tc){ return (tc->has_tool("CC") || tc->has_tool("CXX")); });
152 if(i!=toolchain.get_toolchains().end())
154 current_arch->refine((*i)->get_name());
156 update_auto_prefix();
160 void Builder::set_logger(const Logger *l)
162 logger = (l ? l : &default_logger);
165 vector<string> Builder::collect_problems() const
167 vector<string> problems;
168 set<const Package *> broken_packages;
169 set<const Component *> broken_components;
170 set<const Tool *> broken_tools;
172 for(const auto &kvp: build_graph.get_targets())
173 if(kvp.second->is_broken())
175 for(const string &p: kvp.second->get_problems())
176 problems.push_back(format("%s: %s", kvp.second->get_name(), p));
178 const Package *package = kvp.second->get_package();
179 if(package && !package->get_problems().empty())
180 broken_packages.insert(package);
182 const Component *component = kvp.second->get_component();
183 if(component && !component->get_problems().empty())
184 broken_components.insert(component);
186 const Tool *tool = kvp.second->get_tool();
187 if(tool && !tool->get_problems().empty())
188 broken_tools.insert(tool);
191 // TODO Sort components after their packages, and targets last
192 for(const Package *p: broken_packages)
193 for(const string &b: p->get_problems())
194 problems.push_back(format("%s: %s", p->get_name(), b));
196 for(const Component *c: broken_components)
197 for(const string &b: c->get_problems())
198 problems.push_back(format("%s/%s: %s", c->get_package().get_name(), c->get_name(), b));
200 for(const Tool *t: broken_tools)
201 for(const string &b: t->get_problems())
202 problems.push_back(format("%s: %s", t->get_tag(), b));
207 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
209 IO::BufferedFile in(fn.str());
211 get_logger().log("files", "Reading %s", fn);
213 DataFile::Parser parser(in, fn.str());
214 Loader loader(*this, opts, all);
218 void Builder::save_caches()
220 for(const auto &kvp: package_manager.get_packages())
221 kvp.second->save_caches();
224 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
226 unsigned total = build_graph.count_rebuild_targets();
230 get_logger().log("summary", "Already up to date");
233 get_logger().log("summary", "Will build %d target%s", total, (total!=1 ? "s" : ""));
235 vector<Task *> tasks;
241 bool starved = false;
245 if(tasks.size()<jobs && !fail && !starved)
247 Target *tgt = build_graph.get_buildable_target();
254 get_logger().log("tasks", "%-4s %s", tgt->get_tool()->get_tag(), tgt->get_name());
256 Task *task = tgt->build();
259 get_logger().log("commands", "%s", task->get_command());
262 task->signal_finished.emit(true);
268 tasks.push_back(task);
273 IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
275 else if(tasks.empty())
281 Time::sleep(10*Time::msec);
283 for(unsigned i=0; i<tasks.size();)
286 if(jobs==1 || (tasks.size()==1 && starved))
287 status = tasks[i]->wait();
289 status = tasks[i]->check();
291 if(status!=Task::RUNNING)
296 tasks.erase(tasks.begin()+i);
297 if(status==Task::ERROR)
299 if(tasks.empty() && fail)
311 get_logger().log("summary", "Build failed");
312 else if(show_progress)
313 get_logger().log("summary", "Build complete");
318 int Builder::clean(bool all, bool dry_run)
320 // Cleaning doesn't care about ordering, so a simpler method can be used
322 set<Target *> clean_tgts;
323 deque<Target *> queue;
324 queue.push_back(&build_graph.get_goals());
326 while(!queue.empty())
328 Target *tgt = queue.front();
331 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
332 clean_tgts.insert(tgt);
334 for(Target *t: tgt->get_dependencies())
335 if(!clean_tgts.count(t))
339 for(Target *t: clean_tgts)
341 get_logger().log("tasks", "RM %s", t->get_name());
350 Builder::LoadedPlugin::LoadedPlugin(LoadedPlugin &&other):
351 path(move(other.path)),
352 module(other.module),
359 Builder::LoadedPlugin::~LoadedPlugin()
366 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
367 DataFile::ObjectLoader<Builder>(b),
371 add("architecture", &Loader::architecture);
372 add("binary_package", &Loader::binpkg);
373 add("build_type", &Loader::build_type);
374 add("package", &Loader::package);
377 obj.top_loader = this;
378 else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
379 options = obj.top_loader->options;
382 Builder::Loader::~Loader()
384 if(obj.top_loader==this)
388 void Builder::Loader::architecture(const string &n)
390 if(obj.current_arch->match_name(n))
391 load_sub(*obj.current_arch);
394 void Builder::Loader::binpkg(const string &n)
396 BinaryPackage *pkg = new BinaryPackage(obj, n);
400 void Builder::Loader::build_type(const string &n)
404 auto i = obj.build_types.insert({ n, btype }).first;
406 obj.build_type = &i->second;
409 void Builder::Loader::package(const string &n)
411 SourcePackage *pkg = new SourcePackage(obj, n, get_source());
413 load_sub(*pkg, options);
416 pkg->set_build_type(*obj.build_type);