3 #include <msp/progress.h>
4 #include <msp/strconv.h>
5 #include <msp/strutils.h>
6 #include <msp/core/error.h>
7 #include <msp/getopt++/getopt++.h>
8 #include <msp/parser/parser.h>
9 #include <msp/path/utils.h>
10 #include <msp/time/units.h>
11 #include <msp/time/utils.h>
15 #include "executable.h"
19 #include "objectfile.h"
21 #include "systemlibrary.h"
22 #include "virtualtarget.h"
27 Builder::Builder(int argc, char **argv):
37 getopt.add_option(GetOpt::Option('v', "verbose", GetOpt::NONE));
38 getopt.add_option(GetOpt::Option('a', "analyze", GetOpt::REQUIRED));
39 getopt.add_option(GetOpt::Option('b', "build", GetOpt::NONE));
40 getopt.add_option(GetOpt::Option("max-depth", GetOpt::REQUIRED));
41 getopt.add_option(GetOpt::Option('n', "dry-run", GetOpt::NONE));
42 getopt.add_option(GetOpt::Option('W', "what-if", GetOpt::REQUIRED));
43 getopt.add_option(GetOpt::Option('B', "build-all", GetOpt::NONE));
44 getopt.add_option(GetOpt::Option('C', "chdir", GetOpt::REQUIRED));
45 getopt.add_option(GetOpt::Option('j', "jobs", GetOpt::REQUIRED, "1"));
46 getopt.add_option(GetOpt::Option('h', "help", GetOpt::NONE));
47 getopt.add_option(GetOpt::Option('c', "clean", GetOpt::NONE));
48 getopt.add_option(GetOpt::Option('f', "file", GetOpt::REQUIRED, "Build"));
49 getopt.add_option(GetOpt::Option("chrome", GetOpt::NONE));
50 getopt.add_option(GetOpt::Option("full-paths", GetOpt::NONE));
51 getopt.add_option(GetOpt::Option('A', "conf-all", GetOpt::NONE));
52 int index=getopt(argc, argv);
54 verbose+=getopt['v'].count();
58 analyzer=new Analyzer(*this);
60 string mode=getopt['a'].arg();
62 analyzer->set_mode(Analyzer::DEPS);
63 else if(mode=="alldeps")
64 analyzer->set_mode(Analyzer::ALLDEPS);
65 else if(mode=="rebuild")
66 analyzer->set_mode(Analyzer::REBUILD);
67 else if(mode=="rdeps")
68 analyzer->set_mode(Analyzer::RDEPS);
70 throw UsageError("Invalid analysis mode");
72 if(getopt["max-depth"])
73 analyzer->set_max_depth(strtol(getopt["max-depth"].arg()));
74 analyzer->set_full_paths(getopt["full-paths"]);
82 jobs=max(strtol(getopt['j'].arg()), 1L);
84 chrome=getopt["chrome"];
89 chdir(getopt['C'].arg().c_str());
91 build_file=getopt['f'].arg();
93 for(int i=index; i<argc; ++i)
96 unsigned equal=v.find('=');
97 if(equal!=string::npos)
98 cmdline_options.insert(RawOptionMap::value_type(v.substr(0, equal), v.substr(equal+1)));
100 cmdline_targets.push_back(argv[i]);
103 if(cmdline_targets.empty())
104 cmdline_targets.push_back("default");
107 what_if.push_back(getopt['W'].arg());
111 Gets a package with the specified name, possibly creating it.
113 @param n Package name
115 @return Pointer to the package, or 0 if the package could not be located
117 Package *Builder::get_package(const string &n)
119 PackageMap::iterator i=packages.find(n);
120 if(i!=packages.end())
123 // Try to get source directory with pkgconfig
125 argv.push_back("pkg-config");
126 argv.push_back("--variable=source");
128 string srcdir=strip(run_command(argv));
132 dirs.push_back(srcdir);
134 // Make some other guesses about the source directory
136 if(!dirname.compare(0, 3, "msp"))
138 dirs.push_back(cwd/dirname);
139 dirs.push_back(cwd/".."/dirname);
141 // Go through the candidate directories and look for a Build file
142 for(PathList::iterator j=dirs.begin(); j!=dirs.end(); ++j)
143 if(!load_build_file(*j/"Build"))
146 if(i!=packages.end())
151 // Package source not found - create a binary package
152 Package *pkg=Package::create(*this, n);
153 packages.insert(PackageMap::value_type(n, pkg));
155 new_pkgs.push_back(pkg);
161 Returns the target with the given name, or 0 if no such target exists.
163 Target *Builder::get_target(const string &n)
165 TargetMap::iterator i=targets.find(n);
172 Tries to locate a header included from a given location and with a given include
173 path. Considers known targets as well as existing files.
175 Target *Builder::get_header(const string &include, const string &from, const list<string> &path)
178 update_hash(hash, from);
179 for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
180 update_hash(hash, *i);
182 string id=hash+include;
183 TargetMap::iterator i=includes.find(id);
184 if(i!=includes.end())
187 string fn=include.substr(1);
189 if(include[0]=='"' && (tgt=check_header(Path::Path(from)/fn)))
191 if((tgt=check_header(Path::Path("/usr/include")/fn)))
193 //XXX Determine the C++ header location dynamically
194 if((tgt=check_header(Path::Path("/usr/include/c++/4.1.2")/fn)))
196 for(list<string>::const_iterator j=path.begin(); j!=path.end(); ++j)
197 if((tgt=check_header(Path::Path(*j)/fn)))
203 Target *Builder::get_library(const string &lib, const list<string> &path)
206 for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
207 update_hash(hash, *i);
210 TargetMap::iterator i=libraries.find(id);
211 if(i!=libraries.end())
214 string basename="lib"+lib+".so";
215 for(list<string>::const_iterator j=path.begin(); j!=path.end(); ++j)
217 string full=(Path::Path(*j)/basename).str();
218 Target *tgt=get_target(full);
221 if(Path::exists(full))
223 add_target(tgt=new SystemLibrary(*this, full));
233 if(load_build_file(cwd/build_file))
235 cerr<<"No build info here.\n";
239 default_pkg=packages.begin()->second;
241 while(!new_pkgs.empty())
243 Package *pkg=new_pkgs.front();
244 if(pkg==default_pkg || conf_all)
245 pkg->process_options(cmdline_options);
246 new_pkgs.erase(new_pkgs.begin());
250 std::list<std::string> missing;
251 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
253 missing.push_back(i->first);
258 cerr<<"The following packages were not found on the system:\n";
259 for(list<string>::iterator i=missing.begin(); i!=missing.end(); ++i)
261 cerr<<"Please install them and try again.\n";
265 default_pkg->create_build_info();
270 cout<<packages.size()<<" packages, "<<targets.size()<<" targets\n";
273 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
275 cout<<' '<<i->second->get_name();
276 if(i->second->get_buildable())
279 for(TargetMap::iterator j=targets.begin(); j!=targets.end(); ++j)
280 if(j->second->get_package()==i->second)
282 cout<<" ("<<count<<" targets)\n";
297 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
299 for(TargetMap::iterator i=targets.begin(); i!=targets.end(); ++i)
304 int Builder::load_build_file(const Path::Path &fn)
306 ifstream in(fn.str().c_str());
310 Parser::Parser parser(in, fn.str());
311 Loader loader(*this, fn.subpath(0, fn.size()-1));
317 int Builder::create_targets()
319 Target *world=new VirtualTarget(*this, "world");
322 Target *def_tgt=new VirtualTarget(*this, "default");
324 world->add_depend(def_tgt);
326 Target *install=new VirtualTarget(*this, "install");
328 world->add_depend(install);
330 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
334 if(!i->second->get_buildable())
337 Path::Path inst_base;
338 if(i->second->get_config().is_option("prefix"))
339 inst_base=i->second->get_config().get_option("prefix").value;
341 const ComponentList &components=i->second->get_components();
342 for(ComponentList::const_iterator j=components.begin(); j!=components.end(); ++j)
345 const PathList &sources=j->get_sources();
346 for(PathList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
350 if(S_ISDIR(st.st_mode))
352 list<string> sfiles=list_files(*k);
353 for(list<string>::iterator l=sfiles.begin(); l!=sfiles.end(); ++l)
354 files.push_back(*k / *l);
360 bool build_exe=j->get_type()!=Component::HEADERS;
362 list<ObjectFile *> objs;
363 for(PathList::iterator k=files.begin(); k!=files.end(); ++k)
365 string basename=(*k)[-1];
366 string ext=Path::splitext(basename).ext;
367 if(ext==".cpp" || ext==".c")
371 SourceFile *src=new SourceFile(*this, &*j, k->str());
374 ObjectFile *obj=new ObjectFile(*this, *j, *src);
381 Target *hdr=get_target(k->str());
384 hdr=new Header(*this, &*j, k->str());
387 if(!j->get_install_headers().empty())
389 Path::Path inst_path=inst_base/"include"/j->get_install_headers()/basename;
390 Install *inst=new Install(*this, *i->second, *hdr, inst_path.str());
392 install->add_depend(inst);
399 Executable *exe=new Executable(*this, *j, objs);
401 if(i->second==default_pkg)
402 def_tgt->add_depend(exe);
404 world->add_depend(exe);
409 if(j->get_type()==Component::PROGRAM)
411 else if(j->get_type()==Component::LIBRARY)
413 if(!inst_dir.empty())
415 Install *inst=new Install(*this, *i->second, *exe, (inst_base/inst_dir/Path::basename(exe->get_name())).str());
417 install->add_depend(inst);
424 while(!new_tgts.empty())
426 Target *tgt=new_tgts.front();
427 new_tgts.erase(new_tgts.begin());
429 if(!tgt->get_depends_ready())
430 new_tgts.push_back(tgt);
433 for(list<string>::iterator i=what_if.begin(); i!=what_if.end(); ++i)
435 Target *tgt=get_target((cwd/ *i).str());
438 cerr<<"Unknown what-if target "<<*i<<'\n';
444 Target *cmdline=new VirtualTarget(*this, "cmdline");
446 world->add_depend(cmdline);
447 for(list<string>::iterator i=cmdline_targets.begin(); i!=cmdline_targets.end(); ++i)
449 Target *tgt=get_target(*i);
451 tgt=get_target((cwd/ *i).str());
454 cerr<<"I don't know anything about "<<*i<<'\n';
457 cmdline->add_depend(tgt);
465 Target *Builder::check_header(const Msp::Path::Path &fn)
467 Target *tgt=get_target(fn.str());
472 add_target(tgt=new SystemHeader(*this, fn.str()));
478 void Builder::add_target(Target *t)
480 targets.insert(TargetMap::value_type(t->get_name(), t));
481 new_tgts.push_back(t);
484 void Builder::update_hash(string &hash, const string &value)
486 for(unsigned i=0; i<value.size(); ++i)
487 hash[i%hash.size()]^=value[i];
491 This function supervises the build process, starting new actions when slots
496 Target *cmdline=get_target("cmdline");
498 unsigned total=cmdline->count_rebuild();
501 cout<<"Already up to date\n";
504 cout<<"Will build "<<total<<" target(s)\n";
506 vector<Action *> actions;
509 cout<<"0 targets built\n";
517 if(actions.size()<jobs && !fail)
519 Target *tgt=cmdline->get_buildable_target();
522 Action *action=tgt->build();
524 actions.push_back(action);
526 else if(actions.empty())
530 Time::sleep(10*Time::msec);
532 for(unsigned i=0; i<actions.size();)
534 int status=actions[i]->check();
540 cout<<"\e["<<actions.size()+1<<'A';
541 cout<<count<<" targets built\n";
545 if(i<actions.size()-1)
546 cout<<"\e["<<actions.size()-i-1<<"B";
550 actions.erase(actions.begin()+i);
553 if(actions.empty() && fail)
564 Application::RegApp<Builder> Builder::reg;
566 Builder::Loader::Loader(Builder &b, const Path::Path &s):
570 add("package", &Loader::package);
573 void Builder::Loader::package(const string &n)
575 Package *pkg=new Package(bld, n, src);
577 bld.packages.insert(PackageMap::value_type(n, pkg));
578 bld.new_pkgs.push_back(pkg);