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