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