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