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);
140 Target *Builder::get_target(const string &n)
142 TargetMap::iterator i=targets.find(n);
148 Target *Builder::get_header(const string &include, const string &from, const list<string> &path)
150 //XXX Should really hash the include path here
151 string id=from+":"+include;
152 TargetMap::iterator i=includes.find(id);
153 if(i!=includes.end())
156 string fn=include.substr(1);
158 if(include[0]=='"' && (tgt=check_header(Path::Path(from)/fn)))
160 if((tgt=check_header(Path::Path("/usr/include")/fn)))
162 if((tgt=check_header(Path::Path("/usr/include/c++/4.1.2")/fn)))
164 for(list<string>::const_iterator j=path.begin(); j!=path.end(); ++j)
165 if((tgt=check_header(Path::Path(*j)/fn)))
171 Target *Builder::get_library(const string &lib, const list<string> &path)
174 for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
175 for(unsigned j=0; j<i->size(); ++j)
178 string basename="lib"+lib+".so";
179 for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
181 string full=(Path::Path(*i)/basename).str();
182 Target *tgt=get_target(full);
185 if(Path::exists(full))
187 add_target(tgt=new SystemLibrary(*this, full));
197 if(load_build_file(cwd/"Build"))
199 cerr<<"No build info here.\n";
203 default_pkg=packages.begin()->second;
205 while(!new_pkgs.empty())
207 Package *pkg=new_pkgs.front();
209 pkg->process_options(cmdline_options);
210 new_pkgs.erase(new_pkgs.begin());
214 std::list<std::string> missing;
215 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
217 const list<PackageRef> &requires=i->second->get_requires();
218 for(list<PackageRef>::const_iterator j=requires.begin(); j!=requires.end(); ++j)
219 if(!j->get_package())
220 missing.push_back(j->get_name());
227 cerr<<"The following packages were not found on the system:\n";
228 for(list<string>::iterator i=missing.begin(); i!=missing.end(); ++i)
230 cerr<<"Please install them and try again.\n";
234 default_pkg->create_build_info();
239 cout<<packages.size()<<" packages, "<<targets.size()<<" targets\n";
242 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
244 cout<<' '<<i->second->get_name();
245 if(i->second->get_buildable())
248 for(TargetMap::iterator j=targets.begin(); j!=targets.end(); ++j)
249 if(j->second->get_package()==i->second)
251 cout<<" ("<<count<<" targets)\n";
265 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
267 for(TargetMap::iterator i=targets.begin(); i!=targets.end(); ++i)
272 int Builder::load_build_file(const Path::Path &fn)
274 ifstream in(fn.str().c_str());
278 Parser::Parser parser(in, fn.str());
279 Loader loader(*this, fn.subpath(0, fn.size()-1));
285 int Builder::create_targets()
287 Target *world=new VirtualTarget(*this, "world");
290 Target *def_tgt=new VirtualTarget(*this, "default");
292 world->add_depend(def_tgt);
294 Target *install=new VirtualTarget(*this, "install");
296 world->add_depend(install);
298 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
302 if(!i->second->get_buildable())
305 const ComponentList &components=i->second->get_components();
306 for(ComponentList::const_iterator j=components.begin(); j!=components.end(); ++j)
309 const PathList &sources=j->get_sources();
310 for(PathList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
312 list<string> sfiles=list_files(*k);
313 for(list<string>::iterator l=sfiles.begin(); l!=sfiles.end(); ++l)
314 files.push_back(*k / *l);
317 Path::Path inst_base=i->second->get_config().get_option("prefix").value;
319 bool build_exe=j->get_type()!=Component::HEADERS;
321 list<ObjectFile *> objs;
322 for(PathList::iterator k=files.begin(); k!=files.end(); ++k)
324 string basename=(*k)[-1];
325 string ext=Path::splitext(basename).ext;
326 if(ext==".cpp" || ext==".c")
330 SourceFile *src=new SourceFile(*this, &*j, k->str());
333 ObjectFile *obj=new ObjectFile(*this, *j, *src);
340 Target *hdr=get_target(k->str());
343 hdr=new Header(*this, &*j, k->str());
346 if(!j->get_install_headers().empty())
348 Path::Path inst_path=inst_base/"include"/j->get_install_headers()/basename;
349 Install *inst=new Install(*this, *i->second, *hdr, inst_path.str());
351 install->add_depend(inst);
358 Executable *exe=new Executable(*this, *j, objs);
360 if(i->second==default_pkg)
361 def_tgt->add_depend(exe);
363 world->add_depend(exe);
368 if(j->get_type()==Component::PROGRAM)
370 else if(j->get_type()==Component::LIBRARY)
372 if(!inst_dir.empty())
374 Install *inst=new Install(*this, *i->second, *exe, (inst_base/inst_dir/Path::basename(exe->get_name())).str());
376 install->add_depend(inst);
383 while(!new_tgts.empty())
385 Target *tgt=new_tgts.front();
386 new_tgts.erase(new_tgts.begin());
390 Target *cmdline=new VirtualTarget(*this, "cmdline");
392 world->add_depend(cmdline);
393 for(list<string>::iterator i=cmdline_targets.begin(); i!=cmdline_targets.end(); ++i)
395 Target *tgt=get_target(*i);
397 tgt=get_target((cwd/ *i).str());
400 cerr<<"I don't know anything about "<<*i<<'\n';
403 cmdline->add_depend(tgt);
411 Target *Builder::check_header(const Msp::Path::Path &fn)
413 Target *tgt=get_target(fn.str());
418 add_target(tgt=new SystemHeader(*this, fn.str()));
424 void Builder::add_target(Target *t)
426 targets.insert(TargetMap::value_type(t->get_name(), t));
427 new_tgts.push_back(t);
432 Target *cmdline=get_target("cmdline");
434 unsigned total=cmdline->count_rebuild();
437 cout<<"Already up to date\n";
440 cout<<"Will build "<<total<<" target(s)\n";
442 vector<Action *> actions;
444 //ProgressBar *progress=0;
447 //progress=new ProgressBar(cout, total);
448 cout<<"0 targets built\n";
457 if(actions.size()<jobs && !fail)
459 Target *tgt=cmdline->get_buildable_target();
464 cout<<"\e["<<actions.size()+1<<'A';
465 //progress->set(count);
466 cout<<"\e["<<actions.size()+1<<'B';
468 Action *action=tgt->build();
470 actions.push_back(action);
472 else if(actions.empty())
476 Time::sleep(10*Time::msec);
478 for(unsigned i=0; i<actions.size();)
480 int status=actions[i]->check();
486 cout<<"\e["<<actions.size()+1<<'A';
487 cout<<count<<" targets built\n";
491 if(i<actions.size()-1)
492 cout<<"\e["<<actions.size()-i-1<<"B";
496 actions.erase(actions.begin()+i);
499 if(actions.empty() && fail)
512 Application::RegApp<Builder> Builder::reg;
514 Builder::Loader::Loader(Builder &b, const Path::Path &s):
518 add("package", &Loader::package);
521 void Builder::Loader::package(const string &n)
523 Package *pkg=new Package(bld, n, src);
525 bld.packages.insert(PackageMap::value_type(n, pkg));
526 bld.new_pkgs.push_back(pkg);