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