2 #include <msp/core/maputils.h>
3 #include <msp/datafile/parser.h>
4 #include <msp/fs/dir.h>
5 #include <msp/fs/utils.h>
6 #include <msp/io/buffered.h>
7 #include <msp/io/file.h>
8 #include <msp/io/print.h>
9 #include <msp/strings/format.h>
10 #include <msp/time/units.h>
11 #include <msp/time/utils.h>
12 #include "binarypackage.h"
16 #include "gnuarchiver.h"
17 #include "gnuccompiler.h"
18 #include "gnucxxcompiler.h"
19 #include "gnulinker.h"
20 #include "installedfile.h"
21 #include "mingwdlltool.h"
23 #include "pkgconfiggenerator.h"
24 #include "sharedlibrary.h"
25 #include "sourcepackage.h"
28 #include "virtualtarget.h"
34 package_manager(*this),
35 native_arch(*this, string()),
40 logger(&default_logger),
44 set_architecture(string());
49 if(current_arch!=&native_arch)
53 void Builder::set_architecture(const string &name)
57 current_arch = &native_arch;
58 prefix = FS::get_home_dir()/"local";
62 current_arch = new Architecture(*this, name);
63 prefix = FS::get_home_dir()/"local"/current_arch->get_name();
67 void Builder::set_build_type(const string &name)
69 build_type = &get_item(build_types, name);
72 void Builder::set_prefix(const FS::Path &p)
77 void Builder::set_temp_directory(const FS::Path &p)
82 void Builder::add_default_tools()
84 toolchain.add_tool(new GnuCCompiler(*this, *current_arch));
85 toolchain.add_tool(new GnuCxxCompiler(*this, *current_arch));
86 toolchain.add_tool(new GnuLinker(*this, *current_arch));
87 toolchain.add_tool(new GnuArchiver(*this, *current_arch));
88 toolchain.add_tool(new Copy(*this));
89 toolchain.add_tool(new Tar(*this));
90 toolchain.add_tool(new PkgConfigGenerator(*this));
91 if(current_arch->get_system()=="windows")
92 toolchain.add_tool(new MingwDllTool(*this, *current_arch));
93 toolchain.add_tool(new DataTool(*this));
96 void Builder::set_logger(const Logger *l)
98 logger = (l ? l : &default_logger);
101 list<string> Builder::collect_problems() const
103 list<string> problems;
104 set<const Package *> broken_packages;
105 set<const Component *> broken_components;
106 set<const Tool *> broken_tools;
108 const BuildGraph::TargetMap &targets = build_graph.get_targets();
109 for(BuildGraph::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
110 if(i->second->is_broken())
112 const list<string> &tgt_problems = i->second->get_problems();
113 for(list<string>::const_iterator j=tgt_problems.begin(); j!=tgt_problems.end(); ++j)
114 problems.push_back(format("%s: %s", i->second->get_name(), *j));
116 const Package *package = i->second->get_package();
117 if(package && !package->get_problems().empty())
118 broken_packages.insert(package);
120 const Component *component = i->second->get_component();
121 if(component && !component->get_problems().empty())
122 broken_components.insert(component);
124 const Tool *tool = i->second->get_tool();
125 if(tool && !tool->get_problems().empty())
126 broken_tools.insert(tool);
129 // TODO Sort components after their packages, and targets last
130 for(set<const Package *>::const_iterator i=broken_packages.begin(); i!=broken_packages.end(); ++i)
132 const list<string> &pkg_problems = (*i)->get_problems();
133 for(list<string>::const_iterator j=pkg_problems.begin(); j!=pkg_problems.end(); ++j)
134 problems.push_back(format("%s: %s", (*i)->get_name(), *j));
137 for(set<const Component *>::const_iterator i=broken_components.begin(); i!=broken_components.end(); ++i)
139 const list<string> &comp_problems = (*i)->get_problems();
140 for(list<string>::const_iterator j=comp_problems.begin(); j!=comp_problems.end(); ++j)
141 problems.push_back(format("%s/%s: %s", (*i)->get_package().get_name(), (*i)->get_name(), *j));
144 for(set<const Tool *>::const_iterator i=broken_tools.begin(); i!=broken_tools.end(); ++i)
146 const list<string> &tool_problems = (*i)->get_problems();
147 for(list<string>::const_iterator j=tool_problems.begin(); j!=tool_problems.end(); ++j)
148 problems.push_back(format("%s: %s", (*i)->get_tag(), *j));
154 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
156 IO::BufferedFile in(fn.str());
158 get_logger().log("files", format("Reading %s", fn));
160 DataFile::Parser parser(in, fn.str());
161 Loader loader(*this, opts, all);
165 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
167 unsigned total = build_graph.count_rebuild_targets();
171 get_logger().log("summary", "Already up to date");
174 get_logger().log("summary", format("Will build %d target%s", total, (total!=1 ? "s" : "")));
176 vector<Task *> tasks;
182 bool starved = false;
186 if(tasks.size()<jobs && !fail && !starved)
188 Target *tgt = build_graph.get_buildable_target();
192 get_logger().log("tasks", format("%-4s %s", tgt->get_tool()->get_tag(), tgt->get_name()));
193 Task *task = tgt->build();
196 get_logger().log("commands", format("%s", task->get_command()));
199 task->signal_finished.emit(true);
205 tasks.push_back(task);
210 IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
212 else if(tasks.empty())
218 Time::sleep(10*Time::msec);
220 for(unsigned i=0; i<tasks.size();)
223 if(jobs==1 || (tasks.size()==1 && starved))
224 status = tasks[i]->wait();
226 status = tasks[i]->check();
228 if(status!=Task::RUNNING)
233 tasks.erase(tasks.begin()+i);
234 if(status==Task::ERROR)
236 if(tasks.empty() && fail)
248 get_logger().log("summary", "Build failed");
249 else if(show_progress)
250 get_logger().log("summary", "Build complete");
254 const PackageManager::PackageMap &packages = package_manager.get_packages();
255 for(PackageManager::PackageMap::const_iterator i=packages.begin(); i!=packages.end(); ++i)
256 i->second->save_caches();
262 int Builder::clean(bool all, bool dry_run)
264 // Cleaning doesn't care about ordering, so a simpler method can be used
266 set<Target *> clean_tgts;
267 list<Target *> queue;
268 queue.push_back(build_graph.get_target("cmdline"));
270 while(!queue.empty())
272 Target *tgt = queue.front();
273 queue.erase(queue.begin());
275 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
276 clean_tgts.insert(tgt);
278 const Target::Dependencies &deps = tgt->get_dependencies();
279 for(list<Target *>::const_iterator i=deps.begin(); i!=deps.end(); ++i)
280 if(!clean_tgts.count(*i))
284 for(set<Target *>::iterator i=clean_tgts.begin(); i!=clean_tgts.end(); ++i)
286 get_logger().log("tasks", format("RM %s", (*i)->get_name()));
295 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
296 DataFile::ObjectLoader<Builder>(b),
300 add("architecture", &Loader::architecture);
301 add("binary_package", &Loader::binpkg);
302 add("build_type", &Loader::build_type);
303 add("profile", &Loader::profile);
304 add("package", &Loader::package);
307 obj.top_loader = this;
308 else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
309 options = obj.top_loader->options;
312 Builder::Loader::~Loader()
314 if(obj.top_loader==this)
318 void Builder::Loader::architecture(const string &n)
320 if(obj.current_arch->match_name(n))
321 load_sub(*obj.current_arch);
324 void Builder::Loader::binpkg(const string &n)
326 BinaryPackage *pkg = new BinaryPackage(obj, n);
330 void Builder::Loader::build_type(const string &n)
334 BuildTypeMap::iterator i = obj.build_types.insert(BuildTypeMap::value_type(n, btype)).first;
336 obj.build_type = &i->second;
339 void Builder::Loader::profile(const string &)
341 IO::print("Profiles are deprecated\n");
344 void Builder::Loader::package(const string &n)
346 SourcePackage *pkg = new SourcePackage(obj, n, get_source());
349 load_sub(*pkg, *options);
354 pkg->set_build_type(*obj.build_type);