]> git.tdb.fi Git - builder.git/blobdiff - source/sourcepackage.cpp
Split Component into several subclasses
[builder.git] / source / sourcepackage.cpp
index ddfa00fb9bf3175eb59a7391b2e16cd38555370b..1dd7dbd1062263b91f71bc7fd0cd007cd28e8f97 100644 (file)
@@ -1,13 +1,19 @@
+#include <algorithm>
 #include <cstdlib>
+#include <msp/core/maputils.h>
 #include <msp/fs/utils.h>
 #include <msp/io/print.h>
 #include <msp/strings/lexicalcast.h>
 #include <msp/strings/utils.h>
+#include "binarycomponent.h"
 #include "binarypackage.h"
 #include "booleanevaluator.h"
 #include "builder.h"
+#include "datapackcomponent.h"
 #include "file.h"
+#include "installcomponent.h"
 #include "pkgconfigfile.h"
+#include "tarballcomponent.h"
 #include "tool.h"
 #include "sourcegenerator.h"
 #include "sourcepackage.h"
 using namespace std;
 using namespace Msp;
 
-namespace {
-
-bool component_sort(const Component &c1, const Component &c2)
-{ return c1.get_type()<c2.get_type(); }
-
-}
-
-
 SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &f):
        Package(b, n),
        source_dir(FS::dirname(f)),
@@ -35,8 +33,14 @@ SourcePackage::SourcePackage(Builder &b, const string &n, const FS::Path &f):
        build_file = builder.get_vfs().get_target(f);
        if(!build_file)
                build_file = new File(builder, *this, f);
-       components.push_back(Component(*this, Component::TARBALL, "@src"));
-       source_tarball = &components.back();
+       source_tarball = new TarballComponent(*this, "@src");
+       components.push_back(source_tarball);
+}
+
+SourcePackage::~SourcePackage()
+{
+       for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
+               delete *i;
 }
 
 FS::Path SourcePackage::get_temp_directory() const
@@ -64,6 +68,14 @@ FS::Path SourcePackage::get_output_directory() const
                return source_dir/arch.get_name();
 }
 
+const Component &SourcePackage::get_component(const string &n) const
+{
+       for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
+               if((*i)->get_name()==n)
+                       return **i;
+       throw key_error(n);
+}
+
 bool SourcePackage::match_feature(const string &feat, const string *comp) const
 {
        string value = config.get_option("with_"+feat).value;
@@ -101,37 +113,24 @@ void SourcePackage::do_prepare()
                        build_info.defines[ident] = "1";
        }
 
-       bool export_paths = false;
-       for(list<Component>::iterator i=components.begin(); i!=components.end(); ++i)
+       for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
        {
-               i->prepare();
-               i->create_build_info();
-
-               if(i->get_type()==Component::LIBRARY)
-               {
-                       export_binfo.libs.push_back(i->get_name());
-                       export_paths = true;
-               }
-       }
+               (*i)->prepare();
+               (*i)->create_build_info();
 
-       if(export_paths)
-       {
-               export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
-               export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
+               (*i)->update_exported_build_info(export_binfo);
        }
 
        cache.load();
 
-       bool pc_needed = false;
-       for(ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
-       {
-               i->create_targets();
-               if(i->get_type()==Component::LIBRARY)
-                       pc_needed = true;
-       }
+       for(ComponentList::iterator i=components.begin(); i!=components.end(); ++i)
+               (*i)->create_targets();
 
-       if(pc_needed)
+       if(!export_binfo.libs.empty())
        {
+               export_binfo.incpath.push_back((builder.get_prefix()/"include").str());
+               export_binfo.libpath.push_back((builder.get_prefix()/"lib").str());
+
                PkgConfigFile *pc = new PkgConfigFile(builder, *this);
                builder.get_build_graph().get_target("install")->add_dependency(*builder.get_toolchain().get_tool("CP").create_target(*pc));
        }
@@ -164,12 +163,12 @@ void SourcePackage::Loader::init(const Config::InputOptions *o)
        add("feature",     &Loader::feature);
        add("generate",    &Loader::generate);
        add("if_feature",  &Loader::if_feature);
-       add("program",     &Loader::component<Component::PROGRAM>);
-       add("library",     &Loader::component<Component::LIBRARY>);
-       add("module",      &Loader::component<Component::MODULE>);
-       add("install",     &Loader::component<Component::INSTALL>);
+       add("program",     &Loader::component_arg<BinaryComponent, BinaryComponent::Type, BinaryComponent::PROGRAM>);
+       add("library",     &Loader::component_arg<BinaryComponent, BinaryComponent::Type, BinaryComponent::LIBRARY>);
+       add("module",      &Loader::component_arg<BinaryComponent, BinaryComponent::Type, BinaryComponent::MODULE>);
+       add("install",     &Loader::component<InstallComponent>);
        add("interface_version", &Loader::interface_version);
-       add("datapack",    &Loader::component<Component::DATAPACK>);
+       add("datapack",    &Loader::component<DataPackComponent>);
        add("source_tarball", &Loader::source_tarball);
        add("tarball",     &Loader::tarball);
        add("version",     &Loader::version);
@@ -177,7 +176,11 @@ void SourcePackage::Loader::init(const Config::InputOptions *o)
 
 void SourcePackage::Loader::finish()
 {
-       obj.components.sort(component_sort);
+       /* Make sure the source tarball is last in the list so targets from all
+       other components wil be created first */
+       ComponentList::iterator i = find(obj.components.begin(), obj.components.end(), obj.source_tarball);
+       if(i!=obj.components.end())
+               obj.components.splice(obj.components.end(), obj.components, i);
 }
 
 void SourcePackage::Loader::feature(const string &n, const string &d)
@@ -196,11 +199,19 @@ void SourcePackage::Loader::feature(const string &n, const string &d)
        }
 }
 
-template<Component::Type t>
+template<typename C>
 void SourcePackage::Loader::component(const string &n)
 {
-       Component comp(obj, t, n);
-       load_sub(comp);
+       C *comp = new C(obj, n);
+       load_sub(*comp);
+       obj.components.push_back(comp);
+}
+
+template<typename C, typename A, A a>
+void SourcePackage::Loader::component_arg(const string &n)
+{
+       C *comp = new C(obj, n, a);
+       load_sub(*comp);
        obj.components.push_back(comp);
 }
 
@@ -239,7 +250,7 @@ void SourcePackage::Loader::source_tarball()
 
 void SourcePackage::Loader::tarball(const string &n)
 {
-       Component trbl(obj, Component::TARBALL, n);
+       TarballComponent trbl(obj, n);
        load_sub(trbl);
 }