2 #include <msp/core/algorithm.h>
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 "android/androidapplicationcomponent.h"
9 #include "binarycomponent.h"
10 #include "binarypackage.h"
12 #include "datafile/datapackcomponent.h"
14 #include "installcomponent.h"
16 #include "sourcearchivecomponent.h"
17 #include "sourcegenerator.h"
18 #include "sourcepackage.h"
24 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &f):
26 source_dir(FS::dirname(f)),
32 build_file = builder.get_vfs().get_target(f);
34 build_file = new File(builder, *this, f);
35 source_archive = new SourceArchiveComponent(*this);
36 components.push_back(source_archive);
39 SourcePackage::~SourcePackage()
41 for(Component *c: components)
45 FS::Path SourcePackage::get_temp_directory() const
47 string subdir = builder.get_current_arch().get_name();
51 subdir += build_type->get_name();
54 const FS::Path &temp = builder.get_temp_directory();
55 if(temp.is_absolute())
56 return temp/name/subdir;
58 return source_dir/temp/subdir;
61 FS::Path SourcePackage::get_output_directory() const
63 const Architecture &arch = builder.get_current_arch();
67 return source_dir/arch.get_name();
70 const Component &SourcePackage::get_component(const string &n) const
72 auto i = find_if(components, [&n](const Component *c){ return c->get_name()==n; });
73 if(i!=components.end())
78 bool SourcePackage::match_feature(const string &feat, const string *comp) const
80 string value = config.get_option("with_"+feat).value;
84 return lexical_cast<bool>(value);
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(const Feature &f: features)
107 string ident = "WITH_"+toupper(f.name);
108 string value = config.get_option("with_"+f.name).value;
110 if(f.choices.empty())
112 if(!lexical_cast<bool>(value))
117 build_info.defines[ident] = value;
119 export_binfo.defines[ident] = value;
122 for(Component *c: components)
125 c->create_build_info();
127 c->update_exported_build_info(export_binfo);
132 for(Component *c: components)
135 if(!export_binfo.libs.empty())
137 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
138 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
141 export_binfo.standards = build_info.standards;
143 builder.call_plugins([this](const Plugin &p){ p.create_targets(*this); });
146 void SourcePackage::save_caches()
153 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions *o):
154 DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>(p),
155 FeatureConditional(p, p.name),
158 add("android_application", &Loader::component<AndroidApplicationComponent>);
159 add("build_info", &Loader::build_info);
160 add("datapack", &Loader::component<DataPackComponent>);
161 add("description", &SourcePackage::description);
162 add("feature", &Loader::feature);
163 add("generate", &Loader::generate);
164 add("install", &Loader::component<InstallComponent>);
165 add("interface_version", &Loader::interface_version);
166 add("library", &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::LIBRARY);
167 add("module", &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::MODULE);
168 add("program", &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::PROGRAM);
169 add("source_archive", &Loader::source_archive);
170 add("source_tarball", &Loader::source_archive);
171 add("tarball", &Loader::tarball);
172 add("version", &Loader::version);
175 void SourcePackage::Loader::finish()
177 /* Make sure the source tarball is last in the list so targets from all
178 other components wil be created first */
179 auto i = find(obj.components, obj.source_archive);
180 if(i!=obj.components.end())
182 obj.components.erase(i);
183 obj.components.push_back(obj.source_archive);
187 void SourcePackage::Loader::feature(const string &n, const string &d)
190 feat.description = d;
192 obj.features.push_back(feat);
194 const Config::Option &opt = obj.config.add_option(feat);
197 auto i = options->find(opt.name);
198 if(i!=options->end())
199 obj.config.set_option(opt.name, i->second);
203 template<typename C, typename... Args>
204 void SourcePackage::Loader::component(Args... args, const string &n)
206 if(any_of(obj.components.begin(), obj.components.end(), [&n](const Component *c){ return c->get_name()==n; }))
208 C *comp = new C(obj, n, args...);
210 obj.components.push_back(comp);
213 void SourcePackage::Loader::build_info()
215 load_sub(obj.build_info);
218 void SourcePackage::Loader::generate(const string &tag)
220 SourceGenerator *gen = new SourceGenerator(obj.builder, obj, tag);
222 obj.local_tools.add_tool(gen);
225 void SourcePackage::Loader::interface_version(const string &v)
227 obj.interface_version = v;
228 if(obj.version.empty())
232 void SourcePackage::Loader::source_archive()
234 load_sub(*obj.source_archive);
237 void SourcePackage::Loader::tarball(const string &)
239 IO::print("%s: Deprecated tarball component ignored\n", get_source());
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);