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_feature", &Loader::if_feature);
170 add("program", &Loader::component<Component::PROGRAM>);
171 add("library", &Loader::component<Component::LIBRARY>);
172 add("module", &Loader::component<Component::MODULE>);
173 add("install", &Loader::component<Component::INSTALL>);
174 add("interface_version", &Loader::interface_version);
175 add("datapack", &Loader::component<Component::DATAPACK>);
176 add("source_tarball", &Loader::source_tarball);
177 add("tarball", &Loader::tarball);
178 add("version", &Loader::version);
181 void SourcePackage::Loader::finish()
183 obj.components.sort(component_sort);
186 void SourcePackage::Loader::feature(const string &n, const string &d)
190 feat.def_value = "no";
192 obj.features.push_back(feat);
193 string config_key = "with_"+feat.name;
194 obj.config.add_option(config_key, feat.def_value, feat.descr);
197 Config::InputOptions::const_iterator i = options->find(config_key);
198 if(i!=options->end())
199 obj.config.set_option(config_key, i->second);
203 template<Component::Type t>
204 void SourcePackage::Loader::component(const string &n)
206 Component comp(obj, t, n);
208 obj.components.push_back(comp);
211 void SourcePackage::Loader::build_info()
213 load_sub(obj.build_info);
216 void SourcePackage::Loader::if_feature(const string &cond)
218 bool match = obj.match_feature(cond);
219 obj.builder.get_logger().log("configure", format("%s: feature %s %smatched", obj.name, cond, (match ? "" : "not ")));
221 load_sub_with(*this);
224 void SourcePackage::Loader::interface_version(const string &v)
226 obj.interface_version = v;
227 if(obj.version.empty())
231 void SourcePackage::Loader::source_tarball()
233 load_sub(*obj.source_tarball);
236 void SourcePackage::Loader::tarball(const string &n)
238 Component trbl(obj, Component::TARBALL, n);
242 void SourcePackage::Loader::version(const string &v)
246 string::size_type i = 0;
247 for(unsigned dots=0; i<obj.version.size(); ++i)
248 if(obj.version[i]=='.' && ++dots>=2)
250 obj.interface_version = obj.version.substr(0, i);