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_dir/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());
61 return source_dir/detail;
63 return source_dir/arch.get_name()/detail;
66 void SourcePackage::create_build_info()
69 build_info.update_from(build_type->get_build_info());
71 build_info.incpath.push_back((builder.get_prefix()/"include").str());
72 build_info.libpath.push_back((builder.get_prefix()/"lib").str());
74 bool export_paths = false;
75 for(ComponentList::const_iterator i=components.begin(); (!export_paths && i!=components.end()); ++i)
76 export_paths = (i->get_type()==Component::LIBRARY);
80 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
81 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
84 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
85 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
86 build_info.defines["WITH_"+toupper(i->name)] = "1";
88 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
91 i->create_build_info();
92 if(i->get_type()==Component::LIBRARY)
93 export_binfo.libs.push_back(i->get_name());
97 void SourcePackage::create_targets()
101 bool pc_needed = false;
102 for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
105 if(i->get_type()==Component::LIBRARY)
111 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
112 builder.get_target("install")->add_dependency(*builder.get_toolchain().get_tool("CP").create_target(*pc));
116 void SourcePackage::save_caches()
123 SourcePackage::Loader::Loader(SourcePackage &p):
124 DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
129 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions &o):
130 DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
135 void SourcePackage::Loader::init(const Config::InputOptions *o)
138 add("version", &SourcePackage::version);
139 add("description", &SourcePackage::description);
140 add("build_info", &Loader::build_info);
141 add("feature", &Loader::feature);
142 add("if", &Loader::condition);
143 add("if_arch", &Loader::if_arch);
144 add("if_feat", &Loader::if_feature);
145 add("program", &Loader::component<Component::PROGRAM>);
146 add("library", &Loader::component<Component::LIBRARY>);
147 add("module", &Loader::component<Component::MODULE>);
148 add("headers", &Loader::headers);
149 add("install", &Loader::component<Component::INSTALL>);
150 add("datafile", &Loader::component<Component::DATAFILE>);
151 add("tarball", &Loader::tarball);
152 add("tar_file", &Loader::tar_file);
155 void SourcePackage::Loader::finish()
157 obj.components.sort(component_sort);
159 for(map<string, string>::const_iterator i=install_map.begin(); i!=install_map.end(); ++i)
161 for(ComponentList::iterator j=obj.components.begin(); j!=obj.components.end(); ++j)
163 const StringList &sources = j->get_sources();
164 for(StringList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
166 if(!i->first.compare(0, k->size(), *k))
168 const_cast<InstallMap &>(j->get_install_map()).add_mapping(obj.source_dir/i->first, i->second);
175 void SourcePackage::Loader::feature(const string &n, const string &d)
179 feat.def_value = "no";
181 obj.features.push_back(feat);
182 string config_key = "with_"+feat.name;
183 obj.config.add_option(config_key, feat.def_value, feat.descr);
186 Config::InputOptions::const_iterator i = options->find(config_key);
187 if(i!=options->end())
188 obj.config.set_option(config_key, i->second);
192 void SourcePackage::Loader::condition(const string &c)
194 IO::print("%s: Note: Old-style conditions are deprecated\n", get_source());
195 Condition cond(obj, c);
197 load_sub_with(*this);
200 template<Component::Type t>
201 void SourcePackage::Loader::component(const string &n)
203 Component comp(obj, t, n);
205 obj.components.push_back(comp);
208 void SourcePackage::Loader::build_info()
210 load_sub(obj.build_info);
213 void SourcePackage::Loader::headers(const string &n)
215 IO::print("%s: Note: headers components are deprecated\n", get_source());
216 Component comp(obj, Component::LIBRARY, n);
218 const StringList &sources = comp.get_sources();
219 for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
220 install_map[*i] = "include/"+comp.get_name();
223 void SourcePackage::Loader::if_arch(const string &cond)
225 const Architecture &arch = obj.builder.get_current_arch();
226 bool negate = (cond[0]=='!');
227 bool match = (arch.match_name(cond.substr(negate))!=negate);
228 obj.builder.get_logger().log("configure", format("%s: arch %s %smatched", obj.name, cond, (match ? "" : "not ")));
230 load_sub_with(*this);
233 void SourcePackage::Loader::if_feature(const string &cond)
236 string::size_type equals = cond.find('=');
237 if(equals!=string::npos)
240 error("No feature name specified");
241 bool negate = cond[equals-1]=='!';
242 string name = cond.substr(0, equals-negate);
243 string value = obj.config.get_option("with_"+name).value;
244 match = (value==cond.substr(equals+1))!=negate;
245 value = cond.substr(equals+1);
249 bool negate = (cond[0]=='!');
250 string name = cond.substr(negate);
251 string value = obj.config.get_option("with_"+name).value;
252 match = lexical_cast<bool>(value)!=negate;
254 obj.builder.get_logger().log("configure", format("%s: feature %s %smatched", obj.name, cond, (match ? "" : "not ")));
256 load_sub_with(*this);
259 void SourcePackage::Loader::tarball(const string &n)
263 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
264 if(i->get_type()==Component::TARBALL && i->get_name()==n)
269 Component trbl(obj, Component::TARBALL, n);
274 void SourcePackage::Loader::tar_file(const string &f)
276 IO::print("%s: Note: tar_file is deprecated\n", get_source());
277 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
278 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
279 const_cast<StringList &>(i->get_sources()).push_back((obj.source_dir/f).str());