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("feature", &Loader::feature);
159 add("generate", &Loader::generate);
160 add("install", &Loader::component<InstallComponent>);
161 add("interface_version", &Loader::interface_version);
162 add("library", &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::LIBRARY);
163 add("module", &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::MODULE);
164 add("program", &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::PROGRAM);
165 add("source_archive", &Loader::source_archive);
166 add("source_tarball", &Loader::source_archive);
167 add("tarball", &Loader::tarball);
168 add("version", &Loader::version);
170 p.builder.get_component_registry().invoke_all(*this);
173 void SourcePackage::Loader::finish()
175 /* Make sure the source tarball is last in the list so targets from all
176 other components wil be created first */
177 auto i = find(obj.components, obj.source_archive);
178 if(i!=obj.components.end())
180 obj.components.erase(i);
181 obj.components.push_back(obj.source_archive);
185 void SourcePackage::Loader::feature(const string &n, const string &d)
188 feat.description = d;
190 obj.features.push_back(feat);
192 const Config::Option &opt = obj.config.add_option(feat);
195 auto i = options->find(opt.name);
196 if(i!=options->end())
197 obj.config.set_option(opt.name, i->second);
201 void SourcePackage::Loader::build_info()
203 load_sub(obj.build_info);
206 void SourcePackage::Loader::generate(const string &tag)
208 SourceGenerator *gen = new SourceGenerator(obj.builder, obj, tag);
210 obj.local_tools.add_tool(gen);
213 void SourcePackage::Loader::interface_version(const string &v)
215 obj.interface_version = v;
216 if(obj.version.empty())
220 void SourcePackage::Loader::source_archive()
222 load_sub(*obj.source_archive);
225 void SourcePackage::Loader::tarball(const string &)
227 IO::print("%s: Deprecated tarball component ignored\n", get_source());
230 void SourcePackage::Loader::version(const string &v)
234 string::size_type i = 0;
235 for(unsigned dots=0; i<obj.version.size(); ++i)
236 if(obj.version[i]=='.' && ++dots>=2)
238 obj.interface_version = obj.version.substr(0, i);