]> git.tdb.fi Git - builder.git/blob - source/sourcepackage.cpp
Miscellaneous minor code cleanups
[builder.git] / source / sourcepackage.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2007-2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/io/print.h>
9 #include <msp/strings/lexicalcast.h>
10 #include <msp/strings/utils.h>
11 #include "binarypackage.h"
12 #include "builder.h"
13 #include "misc.h"
14 #include "sourcepackage.h"
15
16 using namespace std;
17 using namespace Msp;
18
19 namespace {
20
21 bool component_sort(const Component &c1, const Component &c2)
22 { return c1.get_type()<c2.get_type(); }
23
24 }
25
26
27 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &s):
28         Package(b, n),
29         source(s),
30         config(*this),
31         deps_cache(*this)
32 {
33         components.push_back(Component(*this, Component::TARBALL, "@src"));
34 }
35
36 FS::Path SourcePackage::get_temp_dir() const
37 {
38         return source/config.get_option("tempdir").value/builder.get_current_arch().get_name()/config.get_option("profile").value;
39 }
40
41 FS::Path SourcePackage::get_out_dir() const
42 {
43         const Architecture &arch=builder.get_current_arch();
44         if(arch.is_native())
45                 return source/config.get_option("outdir").value;
46         else
47                 return source/arch.get_name()/config.get_option("outdir").value;
48 }
49
50 unsigned SourcePackage::get_install_flags()
51 {
52         unsigned flags=0;
53         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
54                 if(i->get_install())
55                 {
56                         if(i->get_type()==Component::PROGRAM)
57                                 flags|=BIN;
58                         else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
59                                 flags|=LIB;
60                         else if(i->get_type()==Component::HEADERS)
61                                 flags|=INCLUDE;
62                 }
63
64         return flags;
65 }
66
67 LibMode SourcePackage::get_library_mode() const
68 {
69         const string &mode=config.get_option("staticlibs").value;
70         if(mode=="all")
71                 return ALL_STATIC;
72         else if(mode=="local")
73                 return LOCAL_STATIC;
74         else if(mode=="none")
75                 return DYNAMIC;
76         else
77                 throw Exception("Unknown library mode");
78 }
79
80 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
81 {
82         init_config();
83
84         StringMap::const_iterator prof=opts.find("profile");
85         if(prof!=opts.end() && flag)
86                 config.select_profile(prof->second);
87         else
88                 config.select_last_profile();
89
90         if(flag && config.update(opts))
91         {
92                 if(builder.get_verbose()>=2)
93                         IO::print("Configuration of %s changed\n", name);
94                 if(!builder.get_dry_run())
95                         config.save();
96         }
97
98         config.finish();
99
100         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
101                 if(i->eval())
102                 {
103                         const StringList &reqs=i->get_requires();
104                         for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
105                                 if(Package *pkg=builder.get_package(*j))
106                                         requires.push_back(pkg);
107                 }
108
109         base_reqs=requires;
110
111         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
112         {
113                 const PackageList &reqs=i->get_requires();
114                 requires.insert(requires.end(), reqs.begin(), reqs.end());
115         }
116
117         for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
118         {
119                 BinaryPackage *bpkg=dynamic_cast<BinaryPackage *>(*i);
120                 if(bpkg && bpkg->get_need_path())
121                         bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
122         }
123
124         deps_cache.load();
125
126         /*for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
127                 (*i)->configure(opts, flag&2);*/
128 }
129
130 void SourcePackage::init_config()
131 {
132         config.add_option("profile",    "default", "Configuration profile");
133         config.add_option("tempdir",    "temp",    "Directory for storing temporary files");
134         config.add_option("outdir",     ".",       "Directory to put build results in");
135         config.add_option("optimize",   "0",       "Apply compiler optimizations");
136         config.add_option("strip",      "0",       "Strip symbols from programs");
137         config.add_option("debug",      "0",       "Produce debugging symbols");
138         config.add_option("cpu",        "auto",    "CPU type to optimize for");
139         config.add_option("staticlibs", "local",   "Use static libraries");
140
141         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
142                 config.add_option("with_"+i->name, "0", i->descr);
143
144         for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
145         {
146                 BinaryPackage *bpkg=dynamic_cast<BinaryPackage *>(*i);
147                 if(bpkg && bpkg->get_need_path())
148                         config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
149         }
150 }
151
152 void SourcePackage::create_build_info()
153 {
154         for(PackageList::iterator i=base_reqs.begin(); i!=base_reqs.end(); ++i)
155         {
156                 const BuildInfo &ebi=(*i)->get_exported_binfo();
157                 build_info.add(ebi);
158
159                 export_binfo.cflags.insert(export_binfo.cflags.end(), ebi.cflags.begin(), ebi.cflags.end());
160                 export_binfo.incpath.insert(export_binfo.incpath.end(), ebi.incpath.begin(), ebi.incpath.end());
161                 export_binfo.defines.insert(export_binfo.defines.end(), ebi.defines.begin(), ebi.defines.end());
162         }
163
164         // XXX Currently, a package-specific settings will override cmdline.  This might or might not be desirable.
165         const StringList &warnings=builder.get_warnings();
166         build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
167
168         unsigned flags=get_install_flags();
169
170         build_info.incpath.push_back((builder.get_prefix()/"include").str());
171         build_info.libpath.push_back((builder.get_prefix()/"lib").str());
172
173         if(flags&INCLUDE)
174                 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
175         if(flags&LIB)
176                 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
177
178         string optimize=config.get_option("optimize").value;
179         if(lexical_cast<unsigned>(optimize))
180         {
181                 build_info.cflags.push_back("-O"+optimize);
182                 build_info.ldflags.push_back("-O"+optimize);
183                 string cpu=config.get_option("cpu").value;
184                 if(cpu!="auto")
185                         build_info.cflags.push_back("-march="+cpu);
186         }
187
188         if(lexical_cast<bool>(config.get_option("debug").value))
189         {
190                 build_info.cflags.push_back("-ggdb");
191                 build_info.defines.push_back("DEBUG");
192         }
193
194         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
195                 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
196                         build_info.cflags.push_back("-DWITH_"+toupper(i->name));
197
198         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
199                 if(i->eval())
200                         build_info.add(i->get_build_info());
201
202         build_info.unique();
203
204         for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
205         {
206                 i->create_build_info();
207                 if(i->get_type()==Component::LIBRARY)
208                         export_binfo.libs.push_back(i->get_name());
209         }
210
211         export_binfo.unique();
212 }
213
214
215 SourcePackage::Loader::Loader(Package &p):
216         Package::Loader(p)
217 {
218         add("version",     &SourcePackage::version);
219         add("description", &SourcePackage::description);
220         add("build_info",  &Loader::build_info);
221         add("feature",     &Loader::feature);
222         add("if",          &Loader::condition);
223         add("program",     &Loader::program);
224         add("library",     &Loader::library);
225         add("module",      &Loader::module);
226         add("headers",     &Loader::headers);
227         add("tarball",     &Loader::tarball);
228         add("tar_file",    &Loader::tar_file);
229 }
230
231 void SourcePackage::Loader::finish()
232 {
233         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
234         spkg.components.sort(component_sort);
235 }
236
237 void SourcePackage::Loader::feature(const string &n, const string &d)
238 {
239         static_cast<SourcePackage &>(pkg).features.push_back(Feature(n, d));
240 }
241
242 void SourcePackage::Loader::condition(const string &c)
243 {
244         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
245         Condition cond(spkg, c);
246         load_sub(cond);
247         spkg.conditions.push_back(cond);
248 }
249
250 void SourcePackage::Loader::program(const string &n)
251 {
252         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
253         Component prog(spkg, Component::PROGRAM, n);
254         load_sub(prog);
255         spkg.components.push_back(prog);
256 }
257
258 void SourcePackage::Loader::library(const string &n)
259 {
260         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
261         Component prog(spkg, Component::LIBRARY, n);
262         load_sub(prog);
263         spkg.components.push_back(prog);
264 }
265
266 void SourcePackage::Loader::module(const string &n)
267 {
268         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
269         Component prog(spkg, Component::MODULE, n);
270         load_sub(prog);
271         spkg.components.push_back(prog);
272 }
273
274 void SourcePackage::Loader::headers(const string &n)
275 {
276         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
277         Component prog(spkg, Component::HEADERS, n);
278         load_sub(prog);
279         spkg.components.push_back(prog);
280 }
281
282 void SourcePackage::Loader::build_info()
283 {
284         load_sub(static_cast<SourcePackage &>(pkg).build_info);
285 }
286
287 void SourcePackage::Loader::tarball(const string &n)
288 {
289         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
290         if(n=="@src")
291         {
292                 for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.components.end(); ++i)
293                         if(i->get_type()==Component::TARBALL && i->get_name()==n)
294                                 load_sub(*i);
295         }
296         else
297         {
298                 Component trbl(spkg, Component::TARBALL, n);
299                 load_sub(trbl);
300         }
301 }
302
303 void SourcePackage::Loader::tar_file(const string &f)
304 {
305         IO::print("%s: Note: tar_file is deprecated\n", get_source());
306         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
307         for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.components.end(); ++i)
308                 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
309                         const_cast<PathList &>(i->get_sources()).push_back(spkg.source/f);
310 }