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):
32 components.push_back(Component(*this, Component::TARBALL, "@src"));
35 void SourcePackage::set_build_type(const BuildType &t)
40 FS::Path SourcePackage::get_temp_dir() const
42 string subdir = builder.get_current_arch().get_name();
46 subdir += build_type->get_name();
49 const FS::Path &temp = builder.get_temp_directory();
50 if(temp.is_absolute())
51 return temp/name/subdir;
53 return source/temp/subdir;
56 FS::Path SourcePackage::get_out_dir() const
58 const Architecture &arch = builder.get_current_arch();
59 string detail = (build_type ? build_type->get_name() : string());
63 return source/arch.get_name()/detail;
66 string SourcePackage::expand_string(const string &str) const
69 string::size_type dollar = 0;
71 while((dollar = result.find('$'))!=string::npos)
74 throw bad_expansion("nested too deep");
76 string::size_type end;
78 if(dollar+1<result.size() && result[dollar+1]=='{')
80 end = result.find('}', dollar+2);
82 throw bad_expansion("unterminated variable reference");
83 var = result.substr(dollar+2, end-dollar-2);
88 for(end=dollar+1; (isalnum(result[end]) || result[end]=='_'); ++end) ;
89 var = result.substr(dollar+1, end-dollar-1);
93 if(config.is_option(var))
94 value = config.get_option(var).value;
96 value = builder.get_current_arch().get_name();
97 else if(var=="system")
98 value = builder.get_current_arch().get_system();
99 else if(const char *ptr = getenv(var.c_str()))
102 result.replace(dollar, end-dollar, value);
110 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
116 if(flag && config.update(opts))
117 builder.get_logger().log("configure", format("Configuration of %s changed", name));
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(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
131 i->configure(opts, flag);
134 void SourcePackage::init_config()
136 for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
138 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
139 if(bpkg && bpkg->get_need_path())
140 config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
144 void SourcePackage::create_build_info()
147 build_info.update_from(build_type->get_build_info());
149 // XXX Currently, a package-specific settings will override cmdline. This might or might not be desirable.
150 const StringList &warnings = builder.get_warnings();
151 build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
153 build_info.incpath.push_back((builder.get_prefix()/"include").str());
154 build_info.libpath.push_back((builder.get_prefix()/"lib").str());
156 bool export_paths = false;
157 for(ComponentList::const_iterator i=components.begin(); (!export_paths && i!=components.end()); ++i)
158 export_paths = (i->get_type()==Component::LIBRARY);
162 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
163 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
166 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
167 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
168 build_info.defines["WITH_"+toupper(i->name)] = "1";
170 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
172 i->create_build_info();
173 if(i->get_type()==Component::LIBRARY)
174 export_binfo.libs.push_back(i->get_name());
178 void SourcePackage::create_targets()
180 bool pc_needed = false;
181 for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
184 if(i->get_type()==Component::LIBRARY)
190 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
191 builder.get_target("install")->add_depend(*builder.get_toolchain().get_tool("CP").create_target(*pc));
195 void SourcePackage::save_caches()
202 SourcePackage::Loader::Loader(SourcePackage &p):
203 DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
208 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions &o):
209 DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
214 void SourcePackage::Loader::init(const Config::InputOptions *o)
217 add("version", &SourcePackage::version);
218 add("description", &SourcePackage::description);
219 add("build_info", &Loader::build_info);
220 add("feature", &Loader::feature);
221 add("if", &Loader::condition);
222 add("if_arch", &Loader::if_arch);
223 add("if_feat", &Loader::if_feature);
224 add("program", &Loader::component<Component::PROGRAM>);
225 add("library", &Loader::component<Component::LIBRARY>);
226 add("module", &Loader::component<Component::MODULE>);
227 add("headers", &Loader::headers);
228 add("install", &Loader::component<Component::INSTALL>);
229 add("datafile", &Loader::component<Component::DATAFILE>);
230 add("tarball", &Loader::tarball);
231 add("tar_file", &Loader::tar_file);
234 void SourcePackage::Loader::finish()
236 obj.components.sort(component_sort);
238 for(map<string, string>::const_iterator i=install_map.begin(); i!=install_map.end(); ++i)
240 for(ComponentList::iterator j=obj.components.begin(); j!=obj.components.end(); ++j)
242 const StringList &sources = j->get_sources();
243 for(StringList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
245 if(!i->first.compare(0, k->size(), *k))
247 const_cast<InstallMap &>(j->get_install_map()).add_mapping(obj.source/i->first, i->second);
254 void SourcePackage::Loader::feature(const string &n, const string &d)
258 feat.def_value = "no";
260 obj.features.push_back(feat);
261 string config_key = "with_"+feat.name;
262 obj.config.add_option(config_key, feat.def_value, feat.descr);
265 Config::InputOptions::const_iterator i = options->find(config_key);
266 if(i!=options->end())
267 obj.config.set_option(config_key, i->second);
271 void SourcePackage::Loader::condition(const string &c)
273 IO::print("%s: Note: Old-style conditions are deprecated\n", get_source());
274 Condition cond(obj, c);
276 load_sub_with(*this);
279 template<Component::Type t>
280 void SourcePackage::Loader::component(const string &n)
282 Component comp(obj, t, n);
284 obj.components.push_back(comp);
287 void SourcePackage::Loader::build_info()
289 load_sub(obj.build_info);
292 void SourcePackage::Loader::headers(const string &n)
294 IO::print("%s: Note: headers components are deprecated\n", get_source());
295 Component comp(obj, Component::LIBRARY, n);
297 const StringList &sources = comp.get_sources();
298 for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
299 install_map[*i] = "include/"+comp.get_name();
302 void SourcePackage::Loader::if_arch(const string &cond)
304 const Architecture &arch = obj.builder.get_current_arch();
305 bool negate = (cond[0]=='!');
306 bool match = (arch.match_name(cond.substr(negate))!=negate);
307 obj.builder.get_logger().log("configure", format("%s: arch %s %smatched", obj.name, cond, (match ? "" : "not ")));
309 load_sub_with(*this);
312 void SourcePackage::Loader::if_feature(const string &cond)
315 string::size_type equals = cond.find('=');
316 if(equals!=string::npos)
319 error("No feature name specified");
320 bool negate = cond[equals-1]=='!';
321 string name = cond.substr(0, equals-negate);
322 string value = obj.config.get_option("with_"+name).value;
323 match = (value==cond.substr(equals+1))!=negate;
324 value = cond.substr(equals+1);
328 bool negate = (cond[0]=='!');
329 string name = cond.substr(negate);
330 string value = obj.config.get_option("with_"+name).value;
331 match = lexical_cast<bool>(value)!=negate;
333 obj.builder.get_logger().log("configure", format("%s: feature %s %smatched", obj.name, cond, (match ? "" : "not ")));
335 load_sub_with(*this);
338 void SourcePackage::Loader::tarball(const string &n)
342 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
343 if(i->get_type()==Component::TARBALL && i->get_name()==n)
348 Component trbl(obj, Component::TARBALL, n);
353 void SourcePackage::Loader::tar_file(const string &f)
355 IO::print("%s: Note: tar_file is deprecated\n", get_source());
356 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
357 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
358 const_cast<StringList &>(i->get_sources()).push_back((obj.source/f).str());