]> git.tdb.fi Git - builder.git/blob - source/component.cpp
0186f3e394c3debe464a1d888ad12594fb941f0b
[builder.git] / source / component.cpp
1 #include <deque>
2 #include <msp/core/algorithm.h>
3 #include <msp/fs/dir.h>
4 #include <msp/fs/stat.h>
5 #include <msp/fs/utils.h>
6 #include <msp/strings/format.h>
7 #include "builder.h"
8 #include "component.h"
9 #include "sourcepackage.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 Component::Component(SourcePackage &p, const string &n):
15         package(p),
16         name(n)
17 { }
18
19 void Component::prepare()
20 {
21         for(Package *r: requires)
22                 r->prepare();
23 }
24
25 void Component::create_build_info()
26 {
27         BuildInfo final_build_info;
28
29         const Package::Requirements &pkg_reqs = package.get_required_packages();
30         Package::Requirements direct_reqs = requires;
31         direct_reqs.insert(direct_reqs.end(), pkg_reqs.begin(), pkg_reqs.end());
32         for(Package *r: direct_reqs)
33                 final_build_info.update_from(r->get_exported_build_info(), BuildInfo::DEPENDENCY);
34
35         Package::Requirements all_reqs = direct_reqs;
36         deque<Package *> queue(direct_reqs.begin(), direct_reqs.end());
37         while(!queue.empty())
38         {
39                 Package *req = queue.front();
40                 queue.pop_front();
41
42                 for(Package *r: req->get_required_packages())
43                         if(!any_equals(all_reqs, r))
44                         {
45                                 final_build_info.update_from(r->get_exported_build_info(), BuildInfo::CHAINED);
46                                 all_reqs.push_back(r);
47                                 queue.push_back(r);
48                         }
49         }
50
51         final_build_info.update_from(package.get_build_info());
52         final_build_info.update_from(build_info);
53         build_info = final_build_info;
54
55         for(FS::Path &p: build_info.incpath)
56                 p = (package.get_source_directory()/p).str();
57         for(FS::Path &p: build_info.libpath)
58                 p = (package.get_source_directory()/p).str();
59 }
60
61 BuildInfo Component::get_build_info_for_path(const FS::Path &path) const
62 {
63         // XXX Cache these and check that the directories actually exist before adding them
64         BuildInfo binfo = build_info;
65
66         FS::Path gen_dir = package.get_temp_directory()/"generated";
67         if(FS::descendant_depth(path, gen_dir)>=0)
68         {
69                 FS::Path subdir = FS::dirname(FS::relative(path, gen_dir));
70                 binfo.local_incpath.push_back(package.get_source_directory()/subdir);
71         }
72         else
73         {
74                 FS::Path subdir = FS::dirname(FS::relative(path, package.get_source_directory()));
75                 binfo.local_incpath.push_back(gen_dir/subdir);
76         }
77
78         if(!overlays.empty())
79         {
80                 FS::Path dir = FS::dirname(path);
81                 string last = FS::basename(dir);
82                 if(any_equals(overlays, last))
83                         dir = FS::dirname(dir);
84
85                 if(any_equals(sources, dir))
86                 {
87                         binfo.local_incpath.push_back(dir);
88                         for(const string &o: overlays)
89                                 binfo.local_incpath.push_back(dir/o);
90                 }
91         }
92         return binfo;
93 }
94
95 vector<FS::Path> Component::collect_source_files() const
96 {
97         vector<FS::Path> files;
98         for(const FS::Path &p: sources)
99         {
100                 if(FS::is_dir(p))
101                 {
102                         vector<FS::Path> dirs;
103                         dirs.reserve(1+overlays.size());
104                         dirs.push_back(p);
105                         for(const string &o: overlays)
106                         {
107                                 FS::Path opath = p/o;
108                                 if(FS::is_dir(opath))
109                                         dirs.push_back(opath);
110                         }
111                         set<string> overlay_files;
112                         for(auto j=dirs.begin(); j!=dirs.end(); ++j)
113                         {
114                                 package.get_builder().get_logger().log("files", "Traversing %s", *j);
115                                 for(const string &f: list_files(*j))
116                                 {
117                                         if(j!=dirs.begin())
118                                         {
119                                                 if(overlay_files.count(f))
120                                                         continue;
121                                                 overlay_files.insert(f);
122                                         }
123                                         FS::Path fn = *j/f;
124                                         if(!FS::is_dir(fn))
125                                                 files.push_back(fn);
126                                 }
127                         }
128                 }
129                 else
130                 {
131                         files.push_back(p);
132                         for(const string &o: overlays)
133                         {
134                                 FS::Path opath = FS::dirname(p)/o/FS::basename(p);
135                                 if(FS::is_reg(opath))
136                                         files.push_back(opath);
137                         }
138                 }
139         }
140
141         return files;
142 }
143
144
145 Component::Loader::Loader(Component &c):
146         DataFile::ObjectLoader<Component>(c),
147         ConditionalLoader(c.package, format("%s/%s", c.package.get_name(), c.name))
148 {
149         add("overlay",         &Loader::overlay);
150         add("source",          &Loader::source);
151         add("install",         &Component::install);
152         add("install_map",     &Loader::install_map);
153         add("build_info",      &Loader::build_info);
154         add("require",         &Loader::require);
155         add("default",         &Component::deflt);
156 }
157
158 void Component::Loader::build_info()
159 {
160         load_sub(obj.build_info);
161 }
162
163 void Component::Loader::install_map()
164 {
165         load_sub(obj.install_map, obj.package.get_source_directory());
166 }
167
168 void Component::Loader::overlay(const string &o)
169 {
170         obj.overlays.push_back(o);
171 }
172
173 void Component::Loader::require(const string &n)
174 {
175         Package *req = obj.package.get_builder().get_package_manager().find_package(n);
176         if(req)
177                 obj.requires.push_back(req);
178         else
179                 obj.problems.push_back(format("Required package %s not found", n));
180 }
181
182 void Component::Loader::source(const string &s)
183 {
184         obj.sources.push_back((obj.package.get_source_directory()/s).str());
185 }