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