]> git.tdb.fi Git - builder.git/commitdiff
Also get the linker system path from the actual linker program
authorMikko Rasa <tdb@tdb.fi>
Sun, 3 Dec 2017 11:08:24 +0000 (13:08 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 3 Dec 2017 11:08:24 +0000 (13:08 +0200)
source/gnulinker.cpp
source/gnulinker.h

index a93f809bfa1df263531d157d063ddf35ab12c441..adc3efcfcf6ad72d933b8e401836cbd935e34091 100644 (file)
@@ -30,21 +30,7 @@ GnuLinker::GnuLinker(Builder &b, const Architecture &a, const FS::Path &sysroot)
        processing_unit = COMPONENT;
 
        if(!sysroot.empty())
-       {
                build_info.sysroot = sysroot;
-               system_path.push_back(sysroot/"usr"/"lib");
-       }
-       else 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");
@@ -113,6 +99,89 @@ Target *GnuLinker::create_install(Target &target) const
                return 0;
 }
 
+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))
+       {
+               compiler.prepare();
+               FileTarget *compiler_exe = compiler.get_executable();
+               if(compiler_exe)
+               {
+                       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 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, "=/"))
+                                       {
+                                               search_dir += 2;
+                                               if(sysroot.empty())
+                                                       path = "/";
+                                               else
+                                                       path = sysroot;
+                                       }
+
+                                       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;
+
+                                       start = end+3;
+                               }
+                       }
+                       catch(...)
+                       { }
+               }
+       }
+
+       if(!path_found)
+       {
+               builder.get_logger().log("tools", format("No %s system path found, using defaults", tag));
+               if(!sysroot.empty())
+                       system_path.push_back(sysroot/"usr/lib");
+               else 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("/lib/i386-linux-gnu");
+                               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");
+                       }
+               }
+               else
+                       system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/lib");
+       }
+}
+
 Task *GnuLinker::run(const Target &) const
 {
        throw logic_error("GnuLinker should not be run directly");
index 872234a6d33d645a36676309172c4c58e948a15d..784170023bafb84a33633356a889245ee848acaf 100644 (file)
@@ -37,6 +37,9 @@ public:
 
        virtual Target *create_target(const std::list<Target *> &, const std::string &);
        virtual Target *create_install(Target &) const;
+protected:
+       virtual void do_prepare();
+public:
        virtual Task *run(const Target &) const;
 };