3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include <msp/strings/lexicalcast.h>
10 #include <msp/strings/utils.h>
11 #include "binarypackage.h"
14 #include "sourcepackage.h"
20 Creates a buildable package.
22 SourcePackage::SourcePackage(Builder &b, const string &n, const Path &s):
28 tar_files.push_back(source/"Build");
31 Msp::Path SourcePackage::get_temp_dir() const
33 return source/config.get_option("tempdir").value/config.get_option("profile").value;
36 Msp::Path SourcePackage::get_out_dir() const
38 return source/config.get_option("outdir").value;
42 Checks which kinds of things the components of this package install.
44 @return A bitmask of installed things
46 unsigned SourcePackage::get_install_flags()
49 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
53 if(i->get_type()==Component::PROGRAM)
55 else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
58 if(!i->get_install_headers().empty())
65 LibMode SourcePackage::get_library_mode() const
67 const string &mode=config.get_option("staticlibs").value;
70 else if(mode=="local")
75 throw Exception("Unknown library mode");
81 Processes configuration options that were most likely obtained from the command
84 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
88 StringMap::const_iterator prof=opts.find("profile");
89 if(prof!=opts.end() && flag)
90 config.select_profile(prof->second);
92 config.select_last_profile();
94 if(flag && config.update(opts))
96 if(builder.get_verbose()>=2)
97 cout<<"Configuration of "<<name<<" changed\n";
98 if(!builder.get_dry_run())
104 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
107 const StringList &reqs=i->get_requires();
108 for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
109 if(Package *pkg=builder.get_package(*j))
110 requires.push_back(pkg);
115 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
117 const PackageList &reqs=i->get_requires();
118 requires.insert(requires.end(), reqs.begin(), reqs.end());
121 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
123 BinaryPackage *bpkg=dynamic_cast<BinaryPackage *>(*i);
124 if(bpkg && bpkg->get_need_path())
125 bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
130 /*for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
131 (*i)->configure(opts, flag&2);*/
135 Initializes configuration options.
137 void SourcePackage::init_config()
139 config.add_option("profile", "default", "Configuration profile");
140 config.add_option("tempdir", "temp", "Directory for storing temporary files");
141 config.add_option("outdir", ".", "Directory to put build results in");
142 config.add_option("optimize", "0", "Apply compiler optimizations");
143 config.add_option("strip", "0", "Strip symbols from programs");
144 config.add_option("debug", "0", "Produce debugging symbols");
145 config.add_option("cpu", "auto", "CPU type to optimize for");
146 config.add_option("arch", "native", "Architecture for cross-compiling");
147 config.add_option("staticlibs", "local", "Use static libraries");
149 unsigned flags=get_install_flags();
151 config.add_option("prefix", "$HOME/local", "Installation prefix");
153 config.add_option("includedir", "$prefix/include", "Header installation directory");
155 config.add_option("includedir", "$prefix/bin", "Binary installation directory");
157 config.add_option("includedir", "$prefix/lib", "Library installation directory");
159 config.add_option("includedir", "$prefix/share", "Data installation directory");*/
161 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
162 config.add_option("with_"+i->name, "0", i->descr);
164 for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
166 BinaryPackage *bpkg=dynamic_cast<BinaryPackage *>(*i);
167 if(bpkg && bpkg->get_need_path())
168 config.add_option(bpkg->get_name()+"_path", "", "Path for "+bpkg->get_name());
173 Fills in build info based on configuration. All required packages must be
174 configured when this is called.
176 void SourcePackage::create_build_info()
178 for(PackageList::iterator i=base_reqs.begin(); i!=base_reqs.end(); ++i)
180 const BuildInfo &ebi=(*i)->get_exported_binfo();
183 export_binfo.cflags.insert(export_binfo.cflags.end(), ebi.cflags.begin(), ebi.cflags.end());
184 export_binfo.incpath.insert(export_binfo.incpath.end(), ebi.incpath.begin(), ebi.incpath.end());
185 export_binfo.defines.insert(export_binfo.defines.end(), ebi.defines.begin(), ebi.defines.end());
188 build_info.cflags.push_back("-Wall");
189 build_info.cflags.push_back("-Wshadow");
190 build_info.cflags.push_back("-Wextra");
191 build_info.cflags.push_back("-Wpointer-arith");
192 build_info.cflags.push_back("-Wconversion");
193 build_info.cflags.push_back("-Werror");
195 unsigned flags=get_install_flags();
198 export_binfo.incpath.push_back((Path(config.get_option("prefix").value)/"include").str());
200 export_binfo.libpath.push_back((Path(config.get_option("prefix").value)/"lib").str());
202 string optimize=config.get_option("optimize").value;
203 if(lexical_cast<unsigned>(optimize))
205 build_info.cflags.push_back("-O"+optimize);
206 build_info.ldflags.push_back("-O"+optimize);
207 string cpu=config.get_option("cpu").value;
209 build_info.cflags.push_back("-march="+cpu);
212 if(lexical_cast<bool>(config.get_option("debug").value))
214 build_info.cflags.push_back("-ggdb");
215 build_info.defines.push_back("DEBUG");
218 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
219 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
220 build_info.cflags.push_back("-DWITH_"+toupper(i->name));
222 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
224 build_info.add(i->get_build_info());
228 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
230 i->create_build_info();
231 if(i->get_type()==Component::LIBRARY)
232 export_binfo.libs.push_back(i->get_name());
235 export_binfo.unique();
239 SourcePackage::Loader::Loader(Package &p):
242 add("version", &SourcePackage::version);
243 add("description", &SourcePackage::description);
244 add("build_info", &Loader::build_info);
245 add("feature", &Loader::feature);
246 add("if", &Loader::condition);
247 add("program", &Loader::program);
248 add("library", &Loader::library);
249 add("module", &Loader::module);
250 add("headers", &Loader::headers);
251 add("tar_file", &Loader::tar_file);
254 void SourcePackage::Loader::feature(const string &n, const string &d)
256 static_cast<SourcePackage &>(pkg).features.push_back(Feature(n, d));
259 void SourcePackage::Loader::condition(const string &c)
261 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
262 Condition cond(spkg, c);
264 spkg.conditions.push_back(cond);
267 void SourcePackage::Loader::program(const string &n)
269 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
270 Component prog(spkg, Component::PROGRAM, n);
272 spkg.components.push_back(prog);
275 void SourcePackage::Loader::library(const string &n)
277 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
278 Component prog(spkg, Component::LIBRARY, n);
280 spkg.components.push_back(prog);
283 void SourcePackage::Loader::module(const string &n)
285 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
286 Component prog(spkg, Component::MODULE, n);
288 spkg.components.push_back(prog);
291 void SourcePackage::Loader::headers(const string &n)
293 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
294 Component prog(spkg, Component::HEADERS, n);
296 spkg.components.push_back(prog);
299 void SourcePackage::Loader::build_info()
301 load_sub(static_cast<SourcePackage &>(pkg).build_info);
304 void SourcePackage::Loader::tar_file(const string &f)
306 SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
307 spkg.tar_files.push_back(spkg.source/f);