]> git.tdb.fi Git - builder.git/blob - source/sourcepackage.cpp
Change arch and prefix to global options
[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 &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 SourcePackage::get_temp_dir() const
32 {
33         return source/config.get_option("tempdir").value/builder.get_current_arch().get_name()/config.get_option("profile").value;
34 }
35
36 Msp::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 /*** private ***/
79
80 /**
81 Processes configuration options that were most likely obtained from the command
82 line.
83 */
84 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
85 {
86         init_config();
87
88         StringMap::const_iterator prof=opts.find("profile");
89         if(prof!=opts.end() && flag)
90                 config.select_profile(prof->second);
91         else
92                 config.select_last_profile();
93
94         if(flag && config.update(opts))
95         {
96                 if(builder.get_verbose()>=2)
97                         cout<<"Configuration of "<<name<<" changed\n";
98                 if(!builder.get_dry_run())
99                         config.save();
100         }
101
102         config.finish();
103
104         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
105                 if(i->eval())
106                 {
107                         const StringList &reqs=i->get_requires();
108                         for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
109                                 if(Package *pkg=builder.get_package(*j))
110                                         requires.push_back(pkg);
111                 }
112
113         base_reqs=requires;
114
115         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
116         {
117                 const PackageList &reqs=i->get_requires();
118                 requires.insert(requires.end(), reqs.begin(), reqs.end());
119         }
120
121         for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
122         {
123                 BinaryPackage *bpkg=dynamic_cast<BinaryPackage *>(*i);
124                 if(bpkg && bpkg->get_need_path())
125                         bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
126         }
127
128         deps_cache.load();
129
130         /*for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
131                 (*i)->configure(opts, flag&2);*/
132 }
133
134 /**
135 Initializes configuration options.
136 */
137 void SourcePackage::init_config()
138 {
139         config.add_option("profile",    "default", "Configuration profile");
140         config.add_option("tempdir",    "temp",    "Directory for storing temporary files");
141         config.add_option("outdir",     ".",       "Directory to put build results in");
142         config.add_option("optimize",   "0",       "Apply compiler optimizations");
143         config.add_option("strip",      "0",       "Strip symbols from programs");
144         config.add_option("debug",      "0",       "Produce debugging symbols");
145         config.add_option("cpu",        "auto",    "CPU type to optimize for");
146         config.add_option("staticlibs", "local",   "Use static libraries");
147
148         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
149                 config.add_option("with_"+i->name, "0", i->descr);
150
151         for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
152         {
153                 BinaryPackage *bpkg=dynamic_cast<BinaryPackage *>(*i);
154                 if(bpkg && bpkg->get_need_path())
155                         config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
156         }
157 }
158
159 /**
160 Fills in build info based on configuration.  All required packages must be
161 configured when this is called.
162 */
163 void SourcePackage::create_build_info()
164 {
165         for(PackageList::iterator i=base_reqs.begin(); i!=base_reqs.end(); ++i)
166         {
167                 const BuildInfo &ebi=(*i)->get_exported_binfo();
168                 build_info.add(ebi);
169
170                 export_binfo.cflags.insert(export_binfo.cflags.end(), ebi.cflags.begin(), ebi.cflags.end());
171                 export_binfo.incpath.insert(export_binfo.incpath.end(), ebi.incpath.begin(), ebi.incpath.end());
172                 export_binfo.defines.insert(export_binfo.defines.end(), ebi.defines.begin(), ebi.defines.end());
173         }
174
175         build_info.cflags.push_back("-Wall");
176         build_info.cflags.push_back("-Wshadow");
177         build_info.cflags.push_back("-Wextra");
178         build_info.cflags.push_back("-Wpointer-arith");
179         build_info.cflags.push_back("-Wconversion");
180         build_info.cflags.push_back("-Werror");
181
182         unsigned flags=get_install_flags();
183
184         build_info.incpath.push_back((Path(builder.get_prefix())/"include").str());
185         build_info.libpath.push_back((Path(builder.get_prefix())/"lib").str());
186
187         if(flags&INCLUDE)
188                 export_binfo.incpath.push_back((Path(builder.get_prefix())/"include").str());
189         if(flags&LIB)
190                 export_binfo.libpath.push_back((Path(builder.get_prefix())/"lib").str());
191
192         string optimize=config.get_option("optimize").value;
193         if(lexical_cast<unsigned>(optimize))
194         {
195                 build_info.cflags.push_back("-O"+optimize);
196                 build_info.ldflags.push_back("-O"+optimize);
197                 string cpu=config.get_option("cpu").value;
198                 if(cpu!="auto")
199                         build_info.cflags.push_back("-march="+cpu);
200         }
201
202         if(lexical_cast<bool>(config.get_option("debug").value))
203         {
204                 build_info.cflags.push_back("-ggdb");
205                 build_info.defines.push_back("DEBUG");
206         }
207
208         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
209                 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
210                         build_info.cflags.push_back("-DWITH_"+toupper(i->name));
211
212         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
213                 if(i->eval())
214                         build_info.add(i->get_build_info());
215
216         build_info.unique();
217
218         for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
219         {
220                 i->create_build_info();
221                 if(i->get_type()==Component::LIBRARY)
222                         export_binfo.libs.push_back(i->get_name());
223         }
224
225         export_binfo.unique();
226 }
227
228
229 SourcePackage::Loader::Loader(Package &p):
230         Package::Loader(p)
231 {
232         add("version",     &SourcePackage::version);
233         add("description", &SourcePackage::description);
234         add("build_info",  &Loader::build_info);
235         add("feature",     &Loader::feature);
236         add("if",          &Loader::condition);
237         add("program",     &Loader::program);
238         add("library",     &Loader::library);
239         add("module",      &Loader::module);
240         add("headers",     &Loader::headers);
241         add("tar_file",    &Loader::tar_file);
242 }
243
244 void SourcePackage::Loader::feature(const string &n, const string &d)
245 {
246         static_cast<SourcePackage &>(pkg).features.push_back(Feature(n, d));
247 }
248
249 void SourcePackage::Loader::condition(const string &c)
250 {
251         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
252         Condition cond(spkg, c);
253         load_sub(cond);
254         spkg.conditions.push_back(cond);
255 }
256
257 void SourcePackage::Loader::program(const string &n)
258 {
259         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
260         Component prog(spkg, Component::PROGRAM, n);
261         load_sub(prog);
262         spkg.components.push_back(prog);
263 }
264
265 void SourcePackage::Loader::library(const string &n)
266 {
267         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
268         Component prog(spkg, Component::LIBRARY, n);
269         load_sub(prog);
270         spkg.components.push_back(prog);
271 }
272
273 void SourcePackage::Loader::module(const string &n)
274 {
275         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
276         Component prog(spkg, Component::MODULE, n);
277         load_sub(prog);
278         spkg.components.push_back(prog);
279 }
280
281 void SourcePackage::Loader::headers(const string &n)
282 {
283         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
284         Component prog(spkg, Component::HEADERS, n);
285         load_sub(prog);
286         spkg.components.push_back(prog);
287 }
288
289 void SourcePackage::Loader::build_info()
290 {
291         load_sub(static_cast<SourcePackage &>(pkg).build_info);
292 }
293
294 void SourcePackage::Loader::tar_file(const string &f)
295 {
296         SourcePackage &spkg=static_cast<SourcePackage &>(pkg);
297         spkg.tar_files.push_back(spkg.source/f);
298 }