]> git.tdb.fi Git - builder.git/blob - source/component.cpp
Add support for building datafiles
[builder.git] / source / component.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/core/except.h>
9 #include <msp/fs/dir.h>
10 #include <msp/fs/stat.h>
11 #include <msp/fs/utils.h>
12 #include <msp/io/print.h>
13 #include <msp/strings/lexicalcast.h>
14 #include "builder.h"
15 #include "component.h"
16 #include "datafile.h"
17 #include "executable.h"
18 #include "file.h"
19 #include "header.h"
20 #include "install.h"
21 #include "objectfile.h"
22 #include "sharedlibrary.h"
23 #include "sourcepackage.h"
24 #include "staticlibrary.h"
25 #include "tarball.h"
26 #include "target.h"
27
28 using namespace std;
29 using namespace Msp;
30
31 Component::Component(SourcePackage &p, Type t, const string &n):
32         pkg(p),
33         type(t),
34         name(n),
35         install(false),
36         module_host(0),
37         modular(false),
38         deflt(true)
39 { }
40
41 void Component::create_build_info()
42 {
43         build_info.add(pkg.get_build_info());
44
45         for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
46                 build_info.add((*i)->get_exported_binfo());
47
48         for(StringList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
49                 *i=(pkg.get_source() / *i).str();
50         for(StringList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
51                 *i=(pkg.get_source() / *i).str();
52
53         if(pkg.get_library_mode()!=DYNAMIC)
54         {
55                 // XXX This may pull in some unnecessary libpaths too.  More thought required.
56                 PackageList reqs=pkg.collect_requires();
57                 for(PackageList::iterator i=reqs.begin(); i!=reqs.end(); ++i)
58                 {
59                         const BuildInfo &ebi=(*i)->get_exported_binfo();
60                         build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
61                 }
62         }
63
64         if(type==PROGRAM)
65         {
66                 string strip=pkg.get_config().get_option("strip").value;
67                 if(lexical_cast<bool>(strip))
68                         build_info.ldflags.push_back("-s");
69         }
70
71         if(modular)
72         {
73                 build_info.ldflags.push_back("-rdynamic");
74                 build_info.libs.push_back("dl");
75         }
76         else if(module_host)
77         {
78                 const PathList &host_src=module_host->get_sources();
79                 for(PathList::const_iterator i=host_src.begin(); i!=host_src.end(); ++i)
80                         build_info.incpath.push_back(i->str());
81         }
82
83         build_info.unique();
84 }
85
86 void Component::create_targets() const
87 {
88         Builder &builder=pkg.get_builder();
89         Target *world=builder.get_target("world");
90         Target *def_tgt=builder.get_target("default");
91
92         PathList files=collect_source_files();
93         list<FileTarget *> inst_list;
94
95         string inst_loc;
96         if(type==TARBALL)
97         {
98                 string tarname=name;
99                 if(name=="@src")
100                         tarname=pkg.get_name()+"-"+pkg.get_version();
101                 TarBall *result=new TarBall(builder, pkg, tarname);
102
103                 if(name=="@src")
104                 {
105                         const TargetMap &targets=builder.get_targets();
106                         for(TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
107                                 if(i->second->get_package()==&pkg && !i->second->is_buildable())
108                                         result->add_depend(i->second);
109                         files.push_back(pkg.get_source()/"Build");
110                 }
111
112                 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
113                 {
114                         FileTarget *ft;
115                         if(Target *tgt=builder.get_target(i->str()))
116                                 ft=dynamic_cast<FileTarget *>(tgt);
117                         else
118                                 ft=new File(builder, *i);
119                         result->add_depend(ft);
120                 }
121
122                 Target *tarbls_tgt=builder.get_target("tarballs");
123                 tarbls_tgt->add_depend(result);
124
125                 return;
126         }
127         else if(type==INSTALL)
128         {
129                 inst_loc=name;
130                 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
131                 {
132                         FileTarget *ft;
133                         if(Target *tgt=builder.get_target(i->str()))
134                                 ft=dynamic_cast<FileTarget *>(tgt);
135                         else
136                                 ft=new File(builder, pkg, *i);
137                         inst_list.push_back(ft);
138                 }
139         }
140         else if(type==DATAFILE)
141         {
142                 File *source;
143                 if(Target *tgt=builder.get_target(files.front().str()))
144                         source=dynamic_cast<File *>(tgt);
145                 else
146                         source=new File(builder, pkg, files.front());
147                 ::DataFile *result=new ::DataFile(builder, *this, *source);
148                 inst_list.push_back(result);
149         }
150         else
151         {
152                 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
153                 {
154                         string ext=FS::extpart(FS::basename(*i));
155                         if(ext==".h")
156                         {
157                                 FileTarget *hdr=dynamic_cast<FileTarget *>(builder.get_target(i->str()));
158                                 if(!hdr)
159                                         hdr=new Header(builder, *this, i->str());
160
161                                 // Install headers if requested
162                                 if(type==HEADERS && install)
163                                         inst_list.push_back(hdr);
164                         }
165                 }
166         }
167
168         if(type==PROGRAM || type==LIBRARY || type==MODULE)
169         {
170                 list<ObjectFile *> objs;
171                 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
172                 {
173                         string ext=FS::extpart(FS::basename(*i));
174                         if((ext==".cpp" || ext==".cc" || ext==".c"))
175                         {
176                                 SourceFile *src=new SourceFile(builder, *this, i->str());
177                                 ObjectFile *obj=new ObjectFile(builder, *this, *src);
178                                 objs.push_back(obj);
179                         }
180                 }
181
182                 list<FileTarget *> results;
183                 if(type==LIBRARY)
184                 {
185                         results.push_back(new SharedLibrary(builder, *this, objs));
186                         results.push_back(new StaticLibrary(builder, *this, objs));
187                 }
188                 else
189                         results.push_back(new Executable(builder, *this, objs));
190
191                 for(list<FileTarget *>::const_iterator i=results.begin(); i!=results.end(); ++i)
192                 {
193                         if(&pkg==builder.get_main_package() && deflt)
194                                 def_tgt->add_depend(*i);
195                         else
196                                 world->add_depend(*i);
197                         if(install)
198                                 inst_list.push_back(*i);
199                 }
200         }
201
202         Target *inst_tgt=builder.get_target("install");
203         for(list<FileTarget *>::const_iterator i=inst_list.begin(); i!=inst_list.end(); ++i)
204                 inst_tgt->add_depend(new Install(builder, pkg, **i, inst_loc));
205 }
206
207 PathList Component::collect_source_files() const
208 {
209         PathList files;
210         for(PathList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
211         {
212                 if(FS::is_dir(*i))
213                 {
214                         list<string> sfiles=list_files(*i);
215                         for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
216                                 files.push_back(*i / *j);
217                 }
218                 else
219                         files.push_back(*i);
220         }
221
222         return files;
223 }
224
225
226 Component::Loader::Loader(Component &c):
227         comp(c)
228 {
229         add("source",          &Loader::source);
230         add("install",         &Component::install);
231         add("install_headers", &Loader::install_headers);
232         add("build_info",      &Loader::build_info);
233         add("require",         &Loader::require);
234         add("modular",         &Loader::modular);
235         add("host",            &Loader::host);
236         add("default",         &Component::deflt);
237 }
238
239 void Component::Loader::finish()
240 {
241         if(!inst_hdr.empty())
242         {
243                 Component hdrcomp(comp.pkg, HEADERS, inst_hdr);
244                 hdrcomp.sources=comp.sources;
245                 hdrcomp.install=true;
246                 const_cast<ComponentList &>(comp.pkg.get_components()).push_back(hdrcomp);
247         }
248 }
249
250 void Component::Loader::source(const string &s)
251 {
252         comp.sources.push_back(comp.pkg.get_source()/s);
253 }
254
255 void Component::Loader::require(const string &n)
256 {
257         Package *req=comp.pkg.get_builder().get_package(n);
258         if(req)
259                 comp.requires.push_back(req);
260 }
261
262 void Component::Loader::modular()
263 {
264         if(comp.type!=PROGRAM)
265                 throw Exception("Only programs can be modular");
266         comp.modular=true;
267 }
268
269 void Component::Loader::host(const string &n)
270 {
271         const ComponentList &comps=comp.pkg.get_components();
272         for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
273                 if(i->get_name()==n)
274                 {
275                         if(i->get_type()!=PROGRAM || !i->is_modular())
276                                 throw Exception("Module host must be a modular program");
277                         comp.module_host=&*i;
278                         return;
279                 }
280
281         throw KeyError("Unknown component", n);
282 }
283
284 void Component::Loader::install_headers(const string &p)
285 {
286         IO::print("%s: Note: install_headers is deprecated\n", get_source());
287         if(comp.type==HEADERS)
288         {
289                 comp.name=p;
290                 comp.install=true;
291         }
292         else
293                 inst_hdr=p;
294 }
295
296 void Component::Loader::build_info()
297 {
298         load_sub(comp.build_info);
299 }