]> git.tdb.fi Git - builder.git/blob - source/sourcepackage.cpp
Split class Package into SourcePackage and BinaryPackage
[builder.git] / source / sourcepackage.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <iostream>
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 /**
20 Creates a buildable package.
21 */
22 SourcePackage::SourcePackage(Builder &b, const string &n, const Path::Path &s):
23         Package(b, n),
24         source(s),
25         config(*this),
26         deps_cache(*this)
27 {
28         tar_files.push_back(source/"Build");
29 }
30
31 Msp::Path::Path SourcePackage::get_temp_dir() const
32 {
33         return source/config.get_option("tempdir").value/config.get_option("profile").value;
34 }
35
36 Msp::Path::Path SourcePackage::get_out_dir() const
37 {
38         return source/config.get_option("outdir").value;
39 }
40
41 /**
42 Checks which kinds of things the components of this package install.
43
44 @return  A bitmask of installed things
45 */
46 unsigned SourcePackage::get_install_flags()
47 {
48         unsigned flags=0;
49         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
50         {
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                 }
58                 if(!i->get_install_headers().empty())
59                         flags|=INCLUDE;
60         }
61
62         return flags;
63 }
64
65 LibMode SourcePackage::get_library_mode() const
66 {
67         const string &mode=config.get_option("staticlibs").value;
68         if(mode=="all")
69                 return ALL_STATIC;
70         else if(mode=="local")
71                 return LOCAL_STATIC;
72         else if(mode=="none")
73                 return DYNAMIC;
74         else
75                 throw Exception("Unknown library mode");
76 }
77
78 /**
79 Tries to resolve all references to dependency packages.
80 */
81 void SourcePackage::resolve_refs()
82 {
83         Package::resolve_refs();
84
85         for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
86                 if(i->get_package())
87                         all_reqs.push_back(i->get_package());
88
89         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
90         {
91                 i->resolve_refs();
92                 const PkgRefList &creqs=i->get_requires();
93                 for(PkgRefList::const_iterator j=creqs.begin(); j!=creqs.end(); ++j)
94                         if(j->get_package())
95                                 all_reqs.push_back(j->get_package());
96         }
97
98         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
99         {
100                 i->resolve_refs();
101                 const PkgRefList &creqs=i->get_requires();
102                 for(PkgRefList::const_iterator j=creqs.begin(); j!=creqs.end(); ++j)
103                         if(j->get_package())
104                                 all_reqs.push_back(j->get_package());
105         }
106 }
107
108 /*** private ***/
109
110 /**
111 Processes configuration options that were most likely obtained from the command
112 line.
113 */
114 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
115 {
116         init_config();
117
118         StringMap::const_iterator prof=opts.find("profile");
119         if(prof!=opts.end() && flag)
120                 config.select_profile(prof->second);
121         else
122                 config.select_last_profile();
123
124         if(flag && config.update(opts))
125         {
126                 if(builder.get_verbose()>=2)
127                         cout<<"Configuration of "<<name<<" changed\n";
128                 if(!builder.get_dry_run())
129                         config.save();
130         }
131
132         config.finish();
133
134         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
135                 if(i->eval())
136                 {
137                         const PkgRefList &reqs=i->get_requires();
138                         requires.insert(requires.end(), reqs.begin(), reqs.end());
139                         build_info.add(i->get_build_info());
140                 }
141
142         for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
143         {
144                 BinaryPackage *bpkg=dynamic_cast<BinaryPackage *>(*i);
145                 if(bpkg && bpkg->get_need_path())
146                         bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
147         }
148
149         deps_cache.load();
150
151         for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
152                 (*i)->configure(opts, flag&2);
153 }
154
155 /**
156 Initializes configuration options.
157 */
158 void SourcePackage::init_config()
159 {
160         config.add_option("profile",    "default", "Configuration profile");
161         config.add_option("tempdir",    "temp",    "Directory for storing temporary files");
162         config.add_option("outdir",     ".",       "Directory to put build results in");
163         config.add_option("optimize",   "0",       "Apply compiler optimizations");
164         config.add_option("strip",      "0",       "Strip symbols from programs");
165         config.add_option("debug",      "0",       "Produce debugging symbols");
166         config.add_option("cpu",        "auto",    "CPU type to optimize for");
167         config.add_option("arch",       "native",  "Architecture for cross-compiling");
168         config.add_option("staticlibs", "local",   "Use static libraries");
169
170         unsigned flags=get_install_flags();
171         if(flags)
172                 config.add_option("prefix",     "$HOME/local",     "Installation prefix");
173         /*if(flags&INCLUDE)
174                 config.add_option("includedir", "$prefix/include", "Header installation directory");
175         if(flags&BIN)
176                 config.add_option("includedir", "$prefix/bin",     "Binary installation directory");
177         if(flags&LIB)
178                 config.add_option("includedir", "$prefix/lib",     "Library installation directory");
179         if(flags&DATA)
180                 config.add_option("includedir", "$prefix/share",   "Data installation directory");*/
181
182         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
183                 config.add_option("with_"+i->name, "0", i->descr);
184
185         for(PackageList::const_iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
186         {
187                 BinaryPackage *bpkg=dynamic_cast<BinaryPackage *>(*i);
188                 if(bpkg && bpkg->get_need_path())
189                         config.add_option(bpkg->get_name()+"_path", "", "Path for "+bpkg->get_name());
190         }
191 }
192
193 /**
194 Fills in build info based on configuration.  All required packages must be
195 configured when this is called.
196 */
197 void SourcePackage::create_build_info()
198 {
199         for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
200         {
201                 Package *pkg=i->get_package();
202                 if(!pkg)
203                         continue;
204                 const BuildInfo &ebi=pkg->get_exported_binfo();
205                 build_info.add(ebi);
206
207                 export_binfo.cflags.insert(export_binfo.cflags.end(), ebi.cflags.begin(), ebi.cflags.end());
208                 export_binfo.incpath.insert(export_binfo.incpath.end(), ebi.incpath.begin(), ebi.incpath.end());
209                 export_binfo.defines.insert(export_binfo.defines.end(), ebi.defines.begin(), ebi.defines.end());
210         }
211
212         build_info.cflags.push_back("-Wall");
213         build_info.cflags.push_back("-Wshadow");
214         build_info.cflags.push_back("-Wextra");
215         build_info.cflags.push_back("-Wpointer-arith");
216         build_info.cflags.push_back("-Wconversion");
217         build_info.cflags.push_back("-Werror");
218
219         unsigned flags=get_install_flags();
220
221         if(flags&INCLUDE)
222                 export_binfo.incpath.push_back((Path::Path(config.get_option("prefix").value)/"include").str());
223         if(flags&LIB)
224                 export_binfo.libpath.push_back((Path::Path(config.get_option("prefix").value)/"lib").str());
225
226         string optimize=config.get_option("optimize").value;
227         if(lexical_cast<unsigned>(optimize))
228         {
229                 build_info.cflags.push_back("-O"+optimize);
230                 build_info.ldflags.push_back("-O"+optimize);
231                 string cpu=config.get_option("cpu").value;
232                 if(cpu!="auto")
233                         build_info.cflags.push_back("-march="+cpu);
234         }
235
236         if(lexical_cast<bool>(config.get_option("debug").value))
237         {
238                 build_info.cflags.push_back("-ggdb");
239                 build_info.defines.push_back("DEBUG");
240         }
241
242         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
243                 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
244                         build_info.cflags.push_back("-DWITH_"+toupper(i->name));
245
246         build_info.unique();
247
248         for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
249         {
250                 i->create_build_info();
251                 if(i->get_type()==Component::LIBRARY)
252                         export_binfo.libs.push_back(i->get_name());
253         }
254
255         export_binfo.unique();
256 }
257
258
259 SourcePackage::Loader::Loader(Package &p):
260         Package::Loader(p)
261 {
262         add("version",     &SourcePackage::version);
263         add("description", &SourcePackage::description);
264         add("build_info",  &Loader::build_info);
265         add("feature",     &Loader::feature);
266         add("if",          &Loader::condition);
267         add("program",     &Loader::program);
268         add("library",     &Loader::library);
269         add("module",      &Loader::module);
270         add("headers",     &Loader::headers);
271         add("tar_file",    &Loader::tar_file);
272 }
273
274 void SourcePackage::Loader::feature(const string &n, const string &d)
275 {
276         static_cast<SourcePackage &>(pkg).features.push_back(Feature(n, d));
277 }
278
279 void SourcePackage::Loader::condition(const string &c)
280 {
281         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
282         Condition cond(spkg, c);
283         load_sub(cond);
284         spkg.conditions.push_back(cond);
285 }
286
287 void SourcePackage::Loader::program(const string &n)
288 {
289         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
290         Component prog(spkg, Component::PROGRAM, n);
291         load_sub(prog);
292         spkg.components.push_back(prog);
293 }
294
295 void SourcePackage::Loader::library(const string &n)
296 {
297         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
298         Component prog(spkg, Component::LIBRARY, n);
299         load_sub(prog);
300         spkg.components.push_back(prog);
301 }
302
303 void SourcePackage::Loader::module(const string &n)
304 {
305         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
306         Component prog(spkg, Component::MODULE, n);
307         load_sub(prog);
308         spkg.components.push_back(prog);
309 }
310
311 void SourcePackage::Loader::headers(const string &n)
312 {
313         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
314         Component prog(spkg, Component::HEADERS, n);
315         load_sub(prog);
316         spkg.components.push_back(prog);
317 }
318
319 void SourcePackage::Loader::build_info()
320 {
321         load_sub(static_cast<SourcePackage &>(pkg).build_info);
322 }
323
324 void SourcePackage::Loader::tar_file(const string &f)
325 {
326         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
327         spkg.tar_files.push_back(spkg.source/f);
328 }