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