1 #include <msp/strconv.h>
2 #include <msp/strutils.h>
13 Creates a buildable package.
15 Package::Package(Builder &b, const string &n, const Path::Path &s):
26 Sets the path where the package files were installed. This is only useful for
27 non-buildable packages that don't use pkg-config.
29 void Package::set_path(const Msp::Path::Path &p)
31 path=builder.get_cwd()/p;
35 Checks which kinds of things the components of this package install.
37 @return A bitmask of installed things
39 unsigned Package::get_install_flags()
42 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
46 if(i->get_type()==Component::PROGRAM)
48 else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
51 if(!i->get_install_headers().empty())
59 Tries to resolve all references to dependency packages.
61 void Package::resolve_refs()
63 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
65 Package *pkg=i->resolve();
66 if(pkg) all_reqs.push_back(pkg);
68 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
71 const PkgRefList &creqs=i->get_requires();
72 for(PkgRefList::const_iterator j=creqs.begin(); j!=creqs.end(); ++j)
74 all_reqs.push_back(j->get_package());
79 Processes configuration options that were most likely obtained from the command
82 void Package::configure(const RawOptionMap &opts, unsigned flag)
84 if(conf_done || !buildable)
87 if(builder.get_verbose()>=3)
88 cout<<"Configuring "<<name<<'\n';
92 if(flag && config.process(opts) && !builder.get_dry_run())
94 if(builder.get_verbose()>=2)
95 cout<<"Configuration of "<<name<<" changed.\n";
96 config.save(source/".options.cache");
99 for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
101 (*i)->configure(opts, flag&2);
102 if((*i)->get_need_path())
103 (*i)->set_path(config.get_option((*i)->get_name()+"_path").value);
112 Creates a non-buildable package with the given name. Pkg-config is tried first
113 to get build information. If it fails, a built-in list of known packages is
116 Package *Package::create(Builder &b, const string &name)
119 argv.push_back("pkg-config");
120 argv.push_back("--silence-errors");
121 argv.push_back("--cflags");
122 argv.push_back("--libs");
123 argv.push_back(name);
124 vector<string> info=split(run_command(argv));
126 bool need_path=false;
127 bool use_pkgconfig=true;
132 //XXX Put these in an external file
134 info.push_back("-lGL");
135 else if(name=="pthread")
136 info.push_back("-lpthread");
137 else if(name=="gmpxx")
138 info.push_back("-lgmpxx");
139 else if(name=="fmod4")
145 Package *pkg=new Package(b, name, info);
146 pkg->need_path=need_path;
147 pkg->use_pkgconfig=use_pkgconfig;
153 Package::Package(Builder &b, const string &n, const vector<string> &info):
158 for(vector<string>::const_iterator i=info.begin(); i!=info.end(); ++i)
160 if(!i->compare(0, 2, "-I"))
161 export_binfo.incpath.push_back(i->substr(2));
162 else if(!i->compare(0, 2, "-D"))
163 export_binfo.defines.push_back(i->substr(2));
164 else if(!i->compare(0, 2, "-L"))
165 export_binfo.libpath.push_back(i->substr(2));
166 else if(!i->compare(0, 2, "-l"))
167 export_binfo.libs.push_back(i->substr(2));
172 Initializes configuration options and loads cached values.
174 void Package::init_config()
176 config.add_option("tempdir", "temp", "Directory for storing temporary files");
177 config.add_option("optimize", "0", "Apply compiler optimizations");
178 config.add_option("debug", "0", "Produce debugging symbols");
179 config.add_option("cpu", "auto", "CPU type to optimize for");
180 config.add_option("arch", "native", "Architecture for cross-compiling");
181 config.add_option("staticlibs", "local", "Use static libraries");
183 const char *home=getenv("HOME");
184 unsigned flags=get_install_flags();
186 config.add_option("prefix", string(home)+"/local"/*"/usr"*/, "Installation prefix");
188 config.add_option("includedir", "$prefix/include", "Header installation directory");
190 config.add_option("includedir", "$prefix/bin", "Binary installation directory");
192 config.add_option("includedir", "$prefix/lib", "Library installation directory");
194 config.add_option("includedir", "$prefix/share", "Data installation directory");*/
196 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
197 if(i->get_package() && i->get_package()->get_need_path())
198 config.add_option(i->get_name()+"_path", "", "Path for "+i->get_name());
200 config.load(source/".options.cache");
204 Fills in build info based on configuration. All required packages must be
205 configured when this is called.
207 void Package::create_build_info()
211 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
213 Package *pkg=i->get_package();
216 build_info.add(pkg->get_exported_binfo());
217 //XXX We probably really only want to pass cflags and defines through
218 export_binfo.add(pkg->get_exported_binfo());
221 build_info.cflags.push_back("-Wall");
222 build_info.cflags.push_back("-Wshadow");
223 build_info.cflags.push_back("-Wextra");
224 build_info.cflags.push_back("-Wpointer-arith");
225 build_info.cflags.push_back("-Wconversion");
226 build_info.cflags.push_back("-Werror");
228 unsigned flags=get_install_flags();
231 export_binfo.incpath.push_back((Path::Path(config.get_option("prefix").value)/"include").str());
233 export_binfo.libpath.push_back((Path::Path(config.get_option("prefix").value)/"lib").str());
235 string optimize=config.get_option("optimize").value;
238 build_info.cflags.push_back("-O"+optimize);
239 string cpu=config.get_option("cpu").value;
241 build_info.cflags.push_back("-march="+cpu);
244 if(strtobool(config.get_option("debug").value))
246 build_info.cflags.push_back("-ggdb");
247 build_info.defines.push_back("DEBUG");
252 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
254 i->create_build_info();
255 if(i->get_type()==Component::LIBRARY)
256 export_binfo.libs.push_back(i->get_name());
259 else if(name=="fmod4")
261 export_binfo.libs.push_back("fmodex");
264 export_binfo.libpath.push_back((path/"api"/"lib").str());
265 export_binfo.incpath.push_back((path/"api"/"inc").str());
268 export_binfo.unique();
271 Package::Loader::Loader(Package &p):
274 add("version", &Package::version);
275 add("description", &Package::description);
276 add("require", &Loader::require);
277 add("program", &Loader::program);
278 add("library", &Loader::library);
279 add("headers", &Loader::headers);
280 add("build_info", &Loader::build_info);
283 void Package::Loader::require(const string &n)
285 pkg.requires.push_back(PackageRef(pkg.builder, n));
288 void Package::Loader::program(const string &n)
290 Component prog(pkg, Component::PROGRAM, n);
292 pkg.components.push_back(prog);
295 void Package::Loader::library(const string &n)
297 Component prog(pkg, Component::LIBRARY, n);
299 pkg.components.push_back(prog);
302 void Package::Loader::headers(const string &n)
304 Component prog(pkg, Component::HEADERS, n);
306 pkg.components.push_back(prog);
309 void Package::Loader::build_info()
311 load_sub(pkg.build_info);