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));
136 new_pkgs.push_back(pkg);
142 Returns the target with the given name, or 0 if no such target exists.
144 Target *Builder::get_target(const string &n)
146 TargetMap::iterator i=targets.find(n);
152 Target *Builder::get_header(const string &include, const string &from, const list<string> &path)
154 //XXX Should really hash the include path here
155 string id=from+":"+include;
156 TargetMap::iterator i=includes.find(id);
157 if(i!=includes.end())
160 string fn=include.substr(1);
162 if(include[0]=='"' && (tgt=check_header(Path::Path(from)/fn)))
164 if((tgt=check_header(Path::Path("/usr/include")/fn)))
166 if((tgt=check_header(Path::Path("/usr/include/c++/4.1.2")/fn)))
168 for(list<string>::const_iterator j=path.begin(); j!=path.end(); ++j)
169 if((tgt=check_header(Path::Path(*j)/fn)))
175 Target *Builder::get_library(const string &lib, const list<string> &path)
178 for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
179 for(unsigned j=0; j<i->size(); ++j)
182 string basename="lib"+lib+".so";
183 for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
185 string full=(Path::Path(*i)/basename).str();
186 Target *tgt=get_target(full);
189 if(Path::exists(full))
191 add_target(tgt=new SystemLibrary(*this, full));
201 if(load_build_file(cwd/"Build"))
203 cerr<<"No build info here.\n";
207 default_pkg=packages.begin()->second;
209 while(!new_pkgs.empty())
211 Package *pkg=new_pkgs.front();
213 pkg->process_options(cmdline_options);
214 new_pkgs.erase(new_pkgs.begin());
218 std::list<std::string> missing;
219 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
221 missing.push_back(i->first);
226 cerr<<"The following packages were not found on the system:\n";
227 for(list<string>::iterator i=missing.begin(); i!=missing.end(); ++i)
229 cerr<<"Please install them and try again.\n";
233 default_pkg->create_build_info();
238 cout<<packages.size()<<" packages, "<<targets.size()<<" targets\n";
241 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
243 cout<<' '<<i->second->get_name();
244 if(i->second->get_buildable())
247 for(TargetMap::iterator j=targets.begin(); j!=targets.end(); ++j)
248 if(j->second->get_package()==i->second)
250 cout<<" ("<<count<<" targets)\n";
264 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
266 for(TargetMap::iterator i=targets.begin(); i!=targets.end(); ++i)
271 int Builder::load_build_file(const Path::Path &fn)
273 ifstream in(fn.str().c_str());
277 Parser::Parser parser(in, fn.str());
278 Loader loader(*this, fn.subpath(0, fn.size()-1));
284 int Builder::create_targets()
286 Target *world=new VirtualTarget(*this, "world");
289 Target *def_tgt=new VirtualTarget(*this, "default");
291 world->add_depend(def_tgt);
293 Target *install=new VirtualTarget(*this, "install");
295 world->add_depend(install);
297 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
301 if(!i->second->get_buildable())
304 Path::Path inst_base;
305 if(i->second->get_config().is_option("prefix"))
306 inst_base=i->second->get_config().get_option("prefix").value;
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)
317 if(S_ISDIR(st.st_mode))
319 list<string> sfiles=list_files(*k);
320 for(list<string>::iterator l=sfiles.begin(); l!=sfiles.end(); ++l)
321 files.push_back(*k / *l);
327 bool build_exe=j->get_type()!=Component::HEADERS;
329 list<ObjectFile *> objs;
330 for(PathList::iterator k=files.begin(); k!=files.end(); ++k)
332 string basename=(*k)[-1];
333 string ext=Path::splitext(basename).ext;
334 if(ext==".cpp" || ext==".c")
338 SourceFile *src=new SourceFile(*this, &*j, k->str());
341 ObjectFile *obj=new ObjectFile(*this, *j, *src);
348 Target *hdr=get_target(k->str());
351 hdr=new Header(*this, &*j, k->str());
354 if(!j->get_install_headers().empty())
356 Path::Path inst_path=inst_base/"include"/j->get_install_headers()/basename;
357 Install *inst=new Install(*this, *i->second, *hdr, inst_path.str());
359 install->add_depend(inst);
366 Executable *exe=new Executable(*this, *j, objs);
368 if(i->second==default_pkg)
369 def_tgt->add_depend(exe);
371 world->add_depend(exe);
376 if(j->get_type()==Component::PROGRAM)
378 else if(j->get_type()==Component::LIBRARY)
380 if(!inst_dir.empty())
382 Install *inst=new Install(*this, *i->second, *exe, (inst_base/inst_dir/Path::basename(exe->get_name())).str());
384 install->add_depend(inst);
391 while(!new_tgts.empty())
393 Target *tgt=new_tgts.front();
394 new_tgts.erase(new_tgts.begin());
396 if(!tgt->get_depends_ready())
397 new_tgts.push_back(tgt);
400 Target *cmdline=new VirtualTarget(*this, "cmdline");
402 world->add_depend(cmdline);
403 for(list<string>::iterator i=cmdline_targets.begin(); i!=cmdline_targets.end(); ++i)
405 Target *tgt=get_target(*i);
407 tgt=get_target((cwd/ *i).str());
410 cerr<<"I don't know anything about "<<*i<<'\n';
413 cmdline->add_depend(tgt);
421 Target *Builder::check_header(const Msp::Path::Path &fn)
423 Target *tgt=get_target(fn.str());
428 add_target(tgt=new SystemHeader(*this, fn.str()));
434 void Builder::add_target(Target *t)
436 targets.insert(TargetMap::value_type(t->get_name(), t));
437 new_tgts.push_back(t);
442 Target *cmdline=get_target("cmdline");
444 unsigned total=cmdline->count_rebuild();
447 cout<<"Already up to date\n";
450 cout<<"Will build "<<total<<" target(s)\n";
452 vector<Action *> actions;
455 cout<<"0 targets built\n";
463 if(actions.size()<jobs && !fail)
465 Target *tgt=cmdline->get_buildable_target();
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)
510 Application::RegApp<Builder> Builder::reg;
512 Builder::Loader::Loader(Builder &b, const Path::Path &s):
516 add("package", &Loader::package);
519 void Builder::Loader::package(const string &n)
521 Package *pkg=new Package(bld, n, src);
523 bld.packages.insert(PackageMap::value_type(n, pkg));
524 bld.new_pkgs.push_back(pkg);