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):
20 build_info_ready(false)
24 Sets the path where the package files were installed. This is only useful for
25 non-buildable packages that don't use pkg-config.
27 void Package::set_path(const Msp::Path::Path &p)
29 path=builder.get_cwd()/p;
33 Tries to resolve all references to dependency packages.
35 void Package::resolve_refs()
37 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
39 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
44 Fills in build info based on configuration.
46 void Package::create_build_info()
53 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
55 Package *pkg=i->get_package();
58 if(pkg->get_need_path())
59 pkg->set_path(config.get_option(pkg->get_name()+"_path").value);
60 pkg->create_build_info();
61 build_info.add(pkg->get_exported_binfo());
62 export_binfo.add(pkg->get_exported_binfo());
65 build_info.cflags.push_back("-Wall");
66 build_info.cflags.push_back("-Wshadow");
67 build_info.cflags.push_back("-Wextra");
68 build_info.cflags.push_back("-Wpointer-arith");
69 build_info.cflags.push_back("-Wconversion");
70 build_info.cflags.push_back("-Werror");
72 unsigned flags=get_install_flags();
75 export_binfo.incpath.push_back((Path::Path(config.get_option("prefix").value)/"include").str());
77 export_binfo.libpath.push_back((Path::Path(config.get_option("prefix").value)/"lib").str());
79 string optimize=config.get_option("optimize").value;
82 build_info.cflags.push_back("-O"+optimize);
83 string cpu=config.get_option("cpu").value;
85 build_info.cflags.push_back("-march="+cpu);
88 if(strtobool(config.get_option("debug").value))
90 build_info.cflags.push_back("-ggdb");
91 build_info.defines.push_back("DEBUG");
96 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
98 i->create_build_info();
99 if(i->get_type()==Component::LIBRARY)
100 export_binfo.libs.push_back(i->get_name());
103 else if(name=="fmod4")
105 export_binfo.libs.push_back("fmodex");
108 export_binfo.libpath.push_back((path/"api"/"lib").str());
109 export_binfo.incpath.push_back((path/"api"/"inc").str());
112 export_binfo.unique();
114 build_info_ready=true;
118 Processes configuration options that were most likely obtained from the command
121 void Package::process_options(const RawOptionMap &opts)
123 if(config.process(opts))
124 config.save(source/".options.cache");
128 Creates a non-buildable package with the given name. Pkg-config is tried first
129 to get build information. If it fails, a built-in list of known packages is
132 Package *Package::create(Builder &b, const string &name)
135 argv.push_back("pkg-config");
136 argv.push_back("--silence-errors");
137 argv.push_back("--cflags");
138 argv.push_back("--libs");
139 argv.push_back(name);
140 vector<string> info=split(run_command(argv));
142 bool need_path=false;
145 //XXX Put these in an external file
147 info.push_back("-lGL");
148 else if(name=="pthread")
149 info.push_back("-lpthread");
150 else if(name=="gmpxx")
151 info.push_back("-lgmpxx");
152 else if(name=="fmod4")
158 Package *pkg=new Package(b, name, info);
159 pkg->need_path=need_path;
165 Package::Package(Builder &b, const string &n, const vector<string> &info):
169 build_info_ready(false)
171 for(vector<string>::const_iterator i=info.begin(); i!=info.end(); ++i)
173 if(!i->compare(0, 2, "-I"))
174 export_binfo.incpath.push_back(i->substr(2));
175 else if(!i->compare(0, 2, "-D"))
176 export_binfo.defines.push_back(i->substr(2));
177 else if(!i->compare(0, 2, "-L"))
178 export_binfo.libpath.push_back(i->substr(2));
179 else if(!i->compare(0, 2, "-l"))
180 export_binfo.libs.push_back(i->substr(2));
185 Initializes a buildable package. Mostly adds configuration options.
187 void Package::init_buildable()
191 config.add_option("tempdir", "temp", "Directory for storing temporary files");
192 config.add_option("optimize", "0", "Apply compiler optimizations");
193 config.add_option("debug", "0", "Produce debugging symbols");
194 config.add_option("cpu", "auto", "CPU type to optimize for");
195 config.add_option("arch", "native", "Architecture for cross-compiling");
197 const char *home=getenv("HOME");
198 unsigned flags=get_install_flags();
200 config.add_option("prefix", string(home)+"/local"/*"/usr"*/, "Installation prefix");
202 config.add_option("includedir", "$prefix/include", "Header installation directory");
204 config.add_option("includedir", "$prefix/bin", "Binary installation directory");
206 config.add_option("includedir", "$prefix/lib", "Library installation directory");
208 config.add_option("includedir", "$prefix/share", "Data installation directory");*/
210 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
211 config.add_option(i->get_name()+"_path", "", "Path for "+i->get_name());
213 config.load(source/".options.cache");
217 Checks which kinds of things the components of this package install.
219 @return A bitmask of installed things
221 unsigned Package::get_install_flags()
224 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
228 if(i->get_type()==Component::PROGRAM)
230 else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
233 if(!i->get_install_headers().empty())
240 Package::Loader::Loader(Package &p):
243 add("version", &Package::version);
244 add("description", &Package::description);
245 add("require", &Loader::require);
246 add("program", &Loader::program);
247 add("library", &Loader::library);
248 add("headers", &Loader::headers);
249 add("build_info", &Loader::build_info);
252 Package::Loader::~Loader()
254 pkg.init_buildable();
257 void Package::Loader::require(const string &n)
259 pkg.requires.push_back(PackageRef(pkg.builder, n));
262 void Package::Loader::program(const string &n)
264 Component prog(pkg, Component::PROGRAM, n);
266 pkg.components.push_back(prog);
269 void Package::Loader::library(const string &n)
271 Component prog(pkg, Component::LIBRARY, n);
273 pkg.components.push_back(prog);
276 void Package::Loader::headers(const string &n)
278 Component prog(pkg, Component::HEADERS, n);
280 pkg.components.push_back(prog);
283 void Package::Loader::build_info()
285 load_sub(pkg.build_info);