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();
57 list<FS::Path> source_filenames = collect_source_files();
58 for(auto i=source_filenames.begin(); i!=source_filenames.end(); ++i)
60 string ext = FS::extpart(FS::basename(*i));
63 Tool *gen = pkg_tools.get_tool_for_suffix(ext);
66 list<Target *> templates;
67 templates.push_back(gen->create_source(*this, *i));
69 Tool::ProcessingUnit processing_unit = gen->get_processing_unit();
70 if(processing_unit!=Tool::ONE_FILE)
72 FS::Path source_dir = FS::dirname(*i);
74 for(++j; 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 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 list<Target *> results;
123 Tool &archiver = toolchain.get_tool("AR");
124 results.push_back(linker.create_target(objs, "shared"));
125 results.push_back(archiver.create_target(objs));
127 else if(type==MODULE)
128 results.push_back(linker.create_target(objs, "shared"));
130 results.push_back(linker.create_target(objs));
132 for(Target *r: results)
134 build_graph.add_primary_target(*r);
136 build_graph.add_installed_target(*r);
140 BinaryComponent::Loader::Loader(BinaryComponent &c):
141 DataFile::DerivedObjectLoader<BinaryComponent, Component::Loader>(c)
143 add("use", &Loader::use);
146 void BinaryComponent::Loader::use(const string &n)
148 const BinaryComponent *comp = dynamic_cast<const BinaryComponent *>(&obj.package.get_component(n));
149 if(!comp || comp->type!=LIBRARY)
150 throw logic_error(n+" is not a library");
152 obj.uses.push_back(comp);