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