2 #include <msp/io/print.h>
3 #include <msp/strings/lexicalcast.h>
4 #include <msp/strings/utils.h>
5 #include "binarypackage.h"
8 #include "sourcepackage.h"
15 bool component_sort(const Component &c1, const Component &c2)
16 { return c1.get_type()<c2.get_type(); }
21 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &s):
27 components.push_back(Component(*this, Component::TARBALL, "@src"));
30 FS::Path SourcePackage::get_temp_dir() const
32 string subdir = format("%s.%s", builder.get_current_arch().get_name(), config.get_option("profile").value);
33 return source/config.get_option("tempdir").value/subdir;
36 FS::Path SourcePackage::get_out_dir() const
38 const Architecture &arch = builder.get_current_arch();
40 return source/config.get_option("outdir").value;
42 return source/arch.get_name()/config.get_option("outdir").value;
45 unsigned SourcePackage::get_install_flags()
48 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
51 if(i->get_type()==Component::PROGRAM)
53 else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
55 else if(i->get_type()==Component::HEADERS)
62 LibMode SourcePackage::get_library_mode() const
64 const string &mode = config.get_option("staticlibs").value;
67 else if(mode=="local")
72 throw runtime_error("unknown library mode");
75 string SourcePackage::expand_string(const string &str) const
78 string::size_type dollar = 0;
80 while((dollar = result.find('$'))!=string::npos)
83 throw bad_expansion("nested too deep");
85 string::size_type end;
87 if(dollar+1<result.size() && result[dollar+1]=='{')
89 end = result.find('}', dollar+2);
91 throw bad_expansion("unterminated variable reference");
92 var = result.substr(dollar+2, end-dollar-2);
97 for(end=dollar+1; (isalnum(result[end]) || result[end]=='_'); ++end) ;
98 var = result.substr(dollar+1, end-dollar-1);
102 if(config.is_option(var))
103 value = config.get_option(var).value;
105 value = builder.get_current_arch().get_name();
106 else if(var=="system")
107 value = builder.get_current_arch().get_system();
108 else if(const char *ptr = getenv(var.c_str()))
111 result.replace(dollar, end-dollar, value);
119 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
123 StringMap::const_iterator prof = opts.find("profile");
124 if(prof!=opts.end() && flag)
125 config.select_profile(prof->second);
127 config.select_last_profile();
129 if(flag && config.update(opts))
131 if(builder.get_verbose()>=2)
132 IO::print("Configuration of %s changed\n", name);
133 if(!builder.get_dry_run())
139 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
142 const StringList &reqs = i->get_requires();
143 for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
144 if(Package *pkg = builder.get_package(*j))
145 requires.push_back(pkg);
148 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
150 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
151 if(bpkg && bpkg->get_need_path())
152 bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
157 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
158 i->configure(opts, flag);
161 void SourcePackage::init_config()
163 config.add_option("profile", "default", "Configuration profile");
164 config.add_option("tempdir", "temp", "Directory for storing temporary files");
165 config.add_option("outdir", ".", "Directory to put build results in");
166 config.add_option("optimize", "0", "Compiler optimization level");
167 config.add_option("strip", "no", "Strip symbols from programs");
168 config.add_option("debug", "no", "Produce debugging symbols");
169 config.add_option("staticlibs", "local", "Use static libraries");
171 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
172 config.add_option("with_"+i->name, i->def_value, i->descr);
174 for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
176 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
177 if(bpkg && bpkg->get_need_path())
178 config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
182 void SourcePackage::create_build_info()
184 // XXX Currently, a package-specific settings will override cmdline. This might or might not be desirable.
185 const StringList &warnings = builder.get_warnings();
186 build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
188 unsigned flags = get_install_flags();
190 build_info.incpath.push_back((builder.get_prefix()/"include").str());
191 build_info.libpath.push_back((builder.get_prefix()/"lib").str());
194 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
196 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
198 string optimize = config.get_option("optimize").value;
199 if(!optimize.empty() && optimize!="0")
201 if(optimize=="s" || optimize=="size")
202 build_info.optimize = -1;
204 build_info.optimize = lexical_cast<unsigned>(optimize);
207 if(lexical_cast<bool>(config.get_option("debug").value))
209 build_info.debug = true;
210 build_info.defines["DEBUG"] = "1";
213 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
214 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
215 build_info.defines["WITH_"+toupper(i->name)] = "1";
217 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
219 build_info.update_from(i->get_build_info());
223 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
225 i->create_build_info();
226 if(i->get_type()==Component::LIBRARY)
227 export_binfo.libs.push_back(i->get_name());
230 export_binfo.unique();
234 SourcePackage::Loader::Loader(Package &p):
237 add("version", &SourcePackage::version);
238 add("description", &SourcePackage::description);
239 add("build_info", &Loader::build_info);
240 add("feature", &Loader::feature);
241 add("if", &Loader::condition);
242 add("program", &Loader::component<Component::PROGRAM>);
243 add("library", &Loader::component<Component::LIBRARY>);
244 add("module", &Loader::component<Component::MODULE>);
245 add("headers", &Loader::component<Component::HEADERS>);
246 add("install", &Loader::component<Component::INSTALL>);
247 add("datafile", &Loader::component<Component::DATAFILE>);
248 add("tarball", &Loader::tarball);
249 add("tar_file", &Loader::tar_file);
252 void SourcePackage::Loader::finish()
254 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
255 spkg.components.sort(component_sort);
258 void SourcePackage::Loader::feature(const string &n, const string &d)
262 feat.def_value = "no";
264 static_cast<SourcePackage &>(pkg).features.push_back(feat);
267 void SourcePackage::Loader::condition(const string &c)
269 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
270 Condition cond(spkg, c);
272 spkg.conditions.push_back(cond);
275 template<Component::Type t>
276 void SourcePackage::Loader::component(const string &n)
278 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
279 Component comp(spkg, t, n);
281 spkg.components.push_back(comp);
284 void SourcePackage::Loader::build_info()
286 load_sub(static_cast<SourcePackage &>(pkg).build_info);
289 void SourcePackage::Loader::tarball(const string &n)
291 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
294 for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.components.end(); ++i)
295 if(i->get_type()==Component::TARBALL && i->get_name()==n)
300 Component trbl(spkg, Component::TARBALL, n);
305 void SourcePackage::Loader::tar_file(const string &f)
307 IO::print("%s: Note: tar_file is deprecated\n", get_source());
308 SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
309 for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.components.end(); ++i)
310 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
311 const_cast<StringList &>(i->get_sources()).push_back((spkg.source/f).str());