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"
24 #include "virtualtarget.h"
30 package_manager(*this),
31 native_arch(*this, string()),
34 logger(&default_logger)
36 set_architecture(string());
41 if(current_arch!=&native_arch)
45 void Builder::load_plugins()
47 using CreateFunc = Plugin *(Builder &);
49 FS::Path plugins_dir = FS::get_sys_lib_dir();
50 logger->log("files", "Traversing %s", plugins_dir);
51 for(const string &f: list_filtered(plugins_dir, "\\.dlm$"))
54 plugin.path = plugins_dir/f;
58 plugin.module = new Module(plugin.path.str());
60 catch(const exception &exc)
62 logger->log("plugins", "Failed to load plugin %s: %s", f, exc.what());
68 CreateFunc *create_func = reinterpret_cast<CreateFunc *>(plugin.module->get_symbol("create_plugin"));
69 plugin.plugin = create_func(*this);
72 logger->log("plugins", "Loaded plugin %s", f);
73 plugins.emplace_back(move(plugin));
77 logger->log("plugins", "Plugin %s refused to initialize", f);
79 catch(const exception &exc)
81 logger->log("plugins", "Failed to initialize plugin %s: %s", f, exc.what());
88 void Builder::set_architecture(const string &name)
90 if(current_arch!=&native_arch)
94 current_arch = &native_arch;
96 current_arch = new Architecture(*this, name);
102 vector<string> Builder::get_build_types() const
105 keys.reserve(build_types.size());
106 for(const auto &kvp: build_types)
107 keys.push_back(kvp.first);
111 const BuildType &Builder::get_build_type() const
114 throw invalid_state("no build type");
118 void Builder::set_build_type(const string &name)
120 build_type = &get_item(build_types, name);
123 void Builder::set_prefix(const FS::Path &p)
129 void Builder::set_temp_directory(const FS::Path &p)
134 void Builder::update_auto_prefix()
136 if(current_arch->is_native())
137 prefix = FS::get_home_dir()/"local";
139 prefix = FS::get_home_dir()/"local"/current_arch->get_name();
142 void Builder::add_default_tools()
144 if(current_arch->get_system()=="android")
145 toolchain.add_toolchain(new AndroidTools(*this, *current_arch));
146 toolchain.add_tool(new DataTool(*this));
147 for(const LoadedPlugin &p: plugins)
148 p.plugin->add_tools(toolchain, *current_arch);
150 auto i = find_if(toolchain.get_toolchains(), [](const Toolchain *tc){ return (tc->has_tool("CC") || tc->has_tool("CXX")); });
151 if(i!=toolchain.get_toolchains().end())
153 current_arch->refine((*i)->get_name());
155 update_auto_prefix();
159 void Builder::set_logger(const Logger *l)
161 logger = (l ? l : &default_logger);
164 vector<string> Builder::collect_problems() const
166 vector<string> problems;
167 set<const Package *> broken_packages;
168 set<const Component *> broken_components;
169 set<const Tool *> broken_tools;
171 for(const auto &kvp: build_graph.get_targets())
172 if(kvp.second->is_broken())
174 for(const string &p: kvp.second->get_problems())
175 problems.push_back(format("%s: %s", kvp.second->get_name(), p));
177 const Package *package = kvp.second->get_package();
178 if(package && !package->get_problems().empty())
179 broken_packages.insert(package);
181 const Component *component = kvp.second->get_component();
182 if(component && !component->get_problems().empty())
183 broken_components.insert(component);
185 const Tool *tool = kvp.second->get_tool();
186 if(tool && !tool->get_problems().empty())
187 broken_tools.insert(tool);
190 // TODO Sort components after their packages, and targets last
191 for(const Package *p: broken_packages)
192 for(const string &b: p->get_problems())
193 problems.push_back(format("%s: %s", p->get_name(), b));
195 for(const Component *c: broken_components)
196 for(const string &b: c->get_problems())
197 problems.push_back(format("%s/%s: %s", c->get_package().get_name(), c->get_name(), b));
199 for(const Tool *t: broken_tools)
200 for(const string &b: t->get_problems())
201 problems.push_back(format("%s: %s", t->get_tag(), b));
206 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
208 IO::BufferedFile in(fn.str());
210 get_logger().log("files", "Reading %s", fn);
212 DataFile::Parser parser(in, fn.str());
213 Loader loader(*this, opts, all);
217 void Builder::save_caches()
219 for(const auto &kvp: package_manager.get_packages())
220 kvp.second->save_caches();
223 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
225 unsigned total = build_graph.count_rebuild_targets();
229 get_logger().log("summary", "Already up to date");
232 get_logger().log("summary", "Will build %d target%s", total, (total!=1 ? "s" : ""));
234 vector<Task *> tasks;
240 bool starved = false;
244 if(tasks.size()<jobs && !fail && !starved)
246 Target *tgt = build_graph.get_buildable_target();
253 get_logger().log("tasks", "%-4s %s", tgt->get_tool()->get_tag(), tgt->get_name());
255 Task *task = tgt->build();
258 get_logger().log("commands", "%s", task->get_command());
261 task->signal_finished.emit(true);
267 tasks.push_back(task);
272 IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
274 else if(tasks.empty())
280 Time::sleep(10*Time::msec);
282 for(unsigned i=0; i<tasks.size();)
285 if(jobs==1 || (tasks.size()==1 && starved))
286 status = tasks[i]->wait();
288 status = tasks[i]->check();
290 if(status!=Task::RUNNING)
295 tasks.erase(tasks.begin()+i);
296 if(status==Task::ERROR)
298 if(tasks.empty() && fail)
310 get_logger().log("summary", "Build failed");
311 else if(show_progress)
312 get_logger().log("summary", "Build complete");
317 int Builder::clean(bool all, bool dry_run)
319 // Cleaning doesn't care about ordering, so a simpler method can be used
321 set<Target *> clean_tgts;
322 deque<Target *> queue;
323 queue.push_back(&build_graph.get_goals());
325 while(!queue.empty())
327 Target *tgt = queue.front();
330 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
331 clean_tgts.insert(tgt);
333 for(Target *t: tgt->get_dependencies())
334 if(!clean_tgts.count(t))
338 for(Target *t: clean_tgts)
340 get_logger().log("tasks", "RM %s", t->get_name());
349 Builder::LoadedPlugin::LoadedPlugin(LoadedPlugin &&other):
350 path(move(other.path)),
351 module(other.module),
358 Builder::LoadedPlugin::~LoadedPlugin()
365 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
366 DataFile::ObjectLoader<Builder>(b),
370 add("architecture", &Loader::architecture);
371 add("binary_package", &Loader::binpkg);
372 add("build_type", &Loader::build_type);
373 add("package", &Loader::package);
376 obj.top_loader = this;
377 else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
378 options = obj.top_loader->options;
381 Builder::Loader::~Loader()
383 if(obj.top_loader==this)
387 void Builder::Loader::architecture(const string &n)
389 if(obj.current_arch->match_name(n))
390 load_sub(*obj.current_arch);
393 void Builder::Loader::binpkg(const string &n)
395 BinaryPackage *pkg = new BinaryPackage(obj, n);
399 void Builder::Loader::build_type(const string &n)
403 auto i = obj.build_types.insert({ n, btype }).first;
405 obj.build_type = &i->second;
408 void Builder::Loader::package(const string &n)
410 SourcePackage *pkg = new SourcePackage(obj, n, get_source());
412 load_sub(*pkg, options);
415 pkg->set_build_type(*obj.build_type);