2 #include <msp/io/print.h>
3 #include <msp/strings/lexicalcast.h>
4 #include <msp/strings/utils.h>
5 #include "binarypackage.h"
8 #include "pkgconfigfile.h"
10 #include "sourcepackage.h"
17 bool component_sort(const Component &c1, const Component &c2)
18 { return c1.get_type()<c2.get_type(); }
23 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &s):
29 components.push_back(Component(*this, Component::TARBALL, "@src"));
32 FS::Path SourcePackage::get_temp_dir() const
34 string subdir = format("%s.%s", builder.get_current_arch().get_name(), config.get_option("profile").value);
35 return source/config.get_option("tempdir").value/subdir;
38 FS::Path SourcePackage::get_out_dir() const
40 const Architecture &arch = builder.get_current_arch();
42 return source/config.get_option("outdir").value;
44 return source/arch.get_name()/config.get_option("outdir").value;
47 unsigned SourcePackage::get_install_flags()
50 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)
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 builder.get_logger().log("configure", format("Configuration of %s changed", name));
132 if(!builder.get_dry_run())
138 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
141 const StringList &reqs = i->get_requires();
142 for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
143 if(Package *pkg = builder.get_package_manager().find_package(*j))
144 requires.push_back(pkg);
147 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
149 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
150 if(bpkg && bpkg->get_need_path())
151 bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
156 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
157 i->configure(opts, flag);
160 void SourcePackage::init_config()
162 config.add_option("profile", "default", "Configuration profile");
163 config.add_option("tempdir", "temp", "Directory for storing temporary files");
164 config.add_option("outdir", ".", "Directory to put build results in");
165 config.add_option("optimize", "0", "Compiler optimization level");
166 config.add_option("strip", "no", "Strip symbols from programs");
167 config.add_option("debug", "no", "Produce debugging symbols");
168 config.add_option("staticlibs", "local", "Use static libraries");
170 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
171 config.add_option("with_"+i->name, i->def_value, i->descr);
173 for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
175 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
176 if(bpkg && bpkg->get_need_path())
177 config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
181 void SourcePackage::create_build_info()
183 // XXX Currently, a package-specific settings will override cmdline. This might or might not be desirable.
184 const StringList &warnings = builder.get_warnings();
185 build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
187 unsigned flags = get_install_flags();
189 build_info.incpath.push_back((builder.get_prefix()/"include").str());
190 build_info.libpath.push_back((builder.get_prefix()/"lib").str());
193 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
195 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
197 string optimize = config.get_option("optimize").value;
198 if(!optimize.empty() && optimize!="0")
200 if(optimize=="s" || optimize=="size")
201 build_info.optimize = -1;
203 build_info.optimize = lexical_cast<unsigned>(optimize);
206 if(lexical_cast<bool>(config.get_option("debug").value))
208 build_info.debug = true;
209 build_info.defines["DEBUG"] = "1";
212 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
213 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
214 build_info.defines["WITH_"+toupper(i->name)] = "1";
216 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
218 build_info.update_from(i->get_build_info());
220 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
222 i->create_build_info();
223 if(i->get_type()==Component::LIBRARY)
224 export_binfo.libs.push_back(i->get_name());
228 void SourcePackage::create_targets()
230 bool pc_needed = false;
231 for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
234 if(i->get_type()==Component::LIBRARY)
240 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
241 builder.get_target("install")->add_depend(builder.get_toolchain().get_tool("CP").create_target(*pc));
246 SourcePackage::Loader::Loader(SourcePackage &p):
247 DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
249 add("version", &SourcePackage::version);
250 add("description", &SourcePackage::description);
251 add("build_info", &Loader::build_info);
252 add("feature", &Loader::feature);
253 add("if", &Loader::condition);
254 add("program", &Loader::component<Component::PROGRAM>);
255 add("library", &Loader::component<Component::LIBRARY>);
256 add("module", &Loader::component<Component::MODULE>);
257 add("headers", &Loader::headers);
258 add("install", &Loader::component<Component::INSTALL>);
259 add("datafile", &Loader::component<Component::DATAFILE>);
260 add("tarball", &Loader::tarball);
261 add("tar_file", &Loader::tar_file);
264 void SourcePackage::Loader::finish()
266 obj.components.sort(component_sort);
268 for(map<string, string>::const_iterator i=install_map.begin(); i!=install_map.end(); ++i)
270 for(ComponentList::iterator j=obj.components.begin(); j!=obj.components.end(); ++j)
272 const StringList &sources = j->get_sources();
273 for(StringList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
275 if(!i->first.compare(0, k->size(), *k))
277 const_cast<InstallMap &>(j->get_install_map()).add_mapping(obj.source/i->first, i->second);
284 void SourcePackage::Loader::feature(const string &n, const string &d)
288 feat.def_value = "no";
290 obj.features.push_back(feat);
293 void SourcePackage::Loader::condition(const string &c)
295 Condition cond(obj, c);
297 obj.conditions.push_back(cond);
300 template<Component::Type t>
301 void SourcePackage::Loader::component(const string &n)
303 Component comp(obj, t, n);
305 obj.components.push_back(comp);
308 void SourcePackage::Loader::build_info()
310 load_sub(obj.build_info);
313 void SourcePackage::Loader::headers(const string &n)
315 IO::print("%s: Note: headers components are deprecated\n", get_source());
316 Component comp(obj, Component::LIBRARY, n);
318 const StringList &sources = comp.get_sources();
319 for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
320 install_map[*i] = "include/"+comp.get_name();
323 void SourcePackage::Loader::tarball(const string &n)
327 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
328 if(i->get_type()==Component::TARBALL && i->get_name()==n)
333 Component trbl(obj, Component::TARBALL, n);
338 void SourcePackage::Loader::tar_file(const string &f)
340 IO::print("%s: Note: tar_file is deprecated\n", get_source());
341 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
342 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
343 const_cast<StringList &>(i->get_sources()).push_back((obj.source/f).str());