]> git.tdb.fi Git - builder.git/commitdiff
Redesign GnuLinker to work without subtools
authorMikko Rasa <tdb@tdb.fi>
Sat, 24 Dec 2022 12:30:54 +0000 (14:30 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 24 Dec 2022 13:10:58 +0000 (15:10 +0200)
source/gnulinker.cpp
source/gnulinker.h

index 31440976e917d31a43c8d9d701ff057a0573a740..619c9e784b3a00ec5bd22e2e537afb90da9e85d9 100644 (file)
@@ -29,14 +29,8 @@ GnuLinker::GnuLinker(Builder &b, const Architecture &a):
 
        processing_unit = COMPONENT;
 
-       default_linker = new Linker(*this, "CC");
-       cxx_linker = new Linker(*this, "CXX");
-}
-
-GnuLinker::~GnuLinker()
-{
-       delete default_linker;
-       delete cxx_linker;
+       set_command("gcc");
+       set_run(_run);
 }
 
 Target *GnuLinker::create_target(const vector<Target *> &sources, const string &arg)
@@ -45,14 +39,8 @@ Target *GnuLinker::create_target(const vector<Target *> &sources, const string &
                throw invalid_argument("GnuLinker::create_target");
        vector<ObjectFile *> objs;
        objs.reserve(sources.size());
-       Linker *linker = default_linker;
        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;
@@ -68,7 +56,7 @@ Target *GnuLinker::create_target(const vector<Target *> &sources, const string &
        }
        else
                bin = new Executable(builder, comp, objs);
-       bin->set_tool(*linker);
+       bin->set_tool(*this);
        return bin;
 }
 
@@ -92,62 +80,74 @@ Target *GnuLinker::create_install(Target &target) const
                return 0;
 }
 
+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()
 {
        bool path_found = false;
        const FS::Path &sysroot = build_info.sysroot;
 
-       Tool &compiler = builder.get_toolchain().get_tool("CC");
-       if(dynamic_cast<GnuCompiler *>(&compiler))
+       if(executable)
        {
-               compiler.prepare();
-               FileTarget *compiler_exe = compiler.get_executable();
-               if(compiler_exe)
+               ExternalTask::Arguments argv;
+               argv.push_back(executable->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", "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", "Running %s", join(argv.begin(), argv.end()));
-                       try
+                       string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
+                       string::size_type start = 0;
+                       while(start<output.size())
                        {
-                               string output = ExternalTask::run_and_capture_output(argv, FS::Path(), 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;
+
+                               FS::Path path;
+                               if(!output.compare(search_dir, 2, "=/"))
                                {
-                                       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, "=/"))
-                                       {
-                                               search_dir += 2;
-                                               if(sysroot.empty())
-                                                       path = "/";
-                                               else
-                                                       path = sysroot;
-                                       }
-
-                                       path /= output.substr(search_dir, end-search_dir);
-                                       builder.get_logger().log("tools", "Got %s system path: %s", tag, path);
-                                       system_path.push_back(path);
-                                       path_found = true;
-
-                                       start = end+3;
+                                       search_dir += 2;
+                                       if(sysroot.empty())
+                                               path = "/";
+                                       else
+                                               path = sysroot;
                                }
+
+                               path /= output.substr(search_dir, end-search_dir);
+                               builder.get_logger().log("tools", "Got %s system path: %s", tag, path);
+                               system_path.push_back(path);
+                               path_found = true;
+
+                               start = end+3;
                        }
-                       catch(...)
-                       { }
                }
+               catch(...)
+               { }
        }
 
        if(!path_found)
@@ -175,56 +175,9 @@ void GnuLinker::do_prepare()
        }
 }
 
-
-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");
-
-       set_run(_run);
-}
-
-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()
+Task *GnuLinker::_run(const Binary &bin)
 {
-       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();
-       }
-}
-
-Task *GnuLinker::Linker::_run(const Binary &bin)
-{
-       const Linker &tool = dynamic_cast<const Linker &>(*bin.get_tool());
+       const Tool &tool = *bin.get_tool();
        const Builder &builder = tool.get_builder();
        const Architecture &arch = *tool.get_architecture();
 
@@ -295,13 +248,18 @@ Task *GnuLinker::Linker::_run(const Binary &bin)
 
        bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
 
+       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,11 +285,14 @@ Task *GnuLinker::Linker::_run(const Binary &bin)
                        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(tool.compiler_tag=="CXX")
+               if(has_cplusplus)
                {
                        auto i = binfo.libmodes.find("stdc++");
                        if(i!=binfo.libmodes.end() && i->second<=BuildInfo::STATIC)
index 713b978064747ab570b15f9cbc764ddaa149b014..c04afd1ffb580c2c618a107cec69e0cafbc20ff5 100644 (file)
@@ -14,34 +14,16 @@ Uses either gcc or g++ depending on what was used to compile the object files.
 */
 class GnuLinker: public Tool
 {
-private:
-       class Linker: public SubTool
-       {
-       private:
-               std::string compiler_tag;
-
-       public:
-               Linker(GnuLinker &, const std::string &);
-
-               std::string create_build_signature(const BuildInfo &) const override;
-       private:
-               void do_prepare() override;
-
-       public:
-               static Task *_run(const Binary &);
-       };
-
-       Linker *default_linker = 0;
-       Linker *cxx_linker = 0;
-
 public:
        GnuLinker(Builder &, const Architecture &);
-       ~GnuLinker();
 
        Target *create_target(const std::vector<Target *> &, const std::string &) override;
        Target *create_install(Target &) const override;
+       std::string create_build_signature(const BuildInfo &) const override;
 protected:
        void do_prepare() override;
+private:
+       static Task *_run(const Binary &);
 };
 
 #endif