]> git.tdb.fi Git - builder.git/blob - source/sourcepackage.cpp
Better way of specifying the source tarball in Build files
[builder.git] / source / sourcepackage.cpp
1 #include <cstdlib>
2 #include <msp/fs/utils.h>
3 #include <msp/io/print.h>
4 #include <msp/strings/lexicalcast.h>
5 #include <msp/strings/utils.h>
6 #include "binarypackage.h"
7 #include "builder.h"
8 #include "file.h"
9 #include "misc.h"
10 #include "pkgconfigfile.h"
11 #include "tool.h"
12 #include "sourcepackage.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 namespace {
18
19 bool component_sort(const Component &c1, const Component &c2)
20 { return c1.get_type()<c2.get_type(); }
21
22 }
23
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         components.push_back(Component(*this, Component::TARBALL, "@src"));
38         source_tarball = &components.back();
39 }
40
41 void SourcePackage::set_build_type(const BuildType &t)
42 {
43         build_type = &t;
44 }
45
46 FS::Path SourcePackage::get_temp_dir() 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_out_dir() 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 void SourcePackage::create_build_info()
72 {
73         BuildInfo final_build_info;
74
75         if(build_type)
76                 final_build_info.update_from(build_type->get_build_info());
77
78         final_build_info.update_from(build_info);
79         build_info = final_build_info;
80
81         build_info.incpath.push_back((builder.get_prefix()/"include").str());
82         build_info.libpath.push_back((builder.get_prefix()/"lib").str());
83
84         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
85                 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
86                         build_info.defines["WITH_"+toupper(i->name)] = "1";
87
88         bool export_paths = false;
89         for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
90         {
91                 i->prepare();
92                 i->create_build_info();
93
94                 if(i->get_type()==Component::LIBRARY)
95                 {
96                         export_binfo.libs.push_back(i->get_name());
97                         export_paths = true;
98                 }
99         }
100
101         if(export_paths)
102         {
103                 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
104                 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
105         }
106 }
107
108 void SourcePackage::create_targets()
109 {
110         cache.load();
111
112         bool pc_needed = false;
113         for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
114         {
115                 i->create_targets();
116                 if(i->get_type()==Component::LIBRARY)
117                         pc_needed = true;
118         }
119
120         if(pc_needed)
121         {
122                 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
123                 builder.get_target("install")->add_dependency(*builder.get_toolchain().get_tool("CP").create_target(*pc));
124         }
125 }
126
127 void SourcePackage::save_caches()
128 {
129         config.save();
130         cache.save();
131 }
132
133
134 SourcePackage::Loader::Loader(SourcePackage &p):
135         DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
136 {
137         init(0);
138 }
139
140 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions &o):
141         DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
142 {
143         init(&o);
144 }
145
146 void SourcePackage::Loader::init(const Config::InputOptions *o)
147 {
148         options = o;
149         add("version",     &SourcePackage::version);
150         add("description", &SourcePackage::description);
151         add("build_info",  &Loader::build_info);
152         add("feature",     &Loader::feature);
153         add("if",          &Loader::condition);
154         add("if_arch",     &Loader::if_arch);
155         add("if_feat",     &Loader::if_feature);
156         add("program",     &Loader::component<Component::PROGRAM>);
157         add("library",     &Loader::component<Component::LIBRARY>);
158         add("module",      &Loader::component<Component::MODULE>);
159         add("headers",     &Loader::headers);
160         add("install",     &Loader::component<Component::INSTALL>);
161         add("datafile",    &Loader::component<Component::DATAFILE>);
162         add("source_tarball", &Loader::source_tarball);
163         add("tarball",     &Loader::tarball);
164         add("tar_file",    &Loader::tar_file);
165 }
166
167 void SourcePackage::Loader::finish()
168 {
169         obj.components.sort(component_sort);
170
171         for(map<string, string>::const_iterator i=install_map.begin(); i!=install_map.end(); ++i)
172         {
173                 for(ComponentList::iterator j=obj.components.begin(); j!=obj.components.end(); ++j)
174                 {
175                         const StringList &sources = j->get_sources();
176                         for(StringList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
177                         {
178                                 if(!i->first.compare(0, k->size(), *k))
179                                 {
180                                         const_cast<InstallMap &>(j->get_install_map()).add_mapping(obj.source_dir/i->first, i->second);
181                                 }
182                         }
183                 }
184         }
185 }
186
187 void SourcePackage::Loader::feature(const string &n, const string &d)
188 {
189         Feature feat(n);
190         feat.descr = d;
191         feat.def_value = "no";
192         load_sub(feat);
193         obj.features.push_back(feat);
194         string config_key = "with_"+feat.name;
195         obj.config.add_option(config_key, feat.def_value, feat.descr);
196         if(options)
197         {
198                 Config::InputOptions::const_iterator i = options->find(config_key);
199                 if(i!=options->end())
200                         obj.config.set_option(config_key, i->second);
201         }
202 }
203
204 void SourcePackage::Loader::condition(const string &c)
205 {
206         IO::print("%s: Note: Old-style conditions are deprecated\n", get_source());
207         Condition cond(obj, c);
208         if(cond.eval())
209                 load_sub_with(*this);
210 }
211
212 template<Component::Type t>
213 void SourcePackage::Loader::component(const string &n)
214 {
215         Component comp(obj, t, n);
216         load_sub(comp);
217         obj.components.push_back(comp);
218 }
219
220 void SourcePackage::Loader::build_info()
221 {
222         load_sub(obj.build_info);
223 }
224
225 void SourcePackage::Loader::headers(const string &n)
226 {
227         IO::print("%s: Note: headers components are deprecated\n", get_source());
228         Component comp(obj, Component::LIBRARY, n);
229         load_sub(comp);
230         const StringList &sources = comp.get_sources();
231         for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
232                 install_map[*i] = "include/"+comp.get_name();
233 }
234
235 void SourcePackage::Loader::if_arch(const string &cond)
236 {
237         const Architecture &arch = obj.builder.get_current_arch();
238         bool negate = (cond[0]=='!');
239         bool match = (arch.match_name(cond.substr(negate))!=negate);
240         obj.builder.get_logger().log("configure", format("%s: arch %s %smatched", obj.name, cond, (match ? "" : "not ")));
241         if(match)
242                 load_sub_with(*this);
243 }
244
245 void SourcePackage::Loader::if_feature(const string &cond)
246 {
247         bool match = false;
248         string::size_type equals = cond.find('=');
249         if(equals!=string::npos)
250         {
251                 if(equals==0)
252                         error("No feature name specified");
253                 bool negate = cond[equals-1]=='!';
254                 string name = cond.substr(0, equals-negate);
255                 string value = obj.config.get_option("with_"+name).value;
256                 match = (value==cond.substr(equals+1))!=negate;
257                 value = cond.substr(equals+1);
258         }
259         else
260         {
261                 bool negate = (cond[0]=='!');
262                 string name = cond.substr(negate);
263                 string value = obj.config.get_option("with_"+name).value;
264                 match = lexical_cast<bool>(value)!=negate;
265         }
266         obj.builder.get_logger().log("configure", format("%s: feature %s %smatched", obj.name, cond, (match ? "" : "not ")));
267         if(match)
268                 load_sub_with(*this);
269 }
270
271 void SourcePackage::Loader::source_tarball()
272 {
273         load_sub(*obj.source_tarball);
274 }
275
276 void SourcePackage::Loader::tarball(const string &n)
277 {
278         if(n=="@src")
279         {
280                 IO::print("%s: Note: Use source_tarball instead of tarball \"@src\"\n", get_source());
281                 load_sub(*obj.source_tarball);
282         }
283         else
284         {
285                 Component trbl(obj, Component::TARBALL, n);
286                 load_sub(trbl);
287         }
288 }
289
290 void SourcePackage::Loader::tar_file(const string &f)
291 {
292         IO::print("%s: Note: tar_file is deprecated\n", get_source());
293         for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
294                 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
295                         const_cast<StringList &>(i->get_sources()).push_back((obj.source_dir/f).str());
296 }