X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fgnulinker.cpp;h=faa62024e250aee1414caab7f52aed7ed794ebe8;hb=f41742cb2b21241634c123561b71ee1667cb1ff4;hp=837e7c7e64ff5baed9926ab1db5006a8f924245f;hpb=000deb52a1374bcedfec5d201c53c1842438ceba;p=builder.git diff --git a/source/gnulinker.cpp b/source/gnulinker.cpp index 837e7c7..faa6202 100644 --- a/source/gnulinker.cpp +++ b/source/gnulinker.cpp @@ -27,17 +27,7 @@ GnuLinker::GnuLinker(Builder &b, const Architecture &a): input_suffixes.push_back(".o"); input_suffixes.push_back(".a"); - 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"); + processing_unit = COMPONENT; default_linker = new Linker(*this, "CC"); cxx_linker = new Linker(*this, "CXX"); @@ -106,6 +96,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"); @@ -124,16 +197,6 @@ GnuLinker::Linker::Linker(GnuLinker &p, const string &ct): throw invalid_argument("GnuLinker::Linker::Linker"); } -Target *GnuLinker::Linker::create_target(const list &sources, const string &arg) -{ - return parent.create_target(sources, arg); -} - -Target *GnuLinker::Linker::create_install(Target &target) const -{ - return parent.create_install(target); -} - string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const { string result = FS::basename(executable->get_path()); @@ -154,6 +217,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)) { @@ -169,9 +236,7 @@ Task *GnuLinker::Linker::run(const Target &target) const vector argv; argv.push_back(executable->get_path().str()); - const Component &comp = *bin.get_component(); - - FS::Path work_dir = comp.get_package().get_source_directory(); + FS::Path work_dir = bin.get_component()->get_package().get_source_directory(); if(const SharedLibrary *shlib = dynamic_cast(&bin)) { @@ -199,7 +264,13 @@ Task *GnuLinker::Linker::run(const Target &target) const } } - const BuildInfo &binfo = comp.get_build_info(); + BuildInfo binfo; + target.collect_build_info(binfo); + + const FS::Path &sysroot = binfo.sysroot; + if(!sysroot.empty()) + argv.push_back("--sysroot="+sysroot.str()); + for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i) argv.push_back("-L"+i->str()); if(binfo.strip) @@ -208,14 +279,19 @@ Task *GnuLinker::Linker::run(const Target &target) const argv.push_back("-pthread"); const Architecture &native_arch = builder.get_native_arch(); - if(architecture->get_bits()!=native_arch.get_bits()) + if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits()) argv.push_back(format("-m%d", architecture->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); + bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC); - bool need_l_objc = false; + + 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) @@ -224,13 +300,7 @@ Task *GnuLinker::Linker::run(const Target &target) const Target *tgt = (*i)->get_real_target(); if(ObjectFile *obj = dynamic_cast(tgt)) - { argv.push_back(relative(obj->get_path(), work_dir).str()); - /* XXX This is a hack. A more generic way is needed for tools to pass - information down the chain. */ - if(obj->get_tool()->get_tag()=="OBJC") - need_l_objc = true; - } else if(StaticLibrary *stlib = dynamic_cast(tgt)) argv.push_back((file?file:stlib)->get_path().str()); else if(SharedLibrary *shlib = dynamic_cast(tgt)) @@ -256,8 +326,6 @@ Task *GnuLinker::Linker::run(const Target &target) const argv.push_back(i->substr(0, i->size()-10)); } - if(need_l_objc) - argv.push_back("-lobjc"); if(static_link_ok) argv.push_back("-static"); else if(architecture->get_system()=="windows")