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