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