]> git.tdb.fi Git - builder.git/blobdiff - source/gnulinker.cpp
Gather more system path information for GnuLinker
[builder.git] / source / gnulinker.cpp
index ddad8d1904abb06fb7252128b7bd586593599a81..591c80be20db9aefc4d7b7d9713c455313e14665 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdexcept>
 #include <vector>
+#include <msp/core/algorithm.h>
 #include <msp/fs/dir.h>
 #include <msp/fs/utils.h>
 #include <msp/strings/format.h>
@@ -22,36 +23,25 @@ using namespace std;
 using namespace Msp;
 
 GnuLinker::GnuLinker(Builder &b, const Architecture &a):
-       Tool(b, a, "LINK")
+       Tool(b, &a, "LINK")
 {
        input_suffixes.push_back(".o");
        input_suffixes.push_back(".a");
 
        processing_unit = COMPONENT;
 
-       default_linker = new Linker(*this, "CC");
-       cxx_linker = new Linker(*this, "CXX");
+       set_command("gcc", true);
+       set_run(_run);
 }
 
-GnuLinker::~GnuLinker()
-{
-       delete default_linker;
-       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;
-       Linker *linker = default_linker;
+       vector<ObjectFile *> objs;
+       objs.reserve(sources.size());
        for(Target *s: sources)
-       {
-               ObjectFile &obj = dynamic_cast<ObjectFile &>(*s);
-               objs.push_back(&obj);
-               if(obj.get_tool()->get_tag()=="CXX")
-                       linker = cxx_linker;
-       }
+               objs.push_back(&dynamic_cast<ObjectFile &>(*s));
 
        const Component &comp = *objs.front()->get_component();
        Binary *bin = 0;
@@ -67,7 +57,7 @@ Target *GnuLinker::create_target(const list<Target *> &sources, const string &ar
        }
        else
                bin = new Executable(builder, comp, objs);
-       bin->set_tool(*linker);
+       bin->set_tool(*this);
        return bin;
 }
 
@@ -91,145 +81,132 @@ Target *GnuLinker::create_install(Target &target) const
                return 0;
 }
 
