]> git.tdb.fi Git - builder.git/blob - source/sourcepackage.cpp
Redesign the library mode system
[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         build_type(0),
27         config(*this),
28         deps_cache(*this)
29 {
30         components.push_back(Component(*this, Component::TARBALL, "@src"));
31 }
32
33 void SourcePackage::set_build_type(const BuildType &t)
34 {
35         build_type = &t;
36 }
37
38 FS::Path SourcePackage::get_temp_dir() const
39 {
40         string subdir = builder.get_current_arch().get_name();
41         if(build_type)
42         {
43                 subdir += '.';
44                 subdir += build_type->get_name();
45         }
46         return source/config.get_option("tempdir").value/subdir;
47 }
48
49 FS::Path SourcePackage::get_out_dir() const
50 {
51         const Architecture &arch = builder.get_current_arch();
52         string detail = (build_type ? build_type->get_name() : string());
53         if(arch.is_native())
54                 return source/detail;
55         else
56                 return source/arch.get_name()/detail;
57 }
58
59 LibMode SourcePackage::get_library_mode() const
60 {
61         const string &mode = config.get_option("staticlibs").value;
62         if(mode=="force")
63                 return FORCE_STATIC;
64         else if(mode=="prefer")
65                 return STATIC;
66         else if(mode=="avoid")
67                 return DYNAMIC;
68         else if(mode=="reject")
69                 return FORCE_DYNAMIC;
70         else
71                 throw runtime_error("unknown library mode");
72 }
73
74 string SourcePackage::expand_string(const string &str) const
75 {
76         string result = str;
77         string::size_type dollar = 0;
78         unsigned n = 0;
79         while((dollar = result.find('$'))!=string::npos)
80         {
81                 if(n>1000)
82                         throw bad_expansion("nested too deep");
83
84                 string::size_type end;
85                 string var;
86                 if(dollar+1<result.size() && result[dollar+1]=='{')
87                 {
88                         end = result.find('}', dollar+2);
89                         if(end==string::npos)
90                                 throw bad_expansion("unterminated variable reference");
91                         var = result.substr(dollar+2, end-dollar-2);
92                         ++end;
93                 }
94                 else
95                 {
96                         for(end=dollar+1; (isalnum(result[end]) || result[end]=='_'); ++end) ;
97                         var = result.substr(dollar+1, end-dollar-1);
98                 }
99
100                 string value;
101                 if(config.is_option(var))
102                         value = config.get_option(var).value;
103                 else if(var=="arch")
104                         value = builder.get_current_arch().get_name();
105                 else if(var=="system")
106                         value = builder.get_current_arch().get_system();
107                 else if(const char *ptr = getenv(var.c_str()))
108                         value = ptr;
109
110                 result.replace(dollar, end-dollar, value);
111
112                 ++n;
113         }
114
115         return result;
116 }
117
118 void SourcePackage::do_configure(const StringMap &opts, unsigned flag)
119 {
120         init_config();
121
122         config.load();
123
124         if(flag && config.update(opts))
125                 builder.get_logger().log("configure", format("Configuration of %s changed", name));
126
127         config.finish();
128
129         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
130                 if(i->eval())
131                 {
132                         const StringList &reqs = i->get_requires();
133                         for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
134                                 if(Package *pkg = builder.get_package_manager().find_package(*j))
135                                         requires.push_back(pkg);
136                 }
137
138         for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
139         {
140                 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
141                 if(bpkg && bpkg->get_need_path())
142                         bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
143         }
144
145         deps_cache.load();
146
147         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
148                 i->configure(opts, flag);
149 }
150
151 void SourcePackage::init_config()
152 {
153         config.add_option("tempdir",    "temp",    "Directory for storing temporary files");
154         config.add_option("staticlibs", "local",   "Use static libraries");
155
156         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
157                 config.add_option("with_"+i->name, i->def_value, i->descr);
158
159         for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
160         {
161                 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
162                 if(bpkg && bpkg->get_need_path())
163                         config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
164         }
165 }
166
167 void SourcePackage::create_build_info()
168 {
169         if(build_type)
170                 build_info.update_from(build_type->get_build_info());
171
172         // XXX Currently, a package-specific settings will override cmdline.  This might or might not be desirable.
173         const StringList &warnings = builder.get_warnings();
174         build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
175
176         build_info.incpath.push_back((builder.get_prefix()/"include").str());
177         build_info.libpath.push_back((builder.get_prefix()/"lib").str());
178
179         bool export_paths = false;
180         for(ComponentList::const_iterator i=components.begin(); (!export_paths && i!=components.end()); ++i)
181                 export_paths = (i->get_type()==Component::LIBRARY);
182
183         if(export_paths)
184         {
185                 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
186                 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
187         }
188
189         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
190                 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
191                         build_info.defines["WITH_"+toupper(i->name)] = "1";
192
193         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
194                 if(i->eval())
195                         build_info.update_from(i->get_build_info());
196
197         for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
198         {
199                 i->create_build_info();
200                 if(i->get_type()==Component::LIBRARY)
201                         export_binfo.libs.push_back(i->get_name());
202         }
203 }
204
205 void SourcePackage::create_targets()
206 {
207         bool pc_needed = false;
208         for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
209         {
210                 i->create_targets();
211                 if(i->get_type()==Component::LIBRARY)
212                         pc_needed = true;
213         }
214
215         if(pc_needed)
216         {
217                 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
218                 builder.get_target("install")->add_depend(*builder.get_toolchain().get_tool("CP").create_target(*pc));
219         }
220 }
221
222 void SourcePackage::save_caches()
223 {
224         config.save();
225         deps_cache.save();
226 }
227
228
229 SourcePackage::Loader::Loader(SourcePackage &p):
230         DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
231 {
232         add("version",     &SourcePackage::version);
233         add("description", &SourcePackage::description);
234         add("build_info",  &Loader::build_info);
235         add("feature",     &Loader::feature);
236         add("if",          &Loader::condition);
237         add("program",     &Loader::component<Component::PROGRAM>);
238         add("library",     &Loader::component<Component::LIBRARY>);
239         add("module",      &Loader::component<Component::MODULE>);
240         add("headers",     &Loader::headers);
241         add("install",     &Loader::component<Component::INSTALL>);
242         add("datafile",    &Loader::component<Component::DATAFILE>);
243         add("tarball",     &Loader::tarball);
244         add("tar_file",    &Loader::tar_file);
245 }
246
247 void SourcePackage::Loader::finish()
248 {
249         obj.components.sort(component_sort);
250
251         for(map<string, string>::const_iterator i=install_map.begin(); i!=install_map.end(); ++i)
252         {
253                 for(ComponentList::iterator j=obj.components.begin(); j!=obj.components.end(); ++j)
254                 {
255                         const StringList &sources = j->get_sources();
256                         for(StringList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
257                         {
258                                 if(!i->first.compare(0, k->size(), *k))
259                                 {
260                                         const_cast<InstallMap &>(j->get_install_map()).add_mapping(obj.source/i->first, i->second);
261                                 }
262                         }
263                 }
264         }
265 }
266
267 void SourcePackage::Loader::feature(const string &n, const string &d)
268 {
269         Feature feat(n);
270         feat.descr = d;
271         feat.def_value = "no";
272         load_sub(feat);
273         obj.features.push_back(feat);
274 }
275
276 void SourcePackage::Loader::condition(const string &c)
277 {
278         Condition cond(obj, c);
279         load_sub(cond);
280         obj.conditions.push_back(cond);
281 }
282
283 template<Component::Type t>
284 void SourcePackage::Loader::component(const string &n)
285 {
286         Component comp(obj, t, n);
287         load_sub(comp);
288         obj.components.push_back(comp);
289 }
290
291 void SourcePackage::Loader::build_info()
292 {
293         load_sub(obj.build_info);
294 }
295
296 void SourcePackage::Loader::headers(const string &n)
297 {
298         IO::print("%s: Note: headers components are deprecated\n", get_source());
299         Component comp(obj, Component::LIBRARY, n);
300         load_sub(comp);
301         const StringList &sources = comp.get_sources();
302         for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
303                 install_map[*i] = "include/"+comp.get_name();
304 }
305
306 void SourcePackage::Loader::tarball(const string &n)
307 {
308         if(n=="@src")
309         {
310                 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
311                         if(i->get_type()==Component::TARBALL && i->get_name()==n)
312                                 load_sub(*i);
313         }
314         else
315         {
316                 Component trbl(obj, Component::TARBALL, n);
317                 load_sub(trbl);
318         }
319 }
320
321 void SourcePackage::Loader::tar_file(const string &f)
322 {
323         IO::print("%s: Note: tar_file is deprecated\n", get_source());
324         for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
325                 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
326                         const_cast<StringList &>(i->get_sources()).push_back((obj.source/f).str());
327 }