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