]> git.tdb.fi Git - builder.git/blobdiff - source/gnulinker.cpp
Redesign how tools are run
[builder.git] / source / gnulinker.cpp
index 2d37a4d93533b4486d865a043f691f3725ff4aa5..31440976e917d31a43c8d9d701ff057a0573a740 100644 (file)
@@ -39,22 +39,19 @@ GnuLinker::~GnuLinker()
        delete cxx_linker;
 }
 
-Target *GnuLinker::create_target(const list<Target *> &sources, const string &arg)
+Target *GnuLinker::create_target(const vector<Target *> &sources, const string &arg)
 {
        if(sources.empty())
                throw invalid_argument("GnuLinker::create_target");
-       list<ObjectFile *> objs;
+       vector<ObjectFile *> objs;
+       objs.reserve(sources.size());
        Linker *linker = default_linker;
-       for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
+       for(Target *s: sources)
        {
-               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");
+               ObjectFile &obj = dynamic_cast<ObjectFile &>(*s);
+               objs.push_back(&obj);
+               if(obj.get_tool()->get_tag()=="CXX")
+                       linker = cxx_linker;
        }
 
        const Component &comp = *objs.front()->get_component();
@@ -114,7 +111,7 @@ void GnuLinker::do_prepare()
                        if(!sysroot.empty())
                                argv.push_back("--sysroot="+sysroot.str());
 
-                       builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
+                       builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
                        try
                        {
                                string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
@@ -141,7 +138,7 @@ void GnuLinker::do_prepare()
                                        }
 
                                        path /= output.substr(search_dir, end-search_dir);
-                                       builder.get_logger().log("tools", format("Got %s system path: %s", tag, path));
+                                       builder.get_logger().log("tools", "Got %s system path: %s", tag, path);
                                        system_path.push_back(path);
                                        path_found = true;
 
@@ -155,7 +152,7 @@ void GnuLinker::do_prepare()
 
        if(!path_found)
        {
-               builder.get_logger().log("tools", format("No %s system path found, using defaults", tag));
+               builder.get_logger().log("tools", "No %s system path found, using defaults", tag);
                if(!sysroot.empty())
                        system_path.push_back(sysroot/"usr/lib");
                else if(architecture->is_native())
@@ -174,15 +171,10 @@ void GnuLinker::do_prepare()
                        }
                }
                else
-                       system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/lib");
+                       system_path.push_back(format("/usr/%s/lib", architecture->get_cross_prefix()));
        }
 }
 
-Task *GnuLinker::run(const Target &) const
-{
-       throw logic_error("GnuLinker should not be run directly");
-}
-
 
 GnuLinker::Linker::Linker(GnuLinker &p, const string &ct):
        SubTool(p),
@@ -194,11 +186,13 @@ GnuLinker::Linker::Linker(GnuLinker &p, const string &ct):
                set_command("g++", true);
        else
                throw invalid_argument("GnuLinker::Linker::Linker");
+
+       set_run(_run);
 }
 
 string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
 {
-       string result = FS::basename(executable->get_path());
+       string result = Tool::create_build_signature(binfo);
        result += ',';
        if(binfo.libmode<=BuildInfo::STATIC)
                result += 't';
@@ -228,12 +222,14 @@ void GnuLinker::Linker::do_prepare()
        }
 }
 
