]> git.tdb.fi Git - builder.git/blob - source/lib/binarycomponent.cpp
Refactor transitive dependencies to work on all targets
[builder.git] / source / lib / binarycomponent.cpp
1 #include <msp/fs/utils.h>
2 #include "binarycomponent.h"
3 #include "builder.h"
4 #include "filetarget.h"
5 #include "sourcepackage.h"
6 #include "tool.h"
7
8 using namespace std;
9 using namespace Msp;
10
11 void BinaryComponent::create_build_info()
12 {
13         Component::create_build_info();
14
15         for(const Component *u: uses)
16         {
17                 build_info.libs.push_back(u->get_name());
18                 if(!u->get_install())
19                 {
20                         build_info.libmodes[u->get_name()] = BuildInfo::STATIC;
21                         build_info.libpath.push_back(u->get_package().get_output_directory());
22                 }
23         }
24
25         if(type==LIBRARY || type==MODULE)
26                 if(build_info.libmode<BuildInfo::DYNAMIC)
27                         build_info.libmode = BuildInfo::DYNAMIC;
28 }
29
30 void BinaryComponent::update_exported_build_info(BuildInfo &binfo) const
31 {
32         if(type==LIBRARY)
33                 binfo.libs.push_back(name);
34 }
35
36 void BinaryComponent::create_targets() const
37 {
38         Builder &builder = package.get_builder();
39         BuildGraph &build_graph = builder.get_build_graph();
40         const Toolchain &toolchain = builder.get_toolchain();
41         const Toolchain &pkg_tools = package.get_toolchain();
42
43         Tool &linker = toolchain.get_tool("LINK");
44
45         vector<FileTarget *> pending = create_sources();
46
47         vector<Target *> objs;
48         while(!pending.empty())
49         {
50                 Tool *tool = 0;
51                 for(auto i=pending.begin(); i!=pending.end(); )
52                 {
53                         string ext = FS::extpart(FS::basename((*i)->get_path()));
54                         if(linker.accepts_suffix(ext))
55                         {
56                                 /* Targets accepted by the linker should not be processed further
57                                 before feeding them to the linker */
58                                 objs.push_back(*i);
59                                 i = pending.erase(i);
60                                 continue;
61                         }
62
63                         // Pick the tool with the smallest processing unit
64                         Tool *tgt_tool = pkg_tools.get_tool_for_suffix(ext);
65                         if(!tgt_tool)
66                                 tgt_tool = toolchain.get_tool_for_suffix(ext);
67                         if(tgt_tool && (!tool || tgt_tool->get_processing_unit()<tool->get_processing_unit()))
68                         {
69                                 tool = tgt_tool;
70                                 if(tool->get_processing_unit()==Tool::ONE_FILE)
71                                         break;
72                         }
73
74                         ++i;
75                 }
76
77                 if(!tool)
78                         break;
79
80                 Tool::ProcessingUnit processing_unit = tool->get_processing_unit();
81                 if(processing_unit==Tool::ONE_FILE)
82                 {
83                         // Process all pending accepted by the tool
84                         for(FileTarget *&t: pending)
85                         {
86                                 string ext = FS::extpart(FS::basename(t->get_path()));
87                                 if(tool->accepts_suffix(ext))
88                                 {
89                                         t = &dynamic_cast<FileTarget &>(*tool->create_target(*t));
90                                         if(type==LIBRARY)
91                                                 create_install(*t);
92                                 }
93                         }
94                 }
95                 else
96                 {
97                         // Process one group of pending accepted by the tool
98                         vector<Target *> group = extract_group(pending, *tool);
99                         FileTarget &tgt = dynamic_cast<FileTarget &>(*tool->create_target(group));
100                         pending.push_back(&tgt);
101                         if(type==LIBRARY)
102                                 create_install(tgt);
103                 }
104         }
105
106         vector<Target *> results;
107         results.reserve(3);
108         if(type==LIBRARY)
109         {
110                 Tool &archiver = toolchain.get_tool("AR");
111                 Target *shlib = linker.create_target(objs, "shared");
112                 results.push_back(shlib);
113                 if(Target *imp = linker.create_target(*shlib, "import"))
114                         results.push_back(imp);
115                 results.push_back(archiver.create_target(objs));
116         }
117         else if(type==MODULE)
118                 results.push_back(linker.create_target(objs, "shared"));
119         else
120                 results.push_back(linker.create_target(objs));
121
122         for(Target *r: results)
123         {
124                 build_graph.add_primary_target(*r);
125                 if(install)
126                         build_graph.add_installed_target(*r);
127         }
128 }
129
130 vector<FileTarget *> BinaryComponent::create_sources() const
131 {
132         const Toolchain &toolchain = package.get_builder().get_toolchain();
133         const Toolchain &pkg_tools = package.get_toolchain();
134
135         vector<FileTarget *> targets;
136         for(const FS::Path &s: collect_source_files())
137         {
138                 string ext = FS::extpart(FS::basename(s));
139                 Target *src = 0;
140
141                 Tool *tool = pkg_tools.get_tool_for_suffix(ext, true);
142                 if(tool)
143                         src = tool->create_source(*this, s);
144                 if(!src)
145                 {
146                         tool = toolchain.get_tool_for_suffix(ext, true);
147                         if(tool)
148                                 src = tool->create_source(*this, s);
149                 }
150
151                 if(FileTarget *file = dynamic_cast<FileTarget *>(src))
152                 {
153                         targets.push_back(file);
154                         if(type==LIBRARY)
155                                 create_install(*file);
156                 }
157         }
158
159         return targets;
160 }
161
162 vector<Target *> BinaryComponent::extract_group(vector<FileTarget *> &targets, const Tool &tool)
163 {
164         Tool::ProcessingUnit processing_unit = tool.get_processing_unit();
165         vector<Target *> group;
166         FS::Path group_dir;
167         for(auto i=targets.begin(); i!=targets.end(); )
168         {
169                 const FS::Path &path = (*i)->get_path();
170                 string ext = FS::extpart(FS::basename(path));
171                 FS::Path dir = FS::dirname(path);
172                 if(tool.accepts_suffix(ext) && (processing_unit!=Tool::DIRECTORY || group.empty() || dir==group_dir))
173                 {
174                         group.push_back(*i);
175                         group_dir = dir;
176                         i = targets.erase(i);
177                 }
178                 else
179                         ++i;
180         }
181
182         return group;
183 }
184
185 void BinaryComponent::create_install(FileTarget &target) const
186 {
187         BuildGraph &build_graph = package.get_builder().get_build_graph();
188         auto add_func = (install ? &BuildGraph::add_installed_target : &BuildGraph::add_staged_target);
189
190         if(target.is_installable())
191                 (build_graph.*add_func)(target);
192
193         for(Target *s: target.get_side_effects())
194                 if(dynamic_cast<FileTarget &>(*s).is_installable())
195                         (build_graph.*add_func)(*s);
196 }
197
198
199 BinaryComponent::Loader::Loader(BinaryComponent &c):
200         DataFile::DerivedObjectLoader<BinaryComponent, Component::Loader>(c)
201 {
202         add("use", &Loader::use);
203 }
204
205 void BinaryComponent::Loader::use(const string &n)
206 {
207         const BinaryComponent *comp = dynamic_cast<const BinaryComponent *>(&obj.package.get_component(n));
208         if(!comp || comp->type!=LIBRARY)
209                 throw logic_error(n+" is not a library");
210
211         obj.uses.push_back(comp);
212 }