3 #include <msp/fs/dir.h>
4 #include <msp/fs/utils.h>
5 #include <msp/strings/format.h>
6 #include <msp/strings/utils.h>
9 #include "executable.h"
10 #include "externaltask.h"
11 #include "gnucompiler.h"
12 #include "gnulinker.h"
13 #include "objectfile.h"
14 #include "sharedlibrary.h"
15 #include "sourcepackage.h"
16 #include "staticlibrary.h"
21 GnuLinker::GnuLinker(Builder &b, const Architecture &a):
24 input_suffixes.push_back(".o");
25 input_suffixes.push_back(".a");
27 if(architecture->is_native())
29 system_path.push_back("/lib");
30 system_path.push_back("/usr/lib");
31 if(architecture->match_name("pc-32-linux"))
32 system_path.push_back("/usr/lib/i386-linux-gnu");
33 else if(architecture->match_name("pc-64-linux"))
34 system_path.push_back("/usr/lib/x86_64-linux-gnu");
37 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/lib");
39 default_linker = new Linker(*this, "CC");
40 cxx_linker = new Linker(*this, "CXX");
43 GnuLinker::~GnuLinker()
45 delete default_linker;
49 Target *GnuLinker::create_target(const list<Target *> &sources, const string &arg) const
52 throw invalid_argument("GnuLinker::create_target");
53 list<ObjectFile *> objs;
54 Linker *linker = default_linker;
55 for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
57 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
60 if(obj->get_tool()->get_tag()=="CXX")
64 throw invalid_argument("GnuLinker::create_target");
67 const Component &comp = *objs.front()->get_component();
70 bin = new SharedLibrary(builder, comp, objs);
72 bin = new Executable(builder, comp, objs);
73 bin->set_tool(*linker);
77 Task *GnuLinker::run(const Target &) const
79 throw logic_error("GnuLinker should not be run directly");
83 GnuLinker::Linker::Linker(GnuLinker &p, const string &compiler_tag):
86 const Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
87 if(dynamic_cast<const GnuCompiler *>(&compiler))
88 executable = compiler.get_executable();
92 if(compiler_tag=="CC")
94 else if(compiler_tag=="CXX")
97 throw invalid_argument("GnuLinker::Linker::Linker");
98 if(architecture->is_cross())
99 command = format("%s-%s", architecture->get_cross_prefix(), command);
100 executable = builder.get_vfs().find_binary(command);
104 Target *GnuLinker::Linker::create_target(const list<Target *> &sources, const string &arg) const
106 return parent.create_target(sources, arg);
109 string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
111 string result = FS::basename(executable->get_path());
113 if(binfo.libmode<=BuildInfo::STATIC)
120 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
124 Task *GnuLinker::Linker::run(const Target &target) const
126 const Binary &bin = dynamic_cast<const Binary &>(target);
129 argv.push_back(executable->get_path().str());
131 const Component &comp = *bin.get_component();
133 if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
135 argv.push_back("-shared");
136 argv.push_back("-fPIC");
137 if(!shlib->get_soname().empty())
138 argv.push_back("-Wl,-soname,"+shlib->get_soname());
141 const BuildInfo &binfo = comp.get_build_info();
142 for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
143 argv.push_back("-L"+i->str());
145 argv.push_back("-s");
147 argv.push_back("-pthread");
149 const Architecture &native_arch = builder.get_native_arch();
150 if(architecture->get_bits()!=native_arch.get_bits())
151 argv.push_back(format("-m%d", architecture->get_bits()));
153 FS::Path work_dir = comp.get_package().get_source_directory();
155 argv.push_back("-o");
156 argv.push_back(relative(bin.get_path(), work_dir).str());
158 bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
160 const Target::Dependencies &depends = target.get_dependencies();
161 for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
163 Target *tgt = (*i)->get_real_target();
165 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
166 argv.push_back(relative(obj->get_path(), work_dir).str());
167 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
168 argv.push_back(stlib->get_path().str());
169 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
171 argv.push_back("-l"+shlib->get_libname());
172 static_link_ok = false;
177 argv.push_back("-static");
179 if(!builder.get_dry_run())
180 FS::mkpath(FS::dirname(bin.get_path()), 0755);
182 return new ExternalTask(argv, work_dir);