2 #include <msp/fs/utils.h>
3 #include <msp/io/print.h>
4 #include <msp/strings/lexicalcast.h>
5 #include <msp/strings/utils.h>
6 #include "binarypackage.h"
9 #include "pkgconfigfile.h"
11 #include "sourcepackage.h"
18 bool component_sort(const Component &c1, const Component &c2)
19 { return c1.get_type()<c2.get_type(); }
24 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &f):
26 source_dir(FS::dirname(f)),
33 build_file = builder.get_vfs().get_target(f);
35 build_file = new File(builder, *this, f);
36 components.push_back(Component(*this, Component::TARBALL, "@src"));
37 source_tarball = &components.back();
40 FS::Path SourcePackage::get_temp_directory() 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_output_directory() const
58 const Architecture &arch = builder.get_current_arch();
62 return source_dir/arch.get_name();
65 bool SourcePackage::match_feature(const string &cond) const
67 string::size_type equals = cond.find('=');
68 if(equals!=string::npos)
71 throw invalid_argument("SourcePackage::match_feature");
72 bool negate = cond[equals-1]=='!';
73 string feat = cond.substr(0, equals-negate);
74 string value = config.get_option("with_"+feat).value;
75 return (value==cond.substr(equals+1))!=negate;
79 bool negate = (cond[0]=='!');
80 string feat = cond.substr(negate);
81 string value = config.get_option("with_"+feat).value;
82 return lexical_cast<bool>(value)!=negate;
86 void SourcePackage::set_build_type(const BuildType &t)
91 void SourcePackage::do_prepare()
93 BuildInfo final_build_info;
96 final_build_info.update_from(build_type->get_build_info());
98 final_build_info.update_from(build_info);
99 build_info = final_build_info;
101 build_info.incpath.push_back((builder.get_prefix()/"include").str());
102 build_info.libpath.push_back((builder.get_prefix()/"lib").str());
104 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
105 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
106 build_info.defines["WITH_"+toupper(i->name)] = "1";
108 bool export_paths = false;
109 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
112 i->create_build_info();
114 if(i->get_type()==Component::LIBRARY)
116 export_binfo.libs.push_back(i->get_name());
123 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
124 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
129 bool pc_needed = false;
130 for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
133 if(i->get_type()==Component::LIBRARY)
139 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
140 builder.get_build_graph().get_target("install")->add_dependency(*builder.get_toolchain().get_tool("CP").create_target(*pc));
144 void SourcePackage::save_caches()
151 SourcePackage::Loader::Loader(SourcePackage &p):
152 DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>(p)
157 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions &o):
158 DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>(p)
163 void SourcePackage::Loader::init(const Config::InputOptions *o)
166 add("description", &SourcePackage::description);
167 add("build_info", &Loader::build_info);
168 add("feature", &Loader::feature);
169 add("if", &Loader::condition);
170 add("if_feature", &Loader::if_feature);
171 add("program", &Loader::component<Component::PROGRAM>);
172 add("library", &Loader::component<Component::LIBRARY>);
173 add("module", &Loader::component<Component::MODULE>);
174 add("headers", &Loader::headers);
175 add("install", &Loader::component<Component::INSTALL>);
176 add("interface_version", &Loader::interface_version);
177 add("datapack", &Loader::component<Component::DATAPACK>);
178 add("source_tarball", &Loader::source_tarball);
179 add("tarball", &Loader::tarball);
180 add("tar_file", &Loader::tar_file);
181 add("version", &Loader::version);
184 void SourcePackage::Loader::finish()
186 obj.components.sort(component_sort);
188 for(map<string, string>::const_iterator i=install_map.begin(); i!=install_map.end(); ++i)
190 for(ComponentList::iterator j=obj.components.begin(); j!=obj.components.end(); ++j)
192 const Component::SourceList &sources = j->get_sources();
193 for(Component::SourceList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
195 string k_str = k->str();
196 if(!i->first.compare(0, k_str.size(), k_str))
198 const_cast<InstallMap &>(j->get_install_map()).add_mapping(obj.source_dir/i->first, i->second);
205 void SourcePackage::Loader::feature(const string &n, const string &d)
209 feat.def_value = "no";
211 obj.features.push_back(feat);
212 string config_key = "with_"+feat.name;
213 obj.config.add_option(config_key, feat.def_value, feat.descr);
216 Config::InputOptions::const_iterator i = options->find(config_key);
217 if(i!=options->end())
218 obj.config.set_option(config_key, i->second);
222 void SourcePackage::Loader::condition(const string &c)
224 IO::print("%s: Note: Old-style conditions are deprecated\n", get_source());
225 Condition cond(obj, c);
227 load_sub_with(*this);
230 template<Component::Type t>
231 void SourcePackage::Loader::component(const string &n)
233 Component comp(obj, t, n);
235 obj.components.push_back(comp);
238 void SourcePackage::Loader::build_info()
240 load_sub(obj.build_info);
243 void SourcePackage::Loader::headers(const string &n)
245 IO::print("%s: Note: headers components are deprecated\n", get_source());
246 Component comp(obj, Component::LIBRARY, n);
248 const Component::SourceList &sources = comp.get_sources();
249 for(Component::SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
250 install_map[i->str()] = "include/"+comp.get_name();
253 void SourcePackage::Loader::if_feature(const string &cond)
255 bool match = obj.match_feature(cond);
256 obj.builder.get_logger().log("configure", format("%s: feature %s %smatched", obj.name, cond, (match ? "" : "not ")));
258 load_sub_with(*this);
261 void SourcePackage::Loader::interface_version(const string &v)
263 obj.interface_version = v;
264 if(obj.version.empty())
268 void SourcePackage::Loader::source_tarball()
270 load_sub(*obj.source_tarball);
273 void SourcePackage::Loader::tarball(const string &n)
277 IO::print("%s: Note: Use source_tarball instead of tarball \"@src\"\n", get_source());
278 load_sub(*obj.source_tarball);
282 Component trbl(obj, Component::TARBALL, n);
287 void SourcePackage::Loader::tar_file(const string &f)
289 IO::print("%s: Note: tar_file is deprecated\n", get_source());
290 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
291 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
292 const_cast<Component::SourceList &>(i->get_sources()).push_back((obj.source_dir/f).str());
295 void SourcePackage::Loader::version(const string &v)
299 string::size_type i = 0;
300 for(unsigned dots=0; i<obj.version.size(); ++i)
301 if(obj.version[i]=='.' && ++dots>=2)
303 obj.interface_version = obj.version.substr(0, i);