]> git.tdb.fi Git - builder.git/blob - source/sourcepackage.cpp
Use default member initializers and constructor delegation
[builder.git] / source / 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 "androidapplicationcomponent.h"
9 #include "binarycomponent.h"
10 #include "binarypackage.h"
11 #include "builder.h"
12 #include "compilecommandsjson.h"
13 #include "datapackcomponent.h"
14 #include "file.h"
15 #include "installcomponent.h"
16 #include "pkgconfigfile.h"
17 #include "sourcearchivecomponent.h"
18 #include "sourcegenerator.h"
19 #include "sourcepackage.h"
20 #include "tool.h"
21 #include "vcxprojectfile.h"
22 #include "vssolutionfile.h"
23
24 using namespace std;
25 using namespace Msp;
26
27 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &f):
28         Package(b, n),
29         source_dir(FS::dirname(f)),
30         config(*this),
31         cache(*this)
32 {
33         config.load();
34
35         build_file = builder.get_vfs().get_target(f);
36         if(!build_file)
37                 build_file = new File(builder, *this, f);
38         source_archive = new SourceArchiveComponent(*this);
39         components.push_back(source_archive);
40 }
41
42 SourcePackage::~SourcePackage()
43 {
44         for(Component *c: components)
45                 delete c;
46 }
47
48 FS::Path SourcePackage::get_temp_directory() const
49 {
50         string subdir = builder.get_current_arch().get_name();
51         if(build_type)
52         {
53                 subdir += '.';
54                 subdir += build_type->get_name();
55         }
56
57         const FS::Path &temp = builder.get_temp_directory();
58         if(temp.is_absolute())
59                 return temp/name/subdir;
60         else
61                 return source_dir/temp/subdir;
62 }
63
64 FS::Path SourcePackage::get_output_directory() const
65 {
66         const Architecture &arch = builder.get_current_arch();
67         if(arch.is_native())
68                 return source_dir;
69         else
70                 return source_dir/arch.get_name();
71 }
72
73 const Component &SourcePackage::get_component(const string &n) const
74 {
75         auto i = find_if(components, [&n](const Component *c){ return c->get_name()==n; });
76         if(i!=components.end())
77                 return **i;
78         throw key_error(n);
79 }
80
81 bool SourcePackage::match_feature(const string &feat, const string *comp) const
82 {
83         string value = config.get_option("with_"+feat).value;
84         if(comp)
85                 return value==*comp;
86         else
87                 return lexical_cast<bool>(value);
88 }
89
90 void SourcePackage::set_build_type(const BuildType &t)
91 {
92         build_type = &t;
93 }
94
95 void SourcePackage::do_prepare()
96 {
97         BuildInfo final_build_info;
98
99         if(build_type)
100                 final_build_info.update_from(build_type->get_build_info());
101
102         final_build_info.update_from(build_info);
103         build_info = final_build_info;
104
105         build_info.incpath.push_back((builder.get_prefix()/"include").str());
106         build_info.libpath.push_back((builder.get_prefix()/"lib").str());
107
108         for(const Feature &f: features)
109         {
110                 string ident = "WITH_"+toupper(f.name);
111                 string value = config.get_option("with_"+f.name).value;
112
113                 if(f.choices.empty())
114                 {
115                         if(!lexical_cast<bool>(value))
116                                 continue;
117                         value = "1";
118                 }
119
120                 build_info.defines[ident] = value;
121                 if(f.exported)
122                         export_binfo.defines[ident] = value;
123         }
124
125         for(Component *c: components)
126         {
127                 c->prepare();
128                 c->create_build_info();
129
130                 c->update_exported_build_info(export_binfo);
131         }
132
133         cache.load();
134
135         for(Component *c: components)
136                 c->create_targets();
137
138         const Architecture &arch = builder.get_native_arch();
139         if(!export_binfo.libs.empty())
140         {
141                 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
142                 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
143
144                 if(arch.get_system()=="linux")
145                 {
146                         PkgConfigFile *pc = new PkgConfigFile(builder, *this);
147                         builder.get_build_graph().get_target("install")->add_dependency(*builder.get_toolchain().get_tool("CP").create_target(*pc));
148                 }
149         }
150
151         export_binfo.standards = build_info.standards;
152
153         if(arch.get_system()=="windows")
154         {
155                 new VcxProjectFile(builder, *this);
156                 new VsSolutionFile(builder, *this);
157         }
158
159         new CompileCommandsJson(builder, *this);
160 }
161
162 void SourcePackage::save_caches()
163 {
164         config.save();
165         cache.save();
166 }
167
168
169 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions *o):
170         DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>(p),
171         FeatureConditional(p, p.name),
172         options(o)
173 {
174         add("android_application", &Loader::component<AndroidApplicationComponent>);
175         add("build_info",  &Loader::build_info);
176         add("datapack",    &Loader::component<DataPackComponent>);
177         add("description", &SourcePackage::description);
178         add("feature",     &Loader::feature);
179         add("generate",    &Loader::generate);
180         add("install",     &Loader::component<InstallComponent>);
181         add("interface_version", &Loader::interface_version);
182         add("library",     &Loader::component_arg<BinaryComponent, BinaryComponent::Type>, BinaryComponent::LIBRARY);
183         add("module",      &Loader::component_arg<BinaryComponent, BinaryComponent::Type>, BinaryComponent::MODULE);
184         add("program",     &Loader::component_arg<BinaryComponent, BinaryComponent::Type>, BinaryComponent::PROGRAM);
185         add("source_archive", &Loader::source_archive);
186         add("source_tarball", &Loader::source_archive);
187         add("tarball",     &Loader::tarball);
188         add("version",     &Loader::version);
189 }
190
191 void SourcePackage::Loader::finish()
192 {
193         /* Make sure the source tarball is last in the list so targets from all
194         other components wil be created first */
195         auto i = find(obj.components, obj.source_archive);
196         if(i!=obj.components.end())
197         {
198                 obj.components.erase(i);
199                 obj.components.push_back(obj.source_archive);
200         }
201 }
202
203 void SourcePackage::Loader::feature(const string &n, const string &d)
204 {
205         Feature feat(n);
206         feat.description = d;
207         load_sub(feat);
208         obj.features.push_back(feat);
209
210         const Config::Option &opt = obj.config.add_option(feat);
211         if(options)
212         {
213                 auto i = options->find(opt.name);
214                 if(i!=options->end())
215                         obj.config.set_option(opt.name, i->second);
216         }
217 }
218
219 template<typename C>
220 void SourcePackage::Loader::component(const string &n)
221 {
222         C *comp = new C(obj, n);
223         load_sub(*comp);
224         obj.components.push_back(comp);
225 }
226
227 template<typename C, typename A>
228 void SourcePackage::Loader::component_arg(A a, const string &n)
229 {
230         C *comp = new C(obj, n, a);
231         load_sub(*comp);
232         obj.components.push_back(comp);
233 }
234
235 void SourcePackage::Loader::build_info()
236 {
237         load_sub(obj.build_info);
238 }
239
240 void SourcePackage::Loader::generate(const string &tag)
241 {
242         SourceGenerator *gen = new SourceGenerator(obj.builder, obj, tag);
243         load_sub(*gen);
244         obj.local_tools.add_tool(gen);
245 }
246
247 void SourcePackage::Loader::interface_version(const string &v)
248 {
249         obj.interface_version = v;
250         if(obj.version.empty())
251                 obj.version = v;
252 }
253
254 void SourcePackage::Loader::source_archive()
255 {
256         load_sub(*obj.source_archive);
257 }
258
259 void SourcePackage::Loader::tarball(const string &)
260 {
261         IO::print("%s: Deprecated tarball component ignored\n", get_source());
262 }
263
264 void SourcePackage::Loader::version(const string &v)
265 {
266         obj.version = v;
267
268         string::size_type i = 0;
269         for(unsigned dots=0; i<obj.version.size(); ++i)
270                 if(obj.version[i]=='.' && ++dots>=2)
271                         break;
272         obj.interface_version = obj.version.substr(0, i);
273 }