-#include <msp/fs/utils.h>
-#include "binarycomponent.h"
-#include "builder.h"
-#include "filetarget.h"
-#include "sourcepackage.h"
-#include "tool.h"
-
-using namespace std;
-using namespace Msp;
-
-BinaryComponent::BinaryComponent(SourcePackage &p, const string &n, Type t):
- Component(p, n),
- type(t)
-{ }
-
-void BinaryComponent::create_build_info()
-{
- Component::create_build_info();
-
- for(UseList::const_iterator i=uses.begin(); i!=uses.end(); ++i)
- {
- /* Select an include path that contains all the sources for this and the
- used component. This should produce a sensible result in most cases. */
- FS::Path base;
- for(SourceList::const_iterator j=sources.begin(); j!=sources.end(); ++j)
- base = base.empty() ? *j : FS::common_ancestor(base, *j);
- const SourceList &use_sources = (*i)->get_sources();
- for(SourceList::const_iterator j=use_sources.begin(); j!=use_sources.end(); ++j)
- base = FS::common_ancestor(base, *j);
- build_info.incpath.push_back(base);
- build_info.libs.push_back((*i)->get_name());
- if(!(*i)->get_install())
- {
- build_info.libmodes[(*i)->get_name()] = BuildInfo::STATIC;
- build_info.libpath.push_back((*i)->get_package().get_output_directory());
- }
- }
-
- if(type==LIBRARY || type==MODULE)
- if(build_info.libmode<BuildInfo::DYNAMIC)
- build_info.libmode = BuildInfo::DYNAMIC;
-}
-
-void BinaryComponent::update_exported_build_info(BuildInfo &binfo) const
-{
- if(type==LIBRARY)
- binfo.libs.push_back(name);
-}
-
-void BinaryComponent::create_targets() const
-{
- Builder &builder = package.get_builder();
- BuildGraph &build_graph = builder.get_build_graph();
- const Toolchain &toolchain = builder.get_toolchain();
- const Toolchain &pkg_tools = package.get_toolchain();
-
- list<Target *> objs;
- SourceList source_filenames = collect_source_files();
- for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
- {
- string ext = FS::extpart(FS::basename(*i));
- Target *src = 0;
-
- Tool *gen = pkg_tools.get_tool_for_suffix(ext);
- if(gen)
- {
- Target *tmpl = gen->create_source(*this, *i);
- if(tmpl)
- {
- src = gen->create_target(*tmpl);
- ext = FS::extpart(FS::basename(dynamic_cast<FileTarget &>(*src).get_path()));
- }
- }
-
- Tool *tool = toolchain.get_tool_for_suffix(ext, true);
- if(tool)
- {
- if(!src)
- src = tool->create_source(*this, *i);
- if(!src)
- continue;
-
- if(tool->accepts_suffix(ext))
- {
- Target *obj = tool->create_target(*src);
- objs.push_back(obj);
- }
-
- if(type==LIBRARY && install)
- {
- if(dynamic_cast<FileTarget *>(src)->is_installable())
- build_graph.add_installed_target(*src);
-
- const Target::Dependencies &side_effects = src->get_side_effects();
- for(Target::Dependencies::const_iterator j=side_effects.begin(); j!=side_effects.end(); ++j)
- if(dynamic_cast<FileTarget *>(*j)->is_installable())
- build_graph.add_installed_target(**j);
- }
- }
- }
-
- Tool &linker = toolchain.get_tool("LINK");
-
- list<Target *> results;
- if(type==LIBRARY)
- {
- Tool &archiver = toolchain.get_tool("AR");
- results.push_back(linker.create_target(objs, "shared"));
- results.push_back(archiver.create_target(objs));
- }
- else if(type==MODULE)
- results.push_back(linker.create_target(objs, "shared"));
- else
- results.push_back(linker.create_target(objs));
-
- for(list<Target *>::const_iterator i=results.begin(); i!=results.end(); ++i)
- {
- build_graph.add_primary_target(**i);
- if(install)
- build_graph.add_installed_target(**i);
- }
-}
-
-BinaryComponent::Loader::Loader(BinaryComponent &c):
- DataFile::DerivedObjectLoader<BinaryComponent, Component::Loader>(c)
-{
- add("use", &Loader::use);
-}
-
-void BinaryComponent::Loader::use(const string &n)
-{
- const BinaryComponent *comp = dynamic_cast<const BinaryComponent *>(&obj.package.get_component(n));
- if(!comp || comp->type!=LIBRARY)
- throw logic_error(n+" is not a library");
-
- obj.uses.push_back(comp);
-}