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