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