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_output_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::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 list<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 SourceList::iterator j = i;
75 for(++j; j!=source_filenames.end(); )
77 if((processing_unit!=Tool::DIRECTORY || FS::dirname(*j)==source_dir) &&
78 pkg_tools.get_tool_for_suffix(FS::extpart(FS::basename(*j)))==gen)
80 templates.push_back(gen->create_source(*this, *j));
81 // Remove additional files so they won't get processed again
82 source_filenames.erase(j++);
89 src = gen->create_target(templates);
90 ext = FS::extpart(FS::basename(dynamic_cast<FileTarget &>(*src).get_path()));
93 Tool *tool = toolchain.get_tool_for_suffix(ext, true);
97 src = tool->create_source(*this, *i);
101 if(tool->accepts_suffix(ext))
103 Target *obj = tool->create_target(*src);
107 if(type==LIBRARY && install)
109 if(dynamic_cast<FileTarget *>(src)->is_installable())
110 build_graph.add_installed_target(*src);
112 const Target::Dependencies &side_effects = src->get_side_effects();
113 for(Target::Dependencies::const_iterator j=side_effects.begin(); j!=side_effects.end(); ++j)
114 if(dynamic_cast<FileTarget *>(*j)->is_installable())
115 build_graph.add_installed_target(**j);
120 Tool &linker = toolchain.get_tool("LINK");
122 list<Target *> results;
125 Tool &archiver = toolchain.get_tool("AR");
126 results.push_back(linker.create_target(objs, "shared"));
127 results.push_back(archiver.create_target(objs));
129 else if(type==MODULE)
130 results.push_back(linker.create_target(objs, "shared"));
132 results.push_back(linker.create_target(objs));
134 for(list<Target *>::const_iterator i=results.begin(); i!=results.end(); ++i)
136 build_graph.add_primary_target(**i);
138 build_graph.add_installed_target(**i);
142 BinaryComponent::Loader::Loader(BinaryComponent &c):
143 DataFile::DerivedObjectLoader<BinaryComponent, Component::Loader>(c)
145 add("use", &Loader::use);
148 void BinaryComponent::Loader::use(const string &n)
150 const BinaryComponent *comp = dynamic_cast<const BinaryComponent *>(&obj.package.get_component(n));
151 if(!comp || comp->type!=LIBRARY)
152 throw logic_error(n+" is not a library");
154 obj.uses.push_back(comp);