]> git.tdb.fi Git - builder.git/blob - source/sourcepackage.cpp
Add support for generating source files
[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 "sourcegenerator.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         source_tarball = &components.back();
39 }
40
41 FS::Path SourcePackage::get_temp_directory() const
42 {
43         string subdir = builder.get_current_arch().get_name();
44         if(build_type)
45         {
46                 subdir += '.';
47                 subdir += build_type->get_name();
48         }
49
50         const FS::Path &temp = builder.get_temp_directory();
51         if(temp.is_absolute())
52                 return temp/name/subdir;
53         else
54                 return source_dir/temp/subdir;
55 }
56
57 FS::Path SourcePackage::get_output_directory() const
58 {
59         const Architecture &arch = builder.get_current_arch();
60         if(arch.is_native())
61                 return source_dir;
62         else
63                 return source_dir/arch.get_name();
64 }
65
66 bool SourcePackage::match_feature(const string &cond) const
67 {
68         string::size_type equals = cond.find('=');
69         if(equals!=string::npos)
70         {
71                 if(equals==0)
72                         throw invalid_argument("SourcePackage::match_feature");
73                 bool negate = cond[equals-1]=='!';
74                 string feat = cond.substr(0, equals-negate);
75                 string value = config.get_option("with_"+feat).value;
76                 return (value==cond.substr(equals+1))!=negate;
77         }
78         else
79         {
80                 bool negate = (cond[0]=='!');
81                 string feat = cond.substr(negate);
82                 string value = config.get_option("with_"+feat).value;
83                 return lexical_cast<bool>(value)!=negate;
84         }
85 }
86
87 void SourcePackage::set_build_type(const BuildType &t)
88 {
89         build_type = &t;
90 }
91
92 void SourcePackage::do_prepare()
93 {
94         BuildInfo final_build_info;
95
96         if(build_type)
97                 final_build_info.update_from(build_type->get_build_info());
98
99         final_build_info.update_from(build_info);
100         build_info = final_build_info;
101
102         build_info.incpath.push_back((builder.get_prefix()/"include").str());
103         build_info.libpath.push_back((builder.get_prefix()/"lib").str());
104
105         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
106                 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
107                         build_info.defines["WITH_"+toupper(i->name)] = "1";
108
109         bool export_paths = false;
110         for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
111         {
112                 i->prepare();
113                 i->create_build_info();
114
115                 if(i->get_type()==Component::LIBRARY)
116                 {
117                         export_binfo.libs.push_back(i->get_name());
118                         export_paths = true;
119                 }
120         }
121
122         if(export_paths)
123         {
124                 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
125                 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
126         }
127
128         cache.load();
129
130         bool pc_needed = false;
131         for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
132         {
133                 i->create_targets();
134                 if(i->get_type()==Component::LIBRARY)
135                         pc_needed = true;
136         }
137
138         if(pc_needed)
139         {
140                 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
141                 builder.get_build_graph().get_target("install")->add_dependency(*builder.get_toolchain().get_tool("CP").create_target(*pc));
142         }
143 }
144
145 void SourcePackage::save_caches()
146 {
147         config.save();
148         cache.save();
149 }
150
151
152 SourcePackage::Loader::Loader(SourcePackage &p):
153         DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>(p)
154 {
155         init(0);
156 }
157
158 SourcePackage::Loader::Loader(SourcePackage &p, const Config::InputOptions &o):
159         DataFile::DerivedObjectLoader<SourcePackage, Package::Loader>(p)
160 {
161         init(&o);
162 }
163
164 void SourcePackage::Loader::init(const Config::InputOptions *o)
165 {
166         options = o;
167         add("description", &SourcePackage::description);
168         add("build_info",  &Loader::build_info);
169         add("feature",     &Loader::feature);
170         add("generate",    &Loader::generate);
171         add("if_feature",  &Loader::if_feature);
172         add("program",     &Loader::component<Component::PROGRAM>);
173         add("library",     &Loader::component<Component::LIBRARY>);
174         add("module",      &Loader::component<Component::MODULE>);
175         add("install",     &Loader::component<Component::INSTALL>);
176         add("interface_version", &Loader::interface_version);
177         add("datapack",    &Loader::component<Component::DATAPACK>);
178         add("source_tarball", &Loader::source_tarball);
179         add("tarball",     &Loader::tarball);
180         add("version",     &Loader::version);
181 }
182
183 void SourcePackage::Loader::finish()
184 {
185         obj.components.sort(component_sort);
186 }
187
188 void SourcePackage::Loader::feature(const string &n, const string &d)
189 {
190         Feature feat(n);
191         feat.descr = d;
192         feat.def_value = "no";
193         load_sub(feat);
194         obj.features.push_back(feat);
195         string config_key = "with_"+feat.name;
196         obj.config.add_option(config_key, feat.def_value, feat.descr);
197         if(options)
198         {
199                 Config::InputOptions::const_iterator i = options->find(config_key);
200                 if(i!=options->end())
201                         obj.config.set_option(config_key, i->second);
202         }
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::generate(const string &tag)
219 {
220         SourceGenerator *gen = new SourceGenerator(obj.builder, obj, tag);
221         load_sub(*gen);
222         obj.local_tools.add_tool(gen);
223 }
224
225 void SourcePackage::Loader::if_feature(const string &cond)
226 {
227         bool match = obj.match_feature(cond);
228         obj.builder.get_logger().log("configure", format("%s: feature %s %smatched", obj.name, cond, (match ? "" : "not ")));
229         if(match)
230                 load_sub_with(*this);
231 }
232
233 void SourcePackage::Loader::interface_version(const string &v)
234 {
235         obj.interface_version = v;
236         if(obj.version.empty())
237                 obj.version = v;
238 }
239
240 void SourcePackage::Loader::source_tarball()
241 {
242         load_sub(*obj.source_tarball);
243 }
244
245 void SourcePackage::Loader::tarball(const string &n)
246 {
247         Component trbl(obj, Component::TARBALL, n);
248         load_sub(trbl);
249 }
250
251 void SourcePackage::Loader::version(const string &v)
252 {
253         obj.version = v;
254
255         string::size_type i = 0;
256         for(unsigned dots=0; i<obj.version.size(); ++i)
257                 if(obj.version[i]=='.' && ++dots>=2)
258                         break;
259         obj.interface_version = obj.version.substr(0, i);
260 }