]> git.tdb.fi Git - builder.git/blob - source/component.cpp
Only use files from the first overlay that has them
[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                         set<string> overlay_files;
103                         for(SourceList::const_iterator j=dirs.begin(); j!=dirs.end(); ++j)
104                         {
105                                 package.get_builder().get_logger().log("files", format("Traversing %s", *j));
106                                 list<string> sfiles = list_files(*j);
107                                 for(list<string>::iterator k=sfiles.begin(); k!=sfiles.end(); ++k)
108                                 {
109                                         if(j!=dirs.begin())
110                                         {
111                                                 if(overlay_files.count(*k))
112                                                         continue;
113                                                 overlay_files.insert(*k);
114                                         }
115                                         files.push_back(*j / *k);
116                                 }
117                         }
118                 }
119                 else
120                 {
121                         files.push_back(path);
122                         for(OverlayList::const_iterator j=overlays.begin(); j!=overlays.end(); ++j)
123                         {
124                                 FS::Path opath = FS::dirname(path)/ *j/FS::basename(path);
125                                 if(FS::is_reg(opath))
126                                         files.push_back(opath);
127                         }
128                 }
129         }
130
131         return files;
132 }
133
134
135 Component::Loader::Loader(Component &c):
136         DataFile::ObjectLoader<Component>(c)
137 {
138         add("if_arch",         &Loader::if_arch);
139         add("if_feature",      &Loader::if_feature);
140         add("overlay",         &Loader::overlay);
141         add("source",          &Loader::source);
142         add("install",         &Component::install);
143         add("install_map",     &Loader::install_map);
144         add("build_info",      &Loader::build_info);
145         add("require",         &Loader::require);
146         add("default",         &Component::deflt);
147 }
148
149 void Component::Loader::build_info()
150 {
151         load_sub(obj.build_info);
152 }
153
154 void Component::Loader::if_arch(const string &cond)
155 {
156         BooleanEvaluator eval(sigc::hide<1>(sigc::mem_fun(&obj.package.get_builder().get_current_arch(), &Architecture::match_name)), false);
157         bool match = eval.evaluate(cond);
158         obj.package.get_builder().get_logger().log("configure",
159                 format("%s/%s: arch %s %smatched", obj.package.get_name(), obj.name, cond, (match ? "" : "not ")));
160         if(match)
161                 load_sub_with(*this);
162 }
163
164 void Component::Loader::if_feature(const string &cond)
165 {
166         BooleanEvaluator eval(sigc::mem_fun(&obj.package, &SourcePackage::match_feature));
167         bool match = eval.evaluate(cond);
168         obj.package.get_builder().get_logger().log("configure",
169                 format("%s/%s: feature %s %smatched", obj.package.get_name(), obj.name, cond, (match ? "" : "not ")));
170         if(match)
171                 load_sub_with(*this);
172 }
173
174 void Component::Loader::install_map()
175 {
176         load_sub(obj.install_map, obj.package.get_source_directory());
177 }
178
179 void Component::Loader::overlay(const string &o)
180 {
181         obj.overlays.push_back(o);
182 }
183
184 void Component::Loader::require(const string &n)
185 {
186         Package *req = obj.package.get_builder().get_package_manager().find_package(n);
187         if(req)
188                 obj.requires.push_back(req);
189         else
190                 obj.problems.push_back(format("Required package %s not found", n));
191 }
192
193 void Component::Loader::source(const string &s)
194 {
195         obj.sources.push_back((obj.package.get_source_directory()/s).str());
196 }