1 #include <msp/fs/utils.h>
2 #include "binarycomponent.h"
4 #include "filetarget.h"
5 #include "sourcepackage.h"
11 BinaryComponent::BinaryComponent(SourcePackage &p, const string &n, Type t):
16 void BinaryComponent::create_build_info()
18 Component::create_build_info();
20 for(const Component *u: uses)
22 /* Select an include path that contains all the sources for this and the
23 used component. This should produce a sensible result in most cases. */
25 for(const FS::Path &s: sources)
26 base = base.empty() ? s : FS::common_ancestor(base, s);
27 for(const FS::Path &s: u->get_sources())
28 base = FS::common_ancestor(base, s);
29 build_info.incpath.push_back(base);
30 build_info.libs.push_back(u->get_name());
33 build_info.libmodes[u->get_name()] = BuildInfo::STATIC;
34 build_info.libpath.push_back(u->get_package().get_output_directory());
38 if(type==LIBRARY || type==MODULE)
39 if(build_info.libmode<BuildInfo::DYNAMIC)
40 build_info.libmode = BuildInfo::DYNAMIC;
43 void BinaryComponent::update_exported_build_info(BuildInfo &binfo) const
46 binfo.libs.push_back(name);
49 void BinaryComponent::create_targets() const
51 Builder &builder = package.get_builder();
52 BuildGraph &build_graph = builder.get_build_graph();
53 const Toolchain &toolchain = builder.get_toolchain();
54 const Toolchain &pkg_tools = package.get_toolchain();
56 vector<Target *> objs;
57 vector<FS::Path> source_filenames = collect_source_files();
58 objs.reserve(source_filenames.size());
59 for(auto i=source_filenames.begin(); i!=source_filenames.end(); ++i)
61 string ext = FS::extpart(FS::basename(*i));
64 Tool *gen = pkg_tools.get_tool_for_suffix(ext);
67 vector<Target *> templates;
68 templates.push_back(gen->create_source(*this, *i));
70 Tool::ProcessingUnit processing_unit = gen->get_processing_unit();
71 if(processing_unit!=Tool::ONE_FILE)
73 FS::Path source_dir = FS::dirname(*i);
74 for(auto j=next(i); j!=source_filenames.end(); )
76 if((processing_unit!=Tool::DIRECTORY || FS::dirname(*j)==source_dir) &&
77 pkg_tools.get_tool_for_suffix(FS::extpart(FS::basename(*j)))==gen)
79 templates.push_back(gen->create_source(*this, *j));
80 // Remove additional files so they won't get processed again
81 j = source_filenames.erase(j);
88 src = gen->create_target(templates);
89 ext = FS::extpart(FS::basename(dynamic_cast<FileTarget &>(*src).get_path()));
92 Tool *tool = toolchain.get_tool_for_suffix(ext, true);
96 src = tool->create_source(*this, *i);
100 if(tool->accepts_suffix(ext))
102 Target *obj = tool->create_target(*src);
106 if(type==LIBRARY && install)
108 if(dynamic_cast<FileTarget *>(src)->is_installable())
109 build_graph.add_installed_target(*src);
111 for(Target *s: src->get_side_effects())
112 if(dynamic_cast<FileTarget *>(s)->is_installable())
113 build_graph.add_installed_target(*s);
118 Tool &linker = toolchain.get_tool("LINK");
120 vector<Target *> results;
124 Tool &archiver = toolchain.get_tool("AR");
125 results.push_back(linker.create_target(objs, "shared"));
126 results.push_back(archiver.create_target(objs));
128 else if(type==MODULE)
129 results.push_back(linker.create_target(objs, "shared"));
131 results.push_back(linker.create_target(objs));
133 for(Target *r: results)
135 build_graph.add_primary_target(*r);
137 build_graph.add_installed_target(*r);
141 BinaryComponent::Loader::Loader(BinaryComponent &c):
142 DataFile::DerivedObjectLoader<BinaryComponent, Component::Loader>(c)
144 add("use", &Loader::use);
147 void BinaryComponent::Loader::use(const string &n)
149 const BinaryComponent *comp = dynamic_cast<const BinaryComponent *>(&obj.package.get_component(n));
150 if(!comp || comp->type!=LIBRARY)
151 throw logic_error(n+" is not a library");
153 obj.uses.push_back(comp);