]> git.tdb.fi Git - builder.git/blob - source/lib/sourcepackage.cpp
dfbf16e92553737e9aa9a333db5b5a4a13d048e0
[builder.git] / source / lib / sourcepackage.cpp
1 #include <cstdlib>
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"
11 #include "builder.h"
12 #include "datafile/datapackcomponent.h"
13 #include "file.h"
14 #include "installcomponent.h"
15 #include "plugin.h"
16 #include "sourcearchivecomponent.h"
17 #include "sourcegenerator.h"
18 #include "sourcepackage.h"
19 #include "tool.h"
20
21 using namespace std;
22 using namespace Msp;
23
24 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &f):
25         Package(b, n),
26         source_dir(FS::dirname(f)),
27         config(*this),
28         cache(*this)
29 {
30         config.load();
31
32         build_file = builder.get_vfs().get_target(f);
33         if(!build_file)
34                 build_file = new File(builder, *this, f);
35         source_archive = new SourceArchiveComponent(*this);
36         components.push_back(source_archive);
37 }
38
39 SourcePackage::~SourcePackage()
40 {
41         for(Component *c: components)
42                 delete c;
43 }
44
45 FS::Path SourcePackage::get_temp_directory() const
46 {
47         string subdir = builder.get_current_arch().get_name();
48         if(build_type)
49         {
50                 subdir += '.';
51                 subdir += build_type->get_name();
52         }
53
54         const FS::Path &temp = builder.get_temp_directory();
55         if(temp.is_absolute())
56                 return temp/name/subdir;
57         else
58                 return source_dir/temp/subdir;
59 }
60
61 FS::Path SourcePackage::get_output_directory() const
62 {
63         const Architecture &arch = builder.get_current_arch();
64         if(arch.is_native())
65                 return source_dir;
66         else
67                 return source_dir/arch.get_name();
68 }
69
70 const Component &SourcePackage::get_component(const string &n) const
71 {
72         auto i = find_if(components, [&n](const Component *c){ return c->get_name()==n; });
73         if(i!=components.end())
74                 return **i;
75         throw key_error(n);
76 }
77
78 bool SourcePackage::match_feature(const string &feat, const string *comp) const
79 {
80         string value = config.get_option("with_"+feat).value;
81         if(comp)
82                 return value==*comp;
83         else
84                 return lexical_cast<bool>(value);
85 }
86
87 void SourcePackage::set_build_type(const BuildType &t)
88 {
89         build_type = &t;
90 }
91
92 void SourcePackage::do_prepare()
93 {
94         BuildInfo final_build_info;
95
96         if(build_type)
97                 final_build_info.update_from(build_type->get_build_info());
98
99         final_build_info.update_from(build_info);
100         build_info = final_build_info;
101
102         build_info.incpath.push_back((builder.get_prefix()/"include").str());
103         build_info.libpath.push_back((builder.get_prefix()/"lib").str());
104
105         for(const Feature &f: features)
106         {
107                 string ident = "WITH_"+toupper(f.name);
108                 string value = config.get_option("with_"+f.name).value;
109
110                 if(f.choices.empty())
111                 {
112                         if(!lexical_cast<bool>(value))
113                                 continue;
114                         value = "1";
115                 }
116
117                 build_info.defines[ident] = value;
118                 if(f.exported)
119                         export_binfo.defines[ident] = value;
120         }
121
122         for(Component *c: components)
123         {
124                 c->prepare();
125                 c->create_build_info();
126
127                 c->update_exported_build_info(export_binfo);
128         }
129
130         cache.load();
131
132         for(Component *c: components)
133                 c->create_targets();
134
135         if(!export_binfo.libs.empty())
136         {
137                 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
138                 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
139         }
140
141         export_binfo.standards = build_info.standards;
142
143         builder.call_plugins([this](const Plugin &p){ p.create_targets(*this); });
144 }
145
146 void SourcePackage::save_caches()
147 {
148         config.save();
149         cache.save();
150 }
151
152
153 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions *o):
154         DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>(p),
155         FeatureConditional(p, p.name),
156         options(o)
157 {
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);
173 }
174
175 void SourcePackage::Loader::finish()
176 {
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())
181         {
182                 obj.components.erase(i);
183                 obj.components.push_back(obj.source_archive);
184         }
185 }
186
187 void SourcePackage::Loader::feature(const string &n, const string &d)
188 {
189         Feature feat(n);
190         feat.description = d;
191         load_sub(feat);
192         obj.features.push_back(feat);
193
194         const Config::Option &opt = obj.config.add_option(feat);
195         if(options)
196         {
197                 auto i = options->find(opt.name);
198                 if(i!=options->end())
199                         obj.config.set_option(opt.name, i->second);
200         }
201 }
202
203 template<typename C, typename... Args>
204 void SourcePackage::Loader::component(Args... args, const string &n)
205 {
206         if(any_of(obj.components.begin(), obj.components.end(), [&n](const Component *c){ return c->get_name()==n; }))
207                 throw key_error(n);
208         C *comp = new C(obj, n, args...);
209         load_sub(*comp);
210         obj.components.push_back(comp);
211 }
212
213 void SourcePackage::Loader::build_info()
214 {
215         load_sub(obj.build_info);
216 }
217
218 void SourcePackage::Loader::generate(const string &tag)
219 {
220         SourceGenerator *gen = new SourceGenerator(obj.builder, obj, tag);
221         load_sub(*gen);
222         obj.local_tools.add_tool(gen);
223 }
224
225 void SourcePackage::Loader::interface_version(const string &v)
226 {
227         obj.interface_version = v;
228         if(obj.version.empty())
229                 obj.version = v;
230 }
231
232 void SourcePackage::Loader::source_archive()
233 {
234         load_sub(*obj.source_archive);
235 }
236
237 void SourcePackage::Loader::tarball(const string &)
238 {
239         IO::print("%s: Deprecated tarball component ignored\n", get_source());
240 }
241
242 void SourcePackage::Loader::version(const string &v)
243 {
244         obj.version = v;
245
246         string::size_type i = 0;
247         for(unsigned dots=0; i<obj.version.size(); ++i)
248                 if(obj.version[i]=='.' && ++dots>=2)
249                         break;
250         obj.interface_version = obj.version.substr(0, i);
251 }