]> git.tdb.fi Git - builder.git/blob - source/component.cpp
Compile libraries with -fPIC
[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         else if(type==LIBRARY)
71         {
72                 build_info.cflags.push_back("-fPIC");
73         }
74
75         if(modular)
76         {
77                 build_info.ldflags.push_back("-rdynamic");
78                 build_info.libs.push_back("dl");
79         }
80         else if(module_host)
81         {
82                 const PathList &host_src=module_host->get_sources();
83                 for(PathList::const_iterator i=host_src.begin(); i!=host_src.end(); ++i)
84                         build_info.incpath.push_back(i->str());
85         }
86
87         build_info.unique();
88 }
89
90 void Component::create_targets() const
91 {
92         Builder &builder=pkg.get_builder();
93         Target *world=builder.get_target("world");
94         Target *def_tgt=builder.get_target("default");
95
96         PathList files=collect_source_files();
97         list<FileTarget *> inst_list;
98
99         string inst_loc;
100         if(type==TARBALL)
101         {
102                 string tarname=name;
103                 if(name=="@src")
104                         tarname=pkg.get_name()+"-"+pkg.get_version();
105                 TarBall *result=new TarBall(builder, pkg, tarname);
106
107                 if(name=="@src")
108                 {
109                         const TargetMap &targets=builder.get_targets();
110                         for(TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
111                                 if(i->second->get_package()==&pkg && !i->second->is_buildable())
112                                         result->add_depend(i->second);
113                         files.push_back(pkg.get_source()/"Build");
114                 }
115
116                 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
117                 {
118                         FileTarget *ft;
119                         if(Target *tgt=builder.get_target(i->str()))
120                                 ft=dynamic_cast<FileTarget *>(tgt);
121                         else
122                                 ft=new File(builder, *i);
123                         result->add_depend(ft);
124                 }
125
126                 Target *tarbls_tgt=builder.get_target("tarballs");
127                 tarbls_tgt->add_depend(result);
128
129                 return;
130         }
131         else if(type==INSTALL)
132         {
133                 inst_loc=name;
134                 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
135                 {
136                         FileTarget *ft;
137                         if(Target *tgt=builder.get_target(i->str()))
138                                 ft=dynamic_cast<FileTarget *>(tgt);
139                         else
140                                 ft=new File(builder, pkg, *i);
141                         inst_list.push_back(ft);
142                 }
143         }
144         else if(type==DATAFILE)
145         {
146                 File *source;
147                 if(Target *tgt=builder.get_target(files.front().str()))
148                         source=dynamic_cast<File *>(tgt);
149                 else
150                         source=new File(builder, pkg, files.front());
151                 ::DataFile *result=new ::DataFile(builder, *this, *source);
152
153                 if(&pkg==builder.get_main_package() && deflt)
154                         def_tgt->add_depend(result);
155                 else
156                         world->add_depend(result);
157                 if(install)
158                         inst_list.push_back(result);
159         }
160         else
161         {
162                 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
163                 {
164                         string ext=FS::extpart(FS::basename(*i));
165                         if(ext==".h")
166                         {
167                                 FileTarget *hdr=dynamic_cast<FileTarget *>(builder.get_target(i->str()));
168                                 if(!hdr)
169                                         hdr=new Header(builder, *this, i->str());
170
171                                 // Install headers if requested
172                                 if(type==HEADERS && install)
173                                         inst_list.push_back(hdr);
174                         }
175                 }
176         }
177
178         if(type==PROGRAM || type==LIBRARY || type==MODULE)
179         {
180                 list<ObjectFile *> objs;
181                 for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
182                 {
183                         string ext=FS::extpart(FS::basename(*i));
184                         if((ext==".cpp" || ext==".cc" || ext==".c"))
185                         {
186                                 SourceFile *src=new SourceFile(builder, *this, i->str());
187                                 ObjectFile *obj=new ObjectFile(builder, *this, *src);
188                                 objs.push_back(obj);
189                         }
190                 }
191
192                 list<FileTarget *> results;
193                 if(type==LIBRARY)
194                 {
195                         results.push_back(new SharedLibrary(builder, *this, objs));
196                         results.push_back(new StaticLibrary(builder, *this, objs));
197                 }
198                 else
199                         results.push_back(new Executable(builder, *this, objs));
200
201                 for(list<FileTarget *>::const_iterator i=results.begin(); i!=results.end(); ++i)
202                 {
203                         if(&pkg==builder.get_main_package() && deflt)
204                                 def_tgt->add_depend(*i);
205                         else
206                                 world->add_depend(*i);
207                         if(install)
208                                 inst_list.push_back(*i);
209                 }
210         }
211
212         Target *inst_tgt=builder.get_target("install");
213         for(list<FileTarget *>::const_iterator i=inst_list.begin(); i!=inst_list.end(); ++i)
214                 inst_tgt->add_depend(new Install(builder, pkg, **i, inst_loc));
215 }
216
217 PathList Component::collect_source_files() const
218 {
219         PathList files;
220         for(PathList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
221         {
222                 if(FS::is_dir(*i))
223                 {
224                         list<string> sfiles=list_files(*i);
225                         for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
226                                 files.push_back(*i / *j);
227                 }
228                 else
229                         files.push_back(*i);
230         }
231
232         return files;
233 }
234
235
236 Component::Loader::Loader(Component &c):
237         comp(c)
238 {
239         add("source",          &Loader::source);
240         add("install",         &Component::install);
241         add("install_headers", &Loader::install_headers);
242         add("build_info",      &Loader::build_info);
243         add("require",         &Loader::require);
244         add("modular",         &Loader::modular);
245         add("host",            &Loader::host);
246         add("default",         &Component::deflt);
247 }
248
249 void Component::Loader::finish()
250 {
251         if(!inst_hdr.empty())
252         {
253                 Component hdrcomp(comp.pkg, HEADERS, inst_hdr);
254                 hdrcomp.sources=comp.sources;
255                 hdrcomp.install=true;
256                 const_cast<ComponentList &>(comp.pkg.get_components()).push_back(hdrcomp);
257         }
258 }
259
260 void Component::Loader::source(const string &s)
261 {
262         comp.sources.push_back(comp.pkg.get_source()/s);
263 }
264
265 void Component::Loader::require(const string &n)
266 {
267         Package *req=comp.pkg.get_builder().get_package(n);
268         if(req)
269                 comp.requires.push_back(req);
270 }
271
272 void Component::Loader::modular()
273 {
274         if(comp.type!=PROGRAM)
275                 throw Exception("Only programs can be modular");
276         comp.modular=true;
277 }
278
279 void Component::Loader::host(const string &n)
280 {
281         const ComponentList &comps=comp.pkg.get_components();
282         for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
283                 if(i->get_name()==n)
284                 {
285                         if(i->get_type()!=PROGRAM || !i->is_modular())
286                                 throw Exception("Module host must be a modular program");
287                         comp.module_host=&*i;
288                         return;
289                 }
290
291         throw KeyError("Unknown component", n);
292 }
293
294 void Component::Loader::install_headers(const string &p)
295 {
296         IO::print("%s: Note: install_headers is deprecated\n", get_source());
297         if(comp.type==HEADERS)
298         {
299                 comp.name=p;
300                 comp.install=true;
301         }
302         else
303                 inst_hdr=p;
304 }
305
306 void Component::Loader::build_info()
307 {
308         load_sub(comp.build_info);
309 }