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):
33 tar_files.push_back(source/"Build");
35 if(builder.get_verbose()>=4)
36 cout<<"Created buildable package "<<n<<" at "<<s<<'\n';
40 Sets the path where the package files were installed. This is only useful for
41 non-buildable packages that don't use pkg-config.
43 void Package::set_path(const Msp::Path::Path &p)
45 path=builder.get_cwd()/p;
48 Msp::Path::Path Package::get_temp_dir() const
50 return source/config.get_option("tempdir").value/config.get_option("profile").value;
53 Msp::Path::Path Package::get_out_dir() const
55 return source/config.get_option("outdir").value;
59 Checks which kinds of things the components of this package install.
61 @return A bitmask of installed things
63 unsigned Package::get_install_flags()
66 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
70 if(i->get_type()==Component::PROGRAM)
72 else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
75 if(!i->get_install_headers().empty())
82 LibMode Package::get_library_mode() const
84 const string &mode=config.get_option("staticlibs").value;
87 else if(mode=="local")
92 throw Exception("Unknown library mode");
96 Tries to resolve all references to dependency packages.
98 void Package::resolve_refs()
100 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
102 Package *pkg=i->resolve();
103 if(pkg) all_reqs.push_back(pkg);
106 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
109 const PkgRefList &creqs=i->get_requires();
110 for(PkgRefList::const_iterator j=creqs.begin(); j!=creqs.end(); ++j)
112 all_reqs.push_back(j->get_package());
115 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
118 const PkgRefList &creqs=i->get_requires();
119 for(PkgRefList::const_iterator j=creqs.begin(); j!=creqs.end(); ++j)
121 all_reqs.push_back(j->get_package());
126 Processes configuration options that were most likely obtained from the command
129 void Package::configure(const StringMap &opts, unsigned flag)
134 if(builder.get_verbose()>=3)
135 cout<<"Configuring "<<name<<'\n';
141 StringMap::const_iterator prof=opts.find("profile");
142 if(prof!=opts.end() && flag)
143 config.select_profile(prof->second);
145 config.select_last_profile();
147 if(flag && config.update(opts))
149 if(builder.get_verbose()>=2)
150 cout<<"Configuration of "<<name<<" changed\n";
151 if(!builder.get_dry_run())
157 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
160 const PkgRefList &reqs=i->get_requires();
161 requires.insert(requires.end(), reqs.begin(), reqs.end());
162 build_info.add(i->get_build_info());
165 for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
167 if((*i)->get_need_path())
168 (*i)->set_path(config.get_option((*i)->get_name()+"_path").value);
169 (*i)->configure(opts, flag&2);
181 Creates a non-buildable package with the given name. Pkg-config is tried first
182 to get build information. If it fails, a built-in list of known packages is
185 Package *Package::create(Builder &b, const string &name)
188 argv.push_back("pkg-config");
189 argv.push_back("--silence-errors");
190 argv.push_back("--cflags");
191 argv.push_back("--libs");
192 argv.push_back(name);
193 vector<string> info=split(run_command(argv));
195 bool need_path=false;
196 bool use_pkgconfig=true;
201 //XXX Put these in an external file
203 info.push_back("-lGL");
204 else if(name=="pthread")
205 info.push_back("-lpthread");
206 else if(name=="gmpxx")
207 info.push_back("-lgmpxx");
208 else if(name=="fmod4")
210 else if(name=="devil")
211 info.push_back("-lIL");
212 else if(name=="Xlib")
213 info.push_back("-lX11");
218 Package *pkg=new Package(b, name, info);
219 pkg->need_path=need_path;
220 pkg->use_pkgconfig=use_pkgconfig;
226 Package::Package(Builder &b, const string &n, const vector<string> &info):
234 for(vector<string>::const_iterator i=info.begin(); i!=info.end(); ++i)
236 if(!i->compare(0, 2, "-I"))
237 export_binfo.incpath.push_back(i->substr(2));
238 else if(!i->compare(0, 2, "-D"))
239 export_binfo.defines.push_back(i->substr(2));
240 else if(!i->compare(0, 2, "-L"))
241 export_binfo.libpath.push_back(i->substr(2));
242 else if(!i->compare(0, 2, "-l"))
243 export_binfo.libs.push_back(i->substr(2));
246 if(builder.get_verbose()>=4)
248 cout<<"Created non-buildable package "<<name<<" with";
249 for(vector<string>::const_iterator i=info.begin(); i!=info.end(); ++i)
256 Initializes configuration options and loads cached values.
258 void Package::init_config()
260 config.add_option("profile", "default", "Configuration profile");
261 config.add_option("tempdir", "temp", "Directory for storing temporary files");
262 config.add_option("outdir", ".", "Directory to put build results in");
263 config.add_option("optimize", "0", "Apply compiler optimizations");
264 config.add_option("debug", "0", "Produce debugging symbols");
265 config.add_option("cpu", "auto", "CPU type to optimize for");
266 config.add_option("arch", "native", "Architecture for cross-compiling");
267 config.add_option("staticlibs", "local", "Use static libraries");
269 unsigned flags=get_install_flags();
271 config.add_option("prefix", "$HOME/local", "Installation prefix");
273 config.add_option("includedir", "$prefix/include", "Header installation directory");
275 config.add_option("includedir", "$prefix/bin", "Binary installation directory");
277 config.add_option("includedir", "$prefix/lib", "Library installation directory");
279 config.add_option("includedir", "$prefix/share", "Data installation directory");*/
281 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
282 config.add_option("with_"+i->name, "0", i->descr);
284 for(PackageList::const_iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
285 if((*i)->get_need_path())
286 config.add_option((*i)->get_name()+"_path", "", "Path for "+(*i)->get_name());
290 Fills in build info based on configuration. All required packages must be
291 configured when this is called.
293 void Package::create_build_info()
297 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
299 Package *pkg=i->get_package();
302 const BuildInfo &ebi=pkg->get_exported_binfo();
305 export_binfo.cflags.insert(export_binfo.cflags.end(), ebi.cflags.begin(), ebi.cflags.end());
306 export_binfo.incpath.insert(export_binfo.incpath.end(), ebi.incpath.begin(), ebi.incpath.end());
307 export_binfo.defines.insert(export_binfo.defines.end(), ebi.defines.begin(), ebi.defines.end());
310 build_info.cflags.push_back("-Wall");
311 build_info.cflags.push_back("-Wshadow");
312 build_info.cflags.push_back("-Wextra");
313 build_info.cflags.push_back("-Wpointer-arith");
314 build_info.cflags.push_back("-Wconversion");
315 build_info.cflags.push_back("-Werror");
317 unsigned flags=get_install_flags();
320 export_binfo.incpath.push_back((Path::Path(config.get_option("prefix").value)/"include").str());
322 export_binfo.libpath.push_back((Path::Path(config.get_option("prefix").value)/"lib").str());
324 string optimize=config.get_option("optimize").value;
325 if(lexical_cast<unsigned>(optimize))
327 build_info.cflags.push_back("-O"+optimize);
328 build_info.ldflags.push_back("-O"+optimize);
329 string cpu=config.get_option("cpu").value;
331 build_info.cflags.push_back("-march="+cpu);
334 if(lexical_cast<bool>(config.get_option("debug").value))
336 build_info.cflags.push_back("-ggdb");
337 build_info.defines.push_back("DEBUG");
340 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
341 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
342 build_info.cflags.push_back("-DWITH_"+toupper(i->name));
346 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
348 i->create_build_info();
349 if(i->get_type()==Component::LIBRARY)
350 export_binfo.libs.push_back(i->get_name());
353 else if(name=="fmod4")
355 export_binfo.libs.push_back("fmodex");
358 export_binfo.libpath.push_back((path/"api"/"lib").str());
359 export_binfo.incpath.push_back((path/"api"/"inc").str());
362 export_binfo.unique();
366 Package::Loader::Loader(Package &p):
369 add("version", &Package::version);
370 add("description", &Package::description);
371 add("require", &Loader::require);
372 add("feature", &Loader::feature);
373 add("if", &Loader::condition);
374 add("program", &Loader::program);
375 add("library", &Loader::library);
376 add("module", &Loader::module);
377 add("headers", &Loader::headers);
378 add("build_info", &Loader::build_info);
379 add("tar_file", &Loader::tar_file);
382 void Package::Loader::require(const string &n)
384 pkg.requires.push_back(PackageRef(pkg.builder, n));
387 void Package::Loader::feature(const string &n, const string &d)
389 pkg.features.push_back(Feature(n, d));
392 void Package::Loader::condition(const string &c)
394 Condition cond(pkg, c);
396 pkg.conditions.push_back(cond);
399 void Package::Loader::program(const string &n)
401 Component prog(pkg, Component::PROGRAM, n);
403 pkg.components.push_back(prog);
406 void Package::Loader::library(const string &n)
408 Component prog(pkg, Component::LIBRARY, n);
410 pkg.components.push_back(prog);
413 void Package::Loader::module(const string &n)
415 Component prog(pkg, Component::MODULE, n);
417 pkg.components.push_back(prog);
420 void Package::Loader::headers(const string &n)
422 Component prog(pkg, Component::HEADERS, n);
424 pkg.components.push_back(prog);
427 void Package::Loader::build_info()
429 load_sub(pkg.build_info);
432 void Package::Loader::tar_file(const string &f)
434 pkg.tar_files.push_back(pkg.source/f);