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