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 "clang/clangtools.h"
20 #include "datafile/datatool.h"
21 #include "gnu/gnutools.h"
22 #include "installedfile.h"
23 #include "msvc/microsofttools.h"
26 #include "sharedlibrary.h"
27 #include "sourcepackage.h"
29 #include "virtualtarget.h"
35 package_manager(*this),
36 native_arch(*this, string()),
39 logger(&default_logger)
41 set_architecture(string());
46 if(current_arch!=&native_arch)
50 void Builder::load_plugins()
52 using CreateFunc = Plugin *(Builder &);
54 FS::Path plugins_dir = FS::get_sys_lib_dir();
55 logger->log("files", "Traversing %s", plugins_dir);
56 for(const string &f: list_filtered(plugins_dir, "\\.dlm$"))
59 plugin.path = plugins_dir/f;
63 plugin.module = new Module(plugin.path.str());
65 catch(const exception &exc)
67 logger->log("plugins", "Failed to load plugin %s: %s", f, exc.what());
73 CreateFunc *create_func = reinterpret_cast<CreateFunc *>(plugin.module->get_symbol("create_plugin"));
74 plugin.plugin = create_func(*this);
77 logger->log("plugins", "Loaded plugin %s", f);
78 plugins.emplace_back(move(plugin));
82 logger->log("plugins", "Plugin %s refused to initialize", f);
84 catch(const exception &exc)
86 logger->log("plugins", "Failed to initialize plugin %s: %s", f, exc.what());
93 void Builder::set_architecture(const string &name)
95 if(current_arch!=&native_arch)
99 current_arch = &native_arch;
101 current_arch = new Architecture(*this, name);
104 update_auto_prefix();
107 vector<string> Builder::get_build_types() const
110 keys.reserve(build_types.size());
111 for(const auto &kvp: build_types)
112 keys.push_back(kvp.first);
116 const BuildType &Builder::get_build_type() const
119 throw invalid_state("no build type");
123 void Builder::set_build_type(const string &name)
125 build_type = &get_item(build_types, name);
128 void Builder::set_prefix(const FS::Path &p)
134 void Builder::set_temp_directory(const FS::Path &p)
139 void Builder::update_auto_prefix()
141 if(current_arch->is_native())
142 prefix = FS::get_home_dir()/"local";
144 prefix = FS::get_home_dir()/"local"/current_arch->get_name();
147 void Builder::add_default_tools()
149 toolchain.add_toolchain(new GnuTools(*this, *current_arch));
150 toolchain.add_toolchain(new ClangTools(*this, *current_arch));
151 if(current_arch->get_system()=="android")
152 toolchain.add_toolchain(new AndroidTools(*this, *current_arch));
153 if(current_arch->get_system()=="windows")
154 toolchain.add_toolchain(new MicrosoftTools(*this, *current_arch));
155 toolchain.add_toolchain(new BuiltinTools(*this));
156 toolchain.add_tool(new DataTool(*this));
157 for(const LoadedPlugin &p: plugins)
158 p.plugin->add_tools(toolchain, *current_arch);
160 auto i = find_if(toolchain.get_toolchains(), [](const Toolchain *tc){ return (tc->has_tool("CC") || tc->has_tool("CXX")); });
161 if(i!=toolchain.get_toolchains().end())
163 current_arch->refine((*i)->get_name());
165 update_auto_prefix();
169 void Builder::set_logger(const Logger *l)
171 logger = (l ? l : &default_logger);
174 vector<string> Builder::collect_problems() const
176 vector<string> problems;
177 set<const Package *> broken_packages;
178 set<const Component *> broken_components;
179 set<const Tool *> broken_tools;
181 for(const auto &kvp: build_graph.get_targets())
182 if(kvp.second->is_broken())
184 for(const string &p: kvp.second->get_problems())
185 problems.push_back(format("%s: %s", kvp.second->get_name(), p));
187 const Package *package = kvp.second->get_package();
188 if(package && !package->get_problems().empty())
189 broken_packages.insert(package);
191 const Component *component = kvp.second->get_component();
192 if(component && !component->get_problems().empty())
193 broken_components.insert(component);
195 const Tool *tool = kvp.second->get_tool();
196 if(tool && !tool->get_problems().empty())
197 broken_tools.insert(tool);
200 // TODO Sort components after their packages, and targets last
201 for(const Package *p: broken_packages)
202 for(const string &b: p->get_problems())
203 problems.push_back(format("%s: %s", p->get_name(), b));
205 for(const Component *c: broken_components)
206 for(const string &b: c->get_problems())
207 problems.push_back(format("%s/%s: %s", c->get_package().get_name(), c->get_name(), b));
209 for(const Tool *t: broken_tools)
210 for(const string &b: t->get_problems())
211 problems.push_back(format("%s: %s", t->get_tag(), b));
216 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
218 IO::BufferedFile in(fn.str());
220 get_logger().log("files", "Reading %s", fn);
222 DataFile::Parser parser(in, fn.str());
223 Loader loader(*this, opts, all);
227 void Builder::save_caches()
229 for(const auto &kvp: package_manager.get_packages())
230 kvp.second->save_caches();
233 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
235 unsigned total = build_graph.count_rebuild_targets();
239 get_logger().log("summary", "Already up to date");
242 get_logger().log("summary", "Will build %d target%s", total, (total!=1 ? "s" : ""));
244 vector<Task *> tasks;
250 bool starved = false;
254 if(tasks.size()<jobs && !fail && !starved)
256 Target *tgt = build_graph.get_buildable_target();
263 get_logger().log("tasks", "%-4s %s", tgt->get_tool()->get_tag(), tgt->get_name());
265 Task *task = tgt->build();
268 get_logger().log("commands", "%s", task->get_command());
271 task->signal_finished.emit(true);
277 tasks.push_back(task);
282 IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
284 else if(tasks.empty())
290 Time::sleep(10*Time::msec);
292 for(unsigned i=0; i<tasks.size();)
295 if(jobs==1 || (tasks.size()==1 && starved))
296 status = tasks[i]->wait();
298 status = tasks[i]->check();
300 if(status!=Task::RUNNING)
305 tasks.erase(tasks.begin()+i);
306 if(status==Task::ERROR)
308 if(tasks.empty() && fail)
320 get_logger().log("summary", "Build failed");
321 else if(show_progress)
322 get_logger().log("summary", "Build complete");
327 int Builder::clean(bool all, bool dry_run)
329 // Cleaning doesn't care about ordering, so a simpler method can be used
331 set<Target *> clean_tgts;
332 deque<Target *> queue;
333 queue.push_back(&build_graph.get_goals());
335 while(!queue.empty())
337 Target *tgt = queue.front();
340 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
341 clean_tgts.insert(tgt);
343 for(Target *t: tgt->get_dependencies())
344 if(!clean_tgts.count(t))
348 for(Target *t: clean_tgts)
350 get_logger().log("tasks", "RM %s", t->get_name());
359 Builder::LoadedPlugin::LoadedPlugin(LoadedPlugin &&other):
360 path(move(other.path)),
361 module(other.module),
368 Builder::LoadedPlugin::~LoadedPlugin()
375 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
376 DataFile::ObjectLoader<Builder>(b),
380 add("architecture", &Loader::architecture);
381 add("binary_package", &Loader::binpkg);
382 add("build_type", &Loader::build_type);
383 add("package", &Loader::package);
386 obj.top_loader = this;
387 else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
388 options = obj.top_loader->options;
391 Builder::Loader::~Loader()
393 if(obj.top_loader==this)
397 void Builder::Loader::architecture(const string &n)
399 if(obj.current_arch->match_name(n))
400 load_sub(*obj.current_arch);
403 void Builder::Loader::binpkg(const string &n)
405 BinaryPackage *pkg = new BinaryPackage(obj, n);
409 void Builder::Loader::build_type(const string &n)
413 auto i = obj.build_types.insert({ n, btype }).first;
415 obj.build_type = &i->second;
418 void Builder::Loader::package(const string &n)
420 SourcePackage *pkg = new SourcePackage(obj, n, get_source());
422 load_sub(*pkg, options);
425 pkg->set_build_type(*obj.build_type);