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(UseList::const_iterator i=uses.begin(); i!=uses.end(); ++i)
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(SourceList::const_iterator j=sources.begin(); j!=sources.end(); ++j)
26 base = base.empty() ? *j : FS::common_ancestor(base, *j);
27 const SourceList &use_sources = (*i)->get_sources();
28 for(SourceList::const_iterator j=use_sources.begin(); j!=use_sources.end(); ++j)
29 base = FS::common_ancestor(base, *j);
30 build_info.incpath.push_back(base);
31 build_info.libs.push_back((*i)->get_name());
32 if(!(*i)->get_install())
34 build_info.libmodes[(*i)->get_name()] = BuildInfo::STATIC;
35 build_info.libpath.push_back((*i)->get_package().get_source_directory());
39 if(type==LIBRARY || type==MODULE)
40 if(build_info.libmode<BuildInfo::DYNAMIC)
41 build_info.libmode = BuildInfo::DYNAMIC;
44 void BinaryComponent::update_exported_build_info(BuildInfo &binfo) const
47 binfo.libs.push_back(name);
50 void BinaryComponent::create_targets() const
52 Builder &builder = package.get_builder();
53 BuildGraph &build_graph = builder.get_build_graph();
54 const Toolchain &toolchain = builder.get_toolchain();
55 const Toolchain &pkg_tools = package.get_toolchain();
58 SourceList source_filenames = collect_source_files();
59 for(SourceList::const_iterator 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 Target *tmpl = gen->create_source(*this, *i);
70 src = gen->create_target(*tmpl);
71 ext = FS::extpart(FS::basename(dynamic_cast<FileTarget &>(*src).get_path()));
75 Tool *tool = toolchain.get_tool_for_suffix(ext, true);
79 src = tool->create_source(*this, *i);
83 if(tool->accepts_suffix(ext))
85 Target *obj = tool->create_target(*src);
89 if(type==LIBRARY && install)
91 if(dynamic_cast<FileTarget *>(src)->is_installable())
92 build_graph.add_installed_target(*src);
94 const Target::Dependencies &side_effects = src->get_side_effects();
95 for(Target::Dependencies::const_iterator j=side_effects.begin(); j!=side_effects.end(); ++j)
96 if(dynamic_cast<FileTarget *>(*j)->is_installable())
97 build_graph.add_installed_target(**j);
102 Tool &linker = toolchain.get_tool("LINK");
104 list<Target *> results;
107 Tool &archiver = toolchain.get_tool("AR");
108 results.push_back(linker.create_target(objs, "shared"));
109 results.push_back(archiver.create_target(objs));
111 else if(type==MODULE)
112 results.push_back(linker.create_target(objs, "shared"));
114 results.push_back(linker.create_target(objs));
116 for(list<Target *>::const_iterator i=results.begin(); i!=results.end(); ++i)
118 build_graph.add_primary_target(**i);
120 build_graph.add_installed_target(**i);
124 BinaryComponent::Loader::Loader(BinaryComponent &c):
125 DerivedObjectLoader<BinaryComponent, Component::Loader>(c)
127 add("use", &Loader::use);
130 void BinaryComponent::Loader::use(const string &n)
132 const BinaryComponent *comp = dynamic_cast<const BinaryComponent *>(&obj.package.get_component(n));
133 if(!comp || comp->type!=LIBRARY)
134 throw logic_error(n+" is not a library");
136 obj.uses.push_back(comp);