]> git.tdb.fi Git - builder.git/blob - source/sourcepackage.cpp
Having to specify package paths manually is stupid and un-modern
[builder.git] / source / sourcepackage.cpp
1 #include <cstdlib>
2 #include <msp/io/print.h>
3 #include <msp/strings/lexicalcast.h>
4 #include <msp/strings/utils.h>
5 #include "binarypackage.h"
6 #include "builder.h"
7 #include "misc.h"
8 #include "pkgconfigfile.h"
9 #include "tool.h"
10 #include "sourcepackage.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 namespace {
16
17 bool component_sort(const Component &c1, const Component &c2)
18 { return c1.get_type()<c2.get_type(); }
19
20 }
21
22
23 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &s):
24         Package(b, n),
25         source(s),
26         build_type(0),
27         config(*this),
28         deps_cache(*this)
29 {
30         config.load();
31
32         components.push_back(Component(*this, Component::TARBALL, "@src"));
33 }
34
35 void SourcePackage::set_build_type(const BuildType &t)
36 {
37         build_type = &t;
38 }
39
40 FS::Path SourcePackage::get_temp_dir() 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/temp/subdir;
54 }
55
56 FS::Path SourcePackage::get_out_dir() const
57 {
58         const Architecture &arch = builder.get_current_arch();
59         string detail = (build_type ? build_type->get_name() : string());
60         if(arch.is_native())
61                 return source/detail;
62         else
63                 return source/arch.get_name()/detail;
64 }
65
66 string SourcePackage::expand_string(const string &str) const
67 {
68         string result = str;
69         string::size_type dollar = 0;
70         unsigned n = 0;
71         while((dollar = result.find('$'))!=string::npos)
72         {
73                 if(n>1000)
74                         throw bad_expansion("nested too deep");
75
76                 string::size_type end;
77                 string var;
78                 if(dollar+1<result.size() && result[dollar+1]=='{')
79                 {
80                         end = result.find('}', dollar+2);
81                         if(end==string::npos)
82                                 throw bad_expansion("unterminated variable reference");
83                         var = result.substr(dollar+2, end-dollar-2);
84                         ++end;
85                 }
86                 else
87                 {
88                         for(end=dollar+1; (isalnum(result[end]) || result[end]=='_'); ++end) ;
89                         var = result.substr(dollar+1, end-dollar-1);
90                 }
91
92                 string value;
93                 if(config.is_option(var))
94                         value = config.get_option(var).value;
95                 else if(var=="arch")
96                         value = builder.get_current_arch().get_name();
97                 else if(var=="system")
98                         value = builder.get_current_arch().get_system();
99                 else if(const char *ptr = getenv(var.c_str()))
100                         value = ptr;
101
102                 result.replace(dollar, end-dollar, value);
103
104                 ++n;
105         }
106
107         return result;
108 }
109
110 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
111 {
112         init_config();
113
114         config.load();
115
116         if(flag && config.update(opts))
117                 builder.get_logger().log("configure", format("Configuration of %s changed", name));
118
119         config.finish();
120
121         deps_cache.load();
122
123         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
124                 i->configure(opts, flag);
125 }
126
127 void SourcePackage::init_config()
128 {
129 }
130
131 void SourcePackage::create_build_info()
132 {
133         if(build_type)
134                 build_info.update_from(build_type->get_build_info());
135
136         // XXX Currently, a package-specific settings will override cmdline.  This might or might not be desirable.
137         const StringList &warnings = builder.get_warnings();
138         build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
139
140         build_info.incpath.push_back((builder.get_prefix()/"include").str());
141         build_info.libpath.push_back((builder.get_prefix()/"lib").str());
142
143         bool export_paths = false;
144         for(ComponentList::const_iterator i=components.begin(); (!export_paths && i!=components.end()); ++i)
145                 export_paths = (i->get_type()==Component::LIBRARY);
146
147         if(export_paths)
148         {
149                 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
150                 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
151         }
152
153         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
154                 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
155                         build_info.defines["WITH_"+toupper(i->name)] = "1";
156
157         for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
158         {
159                 i->create_build_info();
160                 if(i->get_type()==Component::LIBRARY)
161                         export_binfo.libs.push_back(i->get_name());
162         }
163 }
164
165 void SourcePackage::create_targets()
166 {
167         bool pc_needed = false;
168         for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
169         {
170                 i->create_targets();
171                 if(i->get_type()==Component::LIBRARY)
172                         pc_needed = true;
173         }
174
175         if(pc_needed)
176         {
177                 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
178                 builder.get_target("install")->add_depend(*builder.get_toolchain().get_tool("CP").create_target(*pc));
179         }
180 }
181
182 void SourcePackage::save_caches()
183 {
184         config.save();
185         deps_cache.save();
186 }
187
188
189 SourcePackage::Loader::Loader(SourcePackage &p):
190         DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
191 {
192         init(0);
193 }
194
195 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions &o):
196         DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
197 {
198         init(&o);
199 }
200
201 void SourcePackage::Loader::init(const Config::InputOptions *o)
202 {
203         options = o;
204         add("version",     &SourcePackage::version);
205         add("description", &SourcePackage::description);
206         add("build_info",  &Loader::build_info);
207         add("feature",     &Loader::feature);
208         add("if",          &Loader::condition);
209         add("if_arch",     &Loader::if_arch);
210         add("if_feat",     &Loader::if_feature);
211         add("program",     &Loader::component<Component::PROGRAM>);
212         add("library",     &Loader::component<Component::LIBRARY>);
213         add("module",      &Loader::component<Component::MODULE>);
214         add("headers",     &Loader::headers);
215         add("install",     &Loader::component<Component::INSTALL>);
216         add("datafile",    &Loader::component<Component::DATAFILE>);
217         add("tarball",     &Loader::tarball);
218         add("tar_file",    &Loader::tar_file);
219 }
220
221 void SourcePackage::Loader::finish()
222 {
223         obj.components.sort(component_sort);
224
225         for(map<string, string>::const_iterator i=install_map.begin(); i!=install_map.end(); ++i)
226         {
227                 for(ComponentList::iterator j=obj.components.begin(); j!=obj.components.end(); ++j)
228                 {
229                         const StringList &sources = j->get_sources();
230                         for(StringList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
231                         {
232                                 if(!i->first.compare(0, k->size(), *k))
233                                 {
234                                         const_cast<InstallMap &>(j->get_install_map()).add_mapping(obj.source/i->first, i->second);
235                                 }
236                         }
237                 }
238         }
239 }
240
241 void SourcePackage::Loader::feature(const string &n, const string &d)
242 {
243         Feature feat(n);
244         feat.descr = d;
245         feat.def_value = "no";
246         load_sub(feat);
247         obj.features.push_back(feat);
248         string config_key = "with_"+feat.name;
249         obj.config.add_option(config_key, feat.def_value, feat.descr);
250         if(options)
251         {
252                 Config::InputOptions::const_iterator i = options->find(config_key);
253                 if(i!=options->end())
254                         obj.config.set_option(config_key, i->second);
255         }
256 }
257
258 void SourcePackage::Loader::condition(const string &c)
259 {
260         IO::print("%s: Note: Old-style conditions are deprecated\n", get_source());
261         Condition cond(obj, c);
262         if(cond.eval())
263                 load_sub_with(*this);
264 }
265
266 template<Component::Type t>
267 void SourcePackage::Loader::component(const string &n)
268 {
269         Component comp(obj, t, n);
270         load_sub(comp);
271         obj.components.push_back(comp);
272 }
273
274 void SourcePackage::Loader::build_info()
275 {
276         load_sub(obj.build_info);
277 }
278
279 void SourcePackage::Loader::headers(const string &n)
280 {
281         IO::print("%s: Note: headers components are deprecated\n", get_source());
282         Component comp(obj, Component::LIBRARY, n);
283         load_sub(comp);
284         const StringList &sources = comp.get_sources();
285         for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
286                 install_map[*i] = "include/"+comp.get_name();
287 }
288
289 void SourcePackage::Loader::if_arch(const string &cond)
290 {
291         const Architecture &arch = obj.builder.get_current_arch();
292         bool negate = (cond[0]=='!');
293         bool match = (arch.match_name(cond.substr(negate))!=negate);
294         obj.builder.get_logger().log("configure", format("%s: arch %s %smatched", obj.name, cond, (match ? "" : "not ")));
295         if(match)
296                 load_sub_with(*this);
297 }
298
299 void SourcePackage::Loader::if_feature(const string &cond)
300 {
301         bool match = false;
302         string::size_type equals = cond.find('=');
303         if(equals!=string::npos)
304         {
305                 if(equals==0)
306                         error("No feature name specified");
307                 bool negate = cond[equals-1]=='!';
308                 string name = cond.substr(0, equals-negate);
309                 string value = obj.config.get_option("with_"+name).value;
310                 match = (value==cond.substr(equals+1))!=negate;
311                 value = cond.substr(equals+1);
312         }
313         else
314         {
315                 bool negate = (cond[0]=='!');
316                 string name = cond.substr(negate);
317                 string value = obj.config.get_option("with_"+name).value;
318                 match = lexical_cast<bool>(value)!=negate;
319         }
320         obj.builder.get_logger().log("configure", format("%s: feature %s %smatched", obj.name, cond, (match ? "" : "not ")));
321         if(match)
322                 load_sub_with(*this);
323 }
324
325 void SourcePackage::Loader::tarball(const string &n)
326 {
327         if(n=="@src")
328         {
329                 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
330                         if(i->get_type()==Component::TARBALL && i->get_name()==n)
331                                 load_sub(*i);
332         }
333         else
334         {
335                 Component trbl(obj, Component::TARBALL, n);
336                 load_sub(trbl);
337         }
338 }
339
340 void SourcePackage::Loader::tar_file(const string &f)
341 {
342         IO::print("%s: Note: tar_file is deprecated\n", get_source());
343         for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
344                 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
345                         const_cast<StringList &>(i->get_sources()).push_back((obj.source/f).str());
346 }