]> git.tdb.fi Git - builder.git/blob - source/component.cpp
Only get DataTransform directory contents when needed
[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 "booleanevaluator.h"
8 #include "builder.h"
9 #include "component.h"
10 #include "sourcepackage.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 Component::Component(SourcePackage &p, const string &n):
16         package(p),
17         name(n),
18         install(false),
19         deflt(true)
20 { }
21
22 void Component::prepare()
23 {
24         for(Package::Requirements::const_iterator i=requires.begin(); i!=requires.end(); ++i)
25                 (*i)->prepare();
26 }
27
28 void Component::create_build_info()
29 {
30         BuildInfo final_build_info;
31
32         const Package::Requirements &pkg_reqs = package.get_required_packages();
33         Package::Requirements direct_reqs = requires;
34         direct_reqs.insert(direct_reqs.end(), pkg_reqs.begin(), pkg_reqs.end());
35
36         Package::Requirements all_reqs = direct_reqs;
37         for(Package::Requirements::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
38         {
39                 BuildInfo::UpdateLevel level = BuildInfo::CHAINED;
40                 if(find(direct_reqs.begin(), direct_reqs.end(), *i)!=direct_reqs.end())
41                         level = BuildInfo::DEPENDENCY;
42                 final_build_info.update_from((*i)->get_exported_build_info(), level);
43
44                 const Package::Requirements &reqs = (*i)->get_required_packages();
45                 for(Package::Requirements::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
46                         if(find(all_reqs.begin(), all_reqs.end(), *j)==all_reqs.end())
47                                 all_reqs.push_back(*j);
48         }
49
50         final_build_info.update_from(package.get_build_info());
51         final_build_info.update_from(build_info);
52         build_info = final_build_info;
53
54         for(BuildInfo::PathList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
55                 *i = (package.get_source_directory() / *i).str();
56         for(BuildInfo::PathList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
57                 *i = (package.get_source_directory() / *i).str();
58 }
59
60 BuildInfo Component::get_build_info_for_path(const FS::Path &path) const
61 {
62         // XXX Cache these and check that the directories actually exist before adding them
63         BuildInfo binfo = build_info;
64         if(!overlays.empty())
65         {
66                 FS::Path dir = FS::dirname(path);
67                 string last = FS::basename(dir);
68                 for(OverlayList::const_iterator i=overlays.begin(); i!=overlays.end(); ++i)
69                         if(last==*i)
70                         {
71                                 dir = FS::dirname(dir);
72                                 break;
73                         }
74
75                 for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
76                         if(dir==*i)
77                         {
78                                 binfo.local_incpath.push_back(dir);
79                                 for(OverlayList::const_iterator j=overlays.begin(); j!=overlays.end(); ++j)
80                                         binfo.local_incpath.push_back(*i/ *j);
81                         }
82         }
83         return binfo;
84 }
85
86 Component::SourceList Component::collect_source_files() const
87 {
88         SourceList files;
89         for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
90         {
91                 FS::Path path(*i);
92                 if(FS::is_dir(path))
93                 {
94                         SourceList dirs;
95                         dirs.push_back(path);
96                         for(OverlayList::const_iterator j=overlays.begin(); j!=overlays.end(); ++j)
97                         {
98                                 FS::Path opath = path / *j;
99                                 if(FS::is_dir(opath))
100                                         dirs.push_back(opath);
101                         }
102                         for(SourceList::const_iterator j=dirs.begin(); j!=dirs.end(); ++j)
103                         {
104                                 package.get_builder().get_logger().log("files", format("Traversing %s", *j));
105                                 list<string> sfiles = list_files(*j);
106                                 for(list<string>::iterator k=sfiles.begin(); k!=sfiles.end(); ++k)
107                                         files.push_back(*j / *k);
108                         }
109                 }
110                 else
111                 {
112                         files.push_back(path);
113                         for(OverlayList::const_iterator j=overlays.begin(); j!=overlays.end(); ++j)
114                         {
115                                 FS::Path opath = FS::dirname(path)/ *j/FS::basename(path);
116                                 if(FS::is_reg(opath))
117                                         files.push_back(opath);
118                         }
119                 }
120         }
121
122         return files;
123 }
124
125
126 Component::Loader::Loader(Component &c):
127         DataFile::ObjectLoader<Component>(c)
128 {
129         add("if_arch",         &Loader::if_arch);
130         add("if_feature",      &Loader::if_feature);
131         add("overlay",         &Loader::overlay);
132         add("source",          &Loader::source);
133         add("install",         &Component::install);
134         add("install_map",     &Loader::install_map);
135         add("build_info",      &Loader::build_info);
136         add("require",         &Loader::require);
137         add("default",         &Component::deflt);
138 }
139
140 void Component::Loader::build_info()
141 {
142         load_sub(obj.build_info);
143 }
144
145 void Component::Loader::if_arch(const string &cond)
146 {
147         BooleanEvaluator eval(sigc::hide<1>(sigc::mem_fun(&obj.package.get_builder().get_current_arch(), &Architecture::match_name)), false);
148         bool match = eval.evaluate(cond);
149         obj.package.get_builder().get_logger().log("configure",
150                 format("%s/%s: arch %s %smatched", obj.package.get_name(), obj.name, cond, (match ? "" : "not ")));
151         if(match)
152                 load_sub_with(*this);
153 }
154
155 void Component::Loader::if_feature(const string &cond)
156 {
157         BooleanEvaluator eval(sigc::mem_fun(&obj.package, &SourcePackage::match_feature));
158         bool match = eval.evaluate(cond);
159         obj.package.get_builder().get_logger().log("configure",
160                 format("%s/%s: feature %s %smatched", obj.package.get_name(), obj.name, cond, (match ? "" : "not ")));
161         if(match)
162                 load_sub_with(*this);
163 }
164
165 void Component::Loader::install_map()
166 {
167         load_sub(obj.install_map, obj.package.get_source_directory());
168 }
169
170 void Component::Loader::overlay(const string &o)
171 {
172         obj.overlays.push_back(o);
173 }
174
175 void Component::Loader::require(const string &n)
176 {
177         Package *req = obj.package.get_builder().get_package_manager().find_package(n);
178         if(req)
179                 obj.requires.push_back(req);
180         else
181                 obj.problems.push_back(format("Required package %s not found", n));
182 }
183
184 void Component::Loader::source(const string &s)
185 {
186         obj.sources.push_back((obj.package.get_source_directory()/s).str());
187 }