]> git.tdb.fi Git - builder.git/commitdiff
Split GnuLinker into sub-tools and select the appropriate one in create_target
authorMikko Rasa <tdb@tdb.fi>
Sun, 8 Jul 2012 20:15:50 +0000 (23:15 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 8 Jul 2012 21:08:55 +0000 (00:08 +0300)
source/gnulinker.cpp
source/gnulinker.h

index 52ae0f3ea45a90a97108435f27edc573e31e5c86..6eef8b3268da567f17385307e5c3022825fcd68a 100644 (file)
@@ -35,6 +35,15 @@ GnuLinker::GnuLinker(Builder &b):
        }
        else
                system_path.push_back("/usr/"+arch.get_cross_prefix()+"/lib");
+
+       default_linker = new Linker(*this, "CC");
+       cxx_linker = new Linker(*this, "CXX");
+}
+
+GnuLinker::~GnuLinker()
+{
+       delete default_linker;
+       delete cxx_linker;
 }
 
 Target *GnuLinker::create_target(const list<Target *> &sources, const std::string &arg) const
@@ -42,10 +51,15 @@ Target *GnuLinker::create_target(const list<Target *> &sources, const std::strin
        if(sources.empty())
                throw invalid_argument("GnuLinker::create_target");
        list<ObjectFile *> objs;
+       Linker *linker = default_linker;
        for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
        {
                if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
+               {
                        objs.push_back(obj);
+                       if(obj->get_tool()->get_tag()=="CXX")
+                               linker = cxx_linker;
+               }
                else
                        throw invalid_argument("GnuLinker::create_target");
        }
@@ -56,24 +70,35 @@ Target *GnuLinker::create_target(const list<Target *> &sources, const std::strin
                bin = new SharedLibrary(builder, comp, objs);
        else
                bin = new Executable(builder, comp, objs);
-       bin->set_tool(*this);
+       bin->set_tool(*linker);
        return bin;
 }
 
-Task *GnuLinker::run(const Target &target) const
+Task *GnuLinker::run(const Target &) const
 {
-       const Binary &bin = dynamic_cast<const Binary &>(target);
+       throw logic_error("GnuLinker should not be run directly");
+}
 
-       string command = "gcc";
 
-       const Target::Dependencies &depends = target.get_depends();
-       for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
-               if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
-               {
-                       const Tool *tool = obj->get_tool();
-                       if(tool->get_tag()=="CXX")
-                               command = "g++";
-               }
+GnuLinker::Linker::Linker(GnuLinker &p, const string &compiler_tag):
+       SubTool(p)
+{
+       if(compiler_tag=="CC")
+               command = "gcc";
+       else if(compiler_tag=="CXX")
+               command = "g++";
+       else
+               throw invalid_argument("GnuLinker::Linker::Linker");
+}
+
+Target *GnuLinker::Linker::create_target(const list<Target *> &sources, const string &arg) const
+{
+       return parent.create_target(sources, arg);
+}
+
+Task *GnuLinker::Linker::run(const Target &target) const
+{
+       const Binary &bin = dynamic_cast<const Binary &>(target);
 
        vector<string> argv;
        argv.push_back(command);
@@ -107,6 +132,8 @@ Task *GnuLinker::run(const Target &target) const
 
        argv.push_back("-o");
        argv.push_back(relative(bin.get_path(), work_dir).str());
+
+       const Target::Dependencies &depends = target.get_depends();
        for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
        {
                Target *tgt = (*i)->get_real_target();
index 626894433505a10f2f959ad4b2544694a63d0ed6..9f0475adf7202de215696716a10e35ccd14424ba 100644 (file)
@@ -5,8 +5,25 @@
 
 class GnuLinker: public Tool
 {
+private:
+       class Linker: public SubTool
+       {
+       private:
+               std::string command;
+
+       public:
+               Linker(GnuLinker &, const std::string &);
+
+               virtual Target *create_target(const std::list<Target *> &, const std::string &) const;
+               virtual Task *run(const Target &) const;
+       };
+
+       Linker *default_linker;
+       Linker *cxx_linker;
+
 public:
        GnuLinker(Builder &);
+       ~GnuLinker();
 
        virtual Target *create_target(const std::list<Target *> &, const std::string &) const;
        virtual Task *run(const Target &) const;