]> git.tdb.fi Git - builder.git/blob - source/lib/component.cpp
Remove the implicit include path for the generated source directory
[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         if(!overlays.empty())
96         {
97                 FS::Path dir = FS::dirname(path);
98                 string last = FS::basename(dir);
99                 if(any_equals(overlays, last))
100                         dir = FS::dirname(dir);
101
102                 if(any_equals(sources, dir))
103                 {
104                         binfo.local_incpath.push_back(dir);
105                         for(const string &o: overlays)
106                                 binfo.local_incpath.push_back(dir/o);
107                 }
108         }
109         return binfo;
110 }
111
112 vector<FS::Path> Component::collect_source_files() const
113 {
114         vector<FS::Path> files;
115         for(const FS::Path &p: sources)
116         {
117                 if(FS::is_dir(p))
118                 {
119                         vector<FS::Path> dirs;
120                         dirs.reserve(1+overlays.size());
121                         dirs.push_back(p);
122                         for(const string &o: overlays)
123                         {
124                                 FS::Path opath = p/o;
125                                 if(FS::is_dir(opath))
126                                         dirs.push_back(opath);
127                         }
128                         set<string> overlay_files;
129                         for(auto j=dirs.begin(); j!=dirs.end(); ++j)
130                         {
131                                 package.get_builder().get_logger().log("files", "Traversing %s", *j);
132                                 for(const string &f: list_files(*j))
133                                 {
134                                         if(j!=dirs.begin())
135                                         {
136                                                 if(overlay_files.count(f))
137                                                         continue;
138                                                 overlay_files.insert(f);
139                                         }
140                                         FS::Path fn = *j/f;
141                                         if(!FS::is_dir(fn))
142                                                 files.push_back(fn);
143                                 }
144                         }
145                 }
146                 else
147                 {
148                         files.push_back(p);
149                         for(const string &o: overlays)
150                         {
151                                 FS::Path opath = FS::dirname(p)/o/FS::basename(p);
152                                 if(FS::is_reg(opath))
153                                         files.push_back(opath);
154                         }
155                 }
156         }
157
158         return files;
159 }
160
161
162 Component::Loader::Loader(Component &c):
163         DataFile::ObjectLoader<Component>(c),
164         ConditionalLoader(c.package, format("%s/%s", c.package.get_name(), c.name))
165 {
166         add("overlay",         &Loader::overlay);
167         add("source",          &Loader::source);
168         add("install",         &Component::install);
169         add("install_map",     &Loader::install_map);
170         add("build_info",      &Loader::build_info);
171         add("require",         &Loader::require);
172         add("default",         &Component::deflt);
173 }
174
175 void Component::Loader::build_info()
176 {
177         load_sub(obj.build_info);
178 }
179
180 void Component::Loader::install_map()
181 {
182         load_sub(obj.install_map, obj.package.get_source_directory());
183 }
184
185 void Component::Loader::overlay(const string &o)
186 {
187         obj.overlays.push_back(o);
188 }
189
190 void Component::Loader::require(const string &n)
191 {
192         Package *req = obj.package.get_builder().get_package_manager().find_package(n);
193         if(req)
194                 obj.required_pkgs.push_back(req);
195         else
196                 obj.problems.push_back(format("Required package %s not found", n));
197 }
198
199 void Component::Loader::source(const string &s)
200 {
201         FS::Path src_path = obj.package.get_source_directory()/s;
202         if(!FS::exists(src_path))
203                 throw IO::file_not_found(src_path.str());
204         obj.sources.push_back(src_path);
205 }