]> git.tdb.fi Git - builder.git/blob - source/sourcepackage.cpp
Using different out directories per build type is more trouble than it's worth
[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 "misc.h"
10 #include "pkgconfigfile.h"
11 #include "tool.h"
12 #include "sourcepackage.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 namespace {
18
19 bool component_sort(const Component &c1, const Component &c2)
20 { return c1.get_type()<c2.get_type(); }
21
22 }
23
24
25 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &f):
26         Package(b, n),
27         source_dir(FS::dirname(f)),
28         build_type(0),
29         config(*this),
30         cache(*this)
31 {
32         config.load();
33
34         build_file = builder.get_vfs().get_target(f);
35         if(!build_file)
36                 build_file = new File(builder, *this, f);
37         components.push_back(Component(*this, Component::TARBALL, "@src"));
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::create_build_info()
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
107 void SourcePackage::create_targets()
108 {
109         cache.load();
110
111         bool pc_needed = false;
112         for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
113         {
114                 i->create_targets();
115                 if(i->get_type()==Component::LIBRARY)
116                         pc_needed = true;
117         }
118
119         if(pc_needed)
120         {
121                 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
122                 builder.get_target("install")->add_dependency(*builder.get_toolchain().get_tool("CP").create_target(*pc));
123         }
124 }
125
126 void SourcePackage::save_caches()
127 {
128         config.save();
129         cache.save();
130 }
131
132
133 SourcePackage::Loader::Loader(SourcePackage &p):
134         DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
135 {
136         init(0);
137 }
138
139 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions &o):
140         DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
141 {
142         init(&o);
143 }
144
145 void SourcePackage::Loader::init(const Config::InputOptions *o)
146 {
147         options = o;
148         add("version",     &SourcePackage::version);
149         add("description", &SourcePackage::description);
150         add("build_info",  &Loader::build_info);
151         add("feature",     &Loader::feature);
152         add("if",          &Loader::condition);
153         add("if_arch",     &Loader::if_arch);
154         add("if_feat",     &Loader::if_feature);
155         add("program",     &Loader::component<Component::PROGRAM>);
156         add("library",     &Loader::component<Component::LIBRARY>);
157         add("module",      &Loader::component<Component::MODULE>);
158         add("headers",     &Loader::headers);
159         add("install",     &Loader::component<Component::INSTALL>);
160         add("datafile",    &Loader::component<Component::DATAFILE>);
161         add("tarball",     &Loader::tarball);
162         add("tar_file",    &Loader::tar_file);
163 }
164
165 void SourcePackage::Loader::finish()
166 {
167         obj.components.sort(component_sort);
168
169         for(map<string, string>::const_iterator i=install_map.begin(); i!=install_map.end(); ++i)
170         {
171                 for(ComponentList::iterator j=obj.components.begin(); j!=obj.components.end(); ++j)
172                 {
173                         const StringList &sources = j->get_sources();
174                         for(StringList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
175                         {
176                                 if(!i->first.compare(0, k->size(), *k))
177                                 {
178                                         const_cast<InstallMap &>(j->get_install_map()).add_mapping(obj.source_dir/i->first, i->second);
179                                 }
180                         }
181                 }
182         }
183 }
184
185 void SourcePackage::Loader::feature(const string &n, const string &d)
186 {
187         Feature feat(n);
188         feat.descr = d;
189         feat.def_value = "no";
190         load_sub(feat);
191         obj.features.push_back(feat);
192         string config_key = "with_"+feat.name;
193         obj.config.add_option(config_key, feat.def_value, feat.descr);
194         if(options)
195         {
196                 Config::InputOptions::const_iterator i = options->find(config_key);
197                 if(i!=options->end())
198                         obj.config.set_option(config_key, i->second);
199         }
200 }
201
202 void SourcePackage::Loader::condition(const string &c)
203 {
204         IO::print("%s: Note: Old-style conditions are deprecated\n", get_source());
205         Condition cond(obj, c);
206         if(cond.eval())
207                 load_sub_with(*this);
208 }
209
210 template<Component::Type t>
211 void SourcePackage::Loader::component(const string &n)
212 {
213         Component comp(obj, t, n);
214         load_sub(comp);
215         obj.components.push_back(comp);
216 }
217
218 void SourcePackage::Loader::build_info()
219 {
220         load_sub(obj.build_info);
221 }
222
223 void SourcePackage::Loader::headers(const string &n)
224 {
225         IO::print("%s: Note: headers components are deprecated\n", get_source());
226         Component comp(obj, Component::LIBRARY, n);
227         load_sub(comp);
228         const StringList &sources = comp.get_sources();
229         for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
230                 install_map[*i] = "include/"+comp.get_name();
231 }
232
233 void SourcePackage::Loader::if_arch(const string &cond)
234 {
235         const Architecture &arch = obj.builder.get_current_arch();
236         bool negate = (cond[0]=='!');
237         bool match = (arch.match_name(cond.substr(negate))!=negate);
238         obj.builder.get_logger().log("configure", format("%s: arch %s %smatched", obj.name, cond, (match ? "" : "not ")));
239         if(match)
240                 load_sub_with(*this);
241 }
242
243 void SourcePackage::Loader::if_feature(const string &cond)
244 {
245         bool match = false;
246         string::size_type equals = cond.find('=');
247         if(equals!=string::npos)
248         {
249                 if(equals==0)
250                         error("No feature name specified");
251                 bool negate = cond[equals-1]=='!';
252                 string name = cond.substr(0, equals-negate);
253                 string value = obj.config.get_option("with_"+name).value;
254                 match = (value==cond.substr(equals+1))!=negate;
255                 value = cond.substr(equals+1);
256         }
257         else
258         {
259                 bool negate = (cond[0]=='!');
260                 string name = cond.substr(negate);
261                 string value = obj.config.get_option("with_"+name).value;
262                 match = lexical_cast<bool>(value)!=negate;
263         }
264         obj.builder.get_logger().log("configure", format("%s: feature %s %smatched", obj.name, cond, (match ? "" : "not ")));
265         if(match)
266                 load_sub_with(*this);
267 }
268
269 void SourcePackage::Loader::tarball(const string &n)
270 {
271         if(n=="@src")
272         {
273                 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
274                         if(i->get_type()==Component::TARBALL && i->get_name()==n)
275                                 load_sub(*i);
276         }
277         else
278         {
279                 Component trbl(obj, Component::TARBALL, n);
280                 load_sub(trbl);
281         }
282 }
283
284 void SourcePackage::Loader::tar_file(const string &f)
285 {
286         IO::print("%s: Note: tar_file is deprecated\n", get_source());
287         for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
288                 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
289                         const_cast<StringList &>(i->get_sources()).push_back((obj.source_dir/f).str());
290 }