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 const Component &SourcePackage::get_component(const string &n) const
70 auto i = find_if(components, [&n](const Component *c){ return c->get_name()==n; });
71 if(i!=components.end())
76 bool SourcePackage::match_feature(const string &feat, const string *comp) const
78 string value = config.get_option("with_"+feat).value;
82 return lexical_cast<bool>(value);
85 void SourcePackage::set_build_type(const BuildType &t)
90 void SourcePackage::do_prepare()
92 BuildInfo final_build_info;
95 final_build_info.update_from(build_type->get_build_info());
97 final_build_info.update_from(build_info);
98 build_info = final_build_info;
100 build_info.incpath.push_back((builder.get_prefix()/"include").str());
101 build_info.libpath.push_back((builder.get_prefix()/"lib").str());
103 for(const Feature &f: features)
105 string ident = "WITH_"+toupper(f.name);
106 string value = config.get_option("with_"+f.name).value;
108 if(f.choices.empty())
110 if(!lexical_cast<bool>(value))
115 build_info.defines[ident] = value;
117 export_binfo.defines[ident] = value;
120 for(Component *c: components)
123 c->create_build_info();
125 c->update_exported_build_info(export_binfo);
130 for(Component *c: components)
133 if(!export_binfo.libs.empty())
135 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
136 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
139 export_binfo.standards = build_info.standards;
141 builder.call_plugins([this](const Plugin &p){ p.create_targets(*this); });
144 void SourcePackage::save_caches()
151 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions *o):
152 DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>(p),
153 FeatureConditional(p, p.name),
156 add("build_info", &Loader::build_info);
157 add("description", &SourcePackage::description);
158 add("export", &Loader::exported);
159 add("feature", &Loader::feature);
160 add("generate", &Loader::generate);
161 add("install", &Loader::component<InstallComponent>);
162 add("interface_version", &Loader::interface_version);
163 add("library", &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::LIBRARY);
164 add("module", &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::MODULE);
165 add("program", &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::PROGRAM);
166 add("source_archive", &Loader::source_archive);
167 add("source_tarball", &Loader::source_archive);
168 add("tarball", &Loader::tarball);
169 add("version", &Loader::version);
171 p.builder.get_component_registry().invoke_all(*this);
174 void SourcePackage::Loader::finish()
176 /* Make sure the source tarball is last in the list so targets from all
177 other components wil be created first */
178 auto i = find(obj.components, obj.source_archive);
179 if(i!=obj.components.end())
181 obj.components.erase(i);
182 obj.components.push_back(obj.source_archive);
186 void SourcePackage::Loader::exported()
188 ExportLoader ldr(obj);
192 void SourcePackage::Loader::feature(const string &n, const string &d)
195 feat.description = d;
197 obj.features.push_back(feat);
199 const Config::Option &opt = obj.config.add_option(feat);
202 auto i = options->find(opt.name);
203 if(i!=options->end())
204 obj.config.set_option(opt.name, i->second);
208 void SourcePackage::Loader::build_info()
210 load_sub(obj.build_info);
213 void SourcePackage::Loader::generate(const string &tag)
215 SourceGenerator *gen = new SourceGenerator(obj.builder, obj, tag);
217 obj.local_tools.add_tool(gen);
220 void SourcePackage::Loader::interface_version(const string &v)
222 obj.interface_version = v;
223 if(obj.version.empty())
227 void SourcePackage::Loader::source_archive()
229 load_sub(*obj.source_archive);
232 void SourcePackage::Loader::tarball(const string &)
234 IO::print("%s: Deprecated tarball component ignored\n", get_source());
237 void SourcePackage::Loader::version(const string &v)
241 string::size_type i = 0;
242 for(unsigned dots=0; i<obj.version.size(); ++i)
243 if(obj.version[i]=='.' && ++dots>=2)
245 obj.interface_version = obj.version.substr(0, i);
249 SourcePackage::ExportLoader::ExportLoader(SourcePackage &p):
250 ObjectLoader<SourcePackage>(p)
252 add("build_info", &ExportLoader::build_info);
255 void SourcePackage::ExportLoader::build_info()
259 obj.build_info.update_from(bi);
260 obj.export_binfo.update_from(bi);