X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fgnulinker.cpp;h=2d37a4d93533b4486d865a043f691f3725ff4aa5;hb=bf0883b6dd3946612922aa1b7c04a87d06442df7;hp=a93f809bfa1df263531d157d063ddf35ab12c441;hpb=82f1175238a94618b0cf1485b0132fb965996843;p=builder.git diff --git a/source/gnulinker.cpp b/source/gnulinker.cpp index a93f809..2d37a4d 100644 --- a/source/gnulinker.cpp +++ b/source/gnulinker.cpp @@ -21,7 +21,7 @@ using namespace std; using namespace Msp; -GnuLinker::GnuLinker(Builder &b, const Architecture &a, const FS::Path &sysroot): +GnuLinker::GnuLinker(Builder &b, const Architecture &a): Tool(b, a, "LINK") { input_suffixes.push_back(".o"); @@ -29,23 +29,6 @@ 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"); } @@ -102,8 +85,7 @@ Target *GnuLinker::create_install(Target &target) const builder.get_build_graph().add_installed_target(*shlib->get_import_library()); else { - const Pattern &pattern = architecture->get_shared_library_patterns().front(); - string link_name = pattern.apply(shlib->get_libname()); + string link_name = architecture->create_filename(shlib->get_libname()); if(link_name!=FS::basename(inst_tgt->get_path())) inst_tgt->set_symlink(link_name); } @@ -113,6 +95,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(&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(startis_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"); @@ -151,6 +216,10 @@ string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const 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(&compiler)) { @@ -201,6 +270,16 @@ Task *GnuLinker::Linker::run(const Target &target) const if(!sysroot.empty()) argv.push_back("--sysroot="+sysroot.str()); + FS::Path lib_dir = builder.get_prefix()/"lib"; + if(binfo.rpath_mode==BuildInfo::ABSOLUTE) + argv.push_back("-Wl,-rpath,"+lib_dir.str()); + else + { + if(binfo.rpath_mode==BuildInfo::RELATIVE) + argv.push_back("-Wl,-rpath,$ORIGIN/../lib"); + 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()); if(binfo.strip) @@ -220,9 +299,6 @@ Task *GnuLinker::Linker::run(const Target &target) const bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC); - if(architecture->get_system()=="windows" && architecture->get_cross_prefix().find("mingw")!=string::npos) - argv.push_back("-lmingw32"); - const Target::Dependencies &depends = target.get_dependencies(); for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i) { @@ -258,8 +334,18 @@ Task *GnuLinker::Linker::run(const Target &target) const if(static_link_ok) argv.push_back("-static"); - else if(architecture->get_system()=="windows") - argv.push_back("-Wl,--enable-auto-import"); + else + { + if(compiler_tag=="CXX") + { + BuildInfo::LibModeMap::const_iterator i = binfo.libmodes.find("stdc++"); + if(i!=binfo.libmodes.end() && i->second<=BuildInfo::STATIC) + argv.push_back("-static-libstdc++"); + } + + if(architecture->get_system()=="windows") + argv.push_back("-Wl,--enable-auto-import"); + } return new ExternalTask(argv, work_dir); }