]> git.tdb.fi Git - builder.git/blobdiff - source/lib/binarycomponent.cpp
Rearrange sources into subdirectories
[builder.git] / source / lib / binarycomponent.cpp
diff --git a/source/lib/binarycomponent.cpp b/source/lib/binarycomponent.cpp
new file mode 100644 (file)
index 0000000..3209ccc
--- /dev/null
@@ -0,0 +1,149 @@
+#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;
+
+void BinaryComponent::create_build_info()
+{
+       Component::create_build_info();
+
+       for(const Component *u: uses)
+       {
+               /* 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(const FS::Path &s: sources)
+                       base = base.empty() ? s : FS::common_ancestor(base, s);
+               for(const FS::Path &s: u->get_sources())
+                       base = FS::common_ancestor(base, s);
+               build_info.incpath.push_back(base);
+               build_info.libs.push_back(u->get_name());
+               if(!u->get_install())
+               {
+                       build_info.libmodes[u->get_name()] = BuildInfo::STATIC;
+                       build_info.libpath.push_back(u->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();
+
+       vector<Target *> objs;
+       vector<FS::Path> source_filenames = collect_source_files();
+       objs.reserve(source_filenames.size());
+       for(auto 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)
+               {
+                       vector<Target *> templates;
+                       templates.push_back(gen->create_source(*this, *i));
+
+                       Tool::ProcessingUnit processing_unit = gen->get_processing_unit();
+                       if(processing_unit!=Tool::ONE_FILE)
+                       {
+                               FS::Path source_dir = FS::dirname(*i);
+                               for(auto j=next(i); j!=source_filenames.end(); )
+                               {
+                                       if((processing_unit!=Tool::DIRECTORY || FS::dirname(*j)==source_dir) &&
+                                               pkg_tools.get_tool_for_suffix(FS::extpart(FS::basename(*j)))==gen)
+                                       {
+                                               templates.push_back(gen->create_source(*this, *j));
+                                               // Remove additional files so they won't get processed again
+                                               j = source_filenames.erase(j);
+                                       }
+                                       else
+                                               ++j;
+                               }
+                       }
+
+                       src = gen->create_target(templates);
+                       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);
+
+                               for(Target *s: src->get_side_effects())
+                                       if(dynamic_cast<FileTarget *>(s)->is_installable())
+                                               build_graph.add_installed_target(*s);
+                       }
+               }
+       }
+
+       Tool &linker = toolchain.get_tool("LINK");
+
+       vector<Target *> results;
+       results.reserve(2);
+       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(Target *r: results)
+       {
+               build_graph.add_primary_target(*r);
+               if(install)
+                       build_graph.add_installed_target(*r);
+       }
+}
+
+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);
+}