]> git.tdb.fi Git - builder.git/blob - source/sourcepackage.cpp
Externalize dry run handling from Config and DependencyCache
[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 "pkgconfigfile.h"
9 #include "tool.h"
10 #include "sourcepackage.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 namespace {
16
17 bool component_sort(const Component &c1, const Component &c2)
18 { return c1.get_type()<c2.get_type(); }
19
20 }
21
22
23 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &s):
24         Package(b, n),
25         source(s),
26         config(*this),
27         deps_cache(*this)
28 {
29         components.push_back(Component(*this, Component::TARBALL, "@src"));
30 }
31
32 FS::Path SourcePackage::get_temp_dir() const
33 {
34         string subdir = format("%s.%s", builder.get_current_arch().get_name(), config.get_option("profile").value);
35         return source/config.get_option("tempdir").value/subdir;
36 }
37
38 FS::Path SourcePackage::get_out_dir() const
39 {
40         const Architecture &arch = builder.get_current_arch();
41         if(arch.is_native())
42                 return source/config.get_option("outdir").value;
43         else
44                 return source/arch.get_name()/config.get_option("outdir").value;
45 }
46
47 unsigned SourcePackage::get_install_flags()
48 {
49         unsigned flags = 0;
50         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
51                 if(i->get_install())
52                 {
53                         if(i->get_type()==Component::PROGRAM)
54                                 flags |= BIN;
55                         else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
56                                 flags |= LIB|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                 builder.get_logger().log("configure", format("Configuration of %s changed", name));
131
132         config.finish();
133
134         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
135                 if(i->eval())
136                 {
137                         const StringList &reqs = i->get_requires();
138                         for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
139                                 if(Package *pkg = builder.get_package_manager().find_package(*j))
140                                         requires.push_back(pkg);
141                 }
142
143         for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
144         {
145                 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
146                 if(bpkg && bpkg->get_need_path())
147                         bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
148         }
149
150         deps_cache.load();
151
152         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
153                 i->configure(opts, flag);
154 }
155
156 void SourcePackage::init_config()
157 {
158         config.add_option("profile",    "default", "Configuration profile");
159         config.add_option("tempdir",    "temp",    "Directory for storing temporary files");
160         config.add_option("outdir",     ".",       "Directory to put build results in");
161         config.add_option("optimize",   "0",       "Compiler optimization level");
162         config.add_option("strip",      "no",      "Strip symbols from programs");
163         config.add_option("debug",      "no",      "Produce debugging symbols");
164         config.add_option("staticlibs", "local",   "Use static libraries");
165
166         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
167                 config.add_option("with_"+i->name, i->def_value, i->descr);
168
169         for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
170         {
171                 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
172                 if(bpkg && bpkg->get_need_path())
173                         config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
174         }
175 }
176
177 void SourcePackage::create_build_info()
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(!optimize.empty() && optimize!="0")
195         {
196                 if(optimize=="s" || optimize=="size")
197                         build_info.optimize = -1;
198                 else
199                         build_info.optimize = lexical_cast<unsigned>(optimize);
200         }
201
202         if(lexical_cast<bool>(config.get_option("debug").value))
203         {
204                 build_info.debug = true;
205                 build_info.defines["DEBUG"] = "1";
206         }
207
208         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
209                 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
210                         build_info.defines["WITH_"+toupper(i->name)] = "1";
211
212         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
213                 if(i->eval())
214                         build_info.update_from(i->get_build_info());
215
216         for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
217         {
218                 i->create_build_info();
219                 if(i->get_type()==Component::LIBRARY)
220                         export_binfo.libs.push_back(i->get_name());
221         }
222 }
223
224 void SourcePackage::create_targets()
225 {
226         bool pc_needed = false;
227         for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
228         {
229                 i->create_targets();
230                 if(i->get_type()==Component::LIBRARY)
231                         pc_needed = true;
232         }
233
234         if(pc_needed)
235         {
236                 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
237                 builder.get_target("install")->add_depend(*builder.get_toolchain().get_tool("CP").create_target(*pc));
238         }
239 }
240
241 void SourcePackage::save_caches()
242 {
243         config.save();
244         deps_cache.save();
245 }
246
247
248 SourcePackage::Loader::Loader(SourcePackage &p):
249         DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
250 {
251         add("version",     &SourcePackage::version);
252         add("description", &SourcePackage::description);
253         add("build_info",  &Loader::build_info);
254         add("feature",     &Loader::feature);
255         add("if",          &Loader::condition);
256         add("program",     &Loader::component<Component::PROGRAM>);
257         add("library",     &Loader::component<Component::LIBRARY>);
258         add("module",      &Loader::component<Component::MODULE>);
259         add("headers",     &Loader::headers);
260         add("install",     &Loader::component<Component::INSTALL>);
261         add("datafile",    &Loader::component<Component::DATAFILE>);
262         add("tarball",     &Loader::tarball);
263         add("tar_file",    &Loader::tar_file);
264 }
265
266 void SourcePackage::Loader::finish()
267 {
268         obj.components.sort(component_sort);
269
270         for(map<string, string>::const_iterator i=install_map.begin(); i!=install_map.end(); ++i)
271         {
272                 for(ComponentList::iterator j=obj.components.begin(); j!=obj.components.end(); ++j)
273                 {
274                         const StringList &sources = j->get_sources();
275                         for(StringList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
276                         {
277                                 if(!i->first.compare(0, k->size(), *k))
278                                 {
279                                         const_cast<InstallMap &>(j->get_install_map()).add_mapping(obj.source/i->first, i->second);
280                                 }
281                         }
282                 }
283         }
284 }
285
286 void SourcePackage::Loader::feature(const string &n, const string &d)
287 {
288         Feature feat(n);
289         feat.descr = d;
290         feat.def_value = "no";
291         load_sub(feat);
292         obj.features.push_back(feat);
293 }
294
295 void SourcePackage::Loader::condition(const string &c)
296 {
297         Condition cond(obj, c);
298         load_sub(cond);
299         obj.conditions.push_back(cond);
300 }
301
302 template<Component::Type t>
303 void SourcePackage::Loader::component(const string &n)
304 {
305         Component comp(obj, t, n);
306         load_sub(comp);
307         obj.components.push_back(comp);
308 }
309
310 void SourcePackage::Loader::build_info()
311 {
312         load_sub(obj.build_info);
313 }
314
315 void SourcePackage::Loader::headers(const string &n)
316 {
317         IO::print("%s: Note: headers components are deprecated\n", get_source());
318         Component comp(obj, Component::LIBRARY, n);
319         load_sub(comp);
320         const StringList &sources = comp.get_sources();
321         for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
322                 install_map[*i] = "include/"+comp.get_name();
323 }
324
325 void SourcePackage::Loader::tarball(const string &n)
326 {
327         if(n=="@src")
328         {
329                 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
330                         if(i->get_type()==Component::TARBALL && i->get_name()==n)
331                                 load_sub(*i);
332         }
333         else
334         {
335                 Component trbl(obj, Component::TARBALL, n);
336                 load_sub(trbl);
337         }
338 }
339
340 void SourcePackage::Loader::tar_file(const string &f)
341 {
342         IO::print("%s: Note: tar_file is deprecated\n", get_source());
343         for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
344                 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
345                         const_cast<StringList &>(i->get_sources()).push_back((obj.source/f).str());
346 }