3 #include <msp/core/maputils.h>
4 #include <msp/fs/utils.h>
5 #include <msp/io/print.h>
6 #include <msp/strings/lexicalcast.h>
7 #include <msp/strings/utils.h>
8 #include "binarycomponent.h"
9 #include "binarypackage.h"
10 #include "booleanevaluator.h"
12 #include "datapackcomponent.h"
14 #include "installcomponent.h"
15 #include "pkgconfigfile.h"
16 #include "tarballcomponent.h"
18 #include "sourcegenerator.h"
19 #include "sourcepackage.h"
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 source_tarball = new TarballComponent(*this, "@src");
37 components.push_back(source_tarball);
40 SourcePackage::~SourcePackage()
42 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
46 FS::Path SourcePackage::get_temp_directory() const
48 string subdir = builder.get_current_arch().get_name();
52 subdir += build_type->get_name();
55 const FS::Path &temp = builder.get_temp_directory();
56 if(temp.is_absolute())
57 return temp/name/subdir;
59 return source_dir/temp/subdir;
62 FS::Path SourcePackage::get_output_directory() const
64 const Architecture &arch = builder.get_current_arch();
68 return source_dir/arch.get_name();
71 const Component &SourcePackage::get_component(const string &n) const
73 for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
74 if((*i)->get_name()==n)
79 bool SourcePackage::match_feature(const string &feat, const string *comp) const
81 string value = config.get_option("with_"+feat).value;
85 return lexical_cast<bool>(value);
88 void SourcePackage::set_build_type(const BuildType &t)
93 void SourcePackage::do_prepare()
95 BuildInfo final_build_info;
98 final_build_info.update_from(build_type->get_build_info());
100 final_build_info.update_from(build_info);
101 build_info = final_build_info;
103 build_info.incpath.push_back((builder.get_prefix()/"include").str());
104 build_info.libpath.push_back((builder.get_prefix()/"lib").str());
106 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
108 string ident = "WITH_"+toupper(i->name);
109 string value = config.get_option("with_"+i->name).value;
110 if(!i->choices.empty())
111 build_info.defines[ident] = value;
112 else if(lexical_cast<bool>(value))
113 build_info.defines[ident] = "1";
116 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
119 (*i)->create_build_info();
121 (*i)->update_exported_build_info(export_binfo);
126 for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
127 (*i)->create_targets();
129 if(!export_binfo.libs.empty())
131 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
132 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
134 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
135 builder.get_build_graph().get_target("install")->add_dependency(*builder.get_toolchain().get_tool("CP").create_target(*pc));
139 void SourcePackage::save_caches()
146 SourcePackage::Loader::Loader(SourcePackage &p):
147 DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>(p)
152 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions &o):
153 DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>(p)
158 void SourcePackage::Loader::init(const Config::InputOptions *o)
161 add("description", &SourcePackage::description);
162 add("build_info", &Loader::build_info);
163 add("feature", &Loader::feature);
164 add("generate", &Loader::generate);
165 add("if_feature", &Loader::if_feature);
166 add("program", &Loader::component_arg<BinaryComponent, BinaryComponent::Type, BinaryComponent::PROGRAM>);
167 add("library", &Loader::component_arg<BinaryComponent, BinaryComponent::Type, BinaryComponent::LIBRARY>);
168 add("module", &Loader::component_arg<BinaryComponent, BinaryComponent::Type, BinaryComponent::MODULE>);
169 add("install", &Loader::component<InstallComponent>);
170 add("interface_version", &Loader::interface_version);
171 add("datapack", &Loader::component<DataPackComponent>);
172 add("source_tarball", &Loader::source_tarball);
173 add("tarball", &Loader::tarball);
174 add("version", &Loader::version);
177 void SourcePackage::Loader::finish()
179 /* Make sure the source tarball is last in the list so targets from all
180 other components wil be created first */
181 ComponentList::iterator i = find(obj.components.begin(), obj.components.end(), obj.source_tarball);
182 if(i!=obj.components.end())
183 obj.components.splice(obj.components.end(), obj.components, i);
186 void SourcePackage::Loader::feature(const string &n, const string &d)
189 feat.description = d;
191 obj.features.push_back(feat);
193 const Config::Option &opt = obj.config.add_option(feat);
196 Config::InputOptions::const_iterator i = options->find(opt.name);
197 if(i!=options->end())
198 obj.config.set_option(opt.name, i->second);
203 void SourcePackage::Loader::component(const string &n)
205 C *comp = new C(obj, n);
207 obj.components.push_back(comp);
210 template<typename C, typename A, A a>
211 void SourcePackage::Loader::component_arg(const string &n)
213 C *comp = new C(obj, n, a);
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 BooleanEvaluator eval(sigc::mem_fun(&obj, &SourcePackage::match_feature));
233 bool match = eval.evaluate(cond);
234 obj.builder.get_logger().log("configure", format("%s: feature %s %smatched", obj.name, cond, (match ? "" : "not ")));
236 load_sub_with(*this);
239 void SourcePackage::Loader::interface_version(const string &v)
241 obj.interface_version = v;
242 if(obj.version.empty())
246 void SourcePackage::Loader::source_tarball()
248 load_sub(*obj.source_tarball);
251 void SourcePackage::Loader::tarball(const string &n)
253 TarballComponent trbl(obj, n);
257 void SourcePackage::Loader::version(const string &v)
261 string::size_type i = 0;
262 for(unsigned dots=0; i<obj.version.size(); ++i)
263 if(obj.version[i]=='.' && ++dots>=2)
265 obj.interface_version = obj.version.substr(0, i);