]> git.tdb.fi Git - builder.git/blob - source/sourcepackage.cpp
Move package management to a separate class
[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(s),
26         config(*this),
27         deps_cache(*this)
28 {
29         components.push_back(Component(*this, Component::TARBALL, "@src"));
30 }
31
32 FS::Path SourcePackage::get_temp_dir() const
33 {
34         string subdir = format("%s.%s", builder.get_current_arch().get_name(), config.get_option("profile").value);
35         return source/config.get_option("tempdir").value/subdir;
36 }
37
38 FS::Path SourcePackage::get_out_dir() const
39 {
40         const Architecture &arch = builder.get_current_arch();
41         if(arch.is_native())
42                 return source/config.get_option("outdir").value;
43         else
44                 return source/arch.get_name()/config.get_option("outdir").value;
45 }
46
47 unsigned SourcePackage::get_install_flags()
48 {
49         unsigned flags = 0;
50         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
51                 if(i->get_install())
52                 {
53                         if(i->get_type()==Component::PROGRAM)
54                                 flags |= BIN;
55                         else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
56                                 flags |= LIB;
57                         else if(i->get_type()==Component::HEADERS)
58                                 flags |= INCLUDE;
59                 }
60
61         return flags;
62 }
63
64 LibMode SourcePackage::get_library_mode() const
65 {
66         const string &mode = config.get_option("staticlibs").value;
67         if(mode=="all")
68                 return ALL_STATIC;
69         else if(mode=="local")
70                 return LOCAL_STATIC;
71         else if(mode=="none")
72                 return DYNAMIC;
73         else
74                 throw runtime_error("unknown library mode");
75 }
76
77 string SourcePackage::expand_string(const string &str) const
78 {
79         string result = str;
80         string::size_type dollar = 0;
81         unsigned n = 0;
82         while((dollar = result.find('$'))!=string::npos)
83         {
84                 if(n>1000)
85                         throw bad_expansion("nested too deep");
86
87                 string::size_type end;
88                 string var;
89                 if(dollar+1<result.size() && result[dollar+1]=='{')
90                 {
91                         end = result.find('}', dollar+2);
92                         if(end==string::npos)
93                                 throw bad_expansion("unterminated variable reference");
94                         var = result.substr(dollar+2, end-dollar-2);
95                         ++end;
96                 }
97                 else
98                 {
99                         for(end=dollar+1; (isalnum(result[end]) || result[end]=='_'); ++end) ;
100                         var = result.substr(dollar+1, end-dollar-1);
101                 }
102
103                 string value;
104                 if(config.is_option(var))
105                         value = config.get_option(var).value;
106                 else if(var=="arch")
107                         value = builder.get_current_arch().get_name();
108                 else if(var=="system")
109                         value = builder.get_current_arch().get_system();
110                 else if(const char *ptr = getenv(var.c_str()))
111                         value = ptr;
112
113                 result.replace(dollar, end-dollar, value);
114
115                 ++n;
116         }
117
118         return result;
119 }
120
121 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
122 {
123         init_config();
124
125         StringMap::const_iterator prof = opts.find("profile");
126         if(prof!=opts.end() && flag)
127                 config.select_profile(prof->second);
128         else
129                 config.select_last_profile();
130
131         if(flag && config.update(opts))
132         {
133                 if(builder.get_verbose()>=2)
134                         IO::print("Configuration of %s changed\n", name);
135                 if(!builder.get_dry_run())
136                         config.save();
137         }
138
139         config.finish();
140
141         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
142                 if(i->eval())
143                 {
144                         const StringList &reqs = i->get_requires();
145                         for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
146                                 if(Package *pkg = builder.get_package_manager().find_package(*j))
147                                         requires.push_back(pkg);
148                 }
149
150         for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
151         {
152                 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
153                 if(bpkg && bpkg->get_need_path())
154                         bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
155         }
156
157         deps_cache.load();
158
159         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
160                 i->configure(opts, flag);
161 }
162
163 void SourcePackage::init_config()
164 {
165         config.add_option("profile",    "default", "Configuration profile");
166         config.add_option("tempdir",    "temp",    "Directory for storing temporary files");
167         config.add_option("outdir",     ".",       "Directory to put build results in");
168         config.add_option("optimize",   "0",       "Compiler optimization level");
169         config.add_option("strip",      "no",      "Strip symbols from programs");
170         config.add_option("debug",      "no",      "Produce debugging symbols");
171         config.add_option("staticlibs", "local",   "Use static libraries");
172
173         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
174                 config.add_option("with_"+i->name, i->def_value, i->descr);
175
176         for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
177         {
178                 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
179                 if(bpkg && bpkg->get_need_path())
180                         config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
181         }
182 }
183
184 void SourcePackage::create_build_info()
185 {
186         // XXX Currently, a package-specific settings will override cmdline.  This might or might not be desirable.
187         const StringList &warnings = builder.get_warnings();
188         build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
189
190         unsigned flags = get_install_flags();
191
192         build_info.incpath.push_back((builder.get_prefix()/"include").str());
193         build_info.libpath.push_back((builder.get_prefix()/"lib").str());
194
195         if(flags&INCLUDE)
196                 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
197         if(flags&LIB)
198                 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
199
200         string optimize = config.get_option("optimize").value;
201         if(!optimize.empty() && optimize!="0")
202         {
203                 if(optimize=="s" || optimize=="size")
204                         build_info.optimize = -1;
205                 else
206                         build_info.optimize = lexical_cast<unsigned>(optimize);
207         }
208
209         if(lexical_cast<bool>(config.get_option("debug").value))
210         {
211                 build_info.debug = true;
212                 build_info.defines["DEBUG"] = "1";
213         }
214
215         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
216                 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
217                         build_info.defines["WITH_"+toupper(i->name)] = "1";
218
219         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
220                 if(i->eval())
221                         build_info.update_from(i->get_build_info());
222
223         for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
224         {
225                 i->create_build_info();
226                 if(i->get_type()==Component::LIBRARY)
227                         export_binfo.libs.push_back(i->get_name());
228         }
229 }
230
231 void SourcePackage::create_targets()
232 {
233         bool pc_needed = false;
234         for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
235         {
236                 i->create_targets();
237                 if(i->get_type()==Component::LIBRARY || i->get_type()==Component::HEADERS)
238                         pc_needed = true;
239         }
240
241         if(pc_needed)
242         {
243                 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
244                 builder.get_target("install")->add_depend(builder.get_toolchain().get_tool("CP").create_target(*pc));
245         }
246 }
247
248
249 SourcePackage::Loader::Loader(Package &p):
250         Package::Loader(p)
251 {
252         add("version",     &SourcePackage::version);
253         add("description", &SourcePackage::description);
254         add("build_info",  &Loader::build_info);
255         add("feature",     &Loader::feature);
256         add("if",          &Loader::condition);
257         add("program",     &Loader::component<Component::PROGRAM>);
258         add("library",     &Loader::component<Component::LIBRARY>);
259         add("module",      &Loader::component<Component::MODULE>);
260         add("headers",     &Loader::component<Component::HEADERS>);
261         add("install",     &Loader::component<Component::INSTALL>);
262         add("datafile",    &Loader::component<Component::DATAFILE>);
263         add("tarball",     &Loader::tarball);
264         add("tar_file",    &Loader::tar_file);
265 }
266
267 void SourcePackage::Loader::finish()
268 {
269         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
270         spkg.components.sort(component_sort);
271 }
272
273 void SourcePackage::Loader::feature(const string &n, const string &d)
274 {
275         Feature feat(n);
276         feat.descr = d;
277         feat.def_value = "no";
278         load_sub(feat);
279         static_cast<SourcePackage &>(pkg).features.push_back(feat);
280 }
281
282 void SourcePackage::Loader::condition(const string &c)
283 {
284         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
285         Condition cond(spkg, c);
286         load_sub(cond);
287         spkg.conditions.push_back(cond);
288 }
289
290 template<Component::Type t>
291 void SourcePackage::Loader::component(const string &n)
292 {
293         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
294         Component comp(spkg, t, n);
295         load_sub(comp);
296         spkg.components.push_back(comp);
297 }
298
299 void SourcePackage::Loader::build_info()
300 {
301         load_sub(static_cast<SourcePackage &>(pkg).build_info);
302 }
303
304 void SourcePackage::Loader::tarball(const string &n)
305 {
306         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
307         if(n=="@src")
308         {
309                 for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.components.end(); ++i)
310                         if(i->get_type()==Component::TARBALL && i->get_name()==n)
311                                 load_sub(*i);
312         }
313         else
314         {
315                 Component trbl(spkg, Component::TARBALL, n);
316                 load_sub(trbl);
317         }
318 }
319
320 void SourcePackage::Loader::tar_file(const string &f)
321 {
322         IO::print("%s: Note: tar_file is deprecated\n", get_source());
323         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
324         for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.components.end(); ++i)
325                 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
326                         const_cast<StringList &>(i->get_sources()).push_back((spkg.source/f).str());
327 }