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));
98 if(type==LIBRARY && install)
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)
114 vector<Target *> results;
118 Tool &archiver = toolchain.get_tool("AR");
119 results.push_back(linker.create_target(objs, "shared"));
120 results.push_back(archiver.create_target(objs));
122 else if(type==MODULE)
123 results.push_back(linker.create_target(objs, "shared"));
125 results.push_back(linker.create_target(objs));
127 for(Target *r: results)
129 build_graph.add_primary_target(*r);
131 build_graph.add_installed_target(*r);
135 vector<FileTarget *> BinaryComponent::create_sources() const
137 const Toolchain &toolchain = package.get_builder().get_toolchain();
138 const Toolchain &pkg_tools = package.get_toolchain();
140 vector<FileTarget *> targets;
141 for(const FS::Path &s: collect_source_files())
143 string ext = FS::extpart(FS::basename(s));
146 Tool *tool = pkg_tools.get_tool_for_suffix(ext, true);
148 src = tool->create_source(*this, s);
151 tool = toolchain.get_tool_for_suffix(ext, true);
153 src = tool->create_source(*this, s);
156 if(FileTarget *file = dynamic_cast<FileTarget *>(src))
158 targets.push_back(file);
159 if(type==LIBRARY && install)
160 create_install(*file);
167 vector<Target *> BinaryComponent::extract_group(vector<FileTarget *> &targets, const Tool &tool)
169 Tool::ProcessingUnit processing_unit = tool.get_processing_unit();
170 vector<Target *> group;
172 for(auto i=targets.begin(); i!=targets.end(); )
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))
181 i = targets.erase(i);
190 void BinaryComponent::create_install(FileTarget &target) const
192 BuildGraph &build_graph = package.get_builder().get_build_graph();
194 if(target.is_installable())
195 build_graph.add_installed_target(target);
197 for(Target *s: target.get_side_effects())
198 if(dynamic_cast<FileTarget &>(*s).is_installable())
199 build_graph.add_installed_target(*s);
203 BinaryComponent::Loader::Loader(BinaryComponent &c):
204 DataFile::DerivedObjectLoader<BinaryComponent, Component::Loader>(c)
206 add("use", &Loader::use);
209 void BinaryComponent::Loader::use(const string &n)
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");
215 obj.uses.push_back(comp);