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