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)
148 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
152 void GnuLinker::Linker::do_prepare()
154 Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
155 if(dynamic_cast<GnuCompiler *>(&compiler))
158 executable = compiler.get_executable();
162 Task *GnuLinker::Linker::run(const Target &target) const
164 const Binary &bin = dynamic_cast<const Binary &>(target);
167 argv.push_back(executable->get_path().str());
169 const Component &comp = *bin.get_component();
171 FS::Path work_dir = comp.get_package().get_source_directory();
173 if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
175 argv.push_back("-shared");
176 argv.push_back("-fPIC");
177 if(architecture->get_system()!="windows" && !shlib->get_soname().empty())
179 if(architecture->get_system()=="darwin")
181 argv.push_back("-install_name");
182 argv.push_back(shlib->get_soname());
184 const string &ver = shlib->get_package()->get_version();
185 const string &if_ver = shlib->get_package()->get_interface_version();
186 if(!ver.empty() && !if_ver.empty())
188 argv.push_back("-current_version");
190 argv.push_back("-compatibility_version");
191 argv.push_back(if_ver);
195 argv.push_back("-Wl,-soname,"+shlib->get_soname());
199 const BuildInfo &binfo = comp.get_build_info();
200 for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
201 argv.push_back("-L"+i->str());
203 argv.push_back("-s");
204 if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
205 argv.push_back("-pthread");
207 const Architecture &native_arch = builder.get_native_arch();
208 if(architecture->get_bits()!=native_arch.get_bits())
209 argv.push_back(format("-m%d", architecture->get_bits()));
211 argv.push_back("-o");
212 argv.push_back(relative(bin.get_path(), work_dir).str());
214 bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
215 bool need_l_objc = false;
217 const Target::Dependencies &depends = target.get_dependencies();
218 for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
220 FileTarget *file = dynamic_cast<FileTarget *>(*i);
221 Target *tgt = (*i)->get_real_target();
223 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
225 argv.push_back(relative(obj->get_path(), work_dir).str());
226 /* XXX This is a hack. A more generic way is needed for tools to pass
227 information down the chain. */
228 if(obj->get_tool()->get_tag()=="OBJC")
231 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
232 argv.push_back((file?file:stlib)->get_path().str());
233 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
235 argv.push_back("-l"+shlib->get_libname());
236 static_link_ok = false;
238 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
240 shlib = imp->get_shared_library();
242 argv.push_back("-l"+shlib->get_libname());
244 argv.push_back((file?file:imp)->get_path().str());
245 static_link_ok = false;
249 for(BuildInfo::WordList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i)
250 if(i->size()>10 && !i->compare(i->size()-10, 10, ".framework"))
252 argv.push_back("-framework");
253 argv.push_back(i->substr(0, i->size()-10));
257 argv.push_back("-lobjc");
259 argv.push_back("-static");
260 else if(architecture->get_system()=="windows")
261 argv.push_back("-Wl,--enable-auto-import");
263 return new ExternalTask(argv, work_dir);