1 #include <msp/fs/utils.h>
2 #include "binarycomponent.h"
4 #include "filetarget.h"
5 #include "sourcepackage.h"
11 void BinaryComponent::create_build_info()
13 Component::create_build_info();
15 for(const Component *u: uses)
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. */
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());
28 build_info.libmodes[u->get_name()] = BuildInfo::STATIC;
29 build_info.libpath.push_back(u->get_package().get_output_directory());
33 if(type==LIBRARY || type==MODULE)
34 if(build_info.libmode<BuildInfo::DYNAMIC)
35 build_info.libmode = BuildInfo::DYNAMIC;
38 void BinaryComponent::update_exported_build_info(BuildInfo &binfo) const
41 binfo.libs.push_back(name);
44 void BinaryComponent::create_targets() const
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();
51 Tool &linker = toolchain.get_tool("LINK");
53 vector<FileTarget *> pending = create_sources();
55 vector<Target *> objs;
56 while(!pending.empty())
59 for(auto i=pending.begin(); i!=pending.end(); )
61 string ext = FS::extpart(FS::basename((*i)->get_path()));
62 if(linker.accepts_suffix(ext))
64 /* Targets accepted by the linker should not be processed further
65 before feeding them to the linker */
71 // Pick the tool with the smallest processing unit
72 Tool *tgt_tool = pkg_tools.get_tool_for_suffix(ext);
74 tgt_tool = toolchain.get_tool_for_suffix(ext);
75 if(tgt_tool && (!tool || tgt_tool->get_processing_unit()<tool->get_processing_unit()))
78 if(tool->get_processing_unit()==Tool::ONE_FILE)
88 Tool::ProcessingUnit processing_unit = tool->get_processing_unit();
89 if(processing_unit==Tool::ONE_FILE)
91 // Process all pending accepted by the tool
92 for(FileTarget *&t: pending)
94 string ext = FS::extpart(FS::basename(t->get_path()));
95 if(tool->accepts_suffix(ext))
97 t = &dynamic_cast<FileTarget &>(*tool->create_target(*t));
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);
114 vector<Target *> results;
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));
125 else if(type==MODULE)
126 results.push_back(linker.create_target(objs, "shared"));
128 results.push_back(linker.create_target(objs));
130 for(Target *r: results)
132 build_graph.add_primary_target(*r);
134 build_graph.add_installed_target(*r);
138 vector<FileTarget *> BinaryComponent::create_sources() const
140 const Toolchain &toolchain = package.get_builder().get_toolchain();
141 const Toolchain &pkg_tools = package.get_toolchain();
143 vector<FileTarget *> targets;
144 for(const FS::Path &s: collect_source_files())
146 string ext = FS::extpart(FS::basename(s));
149 Tool *tool = pkg_tools.get_tool_for_suffix(ext, true);
151 src = tool->create_source(*this, s);
154 tool = toolchain.get_tool_for_suffix(ext, true);
156 src = tool->create_source(*this, s);
159 if(FileTarget *file = dynamic_cast<FileTarget *>(src))
161 targets.push_back(file);
163 create_install(*file);
170 vector<Target *> BinaryComponent::extract_group(vector<FileTarget *> &targets, const Tool &tool)
172 Tool::ProcessingUnit processing_unit = tool.get_processing_unit();
173 vector<Target *> group;
175 for(auto i=targets.begin(); i!=targets.end(); )
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))
184 i = targets.erase(i);
193 void BinaryComponent::create_install(FileTarget &target) const
195 BuildGraph &build_graph = package.get_builder().get_build_graph();
196 auto add_func = (install ? &BuildGraph::add_installed_target : &BuildGraph::add_staged_target);
198 if(target.is_installable())
199 (build_graph.*add_func)(target);
201 for(Target *s: target.get_side_effects())
202 if(dynamic_cast<FileTarget &>(*s).is_installable())
203 (build_graph.*add_func)(*s);
207 BinaryComponent::Loader::Loader(BinaryComponent &c):
208 DataFile::DerivedObjectLoader<BinaryComponent, Component::Loader>(c)
210 add("use", &Loader::use);
213 void BinaryComponent::Loader::use(const string &n)
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");
219 obj.uses.push_back(comp);