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