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 "androidtools.h"
16 #include "binarypackage.h"
18 #include "builtintools.h"
19 #include "clangtools.h"
22 #include "installedfile.h"
23 #include "microsofttools.h"
25 #include "sharedlibrary.h"
26 #include "sourcepackage.h"
28 #include "virtualtarget.h"
34 package_manager(*this),
35 native_arch(*this, string()),
38 logger(&default_logger)
40 set_architecture(string());
45 if(current_arch!=&native_arch)
49 void Builder::set_architecture(const string &name)
51 if(current_arch!=&native_arch)
56 current_arch = &native_arch;
57 prefix = FS::get_home_dir()/"local";
61 current_arch = new Architecture(*this, name);
62 prefix = FS::get_home_dir()/"local"/current_arch->get_name();
66 vector<string> Builder::get_build_types() const
69 keys.reserve(build_types.size());
70 for(const auto &kvp: build_types)
71 keys.push_back(kvp.first);
75 const BuildType &Builder::get_build_type() const
78 throw invalid_state("no build type");
82 void Builder::set_build_type(const string &name)
84 build_type = &get_item(build_types, name);
87 void Builder::set_prefix(const FS::Path &p)
92 void Builder::set_temp_directory(const FS::Path &p)
97 void Builder::add_default_tools()
99 toolchain.add_toolchain(new GnuTools(*this, *current_arch));
100 toolchain.add_toolchain(new ClangTools(*this, *current_arch));
101 if(current_arch->get_system()=="android")
102 toolchain.add_toolchain(new AndroidTools(*this, *current_arch));
103 if(current_arch->get_system()=="windows")
104 toolchain.add_toolchain(new MicrosoftTools(*this, *current_arch));
105 toolchain.add_toolchain(new BuiltinTools(*this));
106 toolchain.add_tool(new DataTool(*this));
108 auto i = find_if(toolchain.get_toolchains(), [](const Toolchain *tc){ return (tc->has_tool("CC") || tc->has_tool("CXX")); });
109 if(i!=toolchain.get_toolchains().end())
110 current_arch->refine((*i)->get_name());
113 void Builder::set_logger(const Logger *l)
115 logger = (l ? l : &default_logger);
118 vector<string> Builder::collect_problems() const
120 vector<string> problems;
121 set<const Package *> broken_packages;
122 set<const Component *> broken_components;
123 set<const Tool *> broken_tools;
125 for(const auto &kvp: build_graph.get_targets())
126 if(kvp.second->is_broken())
128 for(const string &p: kvp.second->get_problems())
129 problems.push_back(format("%s: %s", kvp.second->get_name(), p));
131 const Package *package = kvp.second->get_package();
132 if(package && !package->get_problems().empty())
133 broken_packages.insert(package);
135 const Component *component = kvp.second->get_component();
136 if(component && !component->get_problems().empty())
137 broken_components.insert(component);
139 const Tool *tool = kvp.second->get_tool();
140 if(tool && !tool->get_problems().empty())
141 broken_tools.insert(tool);
144 // TODO Sort components after their packages, and targets last
145 for(const Package *p: broken_packages)
146 for(const string &b: p->get_problems())
147 problems.push_back(format("%s: %s", p->get_name(), b));
149 for(const Component *c: broken_components)
150 for(const string &b: c->get_problems())
151 problems.push_back(format("%s/%s: %s", c->get_package().get_name(), c->get_name(), b));
153 for(const Tool *t: broken_tools)
154 for(const string &b: t->get_problems())
155 problems.push_back(format("%s: %s", t->get_tag(), b));
160 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
162 IO::BufferedFile in(fn.str());
164 get_logger().log("files", "Reading %s", fn);
166 DataFile::Parser parser(in, fn.str());
167 Loader loader(*this, opts, all);
171 void Builder::save_caches()
173 for(const auto &kvp: package_manager.get_packages())
174 kvp.second->save_caches();
177 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
179 unsigned total = build_graph.count_rebuild_targets();
183 get_logger().log("summary", "Already up to date");
186 get_logger().log("summary", "Will build %d target%s", total, (total!=1 ? "s" : ""));
188 vector<Task *> tasks;
194 bool starved = false;
198 if(tasks.size()<jobs && !fail && !starved)
200 Target *tgt = build_graph.get_buildable_target();
207 get_logger().log("tasks", "%-4s %s", tgt->get_tool()->get_tag(), tgt->get_name());
209 Task *task = tgt->build();
212 get_logger().log("commands", "%s", task->get_command());
215 task->signal_finished.emit(true);
221 tasks.push_back(task);
226 IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
228 else if(tasks.empty())
234 Time::sleep(10*Time::msec);
236 for(unsigned i=0; i<tasks.size();)
239 if(jobs==1 || (tasks.size()==1 && starved))
240 status = tasks[i]->wait();
242 status = tasks[i]->check();
244 if(status!=Task::RUNNING)
249 tasks.erase(tasks.begin()+i);
250 if(status==Task::ERROR)
252 if(tasks.empty() && fail)
264 get_logger().log("summary", "Build failed");
265 else if(show_progress)
266 get_logger().log("summary", "Build complete");
271 int Builder::clean(bool all, bool dry_run)
273 // Cleaning doesn't care about ordering, so a simpler method can be used
275 set<Target *> clean_tgts;
276 deque<Target *> queue;
277 queue.push_back(&build_graph.get_goals());
279 while(!queue.empty())
281 Target *tgt = queue.front();
284 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
285 clean_tgts.insert(tgt);
287 for(Target *t: tgt->get_dependencies())
288 if(!clean_tgts.count(t))
292 for(Target *t: clean_tgts)
294 get_logger().log("tasks", "RM %s", t->get_name());
303 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
304 DataFile::ObjectLoader<Builder>(b),
308 add("architecture", &Loader::architecture);
309 add("binary_package", &Loader::binpkg);
310 add("build_type", &Loader::build_type);
311 add("package", &Loader::package);
314 obj.top_loader = this;
315 else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
316 options = obj.top_loader->options;
319 Builder::Loader::~Loader()
321 if(obj.top_loader==this)
325 void Builder::Loader::architecture(const string &n)
327 if(obj.current_arch->match_name(n))
328 load_sub(*obj.current_arch);
331 void Builder::Loader::binpkg(const string &n)
333 BinaryPackage *pkg = new BinaryPackage(obj, n);
337 void Builder::Loader::build_type(const string &n)
341 auto i = obj.build_types.insert({ n, btype }).first;
343 obj.build_type = &i->second;
346 void Builder::Loader::package(const string &n)
348 SourcePackage *pkg = new SourcePackage(obj, n, get_source());
350 load_sub(*pkg, options);
353 pkg->set_build_type(*obj.build_type);