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 build_info.libs.push_back(u->get_name());
20 build_info.libmodes[u->get_name()] = BuildInfo::STATIC;
21 build_info.libpath.push_back(u->get_package().get_output_directory());
25 if(type==LIBRARY || type==MODULE)
26 if(build_info.libmode<BuildInfo::DYNAMIC)
27 build_info.libmode = BuildInfo::DYNAMIC;
30 void BinaryComponent::update_exported_build_info(BuildInfo &binfo) const
33 binfo.libs.push_back(name);
36 void BinaryComponent::create_targets() const
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();
43 Tool &linker = toolchain.get_tool("LINK");
45 vector<FileTarget *> pending = create_sources();
47 vector<Target *> objs;
48 while(!pending.empty())
51 for(auto i=pending.begin(); i!=pending.end(); )
53 string ext = FS::extpart(FS::basename((*i)->get_path()));
54 if(linker.accepts_suffix(ext))
56 /* Targets accepted by the linker should not be processed further
57 before feeding them to the linker */
63 // Pick the tool with the smallest processing unit
64 Tool *tgt_tool = pkg_tools.get_tool_for_suffix(ext);
66 tgt_tool = toolchain.get_tool_for_suffix(ext);
67 if(tgt_tool && (!tool || tgt_tool->get_processing_unit()<tool->get_processing_unit()))
70 if(tool->get_processing_unit()==Tool::ONE_FILE)
80 Tool::ProcessingUnit processing_unit = tool->get_processing_unit();
81 if(processing_unit==Tool::ONE_FILE)
83 // Process all pending accepted by the tool
84 for(FileTarget *&t: pending)
86 string ext = FS::extpart(FS::basename(t->get_path()));
87 if(tool->accepts_suffix(ext))
89 t = &dynamic_cast<FileTarget &>(*tool->create_target(*t));
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);
106 vector<Target *> results;
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));
117 else if(type==MODULE)
118 results.push_back(linker.create_target(objs, "shared"));
120 results.push_back(linker.create_target(objs));
122 for(Target *r: results)
124 build_graph.add_primary_target(*r);
126 build_graph.add_installed_target(*r);
130 vector<FileTarget *> BinaryComponent::create_sources() const
132 const Toolchain &toolchain = package.get_builder().get_toolchain();
133 const Toolchain &pkg_tools = package.get_toolchain();
135 vector<FileTarget *> targets;
136 for(const FS::Path &s: collect_source_files())
138 string ext = FS::extpart(FS::basename(s));
141 Tool *tool = pkg_tools.get_tool_for_suffix(ext, true);
143 src = tool->create_source(*this, s);
146 tool = toolchain.get_tool_for_suffix(ext, true);
148 src = tool->create_source(*this, s);
151 if(FileTarget *file = dynamic_cast<FileTarget *>(src))
153 targets.push_back(file);
155 create_install(*file);
162 vector<Target *> BinaryComponent::extract_group(vector<FileTarget *> &targets, const Tool &tool)
164 Tool::ProcessingUnit processing_unit = tool.get_processing_unit();
165 vector<Target *> group;
167 for(auto i=targets.begin(); i!=targets.end(); )
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))
176 i = targets.erase(i);
185 void BinaryComponent::create_install(FileTarget &target) const
187 BuildGraph &build_graph = package.get_builder().get_build_graph();
188 auto add_func = (install ? &BuildGraph::add_installed_target : &BuildGraph::add_staged_target);
190 if(target.is_installable())
191 (build_graph.*add_func)(target);
193 for(Target *s: target.get_side_effects())
194 if(dynamic_cast<FileTarget &>(*s).is_installable())
195 (build_graph.*add_func)(*s);
199 BinaryComponent::Loader::Loader(BinaryComponent &c):
200 DataFile::DerivedObjectLoader<BinaryComponent, Component::Loader>(c)
202 add("use", &Loader::use);
205 void BinaryComponent::Loader::use(const string &n)
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");
211 obj.uses.push_back(comp);