]> git.tdb.fi Git - builder.git/blob - source/sourcepackage.cpp
Replace per-file copyright notices with a single file
[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 "sourcepackage.h"
9
10 using namespace std;
11 using namespace Msp;
12
13 namespace {
14
15 bool component_sort(const Component &c1, const Component &c2)
16 { return c1.get_type()<c2.get_type(); }
17
18 }
19
20
21 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &s):
22         Package(b, n),
23         source(s),
24         config(*this),
25         deps_cache(*this)
26 {
27         components.push_back(Component(*this, Component::TARBALL, "@src"));
28 }
29
30 FS::Path SourcePackage::get_temp_dir() const
31 {
32         string subdir = format("%s.%s", builder.get_current_arch().get_name(), config.get_option("profile").value);
33         return source/config.get_option("tempdir").value/subdir;
34 }
35
36 FS::Path SourcePackage::get_out_dir() const
37 {
38         const Architecture &arch = builder.get_current_arch();
39         if(arch.is_native())
40                 return source/config.get_option("outdir").value;
41         else
42                 return source/arch.get_name()/config.get_option("outdir").value;
43 }
44
45 unsigned SourcePackage::get_install_flags()
46 {
47         unsigned flags = 0;
48         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
49                 if(i->get_install())
50                 {
51                         if(i->get_type()==Component::PROGRAM)
52                                 flags |= BIN;
53                         else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
54                                 flags |= LIB;
55                         else if(i->get_type()==Component::HEADERS)
56                                 flags |= 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                 if(builder.get_verbose()>=2)
132                         IO::print("Configuration of %s changed\n", name);
133                 if(!builder.get_dry_run())
134                         config.save();
135         }
136
137         config.finish();
138
139         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
140                 if(i->eval())
141                 {
142                         const StringList &reqs = i->get_requires();
143                         for(StringList::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
144                                 if(Package *pkg = builder.get_package(*j))
145                                         requires.push_back(pkg);
146                 }
147
148         for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
149         {
150                 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
151                 if(bpkg && bpkg->get_need_path())
152                         bpkg->set_path(config.get_option(bpkg->get_name()+"_path").value);
153         }
154
155         deps_cache.load();
156
157         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
158                 i->configure(opts, flag);
159 }
160
161 void SourcePackage::init_config()
162 {
163         config.add_option("profile",    "default", "Configuration profile");
164         config.add_option("tempdir",    "temp",    "Directory for storing temporary files");
165         config.add_option("outdir",     ".",       "Directory to put build results in");
166         config.add_option("optimize",   "0",       "Compiler optimization level");
167         config.add_option("strip",      "no",      "Strip symbols from programs");
168         config.add_option("debug",      "no",      "Produce debugging symbols");
169         config.add_option("staticlibs", "local",   "Use static libraries");
170
171         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
172                 config.add_option("with_"+i->name, i->def_value, i->descr);
173
174         for(PackageList::const_iterator i=requires.begin(); i!=requires.end(); ++i)
175         {
176                 BinaryPackage *bpkg = dynamic_cast<BinaryPackage *>(*i);
177                 if(bpkg && bpkg->get_need_path())
178                         config.add_option(bpkg->get_name()+"_path", "/usr", "Path for "+bpkg->get_name());
179         }
180 }
181
182 void SourcePackage::create_build_info()
183 {
184         build_info.add(builder.get_current_arch().get_build_info());
185
186         // XXX Currently, a package-specific settings will override cmdline.  This might or might not be desirable.
187         const StringList &warnings = builder.get_warnings();
188         build_info.warnings.insert(build_info.warnings.begin(), warnings.begin(), warnings.end());
189
190         unsigned flags = get_install_flags();
191
192         build_info.incpath.push_back((builder.get_prefix()/"include").str());
193         build_info.libpath.push_back((builder.get_prefix()/"lib").str());
194
195         if(flags&INCLUDE)
196                 export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
197         if(flags&LIB)
198                 export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
199
200         string optimize = config.get_option("optimize").value;
201         if(!optimize.empty() && optimize!="0")
202         {
203                 build_info.cflags.push_back("-O"+optimize);
204                 build_info.ldflags.push_back("-O"+optimize);
205         }
206
207         if(lexical_cast<bool>(config.get_option("debug").value))
208         {
209                 build_info.cflags.push_back("-ggdb");
210                 build_info.defines.push_back("DEBUG");
211         }
212
213         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
214                 if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
215                         build_info.cflags.push_back("-DWITH_"+toupper(i->name));
216
217         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
218                 if(i->eval())
219                         build_info.add(i->get_build_info());
220
221         build_info.unique();
222
223         for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
224         {
225                 i->create_build_info();
226                 if(i->get_type()==Component::LIBRARY)
227                         export_binfo.libs.push_back(i->get_name());
228         }
229
230         export_binfo.unique();
231 }
232
233
234 SourcePackage::Loader::Loader(Package &p):
235         Package::Loader(p)
236 {
237         add("version",     &SourcePackage::version);
238         add("description", &SourcePackage::description);
239         add("build_info",  &Loader::build_info);
240         add("feature",     &Loader::feature);
241         add("if",          &Loader::condition);
242         add("program",     &Loader::component<Component::PROGRAM>);
243         add("library",     &Loader::component<Component::LIBRARY>);
244         add("module",      &Loader::component<Component::MODULE>);
245         add("headers",     &Loader::component<Component::HEADERS>);
246         add("install",     &Loader::component<Component::INSTALL>);
247         add("datafile",    &Loader::component<Component::DATAFILE>);
248         add("tarball",     &Loader::tarball);
249         add("tar_file",    &Loader::tar_file);
250 }
251
252 void SourcePackage::Loader::finish()
253 {
254         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
255         spkg.components.sort(component_sort);
256 }
257
258 void SourcePackage::Loader::feature(const string &n, const string &d)
259 {
260         Feature feat(n);
261         feat.descr = d;
262         feat.def_value = "no";
263         load_sub(feat);
264         static_cast<SourcePackage &>(pkg).features.push_back(feat);
265 }
266
267 void SourcePackage::Loader::condition(const string &c)
268 {
269         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
270         Condition cond(spkg, c);
271         load_sub(cond);
272         spkg.conditions.push_back(cond);
273 }
274
275 template<Component::Type t>
276 void SourcePackage::Loader::component(const string &n)
277 {
278         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
279         Component comp(spkg, t, n);
280         load_sub(comp);
281         spkg.components.push_back(comp);
282 }
283
284 void SourcePackage::Loader::build_info()
285 {
286         load_sub(static_cast<SourcePackage &>(pkg).build_info);
287 }
288
289 void SourcePackage::Loader::tarball(const string &n)
290 {
291         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
292         if(n=="@src")
293         {
294                 for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.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(spkg, 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         SourcePackage &spkg = static_cast<SourcePackage &>(pkg);
309         for(ComponentList::iterator i=spkg.components.begin(); i!=spkg.components.end(); ++i)
310                 if(i->get_type()==Component::TARBALL && i->get_name()=="@src")
311                         const_cast<StringList &>(i->get_sources()).push_back((spkg.source/f).str());
312 }