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