3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/strings/lexicalcast.h>
9 #include <msp/strings/utils.h>
20 Creates a buildable package.
22 Package::Package(Builder &b, const string &n, const Path::Path &s):
32 if(builder.get_verbose()>=4)
33 cout<<"Created buildable package "<<n<<" at "<<s<<'\n';
37 Sets the path where the package files were installed. This is only useful for
38 non-buildable packages that don't use pkg-config.
40 void Package::set_path(const Msp::Path::Path &p)
42 path=builder.get_cwd()/p;
45 Msp::Path::Path Package::get_temp_dir() const
47 return source/config.get_option("tempdir").value/config.get_option("profile").value;
50 Msp::Path::Path Package::get_out_dir() const
52 return source/config.get_option("outdir").value;
56 Checks which kinds of things the components of this package install.
58 @return A bitmask of installed things
60 unsigned Package::get_install_flags()
63 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
67 if(i->get_type()==Component::PROGRAM)
69 else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
72 if(!i->get_install_headers().empty())
79 LibMode Package::get_library_mode() const
81 const string &mode=config.get_option("staticlibs").value;
84 else if(mode=="local")
89 throw Exception("Unknown library mode");
93 Tries to resolve all references to dependency packages.
95 void Package::resolve_refs()
97 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
99 Package *pkg=i->resolve();
100 if(pkg) all_reqs.push_back(pkg);
103 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
106 const PkgRefList &creqs=i->get_requires();
107 for(PkgRefList::const_iterator j=creqs.begin(); j!=creqs.end(); ++j)
109 all_reqs.push_back(j->get_package());
112 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
115 const PkgRefList &creqs=i->get_requires();
116 for(PkgRefList::const_iterator j=creqs.begin(); j!=creqs.end(); ++j)
118 all_reqs.push_back(j->get_package());
123 Processes configuration options that were most likely obtained from the command
126 void Package::configure(const StringMap &opts, unsigned flag)
131 if(builder.get_verbose()>=3)
132 cout<<"Configuring "<<name<<'\n';
138 StringMap::const_iterator prof=opts.find("profile");
139 if(prof!=opts.end() && flag)
140 config.select_profile(prof->second);
142 config.select_last_profile();
144 if(flag && config.update(opts))
146 if(builder.get_verbose()>=2)
147 cout<<"Configuration of "<<name<<" changed\n";
148 if(!builder.get_dry_run())
154 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
157 const PkgRefList &reqs=i->get_requires();
158 requires.insert(requires.end(), reqs.begin(), reqs.end());
159 build_info.add(i->get_build_info());
162 for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
164 if((*i)->get_need_path())
165 (*i)->set_path(config.get_option((*i)->get_name()+"_path").value);
166 (*i)->configure(opts, flag&2);
176 Creates a non-buildable package with the given name. Pkg-config is tried first
177 to get build information. If it fails, a built-in list of known packages is
180 Package *Package::create(Builder &b, const string &name)
183 argv.push_back("pkg-config");
184 argv.push_back("--silence-errors");
185 argv.push_back("--cflags");
186 argv.push_back("--libs");
187 argv.push_back(name);
188 vector<string> info=split(run_command(argv));
190 bool need_path=false;
191 bool use_pkgconfig=true;
196 //XXX Put these in an external file
198 info.push_back("-lGL");
199 else if(name=="pthread")
200 info.push_back("-lpthread");
201 else if(name=="gmpxx")
202 info.push_back("-lgmpxx");
203 else if(name=="fmod4")
205 else if(name=="devil")
206 info.push_back("-lIL");
207 else if(name=="Xlib")
208 info.push_back("-lX11");
213 Package *pkg=new Package(b, name, info);
214 pkg->need_path=need_path;
215 pkg->use_pkgconfig=use_pkgconfig;
221 Package::Package(Builder &b, const string &n, const vector<string> &info):
228 for(vector<string>::const_iterator i=info.begin(); i!=info.end(); ++i)
230 if(!i->compare(0, 2, "-I"))
231 export_binfo.incpath.push_back(i->substr(2));
232 else if(!i->compare(0, 2, "-D"))
233 export_binfo.defines.push_back(i->substr(2));
234 else if(!i->compare(0, 2, "-L"))
235 export_binfo.libpath.push_back(i->substr(2));
236 else if(!i->compare(0, 2, "-l"))
237 export_binfo.libs.push_back(i->substr(2));
240 if(builder.get_verbose()>=4)
242 cout<<"Created non-buildable package "<<name<<" with";
243 for(vector<string>::const_iterator i=info.begin(); i!=info.end(); ++i)
250 Initializes configuration options and loads cached values.
252 void Package::init_config()
254 config.add_option("profile", "default", "Configuration profile");
255 config.add_option("tempdir", "temp", "Directory for storing temporary files");
256 config.add_option("outdir", ".", "Directory to put build results in");
257 config.add_option("optimize", "0", "Apply compiler optimizations");
258 config.add_option("debug", "0", "Produce debugging symbols");
259 config.add_option("cpu", "auto", "CPU type to optimize for");
260 config.add_option("arch", "native", "Architecture for cross-compiling");
261 config.add_option("staticlibs", "local", "Use static libraries");
263 unsigned flags=get_install_flags();
265 config.add_option("prefix", "$HOME/local", "Installation prefix");
267 config.add_option("includedir", "$prefix/include", "Header installation directory");
269 config.add_option("includedir", "$prefix/bin", "Binary installation directory");
271 config.add_option("includedir", "$prefix/lib", "Library installation directory");
273 config.add_option("includedir", "$prefix/share", "Data installation directory");*/
275 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
276 config.add_option("with_"+i->name, "0", i->descr);
278 for(PackageList::const_iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
279 if((*i)->get_need_path())
280 config.add_option((*i)->get_name()+"_path", "", "Path for "+(*i)->get_name());
284 Fills in build info based on configuration. All required packages must be
285 configured when this is called.
287 void Package::create_build_info()
291 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
293 Package *pkg=i->get_package();
296 const BuildInfo &ebi=pkg->get_exported_binfo();
299 export_binfo.cflags.insert(export_binfo.cflags.end(), ebi.cflags.begin(), ebi.cflags.end());
300 export_binfo.incpath.insert(export_binfo.incpath.end(), ebi.incpath.begin(), ebi.incpath.end());
301 export_binfo.defines.insert(export_binfo.defines.end(), ebi.defines.begin(), ebi.defines.end());
304 build_info.cflags.push_back("-Wall");
305 build_info.cflags.push_back("-Wshadow");
306 build_info.cflags.push_back("-Wextra");
307 build_info.cflags.push_back("-Wpointer-arith");
308 build_info.cflags.push_back("-Wconversion");
309 build_info.cflags.push_back("-Werror");
311 unsigned flags=get_install_flags();
314 export_binfo.incpath.push_back((Path::Path(config.get_option("prefix").value)/"include").str());
316 export_binfo.libpath.push_back((Path::Path(config.get_option("prefix").value)/"lib").str());
318 string optimize=config.get_option("optimize").value;
319 if(lexical_cast<unsigned>(optimize))
321 build_info.cflags.push_back("-O"+optimize);
322 build_info.ldflags.push_back("-O"+optimize);
323 string cpu=config.get_option("cpu").value;
325 build_info.cflags.push_back("-march="+cpu);
328 if(lexical_cast<bool>(config.get_option("debug").value))
330 build_info.cflags.push_back("-ggdb");
331 build_info.defines.push_back("DEBUG");
334 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
335 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
336 build_info.cflags.push_back("-DWITH_"+toupper(i->name));
340 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
342 i->create_build_info();
343 if(i->get_type()==Component::LIBRARY)
344 export_binfo.libs.push_back(i->get_name());
347 else if(name=="fmod4")
349 export_binfo.libs.push_back("fmodex");
352 export_binfo.libpath.push_back((path/"api"/"lib").str());
353 export_binfo.incpath.push_back((path/"api"/"inc").str());
356 export_binfo.unique();
359 Package::Loader::Loader(Package &p):
362 add("version", &Package::version);
363 add("description", &Package::description);
364 add("require", &Loader::require);
365 add("feature", &Loader::feature);
366 add("if", &Loader::condition);
367 add("program", &Loader::program);
368 add("library", &Loader::library);
369 add("module", &Loader::module);
370 add("headers", &Loader::headers);
371 add("build_info", &Loader::build_info);
374 void Package::Loader::require(const string &n)
376 pkg.requires.push_back(PackageRef(pkg.builder, n));
379 void Package::Loader::feature(const string &n, const string &d)
381 pkg.features.push_back(Feature(n, d));
384 void Package::Loader::condition(const string &c)
386 Condition cond(pkg, c);
388 pkg.conditions.push_back(cond);
391 void Package::Loader::program(const string &n)
393 Component prog(pkg, Component::PROGRAM, n);
395 pkg.components.push_back(prog);
398 void Package::Loader::library(const string &n)
400 Component prog(pkg, Component::LIBRARY, n);
402 pkg.components.push_back(prog);
405 void Package::Loader::module(const string &n)
407 Component prog(pkg, Component::MODULE, n);
409 pkg.components.push_back(prog);
412 void Package::Loader::headers(const string &n)
414 Component prog(pkg, Component::HEADERS, n);
416 pkg.components.push_back(prog);
419 void Package::Loader::build_info()
421 load_sub(pkg.build_info);