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