X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fgnulinker.cpp;h=9f6230e481f2f0260275d19d9a88e2ff4a41e46e;hb=HEAD;hp=a132acecf5b9f0f85bb1220c2edb08098f0a4829;hpb=fe305f49aae7b7dff4c6be6f7bb8d403ccf53768;p=builder.git diff --git a/source/gnulinker.cpp b/source/gnulinker.cpp deleted file mode 100644 index a132ace..0000000 --- a/source/gnulinker.cpp +++ /dev/null @@ -1,345 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "builder.h" -#include "component.h" -#include "executable.h" -#include "exportdefinitions.h" -#include "externaltask.h" -#include "gnucompiler.h" -#include "gnulinker.h" -#include "importlibrary.h" -#include "installedfile.h" -#include "objectfile.h" -#include "sharedlibrary.h" -#include "sourcepackage.h" -#include "staticlibrary.h" - -using namespace std; -using namespace Msp; - -GnuLinker::GnuLinker(Builder &b, const Architecture &a): - 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"); -} - -GnuLinker::~GnuLinker() -{ - delete default_linker; - delete cxx_linker; -} - -Target *GnuLinker::create_target(const list &sources, const string &arg) -{ - if(sources.empty()) - throw invalid_argument("GnuLinker::create_target"); - list objs; - Linker *linker = default_linker; - for(list::const_iterator i=sources.begin(); i!=sources.end(); ++i) - { - if(ObjectFile *obj = dynamic_cast(*i)) - { - objs.push_back(obj); - if(obj->get_tool()->get_tag()=="CXX") - linker = cxx_linker; - } - else - throw invalid_argument("GnuLinker::create_target"); - } - - const Component &comp = *objs.front()->get_component(); - Binary *bin = 0; - if(arg=="shared") - { - SharedLibrary *shlib = new SharedLibrary(builder, comp, objs); - if(architecture->get_system()=="windows") - { - Tool &dlltool = builder.get_toolchain().get_tool("DLL"); - dlltool.create_target(*shlib); - } - bin = shlib; - } - else - bin = new Executable(builder, comp, objs); - bin->set_tool(*linker); - return bin; -} - -Target *GnuLinker::create_install(Target &target) const -{ - if(SharedLibrary *shlib = dynamic_cast(&target)) - { - Tool © = builder.get_toolchain().get_tool("CP"); - InstalledFile *inst_tgt = dynamic_cast(copy.create_target(target)); - if(architecture->get_system()=="windows") - 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()); - if(link_name!=FS::basename(inst_tgt->get_path())) - inst_tgt->set_symlink(link_name); - } - return inst_tgt; - } - else - 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"); -} - - -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 = FS::basename(executable->get_path()); - 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(&compiler)) - { - compiler.prepare(); - executable = compiler.get_executable(); - } -} - -Task *GnuLinker::Linker::run(const Target &target) const -{ - const Binary &bin = dynamic_cast(target); - - vector argv; - argv.push_back(executable->get_path().str()); - - FS::Path work_dir = bin.get_component()->get_package().get_source_directory(); - - if(const SharedLibrary *shlib = dynamic_cast(&bin)) - { - argv.push_back("-shared"); - argv.push_back("-fPIC"); - if(architecture->get_system()!="windows" && !shlib->get_soname().empty()) - { - if(architecture->get_system()=="darwin") - { - argv.push_back("-install_name"); - argv.push_back(shlib->get_soname()); - - const string &ver = shlib->get_package()->get_version(); - const string &if_ver = shlib->get_package()->get_interface_version(); - if(!ver.empty() && !if_ver.empty()) - { - argv.push_back("-current_version"); - argv.push_back(ver); - argv.push_back("-compatibility_version"); - argv.push_back(if_ver); - } - } - else - argv.push_back("-Wl,-soname,"+shlib->get_soname()); - } - } - - BuildInfo binfo; - target.collect_build_info(binfo); - - const FS::Path &sysroot = binfo.sysroot; - 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) - argv.push_back("-s"); - if(binfo.threads && architecture->get_system()!="windows" && architecture->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())); - - 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); - - 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) - { - FileTarget *file = dynamic_cast(*i); - Target *tgt = (*i)->get_real_target(); - - if(ObjectFile *obj = dynamic_cast(tgt)) - argv.push_back(relative(obj->get_path(), work_dir).str()); - else if(StaticLibrary *stlib = dynamic_cast(tgt)) - argv.push_back((file?file:stlib)->get_path().str()); - else if(SharedLibrary *shlib = dynamic_cast(tgt)) - { - argv.push_back("-l"+shlib->get_libname()); - static_link_ok = false; - } - else if(ImportLibrary *imp = dynamic_cast(tgt)) - { - shlib = imp->get_shared_library(); - if(shlib) - argv.push_back("-l"+shlib->get_libname()); - else - argv.push_back((file?file:imp)->get_path().str()); - static_link_ok = false; - } - } - - for(BuildInfo::WordList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i) - if(i->size()>10 && !i->compare(i->size()-10, 10, ".framework")) - { - argv.push_back("-framework"); - argv.push_back(i->substr(0, i->size()-10)); - } - - if(static_link_ok) - argv.push_back("-static"); - else if(architecture->get_system()=="windows") - argv.push_back("-Wl,--enable-auto-import"); - - return new ExternalTask(argv, work_dir); -}