3 #include <msp/fs/dir.h>
4 #include <msp/fs/utils.h>
5 #include <msp/strings/format.h>
8 #include "executable.h"
9 #include "externaltask.h"
10 #include "gnucompiler.h"
11 #include "gnulinker.h"
12 #include "objectfile.h"
13 #include "sharedlibrary.h"
14 #include "sourcepackage.h"
15 #include "staticlibrary.h"
20 GnuLinker::GnuLinker(Builder &b, const Architecture &a):
23 input_suffixes.push_back(".o");
24 input_suffixes.push_back(".a");
26 if(architecture->is_native())
28 system_path.push_back("/lib");
29 system_path.push_back("/usr/lib");
30 if(architecture->match_name("pc-32-linux"))
31 system_path.push_back("/usr/lib/i386-linux-gnu");
32 else if(architecture->match_name("pc-64-linux"))
33 system_path.push_back("/usr/lib/x86_64-linux-gnu");
36 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/lib");
38 default_linker = new Linker(*this, "CC");
39 cxx_linker = new Linker(*this, "CXX");
42 GnuLinker::~GnuLinker()
44 delete default_linker;
48 Target *GnuLinker::create_target(const list<Target *> &sources, const string &arg) const
51 throw invalid_argument("GnuLinker::create_target");
52 list<ObjectFile *> objs;
53 Linker *linker = default_linker;
54 for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
56 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
59 if(obj->get_tool()->get_tag()=="CXX")
63 throw invalid_argument("GnuLinker::create_target");
66 const Component &comp = *objs.front()->get_component();
69 bin = new SharedLibrary(builder, comp, objs);
71 bin = new Executable(builder, comp, objs);
72 bin->set_tool(*linker);
76 Task *GnuLinker::run(const Target &) const
78 throw logic_error("GnuLinker should not be run directly");
82 GnuLinker::Linker::Linker(GnuLinker &p, const string &compiler_tag):
85 const Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
86 if(dynamic_cast<const GnuCompiler *>(&compiler))
87 executable = compiler.get_executable();
91 if(compiler_tag=="CC")
93 else if(compiler_tag=="CXX")
96 throw invalid_argument("GnuLinker::Linker::Linker");
97 if(architecture->is_cross())
98 command = format("%s-%s", architecture->get_cross_prefix(), command);
99 executable = builder.get_vfs().find_binary(command);
103 Target *GnuLinker::Linker::create_target(const list<Target *> &sources, const string &arg) const
105 return parent.create_target(sources, arg);
108 Task *GnuLinker::Linker::run(const Target &target) const
110 const Binary &bin = dynamic_cast<const Binary &>(target);
113 argv.push_back(executable->get_path().str());
115 const Component &comp = *bin.get_component();
117 if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
119 argv.push_back("-shared");
120 argv.push_back("-fPIC");
121 if(!shlib->get_soname().empty())
122 argv.push_back("-Wl,-soname,"+shlib->get_soname());
125 const BuildInfo &binfo = comp.get_build_info();
126 for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
127 argv.push_back("-L"+i->str());
129 argv.push_back("-s");
131 argv.push_back("-pthread");
133 const Architecture &native_arch = builder.get_native_arch();
134 if(architecture->get_bits()!=native_arch.get_bits())
135 argv.push_back(format("-m%d", architecture->get_bits()));
137 FS::Path work_dir = comp.get_package().get_source();
139 argv.push_back("-o");
140 argv.push_back(relative(bin.get_path(), work_dir).str());
142 bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
144 const Target::Dependencies &depends = target.get_depends();
145 for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
147 Target *tgt = (*i)->get_real_target();
149 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
150 argv.push_back(relative(obj->get_path(), work_dir).str());
151 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
152 argv.push_back(stlib->get_path().str());
153 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
155 argv.push_back("-l"+shlib->get_libname());
156 static_link_ok = false;
161 argv.push_back("-static");
163 if(!builder.get_dry_run())
164 FS::mkpath(FS::dirname(bin.get_path()), 0755);
166 return new ExternalTask(argv, work_dir);