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 void Builder::problem(const string &p, const string &d)
103 problems.push_back(Problem(p, d));
106 void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *opts, bool all)
108 IO::BufferedFile in(fn.str());
110 get_logger().log("files", format("Reading %s", fn));
112 DataFile::Parser parser(in, fn.str());
113 Loader loader(*this, opts, all);
117 int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
119 unsigned total = build_graph.count_rebuild_targets();
123 get_logger().log("summary", "Already up to date");
126 get_logger().log("summary", format("Will build %d target%s", total, (total!=1 ? "s" : "")));
128 vector<Task *> tasks;
134 bool starved = false;
138 if(tasks.size()<jobs && !fail && !starved)
140 Target *tgt = build_graph.get_buildable_target();
144 get_logger().log("tasks", format("%-4s %s", tgt->get_tool()->get_tag(), tgt->get_name()));
145 Task *task = tgt->build();
148 get_logger().log("commands", format("%s", task->get_command()));
151 task->signal_finished.emit(true);
157 tasks.push_back(task);
162 IO::print("%d of %d target%s built\033[1G", count, total, (total!=1 ? "s" : ""));
164 else if(tasks.empty())
170 Time::sleep(10*Time::msec);
172 for(unsigned i=0; i<tasks.size();)
175 if(jobs==1 || (tasks.size()==1 && starved))
176 status = tasks[i]->wait();
178 status = tasks[i]->check();
180 if(status!=Task::RUNNING)
185 tasks.erase(tasks.begin()+i);
186 if(status==Task::ERROR)
188 if(tasks.empty() && fail)
200 get_logger().log("summary", "Build failed");
201 else if(show_progress)
202 get_logger().log("summary", "Build complete");
206 const PackageManager::PackageMap &packages = package_manager.get_packages();
207 for(PackageManager::PackageMap::const_iterator i=packages.begin(); i!=packages.end(); ++i)
208 i->second->save_caches();
214 int Builder::clean(bool all, bool dry_run)
216 // Cleaning doesn't care about ordering, so a simpler method can be used
218 set<Target *> clean_tgts;
219 list<Target *> queue;
220 queue.push_back(build_graph.get_target("cmdline"));
222 while(!queue.empty())
224 Target *tgt = queue.front();
225 queue.erase(queue.begin());
227 if(tgt->is_buildable() && (tgt->get_package()==&package_manager.get_main_package() || all))
228 clean_tgts.insert(tgt);
230 const Target::Dependencies &deps = tgt->get_dependencies();
231 for(list<Target *>::const_iterator i=deps.begin(); i!=deps.end(); ++i)
232 if(!clean_tgts.count(*i))
236 for(set<Target *>::iterator i=clean_tgts.begin(); i!=clean_tgts.end(); ++i)
238 get_logger().log("tasks", format("RM %s", (*i)->get_name()));
247 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
248 DataFile::ObjectLoader<Builder>(b),
252 add("architecture", &Loader::architecture);
253 add("binary_package", &Loader::binpkg);
254 add("build_type", &Loader::build_type);
255 add("profile", &Loader::profile);
256 add("package", &Loader::package);
259 obj.top_loader = this;
260 else if(!options && obj.top_loader!=this && obj.top_loader->conf_all)
261 options = obj.top_loader->options;
264 Builder::Loader::~Loader()
266 if(obj.top_loader==this)
270 void Builder::Loader::architecture(const string &n)
272 if(obj.current_arch->match_name(n))
273 load_sub(*obj.current_arch);
276 void Builder::Loader::binpkg(const string &n)
278 BinaryPackage *pkg = new BinaryPackage(obj, n);
282 void Builder::Loader::build_type(const string &n)
286 BuildTypeMap::iterator i = obj.build_types.insert(BuildTypeMap::value_type(n, btype)).first;
288 obj.build_type = &i->second;
291 void Builder::Loader::profile(const string &)
293 IO::print("Profiles are deprecated\n");
296 void Builder::Loader::package(const string &n)
298 SourcePackage *pkg = new SourcePackage(obj, n, get_source());
301 load_sub(*pkg, *options);
306 pkg->set_build_type(*obj.build_type);