]> git.tdb.fi Git - builder.git/blob - source/package.cpp
Add tarball building
[builder.git] / source / package.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/strings/lexicalcast.h>
9 #include <msp/strings/utils.h>
10 #include "builder.h"
11 #include "misc.h"
12 #include "package.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 #include <iostream>
18
19 /**
20 Creates a buildable package.
21 */
22 Package::Package(Builder &b, const string &n, const Path::Path &s):
23         builder(b),
24         name(n),
25         buildable(true),
26         source(s),
27         config(*this),
28         conf_done(false),
29         use_pkgconfig(true),
30         need_path(false)
31 {
32         tar_files.push_back(source/"Build");
33
34         if(builder.get_verbose()>=4)
35                 cout<<"Created buildable package "<<n<<" at "<<s<<'\n';
36 }
37
38 /**
39 Sets the path where the package files were installed.  This is only useful for
40 non-buildable packages that don't use pkg-config.
41 */
42 void Package::set_path(const Msp::Path::Path &p)
43 {
44         path=builder.get_cwd()/p;
45 }
46
47 Msp::Path::Path Package::get_temp_dir() const
48 {
49         return source/config.get_option("tempdir").value/config.get_option("profile").value;
50 }
51
52 Msp::Path::Path Package::get_out_dir() const
53 {
54         return source/config.get_option("outdir").value;
55 }
56
57 /**
58 Checks which kinds of things the components of this package install.
59
60 @return  A bitmask of installed things
61 */
62 unsigned Package::get_install_flags()
63 {
64         unsigned flags=0;
65         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
66         {
67                 if(i->get_install())
68                 {
69                         if(i->get_type()==Component::PROGRAM)
70                                 flags|=BIN;
71                         else if(i->get_type()==Component::LIBRARY || i->get_type()==Component::MODULE)
72                                 flags|=LIB;
73                 }
74                 if(!i->get_install_headers().empty())
75                         flags|=INCLUDE;
76         }
77
78         return flags;
79 }
80
81 LibMode Package::get_library_mode() const
82 {
83         const string &mode=config.get_option("staticlibs").value;
84         if(mode=="all")
85                 return ALL_STATIC;
86         else if(mode=="local")
87                 return LOCAL_STATIC;
88         else if(mode=="none")
89                 return DYNAMIC;
90         else
91                 throw Exception("Unknown library mode");
92 }
93
94 /**
95 Tries to resolve all references to dependency packages.
96 */
97 void Package::resolve_refs()
98 {
99         for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
100         {
101                 Package *pkg=i->resolve();
102                 if(pkg) all_reqs.push_back(pkg);
103         }
104
105         for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
106         {
107                 i->resolve_refs();
108                 const PkgRefList &creqs=i->get_requires();
109                 for(PkgRefList::const_iterator j=creqs.begin(); j!=creqs.end(); ++j)
110                         if(j->get_package())
111                                 all_reqs.push_back(j->get_package());
112         }
113
114         for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
115         {
116                 i->resolve_refs();
117                 const PkgRefList &creqs=i->get_requires();
118                 for(PkgRefList::const_iterator j=creqs.begin(); j!=creqs.end(); ++j)
119                         if(j->get_package())
120                                 all_reqs.push_back(j->get_package());
121         }
122 }
123
124 /**
125 Processes configuration options that were most likely obtained from the command
126 line.
127 */
128 void Package::configure(const StringMap &opts, unsigned flag)
129 {
130         if(conf_done)
131                 return;
132
133         if(builder.get_verbose()>=3)
134                 cout<<"Configuring "<<name<<'\n';
135
136         if(buildable)
137         {
138                 init_config();
139
140                 StringMap::const_iterator prof=opts.find("profile");
141                 if(prof!=opts.end() && flag)
142                         config.select_profile(prof->second);
143                 else
144                         config.select_last_profile();
145
146                 if(flag && config.update(opts))
147                 {
148                         if(builder.get_verbose()>=2)
149                                 cout<<"Configuration of "<<name<<" changed\n";
150                         if(!builder.get_dry_run())
151                                 config.save();
152                 }
153
154                 config.finish();
155
156                 for(ConditionList::iterator i=conditions.begin(); i!=conditions.end(); ++i)
157                         if(i->eval())
158                         {
159                                 const PkgRefList &reqs=i->get_requires();
160                                 requires.insert(requires.end(), reqs.begin(), reqs.end());
161                                 build_info.add(i->get_build_info());
162                         }
163
164                 for(PackageList::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
165                 {
166                         if((*i)->get_need_path())
167                                 (*i)->set_path(config.get_option((*i)->get_name()+"_path").value);
168                         (*i)->configure(opts, flag&2);
169                 }
170         }
171
172         create_build_info();
173
174         conf_done=true;
175 }
176
177 /**
178 Creates a non-buildable package with the given name.  Pkg-config is tried first
179 to get build information.  If it fails, a built-in list of known packages is
180 consulted.
181 */
182 Package *Package::create(Builder &b, const string &name)
183 {
184         list<string> argv;
185         argv.push_back("pkg-config");
186         argv.push_back("--silence-errors");
187         argv.push_back("--cflags");
188         argv.push_back("--libs");
189         argv.push_back(name);
190         vector<string> info=split(run_command(argv));
191
192         bool need_path=false;
193         bool use_pkgconfig=true;
194         if(info.empty())
195         {
196                 use_pkgconfig=false;
197
198                 //XXX Put these in an external file
199                 if(name=="opengl")
200                         info.push_back("-lGL");
201                 else if(name=="pthread")
202                         info.push_back("-lpthread");
203                 else if(name=="gmpxx")
204                         info.push_back("-lgmpxx");
205                 else if(name=="fmod4")
206                         need_path=true;
207                 else if(name=="devil")
208                         info.push_back("-lIL");
209                 else if(name=="Xlib")
210                         info.push_back("-lX11");
211                 else
212                         return 0;
213         }
214
215         Package *pkg=new Package(b, name, info);
216         pkg->need_path=need_path;
217         pkg->use_pkgconfig=use_pkgconfig;
218         return pkg;
219 }
220
221 /*** private ***/
222
223 Package::Package(Builder &b, const string &n, const vector<string> &info):
224         builder(b),
225         name(n),
226         buildable(false),
227         config(*this),
228         conf_done(false)
229 {
230         for(vector<string>::const_iterator i=info.begin(); i!=info.end(); ++i)
231         {
232                 if(!i->compare(0, 2, "-I"))
233                         export_binfo.incpath.push_back(i->substr(2));
234                 else if(!i->compare(0, 2, "-D"))
235                         export_binfo.defines.push_back(i->substr(2));
236                 else if(!i->compare(0, 2, "-L"))
237                         export_binfo.libpath.push_back(i->substr(2));
238                 else if(!i->compare(0, 2, "-l"))
239                         export_binfo.libs.push_back(i->substr(2));
240         }
241
242         if(builder.get_verbose()>=4)
243         {
244                 cout<<"Created non-buildable package "<<name<<" with";
245                 for(vector<string>::const_iterator i=info.begin(); i!=info.end(); ++i)
246                         cout<<' '<<*i;
247                 cout<<'\n';
248         }
249 }
250
251 /**
252 Initializes configuration options and loads cached values.
253 */
254 void Package::init_config()
255 {
256         config.add_option("profile",    "default", "Configuration profile");
257         config.add_option("tempdir",    "temp",    "Directory for storing temporary files");
258         config.add_option("outdir",     ".",       "Directory to put build results in");
259         config.add_option("optimize",   "0",       "Apply compiler optimizations");
260         config.add_option("debug",      "0",       "Produce debugging symbols");
261         config.add_option("cpu",        "auto",    "CPU type to optimize for");
262         config.add_option("arch",       "native",  "Architecture for cross-compiling");
263         config.add_option("staticlibs", "local",   "Use static libraries");
264
265         unsigned flags=get_install_flags();
266         if(flags)
267                 config.add_option("prefix",     "$HOME/local",     "Installation prefix");
268         /*if(flags&INCLUDE)
269                 config.add_option("includedir", "$prefix/include", "Header installation directory");
270         if(flags&BIN)
271                 config.add_option("includedir", "$prefix/bin",     "Binary installation directory");
272         if(flags&LIB)
273                 config.add_option("includedir", "$prefix/lib",     "Library installation directory");
274         if(flags&DATA)
275                 config.add_option("includedir", "$prefix/share",   "Data installation directory");*/
276
277         for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
278                 config.add_option("with_"+i->name, "0", i->descr);
279
280         for(PackageList::const_iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
281                 if((*i)->get_need_path())
282                         config.add_option((*i)->get_name()+"_path", "", "Path for "+(*i)->get_name());
283 }
284
285 /**
286 Fills in build info based on configuration.  All required packages must be
287 configured when this is called.
288 */
289 void Package::create_build_info()
290 {
291         if(buildable)
292         {
293                 for(PkgRefList::iterator i=requires.begin(); i!=requires.end(); ++i)
294                 {
295                         Package *pkg=i->get_package();
296                         if(!pkg)
297                                 continue;
298                         const BuildInfo &ebi=pkg->get_exported_binfo();
299                         build_info.add(ebi);
300
301                         export_binfo.cflags.insert(export_binfo.cflags.end(), ebi.cflags.begin(), ebi.cflags.end());
302                         export_binfo.incpath.insert(export_binfo.incpath.end(), ebi.incpath.begin(), ebi.incpath.end());
303                         export_binfo.defines.insert(export_binfo.defines.end(), ebi.defines.begin(), ebi.defines.end());
304                 }
305
306                 build_info.cflags.push_back("-Wall");
307                 build_info.cflags.push_back("-Wshadow");
308                 build_info.cflags.push_back("-Wextra");
309                 build_info.cflags.push_back("-Wpointer-arith");
310                 build_info.cflags.push_back("-Wconversion");
311                 build_info.cflags.push_back("-Werror");
312
313                 unsigned flags=get_install_flags();
314
315                 if(flags&INCLUDE)
316                         export_binfo.incpath.push_back((Path::Path(config.get_option("prefix").value)/"include").str());
317                 if(flags&LIB)
318                         export_binfo.libpath.push_back((Path::Path(config.get_option("prefix").value)/"lib").str());
319
320                 string optimize=config.get_option("optimize").value;
321                 if(lexical_cast<unsigned>(optimize))
322                 {
323                         build_info.cflags.push_back("-O"+optimize);
324                         build_info.ldflags.push_back("-O"+optimize);
325                         string cpu=config.get_option("cpu").value;
326                         if(cpu!="auto")
327                                 build_info.cflags.push_back("-march="+cpu);
328                 }
329
330                 if(lexical_cast<bool>(config.get_option("debug").value))
331                 {
332                         build_info.cflags.push_back("-ggdb");
333                         build_info.defines.push_back("DEBUG");
334                 }
335
336                 for(FeatureList::iterator i=features.begin(); i!=features.end(); ++i)
337                         if(lexical_cast<bool>(config.get_option("with_"+i->name).value))
338                                 build_info.cflags.push_back("-DWITH_"+toupper(i->name));
339
340                 build_info.unique();
341
342                 for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
343                 {
344                         i->create_build_info();
345                         if(i->get_type()==Component::LIBRARY)
346                                 export_binfo.libs.push_back(i->get_name());
347                 }
348         }
349         else if(name=="fmod4")
350         {
351                 export_binfo.libs.push_back("fmodex");
352                 if(!path.empty())
353                 {
354                         export_binfo.libpath.push_back((path/"api"/"lib").str());
355                         export_binfo.incpath.push_back((path/"api"/"inc").str());
356                 }
357         }
358         export_binfo.unique();
359 }
360
361 Package::Loader::Loader(Package &p):
362         pkg(p)
363 {
364         add("version",     &Package::version);
365         add("description", &Package::description);
366         add("require",     &Loader::require);
367         add("feature",     &Loader::feature);
368         add("if",          &Loader::condition);
369         add("program",     &Loader::program);
370         add("library",     &Loader::library);
371         add("module",      &Loader::module);
372         add("headers",     &Loader::headers);
373         add("build_info",  &Loader::build_info);
374         add("tar_file",    &Loader::tar_file);
375 }
376
377 void Package::Loader::require(const string &n)
378 {
379         pkg.requires.push_back(PackageRef(pkg.builder, n));
380 }
381
382 void Package::Loader::feature(const string &n, const string &d)
383 {
384         pkg.features.push_back(Feature(n, d));
385 }
386
387 void Package::Loader::condition(const string &c)
388 {
389         Condition cond(pkg, c);
390         load_sub(cond);
391         pkg.conditions.push_back(cond);
392 }
393
394 void Package::Loader::program(const string &n)
395 {
396         Component prog(pkg, Component::PROGRAM, n);
397         load_sub(prog);
398         pkg.components.push_back(prog);
399 }
400
401 void Package::Loader::library(const string &n)
402 {
403         Component prog(pkg, Component::LIBRARY, n);
404         load_sub(prog);
405         pkg.components.push_back(prog);
406 }
407
408 void Package::Loader::module(const string &n)
409 {
410         Component prog(pkg, Component::MODULE, n);
411         load_sub(prog);
412         pkg.components.push_back(prog);
413 }
414
415 void Package::Loader::headers(const string &n)
416 {
417         Component prog(pkg, Component::HEADERS, n);
418         load_sub(prog);
419         pkg.components.push_back(prog);
420 }
421
422 void Package::Loader::build_info()
423 {
424         load_sub(pkg.build_info);
425 }
426
427 void Package::Loader::tar_file(const string &f)
428 {
429         pkg.tar_files.push_back(pkg.source/f);
430 }