]> git.tdb.fi Git - builder.git/blob - source/lib/component.cpp
Rename some variables to avoid conflicts with C++20 keywords
[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 BuildInfo Component::get_build_info_for_path(const FS::Path &path) const
69 {
70         // XXX Cache these and check that the directories actually exist before adding them
71         BuildInfo binfo = build_info;
72
73         FS::Path gen_dir = package.get_temp_directory()/"generated";
74         if(FS::descendant_depth(path, gen_dir)>=0)
75         {
76                 FS::Path subdir = FS::dirname(FS::relative(path, gen_dir));
77                 binfo.local_incpath.push_back(package.get_source_directory()/subdir);
78         }
79         else
80         {
81                 FS::Path subdir = FS::dirname(FS::relative(path, package.get_source_directory()));
82                 binfo.local_incpath.push_back(gen_dir/subdir);
83         }
84
85         if(!overlays.empty())
86         {
87                 FS::Path dir = FS::dirname(path);
88                 string last = FS::basename(dir);
89                 if(any_equals(overlays, last))
90                         dir = FS::dirname(dir);
91
92                 if(any_equals(sources, dir))
93                 {
94                         binfo.local_incpath.push_back(dir);
95                         for(const string &o: overlays)
96                                 binfo.local_incpath.push_back(dir/o);
97                 }
98         }
99         return binfo;
100 }
101
102 vector<FS::Path> Component::collect_source_files() const
103 {
104         vector<FS::Path> files;
105         for(const FS::Path &p: sources)
106         {
107                 if(FS::is_dir(p))
108                 {
109                         vector<FS::Path> dirs;
110                         dirs.reserve(1+overlays.size());
111                         dirs.push_back(p);
112                         for(const string &o: overlays)
113                         {
114                                 FS::Path opath = p/o;
115                                 if(FS::is_dir(opath))
116                                         dirs.push_back(opath);
117                         }
118                         set<string> overlay_files;
119                         for(auto j=dirs.begin(); j!=dirs.end(); ++j)
120                         {
121                                 package.get_builder().get_logger().log("files", "Traversing %s", *j);
122                                 for(const string &f: list_files(*j))
123                                 {
124                                         if(j!=dirs.begin())
125                                         {
126                                                 if(overlay_files.count(f))
127                                                         continue;
128                                                 overlay_files.insert(f);
129                                         }
130                                         FS::Path fn = *j/f;
131                                         if(!FS::is_dir(fn))
132                                                 files.push_back(fn);
133                                 }
134                         }
135                 }
136                 else
137                 {
138                         files.push_back(p);
139                         for(const string &o: overlays)
140                         {
141                                 FS::Path opath = FS::dirname(p)/o/FS::basename(p);
142                                 if(FS::is_reg(opath))
143                                         files.push_back(opath);
144                         }
145                 }
146         }
147
148         return files;
149 }
150
151
152 Component::Loader::Loader(Component &c):
153         DataFile::ObjectLoader<Component>(c),
154         ConditionalLoader(c.package, format("%s/%s", c.package.get_name(), c.name))
155 {
156         add("overlay",         &Loader::overlay);
157         add("source",          &Loader::source);
158         add("install",         &Component::install);
159         add("install_map",     &Loader::install_map);
160         add("build_info",      &Loader::build_info);
161         add("require",         &Loader::require);
162         add("default",         &Component::deflt);
163 }
164
165 void Component::Loader::build_info()
166 {
167         load_sub(obj.build_info);
168 }
169
170 void Component::Loader::install_map()
171 {
172         load_sub(obj.install_map, obj.package.get_source_directory());
173 }
174
175 void Component::Loader::overlay(const string &o)
176 {
177         obj.overlays.push_back(o);
178 }
179
180 void Component::Loader::require(const string &n)
181 {
182         Package *req = obj.package.get_builder().get_package_manager().find_package(n);
183         if(req)
184                 obj.required_pkgs.push_back(req);
185         else
186                 obj.problems.push_back(format("Required package %s not found", n));
187 }
188
189 void Component::Loader::source(const string &s)
190 {
191         FS::Path src_path = obj.package.get_source_directory()/s;
192         if(!FS::exists(src_path))
193                 throw IO::file_not_found(src_path.str());
194         obj.sources.push_back(src_path);
195 }