]> git.tdb.fi Git - builder.git/blob - source/component.cpp
Add support for generating source files
[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 "csourcefile.h"
10 #include "datapack.h"
11 #include "executable.h"
12 #include "file.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         package(p),
27         type(t),
28         name(n),
29         install(false),
30         deflt(true)
31 { }
32
33 void Component::prepare()
34 {
35         for(Package::Requirements::const_iterator i=requires.begin(); i!=requires.end(); ++i)
36                 (*i)->prepare();
37 }
38
39 void Component::create_build_info()
40 {
41         BuildInfo final_build_info;
42
43         const Package::Requirements &pkg_reqs = package.get_required_packages();
44         Package::Requirements direct_reqs = requires;
45         direct_reqs.insert(direct_reqs.end(), pkg_reqs.begin(), pkg_reqs.end());
46
47         Package::Requirements all_reqs = direct_reqs;
48         for(Package::Requirements::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
49         {
50                 BuildInfo::UpdateLevel level = BuildInfo::CHAINED;
51                 if(find(direct_reqs.begin(), direct_reqs.end(), *i)!=direct_reqs.end())
52                         level = BuildInfo::DEPENDENCY;
53                 final_build_info.update_from((*i)->get_exported_build_info(), level);
54
55                 const Package::Requirements &reqs = (*i)->get_required_packages();
56                 for(Package::Requirements::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
57                         if(find(all_reqs.begin(), all_reqs.end(), *j)==all_reqs.end())
58                                 all_reqs.push_back(*j);
59         }
60
61         final_build_info.update_from(package.get_build_info());
62         final_build_info.update_from(build_info);
63         build_info = final_build_info;
64
65         for(BuildInfo::PathList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
66                 *i = (package.get_source_directory() / *i).str();
67         for(BuildInfo::PathList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
68                 *i = (package.get_source_directory() / *i).str();
69
70         for(UseList::const_iterator i=uses.begin(); i!=uses.end(); ++i)
71         {
72                 /* Select an include path that contains all the sources for this and the
73                 used component.  This should produce a sensible result in most cases. */
74                 FS::Path base;
75                 for(SourceList::const_iterator j=sources.begin(); j!=sources.end(); ++j)
76                         base = base.empty() ? *j : FS::common_ancestor(base, *j);
77                 const SourceList &use_sources = (*i)->get_sources();
78                 for(SourceList::const_iterator j=use_sources.begin(); j!=use_sources.end(); ++j)
79                         base = FS::common_ancestor(base, *j);
80                 build_info.incpath.push_back(base);
81                 build_info.libs.push_back((*i)->get_name());
82                 if(!(*i)->get_install())
83                 {
84                         build_info.libmodes[(*i)->get_name()] = BuildInfo::STATIC;
85                         build_info.libpath.push_back((*i)->get_package().get_source_directory());
86                 }
87         }
88
89         if(type==LIBRARY || type==MODULE)
90                 if(build_info.libmode<BuildInfo::DYNAMIC)
91                         build_info.libmode = BuildInfo::DYNAMIC;
92
93         if(build_info.libmode<BuildInfo::DYNAMIC)
94         {
95                 for(Package::Requirements::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
96                 {
97                         const BuildInfo &ebi = (*i)->get_exported_build_info();
98                         build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
99                 }
100         }
101 }
102
103 BuildInfo Component::get_build_info_for_path(const FS::Path &path) const
104 {
105         // XXX Cache these and check that the directories actually exist before adding them
106         BuildInfo binfo = build_info;
107         if(!overlays.empty())
108         {
109                 FS::Path dir = FS::dirname(path);
110                 string last = FS::basename(dir);
111                 for(OverlayList::const_iterator i=overlays.begin(); i!=overlays.end(); ++i)
112                         if(last==*i)
113                         {
114                                 dir = FS::dirname(dir);
115                                 break;
116                         }
117
118                 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
119                         if(dir==*i)
120                         {
121                                 binfo.local_incpath.push_back(dir);
122                                 for(OverlayList::const_iterator j=overlays.begin(); j!=overlays.end(); ++j)
123                                         binfo.local_incpath.push_back(*i/ *j);
124                         }
125         }
126         return binfo;
127 }
128
129 void Component::create_targets() const
130 {
131         Builder &builder = package.get_builder();
132         BuildGraph &build_graph = builder.get_build_graph();
133         const Toolchain &toolchain = builder.get_toolchain();
134
135         SourceList source_filenames = collect_source_files();
136
137         string inst_loc;
138         if(type==TARBALL)
139         {
140                 Tool &tar = toolchain.get_tool("TAR");
141
142                 list<Target *> files;
143                 for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
144                 {
145                         FileTarget *file = builder.get_vfs().get_target(*i);
146                         if(!file)
147                                 file = new File(builder, package, *i);
148                         files.push_back(file);
149                 }
150
151                 string tarname = name;
152                 if(name=="@src")
153                 {
154                         tarname = package.get_name()+"-"+package.get_version();
155                         files.insert(files.begin(), &package.get_build_file());
156
157                         const BuildGraph::TargetMap &targets = build_graph.get_targets();
158                         for(BuildGraph::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
159                                 if(i->second->get_package()==&package && !i->second->is_buildable())
160                                         if(find(files.begin(), files.end(), i->second)==files.end())
161                                                 files.push_back(i->second);
162                 }
163
164                 Target *result = tar.create_target(files, tarname);
165
166                 build_graph.get_target("tarballs")->add_dependency(*result);
167
168                 return;
169         }
170         else if(type==INSTALL)
171         {
172                 Target *inst = build_graph.get_target("install");
173                 Tool &copy = toolchain.get_tool("CP");
174                 for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
175                 {
176                         FileTarget *ft;
177                         if(Target *tgt = builder.get_vfs().get_target(*i))
178                                 ft = dynamic_cast<FileTarget *>(tgt);
179                         else
180                                 ft = new File(builder, package, *i);
181                         inst->add_dependency(*copy.create_target(*ft, name));
182                 }
183         }
184         else if(type==DATAPACK)
185         {
186                 Tool &dcomp = toolchain.get_tool("DATA");
187
188                 list<Target *> files;
189                 for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
190                 {
191                         string ext = FS::extpart(FS::basename(*i));
192                         if(ext==".mdt")
193                         {
194                                 Target *src = dcomp.create_source(*this, *i);
195                                 files.push_back(dcomp.create_target(*src, "collection"));
196                         }
197                         else if(Target *tgt = builder.get_vfs().get_target(*i))
198                                 files.push_back(tgt);
199                         else
200                                 files.push_back(new File(builder, package, *i));
201                 }
202
203                 Target *result = dcomp.create_target(files, "pack");
204
205                 build_graph.add_primary_target(*result);
206                 if(install)
207                         build_graph.add_installed_target(*result);
208         }
209
210         if(type==PROGRAM || type==LIBRARY || type==MODULE)
211         {
212                 list<Target *> objs;
213                 const Toolchain &pkg_tools = package.get_toolchain();
214                 for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
215                 {
216                         string ext = FS::extpart(FS::basename(*i));
217                         Target *src = 0;
218
219                         Tool *gen = pkg_tools.get_tool_for_suffix(ext);
220                         if(gen)
221                         {
222                                 Target *tmpl = gen->create_source(*this, *i);
223                                 if(tmpl)
224                                 {
225                                         src = gen->create_target(*tmpl);
226                                         ext = FS::extpart(FS::basename(dynamic_cast<FileTarget &>(*src).get_path()));
227                                 }
228                         }
229
230                         Tool *tool = toolchain.get_tool_for_suffix(ext, true);
231                         if(tool)
232                         {
233                                 if(!src)
234                                         src = tool->create_source(*this, *i);
235                                 if(!src)
236                                         continue;
237
238                                 if(tool->accepts_suffix(ext))
239                                 {
240                                         Target *obj = tool->create_target(*src);
241                                         objs.push_back(obj);
242                                 }
243
244                                 if(type==LIBRARY && install)
245                                 {
246                                         if(dynamic_cast<FileTarget *>(src)->is_installable())
247                                                 build_graph.add_installed_target(*src);
248
249                                         const Target::Dependencies &side_effects = src->get_side_effects();
250                                         for(Target::Dependencies::const_iterator j=side_effects.begin(); j!=side_effects.end(); ++j)
251                                                 if(dynamic_cast<FileTarget *>(*j)->is_installable())
252                                                         build_graph.add_installed_target(**j);
253                                 }
254                         }
255                 }
256
257                 Tool &linker = toolchain.get_tool("LINK");
258
259                 list<Target *> results;
260                 if(type==LIBRARY)
261                 {
262                         Tool &archiver = toolchain.get_tool("AR");
263                         results.push_back(linker.create_target(objs, "shared"));
264                         results.push_back(archiver.create_target(objs));
265                 }
266                 else if(type==MODULE)
267                         results.push_back(linker.create_target(objs, "shared"));
268                 else
269                         results.push_back(linker.create_target(objs));
270
271                 for(list<Target *>::const_iterator i=results.begin(); i!=results.end(); ++i)
272                 {
273                         build_graph.add_primary_target(**i);
274                         if(install)
275                                 build_graph.add_installed_target(**i);
276                 }
277         }
278 }
279
280 Component::SourceList Component::collect_source_files() const
281 {
282         SourceList files;
283         for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
284         {
285                 FS::Path path(*i);
286                 if(FS::is_dir(path))
287                 {
288                         SourceList dirs;
289                         dirs.push_back(path);
290                         for(OverlayList::const_iterator j=overlays.begin(); j!=overlays.end(); ++j)
291                         {
292                                 FS::Path opath = path / *j;
293                                 if(FS::is_dir(opath))
294                                         dirs.push_back(opath);
295                         }
296                         for(SourceList::const_iterator j=dirs.begin(); j!=dirs.end(); ++j)
297                         {
298                                 package.get_builder().get_logger().log("files", format("Traversing %s", *j));
299                                 list<string> sfiles = list_files(*j);
300                                 for(list<string>::iterator k=sfiles.begin(); k!=sfiles.end(); ++k)
301                                         files.push_back(*j / *k);
302                         }
303                 }
304                 else
305                 {
306                         files.push_back(path);
307                         for(OverlayList::const_iterator j=overlays.begin(); j!=overlays.end(); ++j)
308                         {
309                                 FS::Path opath = FS::dirname(path)/ *j/FS::basename(path);
310                                 if(FS::is_reg(opath))
311                                         files.push_back(opath);
312                         }
313                 }
314         }
315
316         return files;
317 }
318
319
320 Component::Loader::Loader(Component &c):
321         DataFile::ObjectLoader<Component>(c)
322 {
323         add("if_arch",         &Loader::if_arch);
324         add("if_feature",      &Loader::if_feature);
325         add("overlay",         &Loader::overlay);
326         add("source",          &Loader::source);
327         add("install",         &Component::install);
328         add("install_map",     &Loader::install_map);
329         add("build_info",      &Loader::build_info);
330         add("require",         &Loader::require);
331         add("default",         &Component::deflt);
332         add("use",             &Loader::use);
333 }
334
335 void Component::Loader::build_info()
336 {
337         load_sub(obj.build_info);
338 }
339
340 void Component::Loader::if_arch(const string &cond)
341 {
342         bool match = obj.package.get_builder().get_current_arch().match_name(cond);
343         obj.package.get_builder().get_logger().log("configure",
344                 format("%s/%s: arch %s %smatched", obj.package.get_name(), obj.name, cond, (match ? "" : "not ")));
345         if(match)
346                 load_sub_with(*this);
347 }
348
349 void Component::Loader::if_feature(const string &cond)
350 {
351         bool match = obj.package.match_feature(cond);
352         obj.package.get_builder().get_logger().log("configure",
353                 format("%s/%s: feature %s %smatched", obj.package.get_name(), obj.name, cond, (match ? "" : "not ")));
354         if(match)
355                 load_sub_with(*this);
356 }
357
358 void Component::Loader::install_map()
359 {
360         load_sub(obj.install_map, obj.package.get_source_directory());
361 }
362
363 void Component::Loader::overlay(const string &o)
364 {
365         obj.overlays.push_back(o);
366 }
367
368 void Component::Loader::require(const string &n)
369 {
370         Package *req = obj.package.get_builder().get_package_manager().find_package(n);
371         if(req)
372                 obj.requires.push_back(req);
373         else
374                 obj.problems.push_back(format("Required package %s not found", n));
375 }
376
377 void Component::Loader::source(const string &s)
378 {
379         obj.sources.push_back((obj.package.get_source_directory()/s).str());
380 }
381
382 void Component::Loader::use(const string &n)
383 {
384         const SourcePackage::ComponentList &components = obj.package.get_components();
385         for(SourcePackage::ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
386                 if(i->get_name()==n && i->get_type()==LIBRARY)
387                 {
388                         obj.uses.push_back(&*i);
389                         return;
390                 }
391         throw invalid_argument("Component::Loader::use");
392 }