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