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