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 "sourcegenerator.h"
12 #include "sourcepackage.h"
19 bool component_sort(const Component &c1, const Component &c2)
20 { return c1.get_type()<c2.get_type(); }
25 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &f):
27 source_dir(FS::dirname(f)),
34 build_file = builder.get_vfs().get_target(f);
36 build_file = new File(builder, *this, f);
37 components.push_back(Component(*this, Component::TARBALL, "@src"));
38 source_tarball = &components.back();
41 FS::Path SourcePackage::get_temp_directory() const
43 string subdir = builder.get_current_arch().get_name();
47 subdir += build_type->get_name();
50 const FS::Path &temp = builder.get_temp_directory();
51 if(temp.is_absolute())
52 return temp/name/subdir;
54 return source_dir/temp/subdir;
57 FS::Path SourcePackage::get_output_directory() const
59 const Architecture &arch = builder.get_current_arch();
63 return source_dir/arch.get_name();
66 bool SourcePackage::match_feature(const string &cond) const
68 string::size_type equals = cond.find('=');
69 if(equals!=string::npos)
72 throw invalid_argument("SourcePackage::match_feature");
73 bool negate = cond[equals-1]=='!';
74 string feat = cond.substr(0, equals-negate);
75 string value = config.get_option("with_"+feat).value;
76 return (value==cond.substr(equals+1))!=negate;
80 bool negate = (cond[0]=='!');
81 string feat = cond.substr(negate);
82 string value = config.get_option("with_"+feat).value;
83 return lexical_cast<bool>(value)!=negate;
87 void SourcePackage::set_build_type(const BuildType &t)
92 void SourcePackage::do_prepare()
94 BuildInfo final_build_info;
97 final_build_info.update_from(build_type->get_build_info());
99 final_build_info.update_from(build_info);
100 build_info = final_build_info;
102 build_info.incpath.push_back((builder.get_prefix()/"include").str());
103 build_info.libpath.push_back((builder.get_prefix()/"lib").str());
105 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
107 string ident = "WITH_"+toupper(i->name);
108 string value = config.get_option("with_"+i->name).value;
109 if(!i->choices.empty())
110 build_info.defines[ident] = value;
111 else if(lexical_cast<bool>(value))
112 build_info.defines[ident] = "1";
115 bool export_paths = false;
116 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
119 i->create_build_info();
121 if(i->get_type()==Component::LIBRARY)
123 export_binfo.libs.push_back(i->get_name());
130 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
131 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
136 bool pc_needed = false;
137 for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
140 if(i->get_type()==Component::LIBRARY)
146 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
147 builder.get_build_graph().get_target("install")->add_dependency(*builder.get_toolchain().get_tool("CP").create_target(*pc));
151 void SourcePackage::save_caches()
158 SourcePackage::Loader::Loader(SourcePackage &p):
159 DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>(p)
164 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions &o):
165 DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>(p)
170 void SourcePackage::Loader::init(const Config::InputOptions *o)
173 add("description", &SourcePackage::description);
174 add("build_info", &Loader::build_info);
175 add("feature", &Loader::feature);
176 add("generate", &Loader::generate);
177 add("if_feature", &Loader::if_feature);
178 add("program", &Loader::component<Component::PROGRAM>);
179 add("library", &Loader::component<Component::LIBRARY>);
180 add("module", &Loader::component<Component::MODULE>);
181 add("install", &Loader::component<Component::INSTALL>);
182 add("interface_version", &Loader::interface_version);
183 add("datapack", &Loader::component<Component::DATAPACK>);
184 add("source_tarball", &Loader::source_tarball);
185 add("tarball", &Loader::tarball);
186 add("version", &Loader::version);
189 void SourcePackage::Loader::finish()
191 obj.components.sort(component_sort);
194 void SourcePackage::Loader::feature(const string &n, const string &d)
197 feat.description = d;
199 obj.features.push_back(feat);
201 const Config::Option &opt = obj.config.add_option(feat);
204 Config::InputOptions::const_iterator i = options->find(opt.name);
205 if(i!=options->end())
206 obj.config.set_option(opt.name, i->second);
210 template<Component::Type t>
211 void SourcePackage::Loader::component(const string &n)
213 Component comp(obj, t, n);
215 obj.components.push_back(comp);
218 void SourcePackage::Loader::build_info()
220 load_sub(obj.build_info);
223 void SourcePackage::Loader::generate(const string &tag)
225 SourceGenerator *gen = new SourceGenerator(obj.builder, obj, tag);
227 obj.local_tools.add_tool(gen);
230 void SourcePackage::Loader::if_feature(const string &cond)
232 bool match = obj.match_feature(cond);
233 obj.builder.get_logger().log("configure", format("%s: feature %s %smatched", obj.name, cond, (match ? "" : "not ")));
235 load_sub_with(*this);
238 void SourcePackage::Loader::interface_version(const string &v)
240 obj.interface_version = v;
241 if(obj.version.empty())
245 void SourcePackage::Loader::source_tarball()
247 load_sub(*obj.source_tarball);
250 void SourcePackage::Loader::tarball(const string &n)
252 Component trbl(obj, Component::TARBALL, n);
256 void SourcePackage::Loader::version(const string &v)
260 string::size_type i = 0;
261 for(unsigned dots=0; i<obj.version.size(); ++i)
262 if(obj.version[i]=='.' && ++dots>=2)
264 obj.interface_version = obj.version.substr(0, i);