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 "builtin/builtintools.h"
19 #include "datafile/datatool.h"
20 #include "installedfile.h"
23 #include "sharedlibrary.h"
24 #include "sourcepackage.h"
26 #include "virtualtarget.h"
32 package_manager(*this),
33 native_arch(*this, string()),
36 logger(&default_logger)
38 set_architecture(string());
43 if(current_arch!=&native_arch)
47 void Builder::load_plugins()
49 using CreateFunc = Plugin *(Builder &);
51 FS::Path plugins_dir = FS::get_sys_lib_dir();
52 logger->log("files", "Traversing %s", plugins_dir);
53 for(const string &f: list_filtered(plugins_dir, "\\.dlm$"))
56 plugin.path = plugins_dir/f;
60 plugin.module = new Module(plugin.path.str());
62 catch(const exception &exc)
64 logger->log("plugins", "Failed to load plugin %s: %s", f, exc.what());
70 CreateFunc *create_func = reinterpret_cast<CreateFunc *>(plugin.module->get_symbol("create_plugin"));
71 plugin.plugin = create_func(*this);
74 logger->log("plugins", "Loaded plugin %s", f);
75 plugins.emplace_back(move(plugin));
79 logger->log("plugins", "Plugin %s refused to initialize", f);
81 catch(const exception &exc)
83 logger->log("plugins", "Failed to initialize plugin %s: %s", f, exc.what());
90 void Builder::set_architecture(const string &name)
92 if(current_arch!=&native_arch)
96 current_arch = &native_arch;
98 current_arch = new Architecture(*this, name);
101 update_auto_prefix();
104 vector<string> Builder::get_build_types() const
107 keys.reserve(build_types.size());
108 for(const auto &kvp: build_types)
109 keys.push_back(kvp.first);
113 const BuildType &Builder::get_build_type() const
116 throw invalid_state("no build type");
120 void Builder::set_build_type(const string &name)
122 build_type = &get_item(build_types, name);
125 void Builder::set_prefix(const FS::Path &p)
131 void Builder::set_temp_directory(const FS::Path &p)
136 void Builder::update_auto_prefix()
138 if(current_arch->is_native())
139 prefix = FS::get_home_dir()/"local";
141 prefix = FS::get_home_dir()/"local"/current_arch->get_name();
144 void Builder::add_default_tools()
146 if(current_arch->get_system()=="android")
147 toolchain.add_toolchain(new AndroidTools(*this, *current_arch));
148 toolchain.add_toolchain(new BuiltinTools(*this));
149 toolchain.add_tool(new DataTool(*this));
150 for(const LoadedPlugin &p: plugins)
151 p.plugin->add_tools(toolchain, *current_arch);
153 auto i = find_if(toolchain.get_toolchains(), [](const Toolchain *tc){ return (tc->has_tool("CC") || tc->has_tool("CXX")); });
154 if(i!=toolchain.get_toolchains().end())
156 current_arch->refine((*i)->get_name());
158 update_auto_prefix();
162 void Builder::set_logger(const Logger *l)
164 logger = (l ? l : &default_logger);
167 vector<string> Builder::collect_problems() const
169 vector<string> problems;
170 set<const Package *> broken_packages;
171 set<const Component *> broken_components;
172 set<const Tool *> broken_tools;
174 for(const auto &kvp: build_graph.get_targets())
175 if(kvp.second->is_broken())
177 for(const string &p: kvp.second->get_problems())
178 problems.push_back(format("%s: %s", kvp.second->get_name(), p));
180 const Package *package = kvp.second->get_package();
181 if(package && !package->get_problems().empty())
182 broken_packages.insert(package);
184 const Component *component = kvp.second->get_component();
185 if(component && !component->get_problems().empty())
186 broken_components.insert(component);
188 const Tool *tool = kvp.second->get_tool();
189 if(tool && !tool->get_problems().empty())
190 broken_tools.insert(tool);
193 // TODO Sort components after their packages, and targets last
194 for(const Package *p: broken_packages)
195 for(const string &b: p->get_problems())
196 problems.push_back(format("%s: %s", p->get_name(), b));
198 for(const Component *c: broken_components)
199 for(const string &b: c->get_problems())
200 problems.push_back(format("%s/%s: %s", c->get_package().get_name(), c->get_name(), b));
202 for(const Tool *t: broken_tools)
203 for(const string &b: t->get_problems())
204 problems.push_back(format("%s: %s", t->get_tag(), b));
209 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
211 IO::BufferedFile in(fn.str());
213 get_logger().log("files", "Reading %s", fn);
215 DataFile::Parser parser(in, fn.str());
216 Loader loader(*this, opts, all);
220 void Builder::save_caches()
222 for(const auto &kvp: package_manager.get_packages())
223 kvp.second->save_caches();
226 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
228 unsigned total = build_graph.count_rebuild_targets();
232 get_logger().log("summary", "Already up to date");
235 get_logger().log("summary", "Will build %d target%s", total, (total!=1 ? "s" : ""));
237 vector<Task *> tasks;
243 bool starved = false;
247 if(tasks.size()<jobs && !fail && !starved)
249 Target *tgt = build_graph.get_buildable_target();
256 get_logger().log("tasks", "%-4s %s", tgt->get_tool()->get_tag(), tgt->get_name());
258 Task *task = tgt->build();
261 get_logger().log("commands", "%s", task->get_command());
264 task->signal_finished.emit(true);
270 tasks.push_back(task);
275 IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
277 else if(tasks.empty())
283 Time::sleep(10*Time::msec);
285 for(unsigned i=0; i<tasks.size();)
288 if(jobs==1 || (tasks.size()==1 && starved))
289 status = tasks[i]->wait();
291 status = tasks[i]->check();
293 if(status!=Task::RUNNING)
298 tasks.erase(tasks.begin()+i);
299 if(status==Task::ERROR)
301 if(tasks.empty() && fail)
313 get_logger().log("summary", "Build failed");
314 else if(show_progress)
315 get_logger().log("summary", "Build complete");
320 int Builder::clean(bool all, bool dry_run)
322 // Cleaning doesn't care about ordering, so a simpler method can be used
324 set<Target *> clean_tgts;
325 deque<Target *> queue;
326 queue.push_back(&build_graph.get_goals());
328 while(!queue.empty())
330 Target *tgt = queue.front();
333 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
334 clean_tgts.insert(tgt);
336 for(Target *t: tgt->get_dependencies())
337 if(!clean_tgts.count(t))
341 for(Target *t: clean_tgts)
343 get_logger().log("tasks", "RM %s", t->get_name());
352 Builder::LoadedPlugin::LoadedPlugin(LoadedPlugin &&other):
353 path(move(other.path)),
354 module(other.module),
361 Builder::LoadedPlugin::~LoadedPlugin()
368 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
369 DataFile::ObjectLoader<Builder>(b),
373 add("architecture", &Loader::architecture);
374 add("binary_package", &Loader::binpkg);
375 add("build_type", &Loader::build_type);
376 add("package", &Loader::package);
379 obj.top_loader = this;
380 else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
381 options = obj.top_loader->options;
384 Builder::Loader::~Loader()
386 if(obj.top_loader==this)
390 void Builder::Loader::architecture(const string &n)
392 if(obj.current_arch->match_name(n))
393 load_sub(*obj.current_arch);
396 void Builder::Loader::binpkg(const string &n)
398 BinaryPackage *pkg = new BinaryPackage(obj, n);
402 void Builder::Loader::build_type(const string &n)
406 auto i = obj.build_types.insert({ n, btype }).first;
408 obj.build_type = &i->second;
411 void Builder::Loader::package(const string &n)
413 SourcePackage *pkg = new SourcePackage(obj, n, get_source());
415 load_sub(*pkg, options);
418 pkg->set_build_type(*obj.build_type);