-Task *GnuLinker::Linker::run(const Target &target) const
+Task *GnuLinker::Linker::_run(const Binary &bin)
 {
-       const Binary &bin = dynamic_cast<const Binary &>(target);
+       const Linker &tool = dynamic_cast<const Linker &>(*bin.get_tool());
+       const Builder &builder = tool.get_builder();
+       const Architecture &arch = *tool.get_architecture();
 
-       vector<string> argv;
-       argv.push_back(executable->get_path().str());
+       ExternalTask::Arguments argv;
+       argv.push_back(tool.get_executable()->get_path().str());
 
        FS::Path work_dir = bin.get_component()->get_package().get_source_directory();
 
@@ -241,9 +237,9 @@ Task *GnuLinker::Linker::run(const Target &target) const
        {
                argv.push_back("-shared");
                argv.push_back("-fPIC");
-               if(architecture->get_system()!="windows" && !shlib->get_soname().empty())
+               if(arch.get_system()!="windows" && !shlib->get_soname().empty())
                {
-                       if(architecture->get_system()=="darwin")
+                       if(arch.get_system()=="darwin")
                        {
                                argv.push_back("-install_name");
                                argv.push_back(shlib->get_soname());
@@ -264,7 +260,7 @@ Task *GnuLinker::Linker::run(const Target &target) const
        }
 
        BuildInfo binfo;
-       target.collect_build_info(binfo);
+       bin.collect_build_info(binfo);
 
        const FS::Path &sysroot = binfo.sysroot;
        if(!sysroot.empty())
@@ -280,30 +276,29 @@ Task *GnuLinker::Linker::run(const Target &target) const
                argv.push_back("-Wl,-rpath-link,"+lib_dir.str());
        }
 
-       for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
-               argv.push_back("-L"+i->str());
+       for(const FS::Path &p: binfo.libpath)
+               argv.push_back("-L"+p.str());
        if(binfo.strip)
                argv.push_back("-s");
-       if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
+       if(binfo.threads && arch.get_system()!="windows" && arch.get_system()!="darwin")
                argv.push_back("-pthread");
 
        const Architecture &native_arch = builder.get_native_arch();
-       if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
-               argv.push_back(format("-m%d", architecture->get_bits()));
+       if(arch.is_native() && arch.get_bits()!=native_arch.get_bits())
+               argv.push_back(format("-m%d", arch.get_bits()));
 
        argv.push_back("-o");
        argv.push_back(relative(bin.get_path(), work_dir).str());
 
-       for(BuildInfo::WordList::const_iterator i=binfo.keep_symbols.begin(); i!=binfo.keep_symbols.end(); ++i)
-               argv.push_back("-u"+*i);
+       for(const string &s: binfo.keep_symbols)
+               argv.push_back("-u"+s);
 
        bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
 
-       const Target::Dependencies &depends = target.get_dependencies();
-       for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
+       for(Target *d: bin.get_dependencies())
        {
-               FileTarget *file = dynamic_cast<FileTarget *>(*i);
-               Target *tgt = (*i)->get_real_target();
+               FileTarget *file = dynamic_cast<FileTarget *>(d);
+               Target *tgt = d->get_real_target();
 
                if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
                        argv.push_back(relative(obj->get_path(), work_dir).str());
@@ -325,25 +320,25 @@ Task *GnuLinker::Linker::run(const Target &target) const
                }
        }
 
-       for(BuildInfo::WordList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i)
-               if(i->size()>10 && !i->compare(i->size()-10, 10, ".framework"))
+       for(const string &l: binfo.libs)
+               if(l.size()>10 && !l.compare(l.size()-10, 10, ".framework"))
                {
                        argv.push_back("-framework");
-                       argv.push_back(i->substr(0, i->size()-10));
+                       argv.push_back(l.substr(0, l.size()-10));
                }
 
        if(static_link_ok)
                argv.push_back("-static");
        else
        {
-               if(compiler_tag=="CXX")
+               if(tool.compiler_tag=="CXX")
                {
-                       BuildInfo::LibModeMap::const_iterator i = binfo.libmodes.find("stdc++");
+                       auto i = binfo.libmodes.find("stdc++");
                        if(i!=binfo.libmodes.end() && i->second<=BuildInfo::STATIC)
                                argv.push_back("-static-libstdc++");
                }
 
-               if(architecture->get_system()=="windows")
+               if(arch.get_system()=="windows")
                        argv.push_back("-Wl,--enable-auto-import");
        }