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