]> git.tdb.fi Git - builder.git/blob - source/sourcepackage.cpp
Variables in Build files weren't such a hot idea. KISS.
[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 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
67 {
68         init_config();
69
70         config.load();
71
72         if(flag && config.update(opts))
73                 builder.get_logger().log("configure", format("Configuration of %s changed", name));
74
75         deps_cache.load();
76
77         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
78                 i->configure(opts, flag);
79 }
80
81 void SourcePackage::init_config()
82 {
83 }
84
85 void SourcePackage::create_build_info()
86 {
87         if(build_type)
88                 build_info.update_from(build_type->get_build_info());
89
90         // XXX Currently, a package-specific settings will override cmdline.  This might or might not be desirable.
91         const StringList &warnings = builder.get_warnings();
92         build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
93
94         build_info.incpath.push_back((builder.get_prefix()/"include").str());
95         build_info.libpath.push_back((builder.get_prefix()/"lib").str());
96
97         bool export_paths = false;
98         for(ComponentList::const_iterator i=components.begin(); (!export_paths && i!=components.end()); ++i)
99                 export_paths = (i->get_type()==Component::LIBRARY);
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         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
108                 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
109                         build_info.defines["WITH_"+toupper(i->name)] = "1";
110
111         for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
112         {
113                 i->create_build_info();
114                 if(i->get_type()==Component::LIBRARY)
115                         export_binfo.libs.push_back(i->get_name());
116         }
117 }
118
119 void SourcePackage::create_targets()
120 {
121         bool pc_needed = false;
122         for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
123         {
124                 i->create_targets();
125                 if(i->get_type()==Component::LIBRARY)
126                         pc_needed = true;
127         }
128
129         if(pc_needed)
130         {
131                 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
132                 builder.get_target("install")->add_depend(*builder.get_toolchain().get_tool("CP").create_target(*pc));
133         }
134 }
135
136 void SourcePackage::save_caches()
137 {
138         config.save();
139         deps_cache.save();
140 }
141
142
143 SourcePackage::Loader::Loader(SourcePackage &p):
144         DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
145 {
146         init(0);
147 }
148
149 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions &o):
150         DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
151 {
152         init(&o);
153 }
154
155 void SourcePackage::Loader::init(const Config::InputOptions *o)
156 {
157         options = o;
158         add("version",     &SourcePackage::version);
159         add("description", &SourcePackage::description);
160         add("build_info",  &Loader::build_info);
161         add("feature",     &Loader::feature);
162         add("if",          &Loader::condition);
163         add("if_arch",     &Loader::if_arch);
164         add("if_feat",     &Loader::if_feature);
165         add("program",     &Loader::component<Component::PROGRAM>);
166         add("library",     &Loader::component<Component::LIBRARY>);
167         add("module",      &Loader::component<Component::MODULE>);
168         add("headers",     &Loader::headers);
169         add("install",     &Loader::component<Component::INSTALL>);
170         add("datafile",    &Loader::component<Component::DATAFILE>);
171         add("tarball",     &Loader::tarball);
172         add("tar_file",    &Loader::tar_file);
173 }
174
175 void SourcePackage::Loader::finish()
176 {
177         obj.components.sort(component_sort);
178
179         for(map<string, string>::const_iterator i=install_map.begin(); i!=install_map.end(); ++i)
180         {
181                 for(ComponentList::iterator j=obj.components.begin(); j!=obj.components.end(); ++j)
182                 {
183                         const StringList &sources = j->get_sources();
184                         for(StringList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
185                         {
186                                 if(!i->first.compare(0, k->size(), *k))
187                                 {
188                                         const_cast<InstallMap &>(j->get_install_map()).add_mapping(obj.source/i->first, i->second);
189                                 }
190                         }
191                 }
192         }
193 }
194
195 void SourcePackage::Loader::feature(const string &n, const string &d)
196 {
197         Feature feat(n);
198         feat.descr = d;
199         feat.def_value = "no";
200         load_sub(feat);
201         obj.features.push_back(feat);
202         string config_key = "with_"+feat.name;
203         obj.config.add_option(config_key, feat.def_value, feat.descr);
204         if(options)
205         {
206                 Config::InputOptions::const_iterator i = options->find(config_key);
207                 if(i!=options->end())
208                         obj.config.set_option(config_key, i->second);
209         }
210 }
211
212 void SourcePackage::Loader::condition(const string &c)
213 {
214         IO::print("%s: Note: Old-style conditions are deprecated\n", get_source());
215         Condition cond(obj, c);
216         if(cond.eval())
217                 load_sub_with(*this);
218 }
219
220 template<Component::Type t>
221 void SourcePackage::Loader::component(const string &n)
222 {
223         Component comp(obj, t, n);
224         load_sub(comp);
225         obj.components.push_back(comp);
226 }
227
228 void SourcePackage::Loader::build_info()
229 {
230         load_sub(obj.build_info);
231 }
232
233 void SourcePackage::Loader::headers(const string &n)
234 {
235         IO::print("%s: Note: headers components are deprecated\n", get_source());
236         Component comp(obj, Component::LIBRARY, n);
237         load_sub(comp);
238         const StringList &sources = comp.get_sources();
239         for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
240                 install_map[*i] = "include/"+comp.get_name();
241 }
242
243 void SourcePackage::Loader::if_arch(const string &cond)
244 {
245         const Architecture &arch = obj.builder.get_current_arch();
246         bool negate = (cond[0]=='!');
247         bool match = (arch.match_name(cond.substr(negate))!=negate);
248         obj.builder.get_logger().log("configure", format("%s: arch %s %smatched", obj.name, cond, (match ? "" : "not ")));
249         if(match)
250                 load_sub_with(*this);
251 }
252
253 void SourcePackage::Loader::if_feature(const string &cond)
254 {
255         bool match = false;
256         string::size_type equals = cond.find('=');
257         if(equals!=string::npos)
258         {
259                 if(equals==0)
260                         error("No feature name specified");
261                 bool negate = cond[equals-1]=='!';
262                 string name = cond.substr(0, equals-negate);
263                 string value = obj.config.get_option("with_"+name).value;
264                 match = (value==cond.substr(equals+1))!=negate;
265                 value = cond.substr(equals+1);
266         }
267         else
268         {
269                 bool negate = (cond[0]=='!');
270                 string name = cond.substr(negate);
271                 string value = obj.config.get_option("with_"+name).value;
272                 match = lexical_cast<bool>(value)!=negate;
273         }
274         obj.builder.get_logger().log("configure", format("%s: feature %s %smatched", obj.name, cond, (match ? "" : "not ")));
275         if(match)
276                 load_sub_with(*this);
277 }
278
279 void SourcePackage::Loader::tarball(const string &n)
280 {
281         if(n=="@src")
282         {
283                 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
284                         if(i->get_type()==Component::TARBALL && i->get_name()==n)
285                                 load_sub(*i);
286         }
287         else
288         {
289                 Component trbl(obj, Component::TARBALL, n);
290                 load_sub(trbl);
291         }
292 }
293
294 void SourcePackage::Loader::tar_file(const string &f)
295 {
296         IO::print("%s: Note: tar_file is deprecated\n", get_source());
297         for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
298                 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
299                         const_cast<StringList &>(i->get_sources()).push_back((obj.source/f).str());
300 }