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