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