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):
36 getopt.add_option(GetOpt::Option('v', "verbose", GetOpt::NONE));
37 getopt.add_option(GetOpt::Option('a', "analyze", GetOpt::REQUIRED));
38 getopt.add_option(GetOpt::Option('b', "build", GetOpt::NONE));
39 getopt.add_option(GetOpt::Option("max-depth", GetOpt::REQUIRED));
40 getopt.add_option(GetOpt::Option('n', "dry-run", GetOpt::NONE));
41 getopt.add_option(GetOpt::Option('W', "what-if", GetOpt::REQUIRED));
42 getopt.add_option(GetOpt::Option('B', "build-all", GetOpt::NONE));
43 getopt.add_option(GetOpt::Option('C', "chdir", GetOpt::REQUIRED));
44 getopt.add_option(GetOpt::Option('j', "jobs", GetOpt::REQUIRED, "1"));
45 getopt.add_option(GetOpt::Option('h', "help", GetOpt::NONE));
46 getopt.add_option(GetOpt::Option('c', "clean", GetOpt::NONE));
47 getopt.add_option(GetOpt::Option('f', "file", GetOpt::REQUIRED, "Build"));
48 getopt.add_option(GetOpt::Option("chrome", GetOpt::NONE));
49 getopt.add_option(GetOpt::Option("full-paths", GetOpt::NONE));
50 getopt.add_option(GetOpt::Option('A', "conf-all", GetOpt::NONE));
51 int index=getopt(argc, argv);
53 verbose+=getopt['v'].count();
57 analyzer=new Analyzer(*this);
59 string mode=getopt['a'].arg();
61 analyzer->set_mode(Analyzer::DEPS);
62 else if(mode=="alldeps")
63 analyzer->set_mode(Analyzer::ALLDEPS);
64 else if(mode=="rebuild")
65 analyzer->set_mode(Analyzer::REBUILD);
66 else if(mode=="rdeps")
67 analyzer->set_mode(Analyzer::RDEPS);
69 throw UsageError("Invalid analysis mode");
71 if(getopt["max-depth"])
72 analyzer->set_max_depth(strtol(getopt["max-depth"].arg()));
73 analyzer->set_full_paths(getopt["full-paths"]);
81 jobs=max(strtol(getopt['j'].arg()), 1L);
83 chrome=getopt["chrome"];
88 chdir(getopt['C'].arg().c_str());
90 build_file=getopt['f'].arg();
92 for(int i=index; i<argc; ++i)
95 unsigned equal=v.find('=');
96 if(equal!=string::npos)
97 cmdline_options.insert(RawOptionMap::value_type(v.substr(0, equal), v.substr(equal+1)));
99 cmdline_targets.push_back(argv[i]);
102 if(cmdline_targets.empty())
103 cmdline_targets.push_back("default");
106 what_if.push_back(getopt['W'].arg());
110 Gets a package with the specified name, possibly creating it.
112 @param n Package name
114 @return Pointer to the package, or 0 if the package could not be located
116 Package *Builder::get_package(const string &n)
118 PackageMap::iterator i=packages.find(n);
119 if(i!=packages.end())
122 // Try to get source directory with pkgconfig
124 argv.push_back("pkg-config");
125 argv.push_back("--variable=source");
127 string srcdir=strip(run_command(argv));
131 dirs.push_back(srcdir);
133 // Make some other guesses about the source directory
135 if(!dirname.compare(0, 3, "msp"))
137 dirs.push_back(cwd/dirname);
138 dirs.push_back(cwd/".."/dirname);
140 // Go through the candidate directories and look for a Build file
141 for(PathList::iterator j=dirs.begin(); j!=dirs.end(); ++j)
142 if(!load_build_file(*j/"Build"))
145 if(i!=packages.end())
150 // Package source not found - create a binary package
151 Package *pkg=Package::create(*this, n);
152 packages.insert(PackageMap::value_type(n, pkg));
154 new_pkgs.push_back(pkg);
160 Returns the target with the given name, or 0 if no such target exists.
162 Target *Builder::get_target(const string &n)
164 TargetMap::iterator i=targets.find(n);
171 Tries to locate a header included from a given location and with a given include
172 path. Considers known targets as well as existing files.
174 Target *Builder::get_header(const string &include, const string &from, const list<string> &path)
177 update_hash(hash, from);
178 for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
179 update_hash(hash, *i);
181 string id=hash+include;
182 TargetMap::iterator i=includes.find(id);
183 if(i!=includes.end())
186 string fn=include.substr(1);
188 if(include[0]=='"' && (tgt=check_header(Path::Path(from)/fn)))
190 if((tgt=check_header(Path::Path("/usr/include")/fn)))
192 //XXX Determine the C++ header location dynamically
193 if((tgt=check_header(Path::Path("/usr/include/c++/4.1.2")/fn)))
195 for(list<string>::const_iterator j=path.begin(); j!=path.end(); ++j)
196 if((tgt=check_header(Path::Path(*j)/fn)))
202 Target *Builder::get_library(const string &lib, const list<string> &path)
205 for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
206 update_hash(hash, *i);
209 TargetMap::iterator i=libraries.find(id);
210 if(i!=libraries.end())
213 string basename="lib"+lib+".so";
214 for(list<string>::const_iterator j=path.begin(); j!=path.end(); ++j)
216 string full=(Path::Path(*j)/basename).str();
217 Target *tgt=get_target(full);
220 if(Path::exists(full))
222 add_target(tgt=new SystemLibrary(*this, full));
232 if(load_build_file(cwd/build_file))
234 cerr<<"No build info here.\n";
238 default_pkg=packages.begin()->second;
240 while(!new_pkgs.empty())
242 Package *pkg=new_pkgs.front();
243 if(pkg==default_pkg || conf_all)
244 pkg->process_options(cmdline_options);
245 new_pkgs.erase(new_pkgs.begin());
249 std::list<std::string> missing;
250 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
252 missing.push_back(i->first);
257 cerr<<"The following packages were not found on the system:\n";
258 for(list<string>::iterator i=missing.begin(); i!=missing.end(); ++i)
260 cerr<<"Please install them and try again.\n";
264 default_pkg->create_build_info();
269 cout<<packages.size()<<" packages, "<<targets.size()<<" targets\n";
272 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
274 cout<<' '<<i->second->get_name();
275 if(i->second->get_buildable())
278 for(TargetMap::iterator j=targets.begin(); j!=targets.end(); ++j)
279 if(j->second->get_package()==i->second)
281 cout<<" ("<<count<<" targets)\n";
296 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
298 for(TargetMap::iterator i=targets.begin(); i!=targets.end(); ++i)
303 int Builder::load_build_file(const Path::Path &fn)
305 ifstream in(fn.str().c_str());
309 Parser::Parser parser(in, fn.str());
310 Loader loader(*this, fn.subpath(0, fn.size()-1));
316 int Builder::create_targets()
318 Target *world=new VirtualTarget(*this, "world");
321 Target *def_tgt=new VirtualTarget(*this, "default");
323 world->add_depend(def_tgt);
325 Target *install=new VirtualTarget(*this, "install");
327 world->add_depend(install);
329 for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
333 if(!i->second->get_buildable())
336 Path::Path inst_base;
337 if(i->second->get_config().is_option("prefix"))
338 inst_base=i->second->get_config().get_option("prefix").value;
340 const ComponentList &components=i->second->get_components();
341 for(ComponentList::const_iterator j=components.begin(); j!=components.end(); ++j)
344 const PathList &sources=j->get_sources();
345 for(PathList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
349 if(S_ISDIR(st.st_mode))
351 list<string> sfiles=list_files(*k);
352 for(list<string>::iterator l=sfiles.begin(); l!=sfiles.end(); ++l)
353 files.push_back(*k / *l);
359 bool build_exe=j->get_type()!=Component::HEADERS;
361 list<ObjectFile *> objs;
362 for(PathList::iterator k=files.begin(); k!=files.end(); ++k)
364 string basename=(*k)[-1];
365 string ext=Path::splitext(basename).ext;
366 if(ext==".cpp" || ext==".c")
370 SourceFile *src=new SourceFile(*this, &*j, k->str());
373 ObjectFile *obj=new ObjectFile(*this, *j, *src);
380 Target *hdr=get_target(k->str());
383 hdr=new Header(*this, &*j, k->str());
386 if(!j->get_install_headers().empty())
388 Path::Path inst_path=inst_base/"include"/j->get_install_headers()/basename;
389 Install *inst=new Install(*this, *i->second, *hdr, inst_path.str());
391 install->add_depend(inst);
398 Executable *exe=new Executable(*this, *j, objs);
400 if(i->second==default_pkg)
401 def_tgt->add_depend(exe);
403 world->add_depend(exe);
408 if(j->get_type()==Component::PROGRAM)
410 else if(j->get_type()==Component::LIBRARY)
412 if(!inst_dir.empty())
414 Install *inst=new Install(*this, *i->second, *exe, (inst_base/inst_dir/Path::basename(exe->get_name())).str());
416 install->add_depend(inst);
423 while(!new_tgts.empty())
425 Target *tgt=new_tgts.front();
426 new_tgts.erase(new_tgts.begin());
428 if(!tgt->get_depends_ready())
429 new_tgts.push_back(tgt);
432 for(list<string>::iterator i=what_if.begin(); i!=what_if.end(); ++i)
434 Target *tgt=get_target((cwd/ *i).str());
437 cerr<<"Unknown what-if target "<<*i<<'\n';
443 Target *cmdline=new VirtualTarget(*this, "cmdline");
445 world->add_depend(cmdline);
446 for(list<string>::iterator i=cmdline_targets.begin(); i!=cmdline_targets.end(); ++i)
448 Target *tgt=get_target(*i);
450 tgt=get_target((cwd/ *i).str());
453 cerr<<"I don't know anything about "<<*i<<'\n';
456 cmdline->add_depend(tgt);
464 Target *Builder::check_header(const Msp::Path::Path &fn)
466 Target *tgt=get_target(fn.str());
471 add_target(tgt=new SystemHeader(*this, fn.str()));
477 void Builder::add_target(Target *t)
479 targets.insert(TargetMap::value_type(t->get_name(), t));
480 new_tgts.push_back(t);
483 void Builder::update_hash(string &hash, const string &value)
485 for(unsigned i=0; i<value.size(); ++i)
486 hash[i%hash.size()]^=value[i];
490 This function supervises the build process, starting new actions when slots
495 Target *cmdline=get_target("cmdline");
497 unsigned total=cmdline->count_rebuild();
500 cout<<"Already up to date\n";
503 cout<<"Will build "<<total<<" target(s)\n";
505 vector<Action *> actions;
508 cout<<"0 targets built\n";
516 if(actions.size()<jobs && !fail)
518 Target *tgt=cmdline->get_buildable_target();
521 Action *action=tgt->build();
523 actions.push_back(action);
525 else if(actions.empty())
529 Time::sleep(10*Time::msec);
531 for(unsigned i=0; i<actions.size();)
533 int status=actions[i]->check();
539 cout<<"\e["<<actions.size()+1<<'A';
540 cout<<count<<" targets built\n";
544 if(i<actions.size()-1)
545 cout<<"\e["<<actions.size()-i-1<<"B";
549 actions.erase(actions.begin()+i);
552 if(actions.empty() && fail)
563 Application::RegApp<Builder> Builder::reg;
565 Builder::Loader::Loader(Builder &b, const Path::Path &s):
569 add("package", &Loader::package);
572 void Builder::Loader::package(const string &n)
574 Package *pkg=new Package(bld, n, src);
576 bld.packages.insert(PackageMap::value_type(n, pkg));
577 bld.new_pkgs.push_back(pkg);