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 tar_files.push_back(source/"Build");
34 if(builder.get_verbose()>=4)
35 cout<<"Created buildable package "<<n<<" at "<<s<<'\n';
39 Sets the path where the package files were installed. This is only useful for
40 non-buildable packages that don't use pkg-config.
42 void Package::set_path(const Msp::Path::Path &p)
44 path=builder.get_cwd()/p;
47 Msp::Path::Path Package::get_temp_dir() const
49 return source/config.get_option("tempdir").value/config.get_option("profile").value;
52 Msp::Path::Path Package::get_out_dir() const
54 return source/config.get_option("outdir").value;
58 Checks which kinds of things the components of this package install.
60 @return A bitmask of installed things
62 unsigned Package::get_install_flags()
65 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
69 if(i->get_type()==Component::PROGRAM)
71 else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
74 if(!i->get_install_headers().empty())
81 LibMode Package::get_library_mode() const
83 const string &mode=config.get_option("staticlibs").value;
86 else if(mode=="local")
91 throw Exception("Unknown library mode");
95 Tries to resolve all references to dependency packages.
97 void Package::resolve_refs()
99 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
101 Package *pkg=i->resolve();
102 if(pkg) all_reqs.push_back(pkg);
105 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
108 const PkgRefList &creqs=i->get_requires();
109 for(PkgRefList::const_iterator j=creqs.begin(); j!=creqs.end(); ++j)
111 all_reqs.push_back(j->get_package());
114 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
117 const PkgRefList &creqs=i->get_requires();
118 for(PkgRefList::const_iterator j=creqs.begin(); j!=creqs.end(); ++j)
120 all_reqs.push_back(j->get_package());
125 Processes configuration options that were most likely obtained from the command
128 void Package::configure(const StringMap &opts, unsigned flag)
133 if(builder.get_verbose()>=3)
134 cout<<"Configuring "<<name<<'\n';
140 StringMap::const_iterator prof=opts.find("profile");
141 if(prof!=opts.end() && flag)
142 config.select_profile(prof->second);
144 config.select_last_profile();
146 if(flag && config.update(opts))
148 if(builder.get_verbose()>=2)
149 cout<<"Configuration of "<<name<<" changed\n";
150 if(!builder.get_dry_run())
156 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
159 const PkgRefList &reqs=i->get_requires();
160 requires.insert(requires.end(), reqs.begin(), reqs.end());
161 build_info.add(i->get_build_info());
164 for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
166 if((*i)->get_need_path())
167 (*i)->set_path(config.get_option((*i)->get_name()+"_path").value);
168 (*i)->configure(opts, flag&2);
178 Creates a non-buildable package with the given name. Pkg-config is tried first
179 to get build information. If it fails, a built-in list of known packages is
182 Package *Package::create(Builder &b, const string &name)
185 argv.push_back("pkg-config");
186 argv.push_back("--silence-errors");
187 argv.push_back("--cflags");
188 argv.push_back("--libs");
189 argv.push_back(name);
190 vector<string> info=split(run_command(argv));
192 bool need_path=false;
193 bool use_pkgconfig=true;
198 //XXX Put these in an external file
200 info.push_back("-lGL");
201 else if(name=="pthread")
202 info.push_back("-lpthread");
203 else if(name=="gmpxx")
204 info.push_back("-lgmpxx");
205 else if(name=="fmod4")
207 else if(name=="devil")
208 info.push_back("-lIL");
209 else if(name=="Xlib")
210 info.push_back("-lX11");
215 Package *pkg=new Package(b, name, info);
216 pkg->need_path=need_path;
217 pkg->use_pkgconfig=use_pkgconfig;
223 Package::Package(Builder &b, const string &n, const vector<string> &info):
230 for(vector<string>::const_iterator i=info.begin(); i!=info.end(); ++i)
232 if(!i->compare(0, 2, "-I"))
233 export_binfo.incpath.push_back(i->substr(2));
234 else if(!i->compare(0, 2, "-D"))
235 export_binfo.defines.push_back(i->substr(2));
236 else if(!i->compare(0, 2, "-L"))
237 export_binfo.libpath.push_back(i->substr(2));
238 else if(!i->compare(0, 2, "-l"))
239 export_binfo.libs.push_back(i->substr(2));
242 if(builder.get_verbose()>=4)
244 cout<<"Created non-buildable package "<<name<<" with";
245 for(vector<string>::const_iterator i=info.begin(); i!=info.end(); ++i)
252 Initializes configuration options and loads cached values.
254 void Package::init_config()
256 config.add_option("profile", "default", "Configuration profile");
257 config.add_option("tempdir", "temp", "Directory for storing temporary files");
258 config.add_option("outdir", ".", "Directory to put build results in");
259 config.add_option("optimize", "0", "Apply compiler optimizations");
260 config.add_option("debug", "0", "Produce debugging symbols");
261 config.add_option("cpu", "auto", "CPU type to optimize for");
262 config.add_option("arch", "native", "Architecture for cross-compiling");
263 config.add_option("staticlibs", "local", "Use static libraries");
265 unsigned flags=get_install_flags();
267 config.add_option("prefix", "$HOME/local", "Installation prefix");
269 config.add_option("includedir", "$prefix/include", "Header installation directory");
271 config.add_option("includedir", "$prefix/bin", "Binary installation directory");
273 config.add_option("includedir", "$prefix/lib", "Library installation directory");
275 config.add_option("includedir", "$prefix/share", "Data installation directory");*/
277 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
278 config.add_option("with_"+i->name, "0", i->descr);
280 for(PackageList::const_iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
281 if((*i)->get_need_path())
282 config.add_option((*i)->get_name()+"_path", "", "Path for "+(*i)->get_name());
286 Fills in build info based on configuration. All required packages must be
287 configured when this is called.
289 void Package::create_build_info()
293 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
295 Package *pkg=i->get_package();
298 const BuildInfo &ebi=pkg->get_exported_binfo();
301 export_binfo.cflags.insert(export_binfo.cflags.end(), ebi.cflags.begin(), ebi.cflags.end());
302 export_binfo.incpath.insert(export_binfo.incpath.end(), ebi.incpath.begin(), ebi.incpath.end());
303 export_binfo.defines.insert(export_binfo.defines.end(), ebi.defines.begin(), ebi.defines.end());
306 build_info.cflags.push_back("-Wall");
307 build_info.cflags.push_back("-Wshadow");
308 build_info.cflags.push_back("-Wextra");
309 build_info.cflags.push_back("-Wpointer-arith");
310 build_info.cflags.push_back("-Wconversion");
311 build_info.cflags.push_back("-Werror");
313 unsigned flags=get_install_flags();
316 export_binfo.incpath.push_back((Path::Path(config.get_option("prefix").value)/"include").str());
318 export_binfo.libpath.push_back((Path::Path(config.get_option("prefix").value)/"lib").str());
320 string optimize=config.get_option("optimize").value;
321 if(lexical_cast<unsigned>(optimize))
323 build_info.cflags.push_back("-O"+optimize);
324 build_info.ldflags.push_back("-O"+optimize);
325 string cpu=config.get_option("cpu").value;
327 build_info.cflags.push_back("-march="+cpu);
330 if(lexical_cast<bool>(config.get_option("debug").value))
332 build_info.cflags.push_back("-ggdb");
333 build_info.defines.push_back("DEBUG");
336 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
337 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
338 build_info.cflags.push_back("-DWITH_"+toupper(i->name));
342 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
344 i->create_build_info();
345 if(i->get_type()==Component::LIBRARY)
346 export_binfo.libs.push_back(i->get_name());
349 else if(name=="fmod4")
351 export_binfo.libs.push_back("fmodex");
354 export_binfo.libpath.push_back((path/"api"/"lib").str());
355 export_binfo.incpath.push_back((path/"api"/"inc").str());
358 export_binfo.unique();
361 Package::Loader::Loader(Package &p):
364 add("version", &Package::version);
365 add("description", &Package::description);
366 add("require", &Loader::require);
367 add("feature", &Loader::feature);
368 add("if", &Loader::condition);
369 add("program", &Loader::program);
370 add("library", &Loader::library);
371 add("module", &Loader::module);
372 add("headers", &Loader::headers);
373 add("build_info", &Loader::build_info);
374 add("tar_file", &Loader::tar_file);
377 void Package::Loader::require(const string &n)
379 pkg.requires.push_back(PackageRef(pkg.builder, n));
382 void Package::Loader::feature(const string &n, const string &d)
384 pkg.features.push_back(Feature(n, d));
387 void Package::Loader::condition(const string &c)
389 Condition cond(pkg, c);
391 pkg.conditions.push_back(cond);
394 void Package::Loader::program(const string &n)
396 Component prog(pkg, Component::PROGRAM, n);
398 pkg.components.push_back(prog);
401 void Package::Loader::library(const string &n)
403 Component prog(pkg, Component::LIBRARY, n);
405 pkg.components.push_back(prog);
408 void Package::Loader::module(const string &n)
410 Component prog(pkg, Component::MODULE, n);
412 pkg.components.push_back(prog);
415 void Package::Loader::headers(const string &n)
417 Component prog(pkg, Component::HEADERS, n);
419 pkg.components.push_back(prog);
422 void Package::Loader::build_info()
424 load_sub(pkg.build_info);
427 void Package::Loader::tar_file(const string &f)
429 pkg.tar_files.push_back(pkg.source/f);