-void GnuLinker::do_prepare()
+string GnuLinker::create_build_signature(const BuildInfo &binfo) const
+{
+       string result = Tool::create_build_signature(binfo);
+       result += ',';
+       if(binfo.libmode<=BuildInfo::STATIC)
+               result += 't';
+       else
+               result += 'd';
+       if(binfo.strip)
+               result += 's';
+       if(!binfo.libs.empty())
+       {
+               result += ",l";
+               result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
+       }
+       return result;
+}
+
+void GnuLinker::do_prepare(ToolData &tool) const
 {
        bool path_found = false;
-       const FS::Path &sysroot = build_info.sysroot;
+       const FS::Path &sysroot = tool.build_info.sysroot;
+       const std::string &tool_tag = static_cast<Tool &>(tool).get_tag();
 
-       Tool &compiler = builder.get_toolchain().get_tool("CC");
-       if(dynamic_cast<GnuCompiler *>(&compiler))
+       const FileTarget *exe = static_cast<Tool &>(tool).get_executable();
+       if(exe)
        {
-               compiler.prepare();
-               FileTarget *compiler_exe = compiler.get_executable();
-               if(compiler_exe)
+               ExternalTask::Arguments argv;
+               argv.push_back(exe->get_path().str());
+               argv.push_back("-v");
+               argv.push_back("-Wl,--verbose");
+               argv.push_back("-nostdlib");
+               if(!sysroot.empty())
+                       argv.push_back("--sysroot="+sysroot.str());
+
+               builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
+               try
                {
-                       ExternalTask::Arguments argv;
-                       argv.push_back(compiler_exe->get_path().str());
-                       argv.push_back("-Wl,--verbose");
-                       argv.push_back("-nostdlib");
-                       if(!sysroot.empty())
-                               argv.push_back("--sysroot="+sysroot.str());
-
-                       builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
-                       try
+                       string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
+
+                       string::size_type lib_path = output.find("LIBRARY_PATH=");
+                       if(lib_path!=string::npos)
                        {
-                               string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
-                               string::size_type start = 0;
-                               while(start<output.size())
+                               string::size_type newline = output.find('\n', lib_path);
+                               for(const string &p: split(output.substr(lib_path+13, newline-lib_path-13), ':'))
                                {
-                                       string::size_type search_dir = output.find("SEARCH_DIR(\"", start);
-                                       if(search_dir==string::npos)
-                                               break;
-
-                                       search_dir += 12;
-                                       string::size_type end = output.find("\");", search_dir);
-                                       if(end==string::npos)
-                                               break;
-
-                                       FS::Path path;
-                                       if(!output.compare(search_dir, 2, "=/"))
+                                       FS::Path path = strip(p);
+                                       if(!any_equals(tool.system_path, path))
                                        {
-                                               search_dir += 2;
-                                               if(sysroot.empty())
-                                                       path = "/";
-                                               else
-                                                       path = sysroot;
+                                               builder.get_logger().log("tools", "Got %s frontend system path: %s", tool_tag, path);
+                                               tool.system_path.push_back(path);
                                        }
-
-                                       path /= output.substr(search_dir, end-search_dir);
-                                       builder.get_logger().log("tools", format("Got %s system path: %s", tag, path));
-                                       system_path.push_back(path);
                                        path_found = true;
+                               }
+                       }
+
+                       string::size_type start = 0;
+                       while(start<output.size())
+                       {
+                               string::size_type search_dir = output.find("SEARCH_DIR(\"", start);
+                               if(search_dir==string::npos)
+                                       break;
+
+                               search_dir += 12;
+                               string::size_type end = output.find("\");", search_dir);
+                               if(end==string::npos)
+                                       break;
 
-                                       start = end+3;
+                               FS::Path path;
+                               if(!output.compare(search_dir, 2, "=/"))
+                               {
+                                       search_dir += 2;
+                                       if(sysroot.empty())
+                                               path = "/";
+                                       else
+                                               path = sysroot;
+                               }
+
+                               path /= output.substr(search_dir, end-search_dir);
+                               if(!any_equals(tool.system_path, path))
+                               {
+                                       builder.get_logger().log("tools", "Got %s implicit system path: %s", tool_tag, path);
+                                       tool.system_path.push_back(path);
                                }
+                               path_found = true;
+
+                               start = end+3;
                        }
-                       catch(...)
-                       { }
                }
+               catch(...)
+               { }
        }
 
        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", tool_tag);
                if(!sysroot.empty())
-                       system_path.push_back(sysroot/"usr/lib");
+                       tool.system_path.push_back(sysroot/"usr/lib");
                else if(architecture->is_native())
                {
-                       system_path.push_back("/lib");
-                       system_path.push_back("/usr/lib");
+                       tool.system_path.push_back("/lib");
+                       tool.system_path.push_back("/usr/lib");
                        if(architecture->match_name("pc-32-linux"))
                        {
-                               system_path.push_back("/lib/i386-linux-gnu");
-                               system_path.push_back("/usr/lib/i386-linux-gnu");
+                               tool.system_path.push_back("/lib/i386-linux-gnu");
+                               tool.system_path.push_back("/usr/lib/i386-linux-gnu");
                        }
                        else if(architecture->match_name("pc-64-linux"))
                        {
-                               system_path.push_back("/lib/x86_64-linux-gnu");
-                               system_path.push_back("/usr/lib/x86_64-linux-gnu");
+                               tool.system_path.push_back("/lib/x86_64-linux-gnu");
+                               tool.system_path.push_back("/usr/lib/x86_64-linux-gnu");
                        }
                }
                else
-                       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),
-       compiler_tag(ct)
-{
-       if(compiler_tag=="CC")
-               set_command("gcc", true);
-       else if(compiler_tag=="CXX")
-               set_command("g++", true);
-       else
-               throw invalid_argument("GnuLinker::Linker::Linker");
-}
-
-string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
-{
-       string result = Tool::create_build_signature(binfo);
-       result += ',';
-       if(binfo.libmode<=BuildInfo::STATIC)
-               result += 't';
-       else
-               result += 'd';
-       if(binfo.strip)
-               result += 's';
-       if(!binfo.libs.empty())
-       {
-               result += ",l";
-               result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
-       }
-       return result;
-}
-
-void GnuLinker::Linker::do_prepare()
-{
-       parent.prepare();
-       build_info = parent.get_build_info();
-       system_path = parent.get_system_path();
-
-       Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
-       if(dynamic_cast<GnuCompiler *>(&compiler))
-       {
-               compiler.prepare();
-               executable = compiler.get_executable();
+                       tool.system_path.push_back(format("/usr/%s/lib", architecture->get_cross_prefix()));
        }
 }
 
-Task *GnuLinker::Linker::run(const Target &target) const
+Task *GnuLinker::_run(const Binary &bin)
 {
-       const Binary &bin = dynamic_cast<const Binary &>(target);
+       const Tool &tool = *bin.get_tool();
+       const Builder &builder = tool.get_builder();
+       const Architecture &arch = *tool.get_architecture();
 
        ExternalTask::Arguments argv;
-       argv.push_back(executable->get_path().str());
+       argv.push_back(tool.get_executable()->get_path().str());
 
        FS::Path work_dir = bin.get_component()->get_package().get_source_directory();
 
@@ -237,9 +214,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());
@@ -260,7 +237,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,12 +257,12 @@ Task *GnuLinker::Linker::run(const Target &target) const
                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());
@@ -295,13 +272,18 @@ Task *GnuLinker::Linker::run(const Target &target) const
 
        bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
 
-       for(Target *d: target.get_dependencies())
+       bool has_cplusplus = false;
+       for(Target *d: bin.get_dependencies())
        {
                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());
+                       if(obj->get_tool()->get_tag()=="CXX")
+                               has_cplusplus = true;
+               }
                else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
                        argv.push_back((file?file:stlib)->get_path().str());
                else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
@@ -327,18 +309,21 @@ Task *GnuLinker::Linker::run(const Target &target) const
                        argv.push_back(l.substr(0, l.size()-10));
                }
 
+       if(has_cplusplus)
+               argv.push_back("-lstdc++");
+
        if(static_link_ok)
                argv.push_back("-static");
        else
        {
-               if(compiler_tag=="CXX")
+               if(has_cplusplus)
                {
                        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");
        }