]> git.tdb.fi Git - builder.git/blob - source/lib/sourcepackage.cpp
Allow new component types to be registered at runtime
[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         p.builder.get_component_registry().invoke_all(*this);
175 }
176
177 void SourcePackage::Loader::finish()
178 {
179         /* Make sure the source tarball is last in the list so targets from all
180         other components wil be created first */
181         auto i = find(obj.components, obj.source_archive);
182         if(i!=obj.components.end())
183         {
184                 obj.components.erase(i);
185                 obj.components.push_back(obj.source_archive);
186         }
187 }
188
189 void SourcePackage::Loader::feature(const string &n, const string &d)
190 {
191         Feature feat(n);
192         feat.description = d;
193         load_sub(feat);
194         obj.features.push_back(feat);
195
196         const Config::Option &opt = obj.config.add_option(feat);
197         if(options)
198         {
199                 auto i = options->find(opt.name);
200                 if(i!=options->end())
201                         obj.config.set_option(opt.name, i->second);
202         }
203 }
204
205 void SourcePackage::Loader::build_info()
206 {
207         load_sub(obj.build_info);
208 }
209
210 void SourcePackage::Loader::generate(const string &tag)
211 {
212         SourceGenerator *gen = new SourceGenerator(obj.builder, obj, tag);
213         load_sub(*gen);
214         obj.local_tools.add_tool(gen);
215 }
216
217 void SourcePackage::Loader::interface_version(const string &v)
218 {
219         obj.interface_version = v;
220         if(obj.version.empty())
221                 obj.version = v;
222 }
223
224 void SourcePackage::Loader::source_archive()
225 {
226         load_sub(*obj.source_archive);
227 }
228
229 void SourcePackage::Loader::tarball(const string &)
230 {
231         IO::print("%s: Deprecated tarball component ignored\n", get_source());
232 }
233
234 void SourcePackage::Loader::version(const string &v)
235 {
236         obj.version = v;
237
238         string::size_type i = 0;
239         for(unsigned dots=0; i<obj.version.size(); ++i)
240                 if(obj.version[i]=='.' && ++dots>=2)
241                         break;
242         obj.interface_version = obj.version.substr(0, i);
243 }