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 vector<Target *> objs;
52 vector<FS::Path> source_filenames = collect_source_files();
53 objs.reserve(source_filenames.size());
54 for(auto i=source_filenames.begin(); i!=source_filenames.end(); ++i)
56 string ext = FS::extpart(FS::basename(*i));
59 Tool *gen = pkg_tools.get_tool_for_suffix(ext);
62 vector<Target *> templates;
63 templates.push_back(gen->create_source(*this, *i));
65 Tool::ProcessingUnit processing_unit = gen->get_processing_unit();
66 if(processing_unit!=Tool::ONE_FILE)
68 FS::Path source_dir = FS::dirname(*i);
69 for(auto j=next(i); j!=source_filenames.end(); )
71 if((processing_unit!=Tool::DIRECTORY || FS::dirname(*j)==source_dir) &&
72 pkg_tools.get_tool_for_suffix(FS::extpart(FS::basename(*j)))==gen)
74 templates.push_back(gen->create_source(*this, *j));
75 // Remove additional files so they won't get processed again
76 j = source_filenames.erase(j);
83 src = gen->create_target(templates);
84 ext = FS::extpart(FS::basename(dynamic_cast<FileTarget &>(*src).get_path()));
87 Tool *tool = toolchain.get_tool_for_suffix(ext, true);
91 src = tool->create_source(*this, *i);
95 if(tool->accepts_suffix(ext))
97 Target *obj = tool->create_target(*src);
101 if(type==LIBRARY && install)
103 if(dynamic_cast<FileTarget *>(src)->is_installable())
104 build_graph.add_installed_target(*src);
106 for(Target *s: src->get_side_effects())
107 if(dynamic_cast<FileTarget *>(s)->is_installable())
108 build_graph.add_installed_target(*s);
113 Tool &linker = toolchain.get_tool("LINK");
115 vector<Target *> results;
119 Tool &archiver = toolchain.get_tool("AR");
120 results.push_back(linker.create_target(objs, "shared"));
121 results.push_back(archiver.create_target(objs));
123 else if(type==MODULE)
124 results.push_back(linker.create_target(objs, "shared"));
126 results.push_back(linker.create_target(objs));
128 for(Target *r: results)
130 build_graph.add_primary_target(*r);
132 build_graph.add_installed_target(*r);
136 BinaryComponent::Loader::Loader(BinaryComponent &c):
137 DataFile::DerivedObjectLoader<BinaryComponent, Component::Loader>(c)
139 add("use", &Loader::use);
142 void BinaryComponent::Loader::use(const string &n)
144 const BinaryComponent *comp = dynamic_cast<const BinaryComponent *>(&obj.package.get_component(n));
145 if(!comp || comp->type!=LIBRARY)
146 throw logic_error(n+" is not a library");
148 obj.uses.push_back(comp);