]> git.tdb.fi Git - builder.git/blob - source/builder.cpp
Add basic support for tools (not configurable yet)
[builder.git] / source / builder.cpp
1 #include <fstream>
2 #include <iostream>
3 #include <msp/progress.h>
4 #include <msp/core/error.h>
5 #include <msp/getopt++/getopt++.h>
6 #include <msp/parser/parser.h>
7 #include <msp/path/utils.h>
8 #include <msp/strings/utils.h>
9 #include <msp/time/units.h>
10 #include <msp/time/utils.h>
11 #include "action.h"
12 #include "analyzer.h"
13 #include "builder.h"
14 #include "executable.h"
15 #include "header.h"
16 #include "install.h"
17 #include "misc.h"
18 #include "objectfile.h"
19 #include "package.h"
20 #include "pkgconfig.h"
21 #include "sharedlibrary.h"
22 #include "staticlibrary.h"
23 #include "systemlibrary.h"
24 #include "virtualtarget.h"
25
26 using namespace std;
27 using namespace Msp;
28
29 Builder::Builder(int argc, char **argv):
30         analyzer(0),
31         do_build(false),
32         dry_run(false),
33         help(false),
34         verbose(1),
35         chrome(false),
36         build_file("Build"),
37         jobs(1),
38         conf_all(false),
39         conf_only(false),
40         build_all(false)
41 {
42         string   analyze_mode;
43         string   work_dir;
44         bool     full_paths=false;
45         unsigned max_depth=5;
46         
47         GetOpt getopt;
48         getopt.add_option('a', "analyze",    analyze_mode, GetOpt::REQUIRED_ARG);
49         getopt.add_option('b', "build",      do_build,     GetOpt::NO_ARG);
50 //      getopt.add_option('c', "clean",      clean,        GetOpt::NO_ARG);
51         getopt.add_option('f', "file",       build_file,   GetOpt::REQUIRED_ARG);
52         getopt.add_option('h', "help",       help,         GetOpt::NO_ARG);
53         getopt.add_option('j', "jobs",       jobs,         GetOpt::REQUIRED_ARG);
54         getopt.add_option('n', "dry-run",    dry_run,      GetOpt::NO_ARG);
55         getopt.add_option('v', "verbose",    verbose,      GetOpt::NO_ARG);
56         getopt.add_option('A', "conf-all",   conf_all,     GetOpt::NO_ARG);
57         getopt.add_option('B', "build-all",  build_all,    GetOpt::NO_ARG);
58         getopt.add_option('C', "chdir",      work_dir,     GetOpt::REQUIRED_ARG);
59         getopt.add_option('W', "what-if",    what_if,      GetOpt::REQUIRED_ARG);
60         getopt.add_option(     "chrome",     chrome,       GetOpt::NO_ARG);
61         getopt.add_option(     "conf-only",  conf_only,    GetOpt::NO_ARG);
62         getopt.add_option(     "full-paths", full_paths,   GetOpt::NO_ARG);
63         getopt.add_option(     "max-depth",  max_depth,    GetOpt::REQUIRED_ARG);
64         getopt(argc, argv);
65
66         if(!analyze_mode.empty())
67         {
68                 analyzer=new Analyzer(*this);
69
70                 if(analyze_mode=="deps")
71                         analyzer->set_mode(Analyzer::DEPS);
72                 else if(analyze_mode=="alldeps")
73                         analyzer->set_mode(Analyzer::ALLDEPS);
74                 else if(analyze_mode=="rebuild")
75                         analyzer->set_mode(Analyzer::REBUILD);
76                 else if(analyze_mode=="rdeps")
77                         analyzer->set_mode(Analyzer::RDEPS);
78                 else
79                         throw UsageError("Invalid analyze mode");
80
81                 analyzer->set_max_depth(max_depth);
82                 analyzer->set_full_paths(full_paths);
83         }
84         else
85                 do_build=true;
86
87         const list<string> &args=getopt.get_args();
88         for(list<string>::const_iterator i=args.begin(); i!=args.end(); ++i)
89         {
90                 unsigned equal=i->find('=');
91                 if(equal!=string::npos)
92                         cmdline_options.insert(RawOptionMap::value_type(i->substr(0, equal), i->substr(equal+1)));
93                 else
94                         cmdline_targets.push_back(*i);
95         }
96
97         if(cmdline_targets.empty())
98                 cmdline_targets.push_back("default");
99
100         if(!work_dir.empty())
101                 chdir(work_dir.c_str());
102
103         cwd=Path::getcwd();
104
105         archs.insert(StringMap::value_type("native", ""));
106         archs.insert(StringMap::value_type("arm", "arm-linux-gnu-"));
107         archs.insert(StringMap::value_type("win32", "i586-mingw32msvc-"));
108
109         StringMap &native_tools=tools.insert(ToolMap::value_type("native", StringMap())).first->second;
110         native_tools.insert(StringMap::value_type("CC",   "gcc"));
111         native_tools.insert(StringMap::value_type("CXX",  "g++"));
112         native_tools.insert(StringMap::value_type("LD",   "gcc"));
113         native_tools.insert(StringMap::value_type("LDXX", "g++"));
114         native_tools.insert(StringMap::value_type("AR",   "ar"));
115 }
116
117 /**
118 Gets a package with the specified name, possibly creating it.
119
120 @param   n  Package name
121
122 @return  Pointer to the package, or 0 if the package could not be located
123 */
124 Package *Builder::get_package(const string &n)
125 {
126         PackageMap::iterator i=packages.find(n);
127         if(i!=packages.end())
128                 return i->second;
129
130         // Try to get source directory with pkgconfig
131         list<string> argv;
132         argv.push_back("pkg-config");
133         argv.push_back("--variable=source");
134         argv.push_back(n);
135         string srcdir=strip(run_command(argv));
136         
137         PathList dirs;
138         if(!srcdir.empty())
139                 dirs.push_back(srcdir);
140
141         // Make some other guesses about the source directory
142         string dirname=n;
143         if(!dirname.compare(0, 3, "msp"))
144                 dirname.erase(0, 3);
145         dirs.push_back(cwd/dirname);
146         dirs.push_back(cwd/".."/dirname);
147
148         // Go through the candidate directories and look for a Build file
149         for(PathList::iterator j=dirs.begin(); j!=dirs.end(); ++j)
150                 if(!load_build_file(*j/"Build"))
151                 {
152                         i=packages.find(n);
153                         if(i!=packages.end())
154                                 return i->second;
155                         break;
156                 }
157
158         // Package source not found - create a binary package
159         Package *pkg=Package::create(*this, n);
160         packages.insert(PackageMap::value_type(n, pkg));
161         if(pkg)
162                 new_pkgs.push_back(pkg);
163
164         return pkg;
165 }
166
167 /**
168 Returns the target with the given name, or 0 if no such target exists.
169 */
170 Target *Builder::get_target(const string &n)
171 {
172         TargetMap::iterator i=targets.find(n);
173         if(i!=targets.end())
174                 return i->second;
175         return 0;
176 }
177
178 /**
179 Tries to locate a header included from a given location and with a given include
180 path.  Considers known targets as well as existing files.  If a matching target
181 is not found but a file exists, a new SystemHeader target will be created and
182 returned.
183 */
184 Target *Builder::get_header(const string &include, const string &from, const list<string> &path)
185 {
186         string hash(8, 0);
187         update_hash(hash, from);
188         for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
189                 update_hash(hash, *i);
190
191         string id=hash+include;
192         TargetMap::iterator i=includes.find(id);
193         if(i!=includes.end())
194                 return i->second;
195
196         string fn=include.substr(1);
197         Target *tgt=0;
198         if(include[0]=='"' && (tgt=get_header(Path::Path(from)/fn)))
199                 ;
200         else if((tgt=get_header(Path::Path("/usr/include")/fn)))
201                 ;
202         //XXX Determine the C++ header location dynamically
203         else if((tgt=get_header(Path::Path("/usr/include/c++/4.1.2")/fn)))
204                 ;
205         else
206         {
207                 for(list<string>::const_iterator j=path.begin(); (j!=path.end() && !tgt); ++j)
208                         tgt=get_header(cwd/ *j/fn);
209         }
210
211         includes.insert(TargetMap::value_type(id, tgt));
212         
213         return tgt;
214 }
215
216 /**
217 Tries to locate a library with the given library path.  Considers known targets
218 as well as existing files.  If a matching target is not found but a file exists,
219 a new SystemLibrary target will be created and returned.
220
221 @param   lib   Name of the library to get (without "lib" prefix or extension)
222 @param   path  List of paths to search for the library
223 @param   mode  Shared / static mode
224
225 @return  Some kind of library target, if a match was found
226 */
227 Target *Builder::get_library(const string &lib, const list<string> &path, LibMode mode)
228 {
229         string hash(8, 0);
230         for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
231                 update_hash(hash, *i);
232
233         //XXX Incorporate mode into id
234         string id=hash+lib;
235         TargetMap::iterator i=libraries.find(id);
236         if(i!=libraries.end())
237                 return i->second;
238
239         Target *tgt=0;
240         if((tgt=get_library(lib, "/lib", mode)))
241                 ;
242         else if((tgt=get_library(lib, "/usr/lib", mode)))
243                 ;
244         else
245         {
246                 for(list<string>::const_iterator j=path.begin(); (j!=path.end() && !tgt); ++j)
247                         tgt=get_library(lib, cwd/ *j, mode);
248         }
249
250         libraries.insert(TargetMap::value_type(id, tgt));
251
252         return tgt;
253 }
254
255 const string &Builder::get_arch_prefix(const string &arch) const
256 {
257         StringMap::const_iterator i=archs.find(arch);
258         if(i==archs.end())
259                 throw InvalidParameterValue("Unknown architecture");
260
261         return i->second;
262 }
263
264 string Builder::get_tool(const std::string &tool, const std::string &arch)
265 {
266         ToolMap::iterator i=tools.find(arch);
267         if(i!=tools.end())
268         {
269                 StringMap::iterator j=i->second.find(tool);
270                 if(j!=i->second.end())
271                         return j->second;
272         }
273
274         // Either the arch, or the tool within the arch was not found
275         i=tools.find("native");
276         StringMap::iterator j=i->second.find(tool);
277         if(j==i->second.end())
278                 throw InvalidParameterValue("Unknown tool");
279
280         return get_arch_prefix(arch)+j->second;
281 }
282
283 int Builder::main()
284 {
285         if(load_build_file(cwd/build_file))
286         {
287                 cerr<<"No build info here.\n";
288                 return 1;
289         }
290
291         default_pkg=packages.begin()->second;
292
293         while(!new_pkgs.empty())
294         {
295                 Package *pkg=new_pkgs.front();
296                 new_pkgs.erase(new_pkgs.begin());
297                 pkg->resolve_refs();
298         }
299
300         default_pkg->configure(cmdline_options, conf_all?2:1);
301
302         if(help)
303         {
304                 usage(0, "builder", false);
305                 cout<<'\n';
306                 package_help();
307                 return 0;
308         }
309
310         StringMap problems;
311         for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
312         {
313                 string prob;
314                 if(!i->second)
315                         prob="missing";
316                 else if(i->second->get_buildable() && i->second->get_arch()!=default_pkg->get_arch())
317                         prob="wrong architecture ("+i->second->get_arch()+")";
318                 if(!prob.empty())
319                         problems.insert(StringMap::value_type(i->first, prob));
320         }
321
322         if(!problems.empty())
323         {
324                 cerr<<"The following problems were detected:\n";
325                 for(StringMap::iterator i=problems.begin(); i!=problems.end(); ++i)
326                         cerr<<"  "<<i->first<<": "<<i->second<<'\n';
327                 cerr<<"Please fix them and try again.\n";
328                 return 1;
329         }
330
331         if(conf_only)
332                 return 0;
333
334         if(create_targets())
335                 return 1;
336
337         cout<<packages.size()<<" packages, "<<targets.size()<<" targets\n";
338         if(verbose>=2)
339         {
340                 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
341                 {
342                         cout<<' '<<i->second->get_name();
343                         if(i->second->get_buildable())
344                                 cout<<'*';
345                         unsigned count=0;
346                         unsigned ood_count=0;
347                         for(TargetMap::iterator j=targets.begin(); j!=targets.end(); ++j)
348                                 if(j->second->get_package()==i->second)
349                                 {
350                                         ++count;
351                                         if(j->second->get_rebuild())
352                                                 ++ood_count;
353                                 }
354                         if(count)
355                         {
356                                 cout<<" ("<<count<<" targets";
357                                 if(ood_count)
358                                         cout<<", "<<ood_count<<" out-of-date";
359                                 cout<<')';
360                         }
361                         cout<<'\n';
362                 }
363         }
364
365         if(analyzer)
366                 analyzer->analyze();
367
368         if(do_build)
369                 build();
370
371         return exit_code;
372 }
373
374 Builder::~Builder()
375 {
376         for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
377                 delete i->second;
378         for(TargetMap::iterator i=targets.begin(); i!=targets.end(); ++i)
379                 delete i->second;
380         delete analyzer;
381 }
382
383 void Builder::usage(const char *reason, const char *argv0, bool brief)
384 {
385         if(reason)
386                 cerr<<reason<<'\n';
387         
388         if(brief)
389                 cerr<<"Usage: "<<argv0<<" [-a|--analyze MODE] [-b|--build] [-c|--clean] [-f|--file FILE] [-h|--help] [-j|--jobs NUM] [-n||--dry-run] [-v|--verbose] [-A|--conf-all] [-B|--build-all] [-C|--chdir DIRECTORY] [-W|--what-if FILE] [--chrome] [--conf-only] [--full-paths] [--max-depth NUM] [<target> ...]";
390         else
391         {
392                 cerr<<
393                         "Usage: "<<argv0<<" [options] [<target> ...]\n"
394                         "\n"
395                         "Options:\n"
396                         "  -a, --analyze MODE  Perform analysis.  MODE can be deps, alldeps or rebuild.\n"
397                         "  -b, --build         Perform build even if doing analysis.\n"
398                         "  -c, --clean         Clean buildable targets.\n"
399                         "  -f, --file FILE     Read info from FILE instead of Build.\n"
400                         "  -h, --help          Print this message.\n"
401                         "  -j, --jobs NUM      Run NUM commands at once, whenever possible.\n"
402                         "  -n, --dry-run       Don't actually do anything, only show what would be done.\n"
403                         "  -v, --verbose       Print more information about what's going on.\n"
404                         "  -A, --conf-all      Apply configuration to all packages.\n"
405                         "  -B, --build-all     Build all targets unconditionally.\n"
406                         "  -C, --chdir DIR     Change to DIR before doing anything else.\n"
407                         "  -W, --what-if FILE  Pretend that FILE has changed.\n"
408                         "  --chrome            Use extra chrome to print status.\n"
409                         "  --conf-only         Stop after configuring packages.\n"
410                         "  --full-paths        Output full paths in analysis.\n"
411                         "  --max-depth NUM     Maximum depth to show in analysis.\n";
412         }
413 }
414
415 /**
416 Loads the given build file.
417
418 @param   fn  Path to the file
419
420 @return  0 on success, -1 if the file could not be opened
421 */
422 int Builder::load_build_file(const Path::Path &fn)
423 {
424         ifstream in(fn.str().c_str());
425         if(!in)
426                 return -1;
427
428         Parser::Parser parser(in, fn.str());
429         Loader loader(*this, fn.subpath(0, fn.size()-1));
430         loader.load(parser);
431
432         return 0;
433 }
434
435 /**
436 Creates targets for all packages and prepares them for building.
437
438 @return  0 if everything went ok, -1 if something bad happened and a build
439          shouldn't be attempted
440 */
441 int Builder::create_targets()
442 {
443         Target *world=new VirtualTarget(*this, "world");
444         add_target(world);
445
446         Target *def_tgt=new VirtualTarget(*this, "default");
447         add_target(def_tgt);
448         world->add_depend(def_tgt);
449
450         Target *install=new VirtualTarget(*this, "install");
451         add_target(install);
452         world->add_depend(install);
453
454         for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
455         {
456                 if(!i->second)
457                         continue;
458                 if(!i->second->get_buildable())
459                         continue;
460
461                 Path::Path inst_base;
462                 if(i->second->get_buildable() && i->second->get_install_flags())
463                         inst_base=i->second->get_prefix();
464
465                 const ComponentList &components=i->second->get_components();
466                 for(ComponentList::const_iterator j=components.begin(); j!=components.end(); ++j)
467                 {
468                         // Collect all files belonging to the component
469                         PathList files;
470                         const PathList &sources=j->get_sources();
471                         for(PathList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
472                         {
473                                 struct stat st;
474                                 stat(*k, st);
475                                 if(S_ISDIR(st.st_mode))
476                                 {
477                                         list<string> sfiles=list_files(*k);
478                                         for(list<string>::iterator l=sfiles.begin(); l!=sfiles.end(); ++l)
479                                                 files.push_back(*k / *l);
480                                 }
481                                 else
482                                         files.push_back(*k);
483                         }
484
485                         bool build_exe=j->get_type()!=Component::HEADERS;
486                         
487                         list<ObjectFile *> objs;
488                         for(PathList::iterator k=files.begin(); k!=files.end(); ++k)
489                         {
490                                 string basename=(*k)[-1];
491                                 string ext=Path::splitext(basename).ext;
492                                 if((ext==".cpp" || ext==".c") && build_exe)
493                                 {
494                                         SourceFile *src=new SourceFile(*this, &*j, k->str());
495                                         add_target(src);
496
497                                         // Compile sources
498                                         ObjectFile *obj=new ObjectFile(*this, *j, *src);
499                                         add_target(obj);
500                                         objs.push_back(obj);
501                                 }
502                                 else if(ext==".h")
503                                 {
504                                         Target *hdr=get_target(k->str());
505                                         if(!hdr)
506                                         {
507                                                 hdr=new Header(*this, &*j, k->str());
508                                                 add_target(hdr);
509                                         }
510
511                                         // Install headers if requested
512                                         if(!j->get_install_headers().empty())
513                                         {
514                                                 Path::Path inst_path=inst_base/"include"/j->get_install_headers()/basename;
515                                                 Install *inst=new Install(*this, *i->second, *hdr, inst_path.str());
516                                                 add_target(inst);
517                                                 install->add_depend(inst);
518                                         }
519                                 }
520                         }
521
522                         if(build_exe)
523                         {
524                                 Executable    *exe=0;
525                                 StaticLibrary *slib=0;
526                                 if(j->get_type()==Component::LIBRARY)
527                                 {
528                                         exe=new SharedLibrary(*this, *j, objs);
529                                         slib=new StaticLibrary(*this, *j, objs);
530                                         add_target(slib);
531                                 }
532                                 else
533                                         exe=new Executable(*this, *j, objs);
534                                 
535                                 add_target(exe);
536                                 if(i->second==default_pkg)
537                                 {
538                                         def_tgt->add_depend(exe);
539                                         if(slib) def_tgt->add_depend(slib);
540                                 }
541                                 else
542                                 {
543                                         world->add_depend(exe);
544                                         if(slib) world->add_depend(slib);
545                                 }
546
547                                 if(j->get_install())
548                                 {
549                                         string inst_dir;
550                                         if(j->get_type()==Component::PROGRAM)
551                                                 inst_dir="bin";
552                                         else if(j->get_type()==Component::LIBRARY)
553                                                 inst_dir="lib";
554                                         if(!inst_dir.empty())
555                                         {
556                                                 Install *inst=new Install(*this, *i->second, *exe, (inst_base/inst_dir/Path::basename(exe->get_name())).str());
557                                                 add_target(inst);
558                                                 install->add_depend(inst);
559
560                                                 if(slib)
561                                                 {
562                                                         inst=new Install(*this, *i->second, *slib, (inst_base/inst_dir/Path::basename(slib->get_name())).str());
563                                                         add_target(inst);
564                                                         install->add_depend(inst);
565                                                 }
566                                         }
567                                 }
568                         }
569                 }
570
571                 if(i->second->get_install_flags()&(Package::LIB|Package::INCLUDE))
572                 {
573                         PkgConfig *pc=new PkgConfig(*this, *i->second);
574                         add_target(pc);
575                         Install *inst=new Install(*this, *i->second, *pc, (inst_base/"lib"/"pkgconfig"/Path::basename(pc->get_name())).str());
576                         add_target(inst);
577                         install->add_depend(inst);
578                 }
579         }
580
581         // Find dependencies until no new targets are created
582         while(!new_tgts.empty())
583         {
584                 Target *tgt=new_tgts.front();
585                 new_tgts.erase(new_tgts.begin());
586                 tgt->find_depends();
587                 if(!tgt->get_depends_ready())
588                         new_tgts.push_back(tgt);
589         }
590
591         // Apply what-ifs
592         for(StringList::iterator i=what_if.begin(); i!=what_if.end(); ++i)
593         {
594                 Target *tgt=get_target((cwd/ *i).str());
595                 if(!tgt)
596                 {
597                         cerr<<"Unknown what-if target "<<*i<<'\n';
598                         return -1;
599                 }
600                 tgt->touch();
601         }
602
603         // Make the cmdline target depend on all targets mentioned on the command line
604         Target *cmdline=new VirtualTarget(*this, "cmdline");
605         add_target(cmdline);
606         world->add_depend(cmdline);
607         for(list<string>::iterator i=cmdline_targets.begin(); i!=cmdline_targets.end(); ++i)
608         {
609                 Target *tgt=get_target(*i);
610                 if(!tgt)
611                         tgt=get_target((cwd/ *i).str());
612                 if(!tgt)
613                 {
614                         cerr<<"I don't know anything about "<<*i<<'\n';
615                         return -1;
616                 }
617                 cmdline->add_depend(tgt);
618         }
619
620         world->prepare();
621
622         return 0;
623 }
624
625 /**
626 Check if a header exists, either as a target or a file.  Either an existing
627 target or a new SystemHeader target will be returned.
628 */
629 Target *Builder::get_header(const Msp::Path::Path &fn)
630 {
631         Target *tgt=get_target(fn.str());
632         if(tgt) return tgt;
633
634         if(Path::exists(fn))
635         {
636                 add_target(tgt=new SystemHeader(*this, fn.str()));
637                 return tgt;
638         }
639         return 0;
640 }
641
642 Target *Builder::get_library(const string &lib, const Path::Path &path, LibMode mode)
643 {
644         string full;
645         if(mode!=DYNAMIC)
646         {
647                 full=(path/("lib"+lib+".a")).str();
648                 Target *tgt=get_target(full);
649                 // Targets can only be associated with buildable packages (or no package at all)
650                 if(tgt && (tgt->get_package() || mode==ALL_STATIC)) return tgt;
651         }
652         if(mode!=ALL_STATIC)
653         {
654                 full=(path/("lib"+lib+".so")).str();
655                 Target *tgt=get_target(full);
656                 if(tgt) return tgt;
657         }
658
659         if(Path::exists(full))
660         {
661                 Target *tgt=new SystemLibrary(*this, full);
662                 add_target(tgt);
663                 return tgt;
664         }
665
666         return 0;
667 }
668
669 /**
670 Adds a target to both the target map and the new target queue.
671 */
672 void Builder::add_target(Target *t)
673 {
674         targets.insert(TargetMap::value_type(t->get_name(), t));
675         new_tgts.push_back(t);
676 }
677
678 /**
679 Updates a hash with a string.  This is used from get_header and get_library.
680 */
681 void Builder::update_hash(string &hash, const string &value)
682 {
683         for(unsigned i=0; i<value.size(); ++i)
684                 hash[i%hash.size()]^=value[i];
685 }
686
687 /**
688 This function supervises the build process, starting new actions when slots
689 become available.
690 */
691 int Builder::build()
692 {
693         Target *cmdline=get_target("cmdline");
694
695         unsigned total=cmdline->count_rebuild();
696         if(!total)
697         {
698                 cout<<"Already up to date\n";
699                 return 0;
700         }
701         cout<<"Will build "<<total<<" target(s)\n";
702
703         vector<Action *> actions;
704
705         if(chrome)
706                 cout<<"0 targets built\n";
707         unsigned count=0;
708
709         bool fail=false;
710         bool finish=false;
711
712         while(!finish)
713         {
714                 if(actions.size()<jobs && !fail)
715                 {
716                         Target *tgt=cmdline->get_buildable_target();
717                         if(tgt)
718                         {
719                                 Action *action=tgt->build();
720                                 if(action)
721                                         actions.push_back(action);
722                         }
723                         else if(actions.empty())
724                                 finish=true;
725                 }
726                 else
727                         Time::sleep(10*Time::msec);
728
729                 for(unsigned i=0; i<actions.size();)
730                 {
731                         int status=actions[i]->check();
732                         if(status>=0)
733                         {
734                                 ++count;
735                                 if(chrome)
736                                 {
737                                         cout<<"\e["<<actions.size()+1<<'A';
738                                         cout<<count<<" targets built\n";
739                                         if(i)
740                                                 cout<<"\e["<<i<<"B";
741                                         cout<<"\e[M";
742                                         if(i<actions.size()-1)
743                                                 cout<<"\e["<<actions.size()-i-1<<"B";
744                                         cout.flush();
745                                 }
746                                 delete actions[i];
747                                 actions.erase(actions.begin()+i);
748                                 if(status>0)
749                                         fail=true;
750                                 if(actions.empty() && fail)
751                                         finish=true;
752                         }
753                         else
754                                 ++i;
755                 }
756         }
757
758         if(fail)
759                 cout<<"Build failed\n";
760
761         return fail?-1:0;
762 }
763
764 /**
765 Prints out information about the default package.
766 */
767 void Builder::package_help()
768 {
769         const Config &config=default_pkg->get_config();
770         const Config::OptionMap &options=config.get_options();
771
772         cout<<"Required packages:\n  ";
773         const PkgRefList &requires=default_pkg->get_requires();
774         for(PkgRefList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
775         {
776                 if(i!=requires.begin())
777                         cout<<", ";
778                 cout<<i->get_name();
779         }
780         cout<<"\n\n";
781         cout<<"Package configuration:\n";
782         for(Config::OptionMap::const_iterator i=options.begin(); i!=options.end(); ++i)
783         {
784                 const Config::Option &opt=i->second;
785                 cout<<"  "<<opt.name<<": "<<opt.descr<<" ("<<opt.value<<") ["<<opt.defv<<"]\n";
786         }
787 }
788
789 Application::RegApp<Builder> Builder::reg;
790
791 Builder::Loader::Loader(Builder &b, const Path::Path &s):
792         bld(b),
793         src(s)
794 {
795         add("package", &Loader::package);
796 }
797
798 void Builder::Loader::package(const string &n)
799 {
800         Package *pkg=new Package(bld, n, src);
801         load_sub(*pkg);
802         bld.packages.insert(PackageMap::value_type(n, pkg));
803         bld.new_pkgs.push_back(pkg);
804 }
805