]> git.tdb.fi Git - builder.git/blobdiff - source/gnulinker.cpp
Rearrange Target members
[builder.git] / source / gnulinker.cpp
index ad9de775f3acecf3534cf61b37b6b630fc2574ab..13352870299778b8ba205092e3e742ed008be813 100644 (file)
@@ -7,7 +7,7 @@
 #include "component.h"
 #include "executable.h"
 #include "externaltask.h"
-#include "gnucxxcompiler.h"
+#include "gnucompiler.h"
 #include "gnulinker.h"
 #include "objectfile.h"
 #include "sharedlibrary.h"
 using namespace std;
 using namespace Msp;
 
-GnuLinker::GnuLinker(Builder &b):
-       Tool(b, "LINK")
+GnuLinker::GnuLinker(Builder &b, const Architecture &a):
+       Tool(b, a, "LINK")
 {
        input_suffixes.push_back(".o");
        input_suffixes.push_back(".a");
+
+       if(architecture->is_native())
+       {
+               system_path.push_back("/lib");
+               system_path.push_back("/usr/lib");
+               if(architecture->match_name("pc-32-linux"))
+                       system_path.push_back("/usr/lib/i386-linux-gnu");
+               else if(architecture->match_name("pc-64-linux"))
+                       system_path.push_back("/usr/lib/x86_64-linux-gnu");
+       }
+       else
+               system_path.push_back("/usr/"+architecture->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
+Target *GnuLinker::create_target(const list<Target *> &sources, const string &arg) const
 {
        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");
        }
 
-       const Component &comp = objs.front()->get_component();
+       const Component &comp = *objs.front()->get_component();
        Binary *bin = 0;
        if(arg=="shared")
                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(dynamic_cast<const GnuCxxCompiler *>(tool))
-                               command = "g++";
-               }
+GnuLinker::Linker::Linker(GnuLinker &p, const string &compiler_tag):
+       SubTool(p)
+{
+       const Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
+       if(dynamic_cast<const GnuCompiler *>(&compiler))
+               executable = compiler.get_executable();
+       else
+       {
+               string command;
+               if(compiler_tag=="CC")
+                       command = "gcc";
+               else if(compiler_tag=="CXX")
+                       command = "g++";
+               else
+                       throw invalid_argument("GnuLinker::Linker::Linker");
+               if(architecture->is_cross())
+                       command = format("%s-%s", architecture->get_cross_prefix(), command);
+               executable = builder.get_vfs().find_binary(command);
+       }
+}
+
+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);
+       argv.push_back(executable->get_path().str());
 
        const Component &comp = *bin.get_component();
 
@@ -78,22 +125,23 @@ Task *GnuLinker::run(const Target &target) const
                argv.push_back("-static");
 
        const BuildInfo &binfo = comp.get_build_info();
-       for(list<string>::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
-               argv.push_back("-L"+*i);
+       for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
+               argv.push_back("-L"+i->str());
        if(binfo.strip)
                argv.push_back("-s");
        if(binfo.threads)
                argv.push_back("-pthread");
 
-       const Architecture &arch = builder.get_current_arch();
        const Architecture &native_arch = builder.get_native_arch();
-       if(arch.get_bits()!=native_arch.get_bits())
-               argv.push_back(format("-m%d", arch.get_bits()));
+       if(architecture->get_bits()!=native_arch.get_bits())
+               argv.push_back(format("-m%d", architecture->get_bits()));
 
        FS::Path work_dir = comp.get_package().get_source();
 
        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();