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):
30 components.push_back(Component(*this, Component::TARBALL, "@src"));
33 void SourcePackage::set_build_type(const BuildType &t)
38 FS::Path SourcePackage::get_temp_dir() const
40 string subdir = builder.get_current_arch().get_name();
44 subdir += build_type->get_name();
46 return source/config.get_option("tempdir").value/subdir;
49 FS::Path SourcePackage::get_out_dir() const
51 const Architecture &arch = builder.get_current_arch();
52 string detail = (build_type ? build_type->get_name() : string());
56 return source/arch.get_name()/detail;
59 LibMode SourcePackage::get_library_mode() const
61 const string &mode = config.get_option("staticlibs").value;
64 else if(mode=="local")
69 throw runtime_error("unknown library mode");
72 string SourcePackage::expand_string(const string &str) const
75 string::size_type dollar = 0;
77 while((dollar = result.find('$'))!=string::npos)
80 throw bad_expansion("nested too deep");
82 string::size_type end;
84 if(dollar+1<result.size() && result[dollar+1]=='{')
86 end = result.find('}', dollar+2);
88 throw bad_expansion("unterminated variable reference");
89 var = result.substr(dollar+2, end-dollar-2);
94 for(end=dollar+1; (isalnum(result[end]) || result[end]=='_'); ++end) ;
95 var = result.substr(dollar+1, end-dollar-1);
99 if(config.is_option(var))
100 value = config.get_option(var).value;
102 value = builder.get_current_arch().get_name();
103 else if(var=="system")
104 value = builder.get_current_arch().get_system();
105 else if(const char *ptr = getenv(var.c_str()))
108 result.replace(dollar, end-dollar, value);
116 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
122 if(flag && config.update(opts))
123 builder.get_logger().log("configure", format("Configuration of %s changed", name));
127 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
130 const StringList &reqs = i->get_requires();
131 for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
132 if(Package *pkg = builder.get_package_manager().find_package(*j))
133 requires.push_back(pkg);
136 for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
138 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
139 if(bpkg && bpkg->get_need_path())
140 bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
145 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
146 i->configure(opts, flag);
149 void SourcePackage::init_config()
151 config.add_option("tempdir", "temp", "Directory for storing temporary files");
152 config.add_option("staticlibs", "local", "Use static libraries");
154 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
155 config.add_option("with_"+i->name, i->def_value, i->descr);
157 for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
159 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
160 if(bpkg && bpkg->get_need_path())
161 config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
165 void SourcePackage::create_build_info()
168 build_info.update_from(build_type->get_build_info());
170 // XXX Currently, a package-specific settings will override cmdline. This might or might not be desirable.
171 const StringList &warnings = builder.get_warnings();
172 build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
174 build_info.incpath.push_back((builder.get_prefix()/"include").str());
175 build_info.libpath.push_back((builder.get_prefix()/"lib").str());
177 bool export_paths = false;
178 for(ComponentList::const_iterator i=components.begin(); (!export_paths && i!=components.end()); ++i)
179 export_paths = (i->get_type()==Component::LIBRARY);
183 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
184 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
187 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
188 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
189 build_info.defines["WITH_"+toupper(i->name)] = "1";
191 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
193 build_info.update_from(i->get_build_info());
195 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
197 i->create_build_info();
198 if(i->get_type()==Component::LIBRARY)
199 export_binfo.libs.push_back(i->get_name());
203 void SourcePackage::create_targets()
205 bool pc_needed = false;
206 for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
209 if(i->get_type()==Component::LIBRARY)
215 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
216 builder.get_target("install")->add_depend(*builder.get_toolchain().get_tool("CP").create_target(*pc));
220 void SourcePackage::save_caches()
227 SourcePackage::Loader::Loader(SourcePackage &p):
228 DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
230 add("version", &SourcePackage::version);
231 add("description", &SourcePackage::description);
232 add("build_info", &Loader::build_info);
233 add("feature", &Loader::feature);
234 add("if", &Loader::condition);
235 add("program", &Loader::component<Component::PROGRAM>);
236 add("library", &Loader::component<Component::LIBRARY>);
237 add("module", &Loader::component<Component::MODULE>);
238 add("headers", &Loader::headers);
239 add("install", &Loader::component<Component::INSTALL>);
240 add("datafile", &Loader::component<Component::DATAFILE>);
241 add("tarball", &Loader::tarball);
242 add("tar_file", &Loader::tar_file);
245 void SourcePackage::Loader::finish()
247 obj.components.sort(component_sort);
249 for(map<string, string>::const_iterator i=install_map.begin(); i!=install_map.end(); ++i)
251 for(ComponentList::iterator j=obj.components.begin(); j!=obj.components.end(); ++j)
253 const StringList &sources = j->get_sources();
254 for(StringList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
256 if(!i->first.compare(0, k->size(), *k))
258 const_cast<InstallMap &>(j->get_install_map()).add_mapping(obj.source/i->first, i->second);
265 void SourcePackage::Loader::feature(const string &n, const string &d)
269 feat.def_value = "no";
271 obj.features.push_back(feat);
274 void SourcePackage::Loader::condition(const string &c)
276 Condition cond(obj, c);
278 obj.conditions.push_back(cond);
281 template<Component::Type t>
282 void SourcePackage::Loader::component(const string &n)
284 Component comp(obj, t, n);
286 obj.components.push_back(comp);
289 void SourcePackage::Loader::build_info()
291 load_sub(obj.build_info);
294 void SourcePackage::Loader::headers(const string &n)
296 IO::print("%s: Note: headers components are deprecated\n", get_source());
297 Component comp(obj, Component::LIBRARY, n);
299 const StringList &sources = comp.get_sources();
300 for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
301 install_map[*i] = "include/"+comp.get_name();
304 void SourcePackage::Loader::tarball(const string &n)
308 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
309 if(i->get_type()==Component::TARBALL && i->get_name()==n)
314 Component trbl(obj, Component::TARBALL, n);
319 void SourcePackage::Loader::tar_file(const string &f)
321 IO::print("%s: Note: tar_file is deprecated\n", get_source());
322 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
323 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
324 const_cast<StringList &>(i->get_sources()).push_back((obj.source/f).str());