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