2 #include <msp/progress.h>
3 #include <msp/strconv.h>
4 #include <msp/strutils.h>
5 #include <msp/core/error.h>
6 #include <msp/getopt++/getopt++.h>
7 #include <msp/parser/parser.h>
8 #include <msp/path/utils.h>
9 #include <msp/time/units.h>
10 #include <msp/time/utils.h>
14 #include "executable.h"
18 #include "objectfile.h"
20 #include "systemlibrary.h"
21 #include "virtualtarget.h"
26 Builder::Builder(int argc, char **argv):
34 getopt.add_option(GetOpt::Option('v', "verbose", GetOpt::NONE));
35 getopt.add_option(GetOpt::Option('a', "analyze", GetOpt::REQUIRED));
36 getopt.add_option(GetOpt::Option('b', "build", GetOpt::NONE));
37 getopt.add_option(GetOpt::Option("max-depth", GetOpt::REQUIRED));
38 getopt.add_option(GetOpt::Option('n', "dry-run", GetOpt::NONE));
39 getopt.add_option(GetOpt::Option('W', "what-if", GetOpt::REQUIRED));
40 getopt.add_option(GetOpt::Option('B', "build-all", GetOpt::NONE));
41 getopt.add_option(GetOpt::Option('C', "chdir", GetOpt::REQUIRED));
42 getopt.add_option(GetOpt::Option('j', "jobs", GetOpt::REQUIRED));
43 getopt.add_option(GetOpt::Option('h', "help", GetOpt::NONE));
44 getopt.add_option(GetOpt::Option('c', "clean", GetOpt::NONE));
45 getopt.add_option(GetOpt::Option("chrome", GetOpt::NONE));
46 getopt.add_option(GetOpt::Option("full-paths", GetOpt::NONE));
47 int index=getopt(argc, argv);
49 verbose+=getopt['v'].count();
53 analyzer=new Analyzer(*this);
55 string mode=getopt['a'].arg();
57 analyzer->set_mode(Analyzer::DEPS);
58 else if(mode=="alldeps")
59 analyzer->set_mode(Analyzer::ALLDEPS);
60 else if(mode=="rebuild")
61 analyzer->set_mode(Analyzer::REBUILD);
62 else if(mode=="rdeps")
63 analyzer->set_mode(Analyzer::RDEPS);
65 throw UsageError("Invalid analysis mode");
67 if(getopt["max-depth"])
68 analyzer->set_max_depth(strtol(getopt["max-depth"].arg()));
69 analyzer->set_full_paths(getopt["full-paths"]);
73 jobs=max(strtol(getopt['j'].arg()), 1L);
79 chdir(getopt['C'].arg().c_str());
81 for(int i=index; i<argc; ++i)
84 unsigned equal=v.find('=');
85 if(equal!=string::npos)
86 cmdline_options.insert(RawOptionMap::value_type(v.substr(0, equal), v.substr(equal+1)));
88 cmdline_targets.push_back(argv[i]);
91 if(cmdline_targets.empty())
92 cmdline_targets.push_back("default");
96 Gets a package with the specified name, possibly creating it.
100 @return Pointer to the package, or 0 if the package could not be located
102 Package *Builder::get_package(const string &n)
104 PackageMap::iterator i=packages.find(n);
105 if(i!=packages.end())
109 argv.push_back("pkg-config");
110 argv.push_back("--variable=source");
112 string srcdir=strip(run_command(argv));
116 dirs.push_back(srcdir);
119 if(!dirname.compare(0, 3, "msp"))
121 dirs.push_back(cwd/dirname);
122 dirs.push_back(cwd/".."/dirname);
124 for(PathList::iterator j=dirs.begin(); j!=dirs.end(); ++j)
125 if(!load_build_file(*j/"Build"))
128 if(i!=packages.end())
133 Package *pkg=Package::create(*this, n);
134 packages.insert(PackageMap::value_type(n, pkg));
135 new_pkgs.push_back(pkg);
141 Returns the target with the given name, or 0 if no such target exists.
143 Target *Builder::get_target(const string &n)
145 TargetMap::iterator i=targets.find(n);
151 Target *Builder::get_header(const string &include, const string &from, const list<string> &path)
153 //XXX Should really hash the include path here
154 string id=from+":"+include;
155 TargetMap::iterator i=includes.find(id);
156 if(i!=includes.end())
159 string fn=include.substr(1);
161 if(include[0]=='"' && (tgt=check_header(Path::Path(from)/fn)))
163 if((tgt=check_header(Path::Path("/usr/include")/fn)))
165 if((tgt=check_header(Path::Path("/usr/include/c++/4.1.2")/fn)))
167 for(list<string>::const_iterator j=path.begin(); j!=path.end(); ++j)
168 if((tgt=check_header(Path::Path(*j)/fn)))
174 Target *Builder::get_library(const string &lib, const list<string> &path)
177 for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
178 for(unsigned j=0; j<i->size(); ++j)
181 string basename="lib"+lib+".so";
182 for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
184 string full=(Path::Path(*i)/basename).str();
185 Target *tgt=get_target(full);
188 if(Path::exists(full))
190 add_target(tgt=new SystemLibrary(*this, full));
200 if(load_build_file(cwd/"Build"))
202 cerr<<"No build info here.\n";
206 default_pkg=packages.begin()->second;
208 while(!new_pkgs.empty())
210 Package *pkg=new_pkgs.front();
212 pkg->process_options(cmdline_options);
213 new_pkgs.erase(new_pkgs.begin());
217 std::list<std::string> missing;
218 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
220 const list<PackageRef> &requires=i->second->get_requires();
221 for(list<PackageRef>::const_iterator j=requires.begin(); j!=requires.end(); ++j)
222 if(!j->get_package())
223 missing.push_back(j->get_name());
230 cerr<<"The following packages were not found on the system:\n";
231 for(list<string>::iterator i=missing.begin(); i!=missing.end(); ++i)
233 cerr<<"Please install them and try again.\n";
237 default_pkg->create_build_info();
242 cout<<packages.size()<<" packages, "<<targets.size()<<" targets\n";
245 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
247 cout<<' '<<i->second->get_name();
248 if(i->second->get_buildable())
251 for(TargetMap::iterator j=targets.begin(); j!=targets.end(); ++j)
252 if(j->second->get_package()==i->second)
254 cout<<" ("<<count<<" targets)\n";
268 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
270 for(TargetMap::iterator i=targets.begin(); i!=targets.end(); ++i)
275 int Builder::load_build_file(const Path::Path &fn)
277 ifstream in(fn.str().c_str());
281 Parser::Parser parser(in, fn.str());
282 Loader loader(*this, fn.subpath(0, fn.size()-1));
288 int Builder::create_targets()
290 Target *world=new VirtualTarget(*this, "world");
293 Target *def_tgt=new VirtualTarget(*this, "default");
295 world->add_depend(def_tgt);
297 Target *install=new VirtualTarget(*this, "install");
299 world->add_depend(install);
301 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
305 if(!i->second->get_buildable())
308 const ComponentList &components=i->second->get_components();
309 for(ComponentList::const_iterator j=components.begin(); j!=components.end(); ++j)
312 const PathList &sources=j->get_sources();
313 for(PathList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
315 list<string> sfiles=list_files(*k);
316 for(list<string>::iterator l=sfiles.begin(); l!=sfiles.end(); ++l)
317 files.push_back(*k / *l);
320 Path::Path inst_base=i->second->get_config().get_option("prefix").value;
322 bool build_exe=j->get_type()!=Component::HEADERS;
324 list<ObjectFile *> objs;
325 for(PathList::iterator k=files.begin(); k!=files.end(); ++k)
327 string basename=(*k)[-1];
328 string ext=Path::splitext(basename).ext;
329 if(ext==".cpp" || ext==".c")
333 SourceFile *src=new SourceFile(*this, &*j, k->str());
336 ObjectFile *obj=new ObjectFile(*this, *j, *src);
343 Target *hdr=get_target(k->str());
346 hdr=new Header(*this, &*j, k->str());
349 if(!j->get_install_headers().empty())
351 Path::Path inst_path=inst_base/"include"/j->get_install_headers()/basename;
352 Install *inst=new Install(*this, *i->second, *hdr, inst_path.str());
354 install->add_depend(inst);
361 Executable *exe=new Executable(*this, *j, objs);
363 if(i->second==default_pkg)
364 def_tgt->add_depend(exe);
366 world->add_depend(exe);
371 if(j->get_type()==Component::PROGRAM)
373 else if(j->get_type()==Component::LIBRARY)
375 if(!inst_dir.empty())
377 Install *inst=new Install(*this, *i->second, *exe, (inst_base/inst_dir/Path::basename(exe->get_name())).str());
379 install->add_depend(inst);
386 while(!new_tgts.empty())
388 Target *tgt=new_tgts.front();
389 new_tgts.erase(new_tgts.begin());
391 if(!tgt->get_depends_ready())
392 new_tgts.push_back(tgt);
395 Target *cmdline=new VirtualTarget(*this, "cmdline");
397 world->add_depend(cmdline);
398 for(list<string>::iterator i=cmdline_targets.begin(); i!=cmdline_targets.end(); ++i)
400 Target *tgt=get_target(*i);
402 tgt=get_target((cwd/ *i).str());
405 cerr<<"I don't know anything about "<<*i<<'\n';
408 cmdline->add_depend(tgt);
416 Target *Builder::check_header(const Msp::Path::Path &fn)
418 Target *tgt=get_target(fn.str());
423 add_target(tgt=new SystemHeader(*this, fn.str()));
429 void Builder::add_target(Target *t)
431 targets.insert(TargetMap::value_type(t->get_name(), t));
432 new_tgts.push_back(t);
437 Target *cmdline=get_target("cmdline");
439 unsigned total=cmdline->count_rebuild();
442 cout<<"Already up to date\n";
445 cout<<"Will build "<<total<<" target(s)\n";
447 vector<Action *> actions;
449 //ProgressBar *progress=0;
452 //progress=new ProgressBar(cout, total);
453 cout<<"0 targets built\n";
462 if(actions.size()<jobs && !fail)
464 Target *tgt=cmdline->get_buildable_target();
469 cout<<"\e["<<actions.size()+1<<'A';
470 //progress->set(count);
471 cout<<"\e["<<actions.size()+1<<'B';
473 Action *action=tgt->build();
475 actions.push_back(action);
477 else if(actions.empty())
481 Time::sleep(10*Time::msec);
483 for(unsigned i=0; i<actions.size();)
485 int status=actions[i]->check();
491 cout<<"\e["<<actions.size()+1<<'A';
492 cout<<count<<" targets built\n";
496 if(i<actions.size()-1)
497 cout<<"\e["<<actions.size()-i-1<<"B";
501 actions.erase(actions.begin()+i);
504 if(actions.empty() && fail)
517 Application::RegApp<Builder> Builder::reg;
519 Builder::Loader::Loader(Builder &b, const Path::Path &s):
523 add("package", &Loader::package);
526 void Builder::Loader::package(const string &n)
528 Package *pkg=new Package(bld, n, src);
530 bld.packages.insert(PackageMap::value_type(n, pkg));
531 bld.new_pkgs.push_back(pkg);