]> git.tdb.fi Git - builder.git/blob - source/lib/binarycomponent.cpp
Install headers of non-installed components in a staging directory
[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)
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)
110                                 create_install(tgt);
111                 }
112         }
113
114         vector<Target *> results;
115         results.reserve(3);
116         if(type==LIBRARY)
117         {
118                 Tool &archiver = toolchain.get_tool("AR");
119                 Target *shlib = linker.create_target(objs, "shared");
120                 results.push_back(shlib);
121                 if(Target *imp = linker.create_target(*shlib, "import"))
122                         results.push_back(imp);
123                 results.push_back(archiver.create_target(objs));
124         }
125         else if(type==MODULE)
126                 results.push_back(linker.create_target(objs, "shared"));
127         else
128                 results.push_back(linker.create_target(objs));
129
130         for(Target *r: results)
131         {
132                 build_graph.add_primary_target(*r);
133                 if(install)
134                         build_graph.add_installed_target(*r);
135         }
136 }
137
138 vector<FileTarget *> BinaryComponent::create_sources() const
139 {
140         const Toolchain &toolchain = package.get_builder().get_toolchain();
141         const Toolchain &pkg_tools = package.get_toolchain();
142
143         vector<FileTarget *> targets;
144         for(const FS::Path &s: collect_source_files())
145         {
146                 string ext = FS::extpart(FS::basename(s));
147                 Target *src = 0;
148
149                 Tool *tool = pkg_tools.get_tool_for_suffix(ext, true);
150                 if(tool)
151                         src = tool->create_source(*this, s);
152                 if(!src)
153                 {
154                         tool = toolchain.get_tool_for_suffix(ext, true);
155                         if(tool)
156                                 src = tool->create_source(*this, s);
157                 }
158
159                 if(FileTarget *file = dynamic_cast<FileTarget *>(src))
160                 {
161                         targets.push_back(file);
162                         if(type==LIBRARY)
163                                 create_install(*file);
164                 }
165         }
166
167         return targets;
168 }
169
170 vector<Target *> BinaryComponent::extract_group(vector<FileTarget *> &targets, const Tool &tool)
171 {
172         Tool::ProcessingUnit processing_unit = tool.get_processing_unit();
173         vector<Target *> group;
174         FS::Path group_dir;
175         for(auto i=targets.begin(); i!=targets.end(); )
176         {
177                 const FS::Path &path = (*i)->get_path();
178                 string ext = FS::extpart(FS::basename(path));
179                 FS::Path dir = FS::dirname(path);
180                 if(tool.accepts_suffix(ext) && (processing_unit!=Tool::DIRECTORY || group.empty() || dir==group_dir))
181                 {
182                         group.push_back(*i);
183                         group_dir = dir;
184                         i = targets.erase(i);
185                 }
186                 else
187                         ++i;
188         }
189
190         return group;
191 }
192
193 void BinaryComponent::create_install(FileTarget &target) const
194 {
195         BuildGraph &build_graph = package.get_builder().get_build_graph();
196         auto add_func = (install ? &BuildGraph::add_installed_target : &BuildGraph::add_staged_target);
197
198         if(target.is_installable())
199                 (build_graph.*add_func)(target);
200
201         for(Target *s: target.get_side_effects())
202                 if(dynamic_cast<FileTarget &>(*s).is_installable())
203                         (build_graph.*add_func)(*s);
204 }
205
206
207 BinaryComponent::Loader::Loader(BinaryComponent &c):
208         DataFile::DerivedObjectLoader<BinaryComponent, Component::Loader>(c)
209 {
210         add("use", &Loader::use);
211 }
212
213 void BinaryComponent::Loader::use(const string &n)
214 {
215         const BinaryComponent *comp = dynamic_cast<const BinaryComponent *>(&obj.package.get_component(n));
216         if(!comp || comp->type!=LIBRARY)
217                 throw logic_error(n+" is not a library");
218
219         obj.uses.push_back(comp);
220 }