]> git.tdb.fi Git - builder.git/blob - source/lib/sourcepackage.cpp
Refactor transitive dependencies to work on all targets
[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 FS::Path SourcePackage::get_staging_directory() const
69 {
70         return get_temp_directory()/"staging";
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(get_staging_directory()/"include");
106         build_info.incpath.push_back(builder.get_prefix()/"include");
107         build_info.libpath.push_back(builder.get_prefix()/"lib");
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         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
145         export_binfo.standards = build_info.standards;
146
147         builder.call_plugins([this](const Plugin &p){ p.create_targets(*this); });
148 }
149
150 void SourcePackage::save_caches()
151 {
152         config.save();
153         cache.save();
154 }
155
156
157 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions *o):
158         DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>(p),
159         FeatureConditional(p, p.name),
160         options(o)
161 {
162         add("build_info",  &Loader::build_info);
163         add("description", &SourcePackage::description);
164         add("export",      &Loader::exported);
165         add("feature",     &Loader::feature);
166         add("generate",    &Loader::generate);
167         add("install",     &Loader::component<InstallComponent>);
168         add("interface_version", &Loader::interface_version);
169         add("library",     &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::LIBRARY);
170         add("module",      &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::MODULE);
171         add("program",     &Loader::component<BinaryComponent, BinaryComponent::Type>, BinaryComponent::PROGRAM);
172         add("source_archive", &Loader::source_archive);
173         add("source_tarball", &Loader::source_archive);
174         add("tarball",     &Loader::tarball);
175         add("version",     &Loader::version);
176
177         p.builder.get_component_registry().invoke_all(*this);
178 }
179
180 void SourcePackage::Loader::finish()
181 {
182         /* Make sure the source tarball is last in the list so targets from all
183         other components wil be created first */
184         auto i = find(obj.components, obj.source_archive);
185         if(i!=obj.components.end())
186         {
187                 obj.components.erase(i);
188                 obj.components.push_back(obj.source_archive);
189         }
190 }
191
192 void SourcePackage::Loader::exported()
193 {
194         ExportLoader ldr(obj);
195         load_sub_with(ldr);
196 }
197
198 void SourcePackage::Loader::feature(const string &n, const string &d)
199 {
200         Feature feat(n);
201         feat.description = d;
202         load_sub(feat);
203         obj.features.push_back(feat);
204
205         const Config::Option &opt = obj.config.add_option(feat);
206         if(options)
207         {
208                 auto i = options->find(opt.name);
209                 if(i!=options->end())
210                         obj.config.set_option(opt.name, i->second);
211         }
212 }
213
214 void SourcePackage::Loader::build_info()
215 {
216         load_sub(obj.build_info);
217 }
218
219 void SourcePackage::Loader::generate(const string &tag)
220 {
221         SourceGenerator *gen = new SourceGenerator(obj.builder, obj, tag);
222         load_sub(*gen);
223         obj.local_tools.add_tool(gen);
224 }
225
226 void SourcePackage::Loader::interface_version(const string &v)
227 {
228         obj.interface_version = v;
229         if(obj.version.empty())
230                 obj.version = v;
231 }
232
233 void SourcePackage::Loader::source_archive()
234 {
235         load_sub(*obj.source_archive);
236 }
237
238 void SourcePackage::Loader::tarball(const string &)
239 {
240         IO::print("%s: Deprecated tarball component ignored\n", get_source());
241 }
242
243 void SourcePackage::Loader::version(const string &v)
244 {
245         obj.version = v;
246
247         string::size_type i = 0;
248         for(unsigned dots=0; i<obj.version.size(); ++i)
249                 if(obj.version[i]=='.' && ++dots>=2)
250                         break;
251         obj.interface_version = obj.version.substr(0, i);
252 }
253
254
255 SourcePackage::ExportLoader::ExportLoader(SourcePackage &p):
256         ObjectLoader<SourcePackage>(p)
257 {
258         add("build_info", &ExportLoader::build_info);
259 }
260
261 void SourcePackage::ExportLoader::build_info()
262 {
263         BuildInfo bi;
264         load_sub(bi);
265         obj.build_info.update_from(bi);
266         obj.export_binfo.update_from(bi);
267 }