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