]> git.tdb.fi Git - builder.git/blob - source/sourcepackage.cpp
Remove deprecated features
[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 "pkgconfigfile.h"
10 #include "tool.h"
11 #include "sourcepackage.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 namespace {
17
18 bool component_sort(const Component &c1, const Component &c2)
19 { return c1.get_type()<c2.get_type(); }
20
21 }
22
23
24 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &f):
25         Package(b, n),
26         source_dir(FS::dirname(f)),
27         build_type(0),
28         config(*this),
29         cache(*this)
30 {
31         config.load();
32
33         build_file = builder.get_vfs().get_target(f);
34         if(!build_file)
35                 build_file = new File(builder, *this, f);
36         components.push_back(Component(*this, Component::TARBALL, "@src"));
37         source_tarball = &components.back();
38 }
39
40 FS::Path SourcePackage::get_temp_directory() 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_output_directory() const
57 {
58         const Architecture &arch = builder.get_current_arch();
59         if(arch.is_native())
60                 return source_dir;
61         else
62                 return source_dir/arch.get_name();
63 }
64
65 bool SourcePackage::match_feature(const string &cond) const
66 {
67         string::size_type equals = cond.find('=');
68         if(equals!=string::npos)
69         {
70                 if(equals==0)
71                         throw invalid_argument("SourcePackage::match_feature");
72                 bool negate = cond[equals-1]=='!';
73                 string feat = cond.substr(0, equals-negate);
74                 string value = config.get_option("with_"+feat).value;
75                 return (value==cond.substr(equals+1))!=negate;
76         }
77         else
78         {
79                 bool negate = (cond[0]=='!');
80                 string feat = cond.substr(negate);
81                 string value = config.get_option("with_"+feat).value;
82                 return lexical_cast<bool>(value)!=negate;
83         }
84 }
85
86 void SourcePackage::set_build_type(const BuildType &t)
87 {
88         build_type = &t;
89 }
90
91 void SourcePackage::do_prepare()
92 {
93         BuildInfo final_build_info;
94
95         if(build_type)
96                 final_build_info.update_from(build_type->get_build_info());
97
98         final_build_info.update_from(build_info);
99         build_info = final_build_info;
100
101         build_info.incpath.push_back((builder.get_prefix()/"include").str());
102         build_info.libpath.push_back((builder.get_prefix()/"lib").str());
103
104         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
105                 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
106                         build_info.defines["WITH_"+toupper(i->name)] = "1";
107
108         bool export_paths = false;
109         for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
110         {
111                 i->prepare();
112                 i->create_build_info();
113
114                 if(i->get_type()==Component::LIBRARY)
115                 {
116                         export_binfo.libs.push_back(i->get_name());
117                         export_paths = true;
118                 }
119         }
120
121         if(export_paths)
122         {
123                 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
124                 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
125         }
126
127         cache.load();
128
129         bool pc_needed = false;
130         for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
131         {
132                 i->create_targets();
133                 if(i->get_type()==Component::LIBRARY)
134                         pc_needed = true;
135         }
136
137         if(pc_needed)
138         {
139                 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
140                 builder.get_build_graph().get_target("install")->add_dependency(*builder.get_toolchain().get_tool("CP").create_target(*pc));
141         }
142 }
143
144 void SourcePackage::save_caches()
145 {
146         config.save();
147         cache.save();
148 }
149
150
151 SourcePackage::Loader::Loader(SourcePackage &p):
152         DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>(p)
153 {
154         init(0);
155 }
156
157 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions &o):
158         DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>(p)
159 {
160         init(&o);
161 }
162
163 void SourcePackage::Loader::init(const Config::InputOptions *o)
164 {
165         options = o;
166         add("description", &SourcePackage::description);
167         add("build_info",  &Loader::build_info);
168         add("feature",     &Loader::feature);
169         add("if_feature",  &Loader::if_feature);
170         add("program",     &Loader::component<Component::PROGRAM>);
171         add("library",     &Loader::component<Component::LIBRARY>);
172         add("module",      &Loader::component<Component::MODULE>);
173         add("install",     &Loader::component<Component::INSTALL>);
174         add("interface_version", &Loader::interface_version);
175         add("datapack",    &Loader::component<Component::DATAPACK>);
176         add("source_tarball", &Loader::source_tarball);
177         add("tarball",     &Loader::tarball);
178         add("version",     &Loader::version);
179 }
180
181 void SourcePackage::Loader::finish()
182 {
183         obj.components.sort(component_sort);
184 }
185
186 void SourcePackage::Loader::feature(const string &n, const string &d)
187 {
188         Feature feat(n);
189         feat.descr = d;
190         feat.def_value = "no";
191         load_sub(feat);
192         obj.features.push_back(feat);
193         string config_key = "with_"+feat.name;
194         obj.config.add_option(config_key, feat.def_value, feat.descr);
195         if(options)
196         {
197                 Config::InputOptions::const_iterator i = options->find(config_key);
198                 if(i!=options->end())
199                         obj.config.set_option(config_key, i->second);
200         }
201 }
202
203 template<Component::Type t>
204 void SourcePackage::Loader::component(const string &n)
205 {
206         Component comp(obj, t, n);
207         load_sub(comp);
208         obj.components.push_back(comp);
209 }
210
211 void SourcePackage::Loader::build_info()
212 {
213         load_sub(obj.build_info);
214 }
215
216 void SourcePackage::Loader::if_feature(const string &cond)
217 {
218         bool match = obj.match_feature(cond);
219         obj.builder.get_logger().log("configure", format("%s: feature %s %smatched", obj.name, cond, (match ? "" : "not ")));
220         if(match)
221                 load_sub_with(*this);
222 }
223
224 void SourcePackage::Loader::interface_version(const string &v)
225 {
226         obj.interface_version = v;
227         if(obj.version.empty())
228                 obj.version = v;
229 }
230
231 void SourcePackage::Loader::source_tarball()
232 {
233         load_sub(*obj.source_tarball);
234 }
235
236 void SourcePackage::Loader::tarball(const string &n)
237 {
238         Component trbl(obj, Component::TARBALL, n);
239         load_sub(trbl);
240 }
241
242 void SourcePackage::Loader::version(const string &v)
243 {
244         obj.version = v;
245
246         string::size_type i = 0;
247         for(unsigned dots=0; i<obj.version.size(); ++i)
248                 if(obj.version[i]=='.' && ++dots>=2)
249                         break;
250         obj.interface_version = obj.version.substr(0, i);
251 }