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 "exportdefinitions.h"
11 #include "externaltask.h"
12 #include "gnucompiler.h"
13 #include "gnulinker.h"
14 #include "importlibrary.h"
15 #include "installedfile.h"
16 #include "objectfile.h"
17 #include "sharedlibrary.h"
18 #include "sourcepackage.h"
19 #include "staticlibrary.h"
24 GnuLinker::GnuLinker(Builder &b, const Architecture &a):
27 input_suffixes.push_back(".o");
28 input_suffixes.push_back(".a");
30 if(architecture->is_native())
32 system_path.push_back("/lib");
33 system_path.push_back("/usr/lib");
34 if(architecture->match_name("pc-32-linux"))
35 system_path.push_back("/usr/lib/i386-linux-gnu");
36 else if(architecture->match_name("pc-64-linux"))
37 system_path.push_back("/usr/lib/x86_64-linux-gnu");
40 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/lib");
42 default_linker = new Linker(*this, "CC");
43 cxx_linker = new Linker(*this, "CXX");
46 GnuLinker::~GnuLinker()
48 delete default_linker;
52 Target *GnuLinker::create_target(const list<Target *> &sources, const string &arg)
55 throw invalid_argument("GnuLinker::create_target");
56 list<ObjectFile *> objs;
57 Linker *linker = default_linker;
58 for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
60 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
63 if(obj->get_tool()->get_tag()=="CXX")
67 throw invalid_argument("GnuLinker::create_target");
70 const Component &comp = *objs.front()->get_component();
74 SharedLibrary *shlib = new SharedLibrary(builder, comp, objs);
75 if(architecture->get_system()=="windows")
77 Tool &dlltool = builder.get_toolchain().get_tool("DLL");
78 dlltool.create_target(*shlib);
83 bin = new Executable(builder, comp, objs);
84 bin->set_tool(*linker);
88 Target *GnuLinker::create_install(Target &target) const
90 if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(&target))
92 Tool © = builder.get_toolchain().get_tool("CP");
93 InstalledFile *inst_tgt = dynamic_cast<InstalledFile *>(copy.create_target(target));
94 if(architecture->get_system()=="windows")
95 builder.get_build_graph().add_installed_target(*shlib->get_import_library());
98 const Pattern &pattern = architecture->get_shared_library_patterns().front();
99 string link_name = pattern.apply(shlib->get_libname());
100 if(link_name!=FS::basename(inst_tgt->get_path()))
101 inst_tgt->set_symlink(link_name);
109 Task *GnuLinker::run(const Target &) const
111 throw logic_error("GnuLinker should not be run directly");
115 GnuLinker::Linker::Linker(GnuLinker &p, const string &ct):
119 if(compiler_tag=="CC")
120 set_command("gcc", true);
121 else if(compiler_tag=="CXX")
122 set_command("g++", true);
124 throw invalid_argument("GnuLinker::Linker::Linker");
127 Target *GnuLinker::Linker::create_target(const list<Target *> &sources, const string &arg)
129 return parent.create_target(sources, arg);
132 Target *GnuLinker::Linker::create_install(Target &target) const
134 return parent.create_install(target);
137 string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
139 string result = FS::basename(executable->get_path());
141 if(binfo.libmode<=BuildInfo::STATIC)
147 if(!binfo.libs.empty())
150 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
155 void GnuLinker::Linker::do_prepare()
157 Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
158 if(dynamic_cast<GnuCompiler *>(&compiler))
161 executable = compiler.get_executable();
165 Task *GnuLinker::Linker::run(const Target &target) const
167 const Binary &bin = dynamic_cast<const Binary &>(target);
170 argv.push_back(executable->get_path().str());
172 const Component &comp = *bin.get_component();
174 FS::Path work_dir = comp.get_package().get_source_directory();
176 if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
178 argv.push_back("-shared");
179 argv.push_back("-fPIC");
180 if(architecture->get_system()!="windows" && !shlib->get_soname().empty())
182 if(architecture->get_system()=="darwin")
184 argv.push_back("-install_name");
185 argv.push_back(shlib->get_soname());
187 const string &ver = shlib->get_package()->get_version();
188 const string &if_ver = shlib->get_package()->get_interface_version();
189 if(!ver.empty() && !if_ver.empty())
191 argv.push_back("-current_version");
193 argv.push_back("-compatibility_version");
194 argv.push_back(if_ver);
198 argv.push_back("-Wl,-soname,"+shlib->get_soname());
202 const BuildInfo &binfo = comp.get_build_info();
203 for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
204 argv.push_back("-L"+i->str());
206 argv.push_back("-s");
207 if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
208 argv.push_back("-pthread");
210 const Architecture &native_arch = builder.get_native_arch();
211 if(architecture->get_bits()!=native_arch.get_bits())
212 argv.push_back(format("-m%d", architecture->get_bits()));
214 argv.push_back("-o");
215 argv.push_back(relative(bin.get_path(), work_dir).str());
217 bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
218 bool need_l_objc = false;
220 const Target::Dependencies &depends = target.get_dependencies();
221 for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
223 FileTarget *file = dynamic_cast<FileTarget *>(*i);
224 Target *tgt = (*i)->get_real_target();
226 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
228 argv.push_back(relative(obj->get_path(), work_dir).str());
229 /* XXX This is a hack. A more generic way is needed for tools to pass
230 information down the chain. */
231 if(obj->get_tool()->get_tag()=="OBJC")
234 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
235 argv.push_back((file?file:stlib)->get_path().str());
236 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
238 argv.push_back("-l"+shlib->get_libname());
239 static_link_ok = false;
241 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
243 shlib = imp->get_shared_library();
245 argv.push_back("-l"+shlib->get_libname());
247 argv.push_back((file?file:imp)->get_path().str());
248 static_link_ok = false;
252 for(BuildInfo::WordList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i)
253 if(i->size()>10 && !i->compare(i->size()-10, 10, ".framework"))
255 argv.push_back("-framework");
256 argv.push_back(i->substr(0, i->size()-10));
260 argv.push_back("-lobjc");
262 argv.push_back("-static");
263 else if(architecture->get_system()=="windows")
264 argv.push_back("-Wl,--enable-auto-import");
266 return new ExternalTask(argv, work_dir);