]> git.tdb.fi Git - builder.git/blob - source/sourcepackage.cpp
Make BuildInfo uniquify things implicitly after update
[builder.git] / source / sourcepackage.cpp
1 #include <cstdlib>
2 #include <msp/io/print.h>
3 #include <msp/strings/lexicalcast.h>
4 #include <msp/strings/utils.h>
5 #include "binarypackage.h"
6 #include "builder.h"
7 #include "misc.h"
8 #include "sourcepackage.h"
9
10 using namespace std;
11 using namespace Msp;
12
13 namespace {
14
15 bool component_sort(const Component &c1, const Component &c2)
16 { return c1.get_type()<c2.get_type(); }
17
18 }
19
20
21 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &s):
22         Package(b, n),
23         source(s),
24         config(*this),
25         deps_cache(*this)
26 {
27         components.push_back(Component(*this, Component::TARBALL, "@src"));
28 }
29
30 FS::Path SourcePackage::get_temp_dir() const
31 {
32         string subdir = format("%s.%s", builder.get_current_arch().get_name(), config.get_option("profile").value);
33         return source/config.get_option("tempdir").value/subdir;
34 }
35
36 FS::Path SourcePackage::get_out_dir() const
37 {
38         const Architecture &arch = builder.get_current_arch();
39         if(arch.is_native())
40                 return source/config.get_option("outdir").value;
41         else
42                 return source/arch.get_name()/config.get_option("outdir").value;
43 }
44
45 unsigned SourcePackage::get_install_flags()
46 {
47         unsigned flags = 0;
48         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
49                 if(i->get_install())
50                 {
51                         if(i->get_type()==Component::PROGRAM)
52                                 flags |= BIN;
53                         else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
54                                 flags |= LIB;
55                         else if(i->get_type()==Component::HEADERS)
56                                 flags |= INCLUDE;
57                 }
58
59         return flags;
60 }
61
62 LibMode SourcePackage::get_library_mode() const
63 {
64         const string &mode = config.get_option("staticlibs").value;
65         if(mode=="all")
66                 return ALL_STATIC;
67         else if(mode=="local")
68                 return LOCAL_STATIC;
69         else if(mode=="none")
70                 return DYNAMIC;
71         else
72                 throw runtime_error("unknown library mode");
73 }
74
75 string SourcePackage::expand_string(const string &str) const
76 {
77         string result = str;
78         string::size_type dollar = 0;
79         unsigned n = 0;
80         while((dollar = result.find('$'))!=string::npos)
81         {
82                 if(n>1000)
83                         throw bad_expansion("nested too deep");
84
85                 string::size_type end;
86                 string var;
87                 if(dollar+1<result.size() && result[dollar+1]=='{')
88                 {
89                         end = result.find('}', dollar+2);
90                         if(end==string::npos)
91                                 throw bad_expansion("unterminated variable reference");
92                         var = result.substr(dollar+2, end-dollar-2);
93                         ++end;
94                 }
95                 else
96                 {
97                         for(end=dollar+1; (isalnum(result[end]) || result[end]=='_'); ++end) ;
98                         var = result.substr(dollar+1, end-dollar-1);
99                 }
100
101                 string value;
102                 if(config.is_option(var))
103                         value = config.get_option(var).value;
104                 else if(var=="arch")
105                         value = builder.get_current_arch().get_name();
106                 else if(var=="system")
107                         value = builder.get_current_arch().get_system();
108                 else if(const char *ptr = getenv(var.c_str()))
109                         value = ptr;
110
111                 result.replace(dollar, end-dollar, value);
112
113                 ++n;
114         }
115
116         return result;
117 }
118
119 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
120 {
121         init_config();
122
123         StringMap::const_iterator prof = opts.find("profile");
124         if(prof!=opts.end() && flag)
125                 config.select_profile(prof->second);
126         else
127                 config.select_last_profile();
128
129         if(flag && config.update(opts))
130         {
131                 if(builder.get_verbose()>=2)
132                         IO::print("Configuration of %s changed\n", name);
133                 if(!builder.get_dry_run())
134                         config.save();
135         }
136
137         config.finish();
138
139         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
140                 if(i->eval())
141                 {
142                         const StringList &reqs = i->get_requires();
143                         for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
144                                 if(Package *pkg = builder.get_package(*j))
145                                         requires.push_back(pkg);
146                 }
147
148         for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
149         {
150                 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
151                 if(bpkg && bpkg->get_need_path())
152                         bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
153         }
154
155         deps_cache.load();
156
157         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
158                 i->configure(opts, flag);
159 }
160
161 void SourcePackage::init_config()
162 {
163         config.add_option("profile",    "default", "Configuration profile");
164         config.add_option("tempdir",    "temp",    "Directory for storing temporary files");
165         config.add_option("outdir",     ".",       "Directory to put build results in");
166         config.add_option("optimize",   "0",       "Compiler optimization level");
167         config.add_option("strip",      "no",      "Strip symbols from programs");
168         config.add_option("debug",      "no",      "Produce debugging symbols");
169         config.add_option("staticlibs", "local",   "Use static libraries");
170
171         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
172                 config.add_option("with_"+i->name, i->def_value, i->descr);
173
174         for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
175         {
176                 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
177                 if(bpkg && bpkg->get_need_path())
178                         config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
179         }
180 }
181
182 void SourcePackage::create_build_info()
183 {
184         // XXX Currently, a package-specific settings will override cmdline.  This might or might not be desirable.
185         const StringList &warnings = builder.get_warnings();
186         build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
187
188         unsigned flags = get_install_flags();
189
190         build_info.incpath.push_back((builder.get_prefix()/"include").str());
191         build_info.libpath.push_back((builder.get_prefix()/"lib").str());
192
193         if(flags&INCLUDE)
194                 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
195         if(flags&LIB)
196                 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
197
198         string optimize = config.get_option("optimize").value;
199         if(!optimize.empty() && optimize!="0")
200         {
201                 if(optimize=="s" || optimize=="size")
202                         build_info.optimize = -1;
203                 else
204                         build_info.optimize = lexical_cast<unsigned>(optimize);
205         }
206
207         if(lexical_cast<bool>(config.get_option("debug").value))
208         {
209                 build_info.debug = true;
210                 build_info.defines["DEBUG"] = "1";
211         }
212
213         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
214                 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
215                         build_info.defines["WITH_"+toupper(i->name)] = "1";
216
217         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
218                 if(i->eval())
219                         build_info.update_from(i->get_build_info());
220
221         for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
222         {
223                 i->create_build_info();
224                 if(i->get_type()==Component::LIBRARY)
225                         export_binfo.libs.push_back(i->get_name());
226         }
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::component<Component::PROGRAM>);
239         add("library",     &Loader::component<Component::LIBRARY>);
240         add("module",      &Loader::component<Component::MODULE>);
241         add("headers",     &Loader::component<Component::HEADERS>);
242         add("install",     &Loader::component<Component::INSTALL>);
243         add("datafile",    &Loader::component<Component::DATAFILE>);
244         add("tarball",     &Loader::tarball);
245         add("tar_file",    &Loader::tar_file);
246 }
247
248 void SourcePackage::Loader::finish()
249 {
250         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
251         spkg.components.sort(component_sort);
252 }
253
254 void SourcePackage::Loader::feature(const string &n, const string &d)
255 {
256         Feature feat(n);
257         feat.descr = d;
258         feat.def_value = "no";
259         load_sub(feat);
260         static_cast<SourcePackage &>(pkg).features.push_back(feat);
261 }
262
263 void SourcePackage::Loader::condition(const string &c)
264 {
265         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
266         Condition cond(spkg, c);
267         load_sub(cond);
268         spkg.conditions.push_back(cond);
269 }
270
271 template<Component::Type t>
272 void SourcePackage::Loader::component(const string &n)
273 {
274         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
275         Component comp(spkg, t, n);
276         load_sub(comp);
277         spkg.components.push_back(comp);
278 }
279
280 void SourcePackage::Loader::build_info()
281 {
282         load_sub(static_cast<SourcePackage &>(pkg).build_info);
283 }
284
285 void SourcePackage::Loader::tarball(const string &n)
286 {
287         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
288         if(n=="@src")
289         {
290                 for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.components.end(); ++i)
291                         if(i->get_type()==Component::TARBALL && i->get_name()==n)
292                                 load_sub(*i);
293         }
294         else
295         {
296                 Component trbl(spkg, Component::TARBALL, n);
297                 load_sub(trbl);
298         }
299 }
300
301 void SourcePackage::Loader::tar_file(const string &f)
302 {
303         IO::print("%s: Note: tar_file is deprecated\n", get_source());
304         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
305         for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.components.end(); ++i)
306                 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
307                         const_cast<StringList &>(i->get_sources()).push_back((spkg.source/f).str());
308 }