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