-int Builder::main()
-{
- if(load_build_file(cwd/build_file))
- {
- cerr<<"No build info here.\n";
- return 1;
- }
-
- main_pkg->configure(cmdline_options, conf_all?2:1);
-
- if(help)
- {
- usage(0, "builder", false);
- cout<<'\n';
- package_help();
- return 0;
- }
-
- if(!conf_only && create_targets())
- return 1;
-
- PackageList all_reqs=main_pkg->collect_requires();
-
- if(conf_only)
- return 0;
-
- if(verbose>=1)
- cout<<all_reqs.size()<<" active packages, "<<targets.size()<<" targets\n";
- if(verbose>=2)
- {
- for(PackageList::const_iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
- {
- cout<<' '<<(*i)->get_name();
- if(dynamic_cast<SourcePackage *>(*i))
- cout<<'*';
- unsigned count=0;
- unsigned ood_count=0;
- for(TargetMap::iterator j=targets.begin(); j!=targets.end(); ++j)
- if(j->second->get_package()==*i)
- {
- ++count;
- if(j->second->get_rebuild())
- ++ood_count;
- }
- if(count)
- {
- cout<<" ("<<count<<" targets";
- if(ood_count)
- cout<<", "<<ood_count<<" out-of-date";
- cout<<')';
- }
- cout<<'\n';
- }
- }
-
- if(analyzer)
- analyzer->analyze();
-
- if(!problems.empty())
- {
- cerr<<"The following problems were detected:\n";
- for(ProblemList::iterator i=problems.begin(); i!=problems.end(); ++i)
- cerr<<" "<<i->package<<": "<<i->descr<<'\n';
- cerr<<"Please fix them and try again.\n";
- return 1;
- }
-
- //if(create_makefile
-
- if(clean)
- exit_code=do_clean();
- else if(build)
- exit_code=do_build();
-
- return exit_code;
-}
-
-Builder::~Builder()
-{
- for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
- delete i->second;
- for(TargetMap::iterator i=targets.begin(); i!=targets.end(); ++i)
- delete i->second;
- delete analyzer;
-}
-
-void Builder::usage(const char *reason, const char *argv0, bool brief)
-{
- if(reason)
- cerr<<reason<<'\n';
-
- if(brief)
- 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> ...]\n";
- else
- {
- cerr<<
- "Usage: "<<argv0<<" [options] [<target> ...]\n"
- "\n"
- "Options:\n"
- " -a, --analyze MODE Perform analysis. MODE can be deps, alldeps or rebuild.\n"
- " -b, --build Perform build even if doing analysis.\n"
- " -c, --clean Clean buildable targets.\n"
- " -f, --file FILE Read info from FILE instead of Build.\n"
- " -h, --help Print this message.\n"
- " -j, --jobs NUM Run NUM commands at once, whenever possible.\n"
- " -n, --dry-run Don't actually do anything, only show what would be done.\n"
- " -v, --verbose Print more information about what's going on.\n"
- " -A, --conf-all Apply configuration to all packages.\n"
- " -B, --build-all Build all targets unconditionally.\n"
- " -C, --chdir DIR Change to DIR before doing anything else.\n"
- " -P, --progress Display progress while building.\n"
- " -W, --what-if FILE Pretend that FILE has changed.\n"
- " --arch ARCH Architecture to build for.\n"
- " --conf-only Stop after configuring packages.\n"
- " --full-paths Output full paths in analysis.\n"
- //" --makefile Create a makefile for this package.\n"
- " --max-depth NUM Maximum depth to show in analysis.\n"
- " --prefix DIR Directory to install things to.\n"
- " --warnings LIST Compiler warnings to use.\n";
- }
-}
-
-/**
-Determines the source directory of a package. pkg-config is consulted first,
-and if it fails, the package path is searched for matches.
-*/
-FS::Path Builder::get_package_location(const string &name)
-{
- if(verbose>=3)
- cout<<"Looking for package "<<name<<'\n';
-
- // Try to get source directory with pkgconfig
- list<string> argv;
- argv.push_back("pkg-config");
- argv.push_back("--variable=source");
- argv.push_back(name);
- if(verbose>=4)
- cout<<"Running "<<join(argv.begin(), argv.end())<<'\n';
- string srcdir=strip(run_command(argv));
- if(!srcdir.empty())
- return srcdir;
-
- if(pkg_dirs.empty())
- {
- for(list<FS::Path>::const_iterator i=pkg_path.begin(); i!=pkg_path.end(); ++i)
- {
- list<string> files=list_files(*i);
- for(list<string>::const_iterator j=files.begin(); j!=files.end(); ++j)
- {
- FS::Path full=*i / *j;
- if(FS::exists(full/"Build"))
- pkg_dirs.push_back(full);
- }
- }
- if(verbose>=3)
- cout<<pkg_dirs.size()<<" packages found in path\n";
- }
-
- bool msp=!name.compare(0, 3, "msp");
- for(list<FS::Path>::const_iterator i=pkg_dirs.begin(); i!=pkg_dirs.end(); ++i)
- {
- string base=basename(*i);
- unsigned dash=base.rfind('-');
-
- if(!base.compare(0, dash, name))
- return *i;
- else if(msp && !base.compare(0, dash-3, name, 3, string::npos))
- return *i;
- }
-
- return FS::Path();
-}
-
-/**
-Loads the given build file.
-
-@param fn Path to the file
-
-@return 0 on success, -1 if the file could not be opened
-*/
-int Builder::load_build_file(const FS::Path &fn)
-{
- try
- {
- IO::File inf(fn.str());
- IO::Buffered in(inf);
-
- if(verbose>=3)
- cout<<"Reading "<<fn<<'\n';
-
- DataFile::Parser parser(in, fn.str());
- Loader loader(*this, fn.subpath(0, fn.size()-1));
- loader.load(parser);
- }
- catch(const IO::FileNotFound &)
- {
- return -1;
- }
-
- return 0;
-}
-
-/**
-Creates targets for all packages and prepares them for building.
-
-@return 0 if everything went ok, -1 if something bad happened and a build
- shouldn't be attempted
-*/
-int Builder::create_targets()
-{
- Target *world=new VirtualTarget(*this, "world");
-
- Target *def_tgt=new VirtualTarget(*this, "default");
- world->add_depend(def_tgt);
-
- Target *install=new VirtualTarget(*this, "install");
- world->add_depend(install);
-
- Target *tarballs=new VirtualTarget(*this, "tarballs");
- world->add_depend(tarballs);
-
- PackageList all_reqs=main_pkg->collect_requires();
- for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
- {
- SourcePackage *spkg=dynamic_cast<SourcePackage *>(*i);
- if(!spkg)
- continue;
-
- const ComponentList &components=spkg->get_components();
- for(ComponentList::const_iterator j=components.begin(); j!=components.end(); ++j)
- j->create_targets();
-
- if(spkg->get_install_flags()&(SourcePackage::LIB|SourcePackage::INCLUDE))
- {
- PkgConfig *pc=new PkgConfig(*this, *spkg);
- install->add_depend(new Install(*this, *spkg, *pc));
- }
-
- tarballs->add_depend(new TarBall(*this, *spkg));
- }
-
- // Find dependencies until no new targets are created
- while(!new_tgts.empty())
- {
- Target *tgt=new_tgts.front();
- new_tgts.erase(new_tgts.begin());
- tgt->find_depends();
- if(!tgt->get_depends_ready())
- new_tgts.push_back(tgt);
- }
-
- // Apply what-ifs
- for(StringList::iterator i=what_if.begin(); i!=what_if.end(); ++i)
- {
- Target *tgt=get_target((cwd/ *i).str());
- if(!tgt)
- {
- cerr<<"Unknown what-if target "<<*i<<'\n';
- return -1;
- }
- tgt->touch();
- }
-
- // Make the cmdline target depend on all targets mentioned on the command line
- Target *cmdline=new VirtualTarget(*this, "cmdline");
- bool build_world=false;
- for(list<string>::iterator i=cmdline_targets.begin(); i!=cmdline_targets.end(); ++i)
- {
- Target *tgt=get_target(*i);
- if(!tgt)
- tgt=get_target((cwd/ *i).str());
- if(!tgt)
- {
- cerr<<"I don't know anything about "<<*i<<'\n';
- return -1;
- }
- if(tgt==world)
- build_world=true;
- cmdline->add_depend(tgt);
- }
-
- /* If world is to be built, prepare cmdline. If not, add cmdline to world
- and prepare world. I don't really like this, but it keeps the graph
- acyclic. */
- if(build_world)
- cmdline->prepare();
- else
- {
- world->add_depend(cmdline);
- world->prepare();
- }
-
- for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
- if(SourcePackage *spkg=dynamic_cast<SourcePackage *>(i->second))
- spkg->get_deps_cache().save();
-
- return 0;
-}
-
-/**
-Check if a header exists, either as a target or a file. Either an existing
-target or a new SystemHeader target will be returned.
-*/
-Target *Builder::get_header(const Msp::FS::Path &fn)