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 "binarycomponent.h"
9 #include "binarypackage.h"
12 #include "installcomponent.h"
14 #include "sourcearchivecomponent.h"
15 #include "sourcegenerator.h"
16 #include "sourcepackage.h"
22 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &f):
24 source_dir(FS::dirname(f)),
30 build_file = builder.get_vfs().get_target(f);
32 build_file = new File(builder, *this, f);
33 source_archive = new SourceArchiveComponent(*this);
34 components.push_back(source_archive);
37 SourcePackage::~SourcePackage()
39 for(Component *c: components)
43 FS::Path SourcePackage::get_temp_directory() const
45 string subdir = builder.get_current_arch().get_name();
49 subdir += build_type->get_name();
52 const FS::Path &temp = builder.get_temp_directory();
53 if(temp.is_absolute())
54 return temp/name/subdir;
56 return source_dir/temp/subdir;
59 FS::Path SourcePackage::get_output_directory() const
61 const Architecture &arch = builder.get_current_arch();
65 return source_dir/arch.get_name();
68 FS::Path SourcePackage::get_staging_directory() const
70 return get_temp_directory()/"staging";
73 const Component &SourcePackage::get_component(const string &n) const
75 auto i = find_if(components, [&n](const Component *c){ return c->get_name()==n; });
76 if(i!=components.end())
81 bool SourcePackage::match_feature(const string &feat, const string *comp) const
83 string value = config.get_option("with_"+feat).value;
87 return lexical_cast<bool>(value);
90 void SourcePackage::set_build_type(const BuildType &t)
95 void SourcePackage::do_prepare()
97 BuildInfo final_build_info;
100 final_build_info.update_from(build_type->get_build_info());
102 final_build_info.update_from(build_info);
103 build_info = final_build_info;
105 build_info.incpath.push_back(get_staging_directory()/"include");
106 build_info.incpath.push_back(builder.get_prefix()/"include");
107 build_info.libpath.push_back(builder.get_prefix()/"lib");
109 for(const Feature &f: features)
111 string ident = "WITH_"+toupper(f.name);
112 string value = config.get_option("with_"+f.name).value;
114 if(f.choices.empty())
116 if(!lexical_cast<bool>(value))
121 build_info.defines[ident] = value;
123 export_binfo.defines[ident] = value;
126 for(Component *c: components)
129 c->create_build_info();
131 c->update_exported_build_info(export_binfo);
136 for(Component *c: components)
139 if(!export_binfo.libs.empty())
141 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
142 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
145 export_binfo.standards = build_info.standards;
147 builder.call_plugins([this](const Plugin &p){ p.create_targets(*this); });
150 void SourcePackage::save_caches()
157 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions *o):
158 DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>(p),
159 FeatureConditional(p, p.name),
162 add("build_info", &Loader::build_info);
163 add("description", &SourcePackage::description);
164 add("export", &Loader::exported);
165 add("feature", &Loader::feature);
166 add("generate", &Loader::generate);
167 add("install", &Loader::component<InstallComponent>);
168 add("interface_version", &Loader::interface_version);
169 add("library", &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::LIBRARY);
170 add("module", &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::MODULE);
171 add("program", &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::PROGRAM);
172 add("source_archive", &Loader::source_archive);
173 add("source_tarball", &Loader::source_archive);
174 add("tarball", &Loader::tarball);
175 add("version", &Loader::version);
177 p.builder.get_component_registry().invoke_all(*this);
180 void SourcePackage::Loader::finish()
182 /* Make sure the source tarball is last in the list so targets from all
183 other components wil be created first */
184 auto i = find(obj.components, obj.source_archive);
185 if(i!=obj.components.end())
187 obj.components.erase(i);
188 obj.components.push_back(obj.source_archive);
192 void SourcePackage::Loader::exported()
194 ExportLoader ldr(obj);
198 void SourcePackage::Loader::feature(const string &n, const string &d)
201 feat.description = d;
203 obj.features.push_back(feat);
205 const Config::Option &opt = obj.config.add_option(feat);
208 auto i = options->find(opt.name);
209 if(i!=options->end())
210 obj.config.set_option(opt.name, i->second);
214 void SourcePackage::Loader::build_info()
216 load_sub(obj.build_info);
219 void SourcePackage::Loader::generate(const string &tag)
221 SourceGenerator *gen = new SourceGenerator(obj.builder, obj, tag);
223 obj.local_tools.add_tool(gen);
226 void SourcePackage::Loader::interface_version(const string &v)
228 obj.interface_version = v;
229 if(obj.version.empty())
233 void SourcePackage::Loader::source_archive()
235 load_sub(*obj.source_archive);
238 void SourcePackage::Loader::tarball(const string &)
240 IO::print("%s: Deprecated tarball component ignored\n", get_source());
243 void SourcePackage::Loader::version(const string &v)
247 string::size_type i = 0;
248 for(unsigned dots=0; i<obj.version.size(); ++i)
249 if(obj.version[i]=='.' && ++dots>=2)
251 obj.interface_version = obj.version.substr(0, i);
255 SourcePackage::ExportLoader::ExportLoader(SourcePackage &p):
256 ObjectLoader<SourcePackage>(p)
258 add("build_info", &ExportLoader::build_info);
261 void SourcePackage::ExportLoader::build_info()
265 obj.build_info.update_from(bi);
266 obj.export_binfo.update_from(bi);