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 void SourcePackage::create_build_info()
69 build_info.update_from(build_type->get_build_info());
71 // XXX Currently, a package-specific settings will override cmdline. This might or might not be desirable.
72 const StringList &warnings = builder.get_warnings();
73 build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
75 build_info.incpath.push_back((builder.get_prefix()/"include").str());
76 build_info.libpath.push_back((builder.get_prefix()/"lib").str());
78 bool export_paths = false;
79 for(ComponentList::const_iterator i=components.begin(); (!export_paths && i!=components.end()); ++i)
80 export_paths = (i->get_type()==Component::LIBRARY);
84 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
85 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
88 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
89 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
90 build_info.defines["WITH_"+toupper(i->name)] = "1";
92 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
95 i->create_build_info();
96 if(i->get_type()==Component::LIBRARY)
97 export_binfo.libs.push_back(i->get_name());
101 void SourcePackage::create_targets()
105 bool pc_needed = false;
106 for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
109 if(i->get_type()==Component::LIBRARY)
115 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
116 builder.get_target("install")->add_depend(*builder.get_toolchain().get_tool("CP").create_target(*pc));
120 void SourcePackage::save_caches()
127 SourcePackage::Loader::Loader(SourcePackage &p):
128 DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
133 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions &o):
134 DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
139 void SourcePackage::Loader::init(const Config::InputOptions *o)
142 add("version", &SourcePackage::version);
143 add("description", &SourcePackage::description);
144 add("build_info", &Loader::build_info);
145 add("feature", &Loader::feature);
146 add("if", &Loader::condition);
147 add("if_arch", &Loader::if_arch);
148 add("if_feat", &Loader::if_feature);
149 add("program", &Loader::component<Component::PROGRAM>);
150 add("library", &Loader::component<Component::LIBRARY>);
151 add("module", &Loader::component<Component::MODULE>);
152 add("headers", &Loader::headers);
153 add("install", &Loader::component<Component::INSTALL>);
154 add("datafile", &Loader::component<Component::DATAFILE>);
155 add("tarball", &Loader::tarball);
156 add("tar_file", &Loader::tar_file);
159 void SourcePackage::Loader::finish()
161 obj.components.sort(component_sort);
163 for(map<string, string>::const_iterator i=install_map.begin(); i!=install_map.end(); ++i)
165 for(ComponentList::iterator j=obj.components.begin(); j!=obj.components.end(); ++j)
167 const StringList &sources = j->get_sources();
168 for(StringList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
170 if(!i->first.compare(0, k->size(), *k))
172 const_cast<InstallMap &>(j->get_install_map()).add_mapping(obj.source/i->first, i->second);
179 void SourcePackage::Loader::feature(const string &n, const string &d)
183 feat.def_value = "no";
185 obj.features.push_back(feat);
186 string config_key = "with_"+feat.name;
187 obj.config.add_option(config_key, feat.def_value, feat.descr);
190 Config::InputOptions::const_iterator i = options->find(config_key);
191 if(i!=options->end())
192 obj.config.set_option(config_key, i->second);
196 void SourcePackage::Loader::condition(const string &c)
198 IO::print("%s: Note: Old-style conditions are deprecated\n", get_source());
199 Condition cond(obj, c);
201 load_sub_with(*this);
204 template<Component::Type t>
205 void SourcePackage::Loader::component(const string &n)
207 Component comp(obj, t, n);
209 obj.components.push_back(comp);
212 void SourcePackage::Loader::build_info()
214 load_sub(obj.build_info);
217 void SourcePackage::Loader::headers(const string &n)
219 IO::print("%s: Note: headers components are deprecated\n", get_source());
220 Component comp(obj, Component::LIBRARY, n);
222 const StringList &sources = comp.get_sources();
223 for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
224 install_map[*i] = "include/"+comp.get_name();
227 void SourcePackage::Loader::if_arch(const string &cond)
229 const Architecture &arch = obj.builder.get_current_arch();
230 bool negate = (cond[0]=='!');
231 bool match = (arch.match_name(cond.substr(negate))!=negate);
232 obj.builder.get_logger().log("configure", format("%s: arch %s %smatched", obj.name, cond, (match ? "" : "not ")));
234 load_sub_with(*this);
237 void SourcePackage::Loader::if_feature(const string &cond)
240 string::size_type equals = cond.find('=');
241 if(equals!=string::npos)
244 error("No feature name specified");
245 bool negate = cond[equals-1]=='!';
246 string name = cond.substr(0, equals-negate);
247 string value = obj.config.get_option("with_"+name).value;
248 match = (value==cond.substr(equals+1))!=negate;
249 value = cond.substr(equals+1);
253 bool negate = (cond[0]=='!');
254 string name = cond.substr(negate);
255 string value = obj.config.get_option("with_"+name).value;
256 match = lexical_cast<bool>(value)!=negate;
258 obj.builder.get_logger().log("configure", format("%s: feature %s %smatched", obj.name, cond, (match ? "" : "not ")));
260 load_sub_with(*this);
263 void SourcePackage::Loader::tarball(const string &n)
267 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
268 if(i->get_type()==Component::TARBALL && i->get_name()==n)
273 Component trbl(obj, Component::TARBALL, n);
278 void SourcePackage::Loader::tar_file(const string &f)
280 IO::print("%s: Note: tar_file is deprecated\n", get_source());
281 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
282 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
283 const_cast<StringList &>(i->get_sources()).push_back((obj.source/f).str());