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)
87 if(builder.get_verbose()>=3)
88 cout<<"Configuring "<<name<<'\n';
94 if(flag && config.process(opts) && !builder.get_dry_run())
96 if(builder.get_verbose()>=2)
97 cout<<"Configuration of "<<name<<" changed\n";
98 config.save(source/".options.cache");
101 for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
103 if((*i)->get_need_path())
104 (*i)->set_path(config.get_option((*i)->get_name()+"_path").value);
105 (*i)->configure(opts, flag&2);
115 Creates a non-buildable package with the given name. Pkg-config is tried first
116 to get build information. If it fails, a built-in list of known packages is
119 Package *Package::create(Builder &b, const string &name)
122 argv.push_back("pkg-config");
123 argv.push_back("--silence-errors");
124 argv.push_back("--cflags");
125 argv.push_back("--libs");
126 argv.push_back(name);
127 vector<string> info=split(run_command(argv));
129 bool need_path=false;
130 bool use_pkgconfig=true;
135 //XXX Put these in an external file
137 info.push_back("-lGL");
138 else if(name=="pthread")
139 info.push_back("-lpthread");
140 else if(name=="gmpxx")
141 info.push_back("-lgmpxx");
142 else if(name=="fmod4")
148 Package *pkg=new Package(b, name, info);
149 pkg->need_path=need_path;
150 pkg->use_pkgconfig=use_pkgconfig;
156 Package::Package(Builder &b, const string &n, const vector<string> &info):
161 for(vector<string>::const_iterator i=info.begin(); i!=info.end(); ++i)
163 if(!i->compare(0, 2, "-I"))
164 export_binfo.incpath.push_back(i->substr(2));
165 else if(!i->compare(0, 2, "-D"))
166 export_binfo.defines.push_back(i->substr(2));
167 else if(!i->compare(0, 2, "-L"))
168 export_binfo.libpath.push_back(i->substr(2));
169 else if(!i->compare(0, 2, "-l"))
170 export_binfo.libs.push_back(i->substr(2));
175 Initializes configuration options and loads cached values.
177 void Package::init_config()
179 config.add_option("tempdir", "temp", "Directory for storing temporary files");
180 config.add_option("optimize", "0", "Apply compiler optimizations");
181 config.add_option("debug", "0", "Produce debugging symbols");
182 config.add_option("cpu", "auto", "CPU type to optimize for");
183 config.add_option("arch", "native", "Architecture for cross-compiling");
184 config.add_option("staticlibs", "local", "Use static libraries");
186 const char *home=getenv("HOME");
187 unsigned flags=get_install_flags();
189 config.add_option("prefix", string(home)+"/local"/*"/usr"*/, "Installation prefix");
191 config.add_option("includedir", "$prefix/include", "Header installation directory");
193 config.add_option("includedir", "$prefix/bin", "Binary installation directory");
195 config.add_option("includedir", "$prefix/lib", "Library installation directory");
197 config.add_option("includedir", "$prefix/share", "Data installation directory");*/
199 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
200 if(i->get_package() && i->get_package()->get_need_path())
201 config.add_option(i->get_name()+"_path", "", "Path for "+i->get_name());
203 config.load(source/".options.cache");
207 Fills in build info based on configuration. All required packages must be
208 configured when this is called.
210 void Package::create_build_info()
214 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
216 Package *pkg=i->get_package();
219 build_info.add(pkg->get_exported_binfo());
220 //XXX We probably really only want to pass cflags and defines through
221 export_binfo.add(pkg->get_exported_binfo());
224 build_info.cflags.push_back("-Wall");
225 build_info.cflags.push_back("-Wshadow");
226 build_info.cflags.push_back("-Wextra");
227 build_info.cflags.push_back("-Wpointer-arith");
228 build_info.cflags.push_back("-Wconversion");
229 build_info.cflags.push_back("-Werror");
231 unsigned flags=get_install_flags();
234 export_binfo.incpath.push_back((Path::Path(config.get_option("prefix").value)/"include").str());
236 export_binfo.libpath.push_back((Path::Path(config.get_option("prefix").value)/"lib").str());
238 string optimize=config.get_option("optimize").value;
241 build_info.cflags.push_back("-O"+optimize);
242 string cpu=config.get_option("cpu").value;
244 build_info.cflags.push_back("-march="+cpu);
247 if(strtobool(config.get_option("debug").value))
249 build_info.cflags.push_back("-ggdb");
250 build_info.defines.push_back("DEBUG");
255 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
257 i->create_build_info();
258 if(i->get_type()==Component::LIBRARY)
259 export_binfo.libs.push_back(i->get_name());
262 else if(name=="fmod4")
265 export_binfo.libs.push_back("fmodex");
268 export_binfo.libpath.push_back((path/"api"/"lib").str());
269 export_binfo.incpath.push_back((path/"api"/"inc").str());
272 export_binfo.unique();
275 Package::Loader::Loader(Package &p):
278 add("version", &Package::version);
279 add("description", &Package::description);
280 add("require", &Loader::require);
281 add("program", &Loader::program);
282 add("library", &Loader::library);
283 add("headers", &Loader::headers);
284 add("build_info", &Loader::build_info);
287 void Package::Loader::require(const string &n)
289 pkg.requires.push_back(PackageRef(pkg.builder, n));
292 void Package::Loader::program(const string &n)
294 Component prog(pkg, Component::PROGRAM, n);
296 pkg.components.push_back(prog);
299 void Package::Loader::library(const string &n)
301 Component prog(pkg, Component::LIBRARY, n);
303 pkg.components.push_back(prog);
306 void Package::Loader::headers(const string &n)
308 Component prog(pkg, Component::HEADERS, n);
310 pkg.components.push_back(prog);
313 void Package::Loader::build_info()
315 load_sub(pkg.build_info);