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