]> git.tdb.fi Git - builder.git/blob - source/sourcepackage.cpp
Make Target::add_depend take a reference since null is not allowed
[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         {
131                 builder.get_logger().log("configure", format("Configuration of %s changed", name));
132                 if(!builder.get_dry_run())
133                         config.save();
134         }
135
136         config.finish();
137
138         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
139                 if(i->eval())
140                 {
141                         const StringList &reqs = i->get_requires();
142                         for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
143                                 if(Package *pkg = builder.get_package_manager().find_package(*j))
144                                         requires.push_back(pkg);
145                 }
146
147         for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
148         {
149                 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
150                 if(bpkg && bpkg->get_need_path())
151                         bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
152         }
153
154         deps_cache.load();
155
156         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
157                 i->configure(opts, flag);
158 }
159
160 void SourcePackage::init_config()
161 {
162         config.add_option("profile",    "default", "Configuration profile");
163         config.add_option("tempdir",    "temp",    "Directory for storing temporary files");
164         config.add_option("outdir",     ".",       "Directory to put build results in");
165         config.add_option("optimize",   "0",       "Compiler optimization level");
166         config.add_option("strip",      "no",      "Strip symbols from programs");
167         config.add_option("debug",      "no",      "Produce debugging symbols");
168         config.add_option("staticlibs", "local",   "Use static libraries");
169
170         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
171                 config.add_option("with_"+i->name, i->def_value, i->descr);
172
173         for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
174         {
175                 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
176                 if(bpkg && bpkg->get_need_path())
177                         config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
178         }
179 }
180
181 void SourcePackage::create_build_info()
182 {
183         // XXX Currently, a package-specific settings will override cmdline.  This might or might not be desirable.
184         const StringList &warnings = builder.get_warnings();
185         build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
186
187         unsigned flags = get_install_flags();
188
189         build_info.incpath.push_back((builder.get_prefix()/"include").str());
190         build_info.libpath.push_back((builder.get_prefix()/"lib").str());
191
192         if(flags&INCLUDE)
193                 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
194         if(flags&LIB)
195                 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
196
197         string optimize = config.get_option("optimize").value;
198         if(!optimize.empty() && optimize!="0")
199         {
200                 if(optimize=="s" || optimize=="size")
201                         build_info.optimize = -1;
202                 else
203                         build_info.optimize = lexical_cast<unsigned>(optimize);
204         }
205
206         if(lexical_cast<bool>(config.get_option("debug").value))
207         {
208                 build_info.debug = true;
209                 build_info.defines["DEBUG"] = "1";
210         }
211
212         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
213                 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
214                         build_info.defines["WITH_"+toupper(i->name)] = "1";
215
216         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
217                 if(i->eval())
218                         build_info.update_from(i->get_build_info());
219
220         for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
221         {
222                 i->create_build_info();
223                 if(i->get_type()==Component::LIBRARY)
224                         export_binfo.libs.push_back(i->get_name());
225         }
226 }
227
228 void SourcePackage::create_targets()
229 {
230         bool pc_needed = false;
231         for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
232         {
233                 i->create_targets();
234                 if(i->get_type()==Component::LIBRARY)
235                         pc_needed = true;
236         }
237
238         if(pc_needed)
239         {
240                 PkgConfigFile *pc = new PkgConfigFile(builder, *this);
241                 builder.get_target("install")->add_depend(*builder.get_toolchain().get_tool("CP").create_target(*pc));
242         }
243 }
244
245
246 SourcePackage::Loader::Loader(SourcePackage &p):
247         DataFile::DerivedObjectLoader<SourcePackage, Package>(p)
248 {
249         add("version",     &SourcePackage::version);
250         add("description", &SourcePackage::description);
251         add("build_info",  &Loader::build_info);
252         add("feature",     &Loader::feature);
253         add("if",          &Loader::condition);
254         add("program",     &Loader::component<Component::PROGRAM>);
255         add("library",     &Loader::component<Component::LIBRARY>);
256         add("module",      &Loader::component<Component::MODULE>);
257         add("headers",     &Loader::headers);
258         add("install",     &Loader::component<Component::INSTALL>);
259         add("datafile",    &Loader::component<Component::DATAFILE>);
260         add("tarball",     &Loader::tarball);
261         add("tar_file",    &Loader::tar_file);
262 }
263
264 void SourcePackage::Loader::finish()
265 {
266         obj.components.sort(component_sort);
267
268         for(map<string, string>::const_iterator i=install_map.begin(); i!=install_map.end(); ++i)
269         {
270                 for(ComponentList::iterator j=obj.components.begin(); j!=obj.components.end(); ++j)
271                 {
272                         const StringList &sources = j->get_sources();
273                         for(StringList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
274                         {
275                                 if(!i->first.compare(0, k->size(), *k))
276                                 {
277                                         const_cast<InstallMap &>(j->get_install_map()).add_mapping(obj.source/i->first, i->second);
278                                 }
279                         }
280                 }
281         }
282 }
283
284 void SourcePackage::Loader::feature(const string &n, const string &d)
285 {
286         Feature feat(n);
287         feat.descr = d;
288         feat.def_value = "no";
289         load_sub(feat);
290         obj.features.push_back(feat);
291 }
292
293 void SourcePackage::Loader::condition(const string &c)
294 {
295         Condition cond(obj, c);
296         load_sub(cond);
297         obj.conditions.push_back(cond);
298 }
299
300 template<Component::Type t>
301 void SourcePackage::Loader::component(const string &n)
302 {
303         Component comp(obj, t, n);
304         load_sub(comp);
305         obj.components.push_back(comp);
306 }
307
308 void SourcePackage::Loader::build_info()
309 {
310         load_sub(obj.build_info);
311 }
312
313 void SourcePackage::Loader::headers(const string &n)
314 {
315         IO::print("%s: Note: headers components are deprecated\n", get_source());
316         Component comp(obj, Component::LIBRARY, n);
317         load_sub(comp);
318         const StringList &sources = comp.get_sources();
319         for(StringList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
320                 install_map[*i] = "include/"+comp.get_name();
321 }
322
323 void SourcePackage::Loader::tarball(const string &n)
324 {
325         if(n=="@src")
326         {
327                 for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
328                         if(i->get_type()==Component::TARBALL && i->get_name()==n)
329                                 load_sub(*i);
330         }
331         else
332         {
333                 Component trbl(obj, Component::TARBALL, n);
334                 load_sub(trbl);
335         }
336 }
337
338 void SourcePackage::Loader::tar_file(const string &f)
339 {
340         IO::print("%s: Note: tar_file is deprecated\n", get_source());
341         for(ComponentList::iterator i=obj.components.begin(); i!=obj.components.end(); ++i)
342                 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
343                         const_cast<StringList &>(i->get_sources()).push_back((obj.source/f).str());
344 }