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