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 processing_unit = COMPONENT;
32 default_linker = new Linker(*this, "CC");
33 cxx_linker = new Linker(*this, "CXX");
36 GnuLinker::~GnuLinker()
38 delete default_linker;
42 Target *GnuLinker::create_target(const list<Target *> &sources, const string &arg)
45 throw invalid_argument("GnuLinker::create_target");
46 list<ObjectFile *> objs;
47 Linker *linker = default_linker;
48 for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
50 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
53 if(obj->get_tool()->get_tag()=="CXX")
57 throw invalid_argument("GnuLinker::create_target");
60 const Component &comp = *objs.front()->get_component();
64 SharedLibrary *shlib = new SharedLibrary(builder, comp, objs);
65 if(architecture->get_system()=="windows")
67 Tool &dlltool = builder.get_toolchain().get_tool("DLL");
68 dlltool.create_target(*shlib);
73 bin = new Executable(builder, comp, objs);
74 bin->set_tool(*linker);
78 Target *GnuLinker::create_install(Target &target) const
80 if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(&target))
82 Tool © = builder.get_toolchain().get_tool("CP");
83 InstalledFile *inst_tgt = dynamic_cast<InstalledFile *>(copy.create_target(target));
84 if(architecture->get_system()=="windows")
85 builder.get_build_graph().add_installed_target(*shlib->get_import_library());
88 const Pattern &pattern = architecture->get_shared_library_patterns().front();
89 string link_name = pattern.apply(shlib->get_libname());
90 if(link_name!=FS::basename(inst_tgt->get_path()))
91 inst_tgt->set_symlink(link_name);
99 void GnuLinker::do_prepare()
101 bool path_found = false;
102 const FS::Path &sysroot = build_info.sysroot;
104 Tool &compiler = builder.get_toolchain().get_tool("CC");
105 if(dynamic_cast<GnuCompiler *>(&compiler))
108 FileTarget *compiler_exe = compiler.get_executable();
111 ExternalTask::Arguments argv;
112 argv.push_back(compiler_exe->get_path().str());
113 argv.push_back("-Wl,--verbose");
114 argv.push_back("-nostdlib");
116 argv.push_back("--sysroot="+sysroot.str());
118 builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
121 string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
122 string::size_type start = 0;
123 while(start<output.size())
125 string::size_type search_dir = output.find("SEARCH_DIR(\"", start);
126 if(search_dir==string::npos)
130 string::size_type end = output.find("\");", search_dir);
131 if(end==string::npos)
135 if(!output.compare(search_dir, 2, "=/"))
144 path /= output.substr(search_dir, end-search_dir);
145 builder.get_logger().log("tools", format("Got %s system path: %s", tag, path));
146 system_path.push_back(path);
159 builder.get_logger().log("tools", format("No %s system path found, using defaults", tag));
161 system_path.push_back(sysroot/"usr/lib");
162 else if(architecture->is_native())
164 system_path.push_back("/lib");
165 system_path.push_back("/usr/lib");
166 if(architecture->match_name("pc-32-linux"))
168 system_path.push_back("/lib/i386-linux-gnu");
169 system_path.push_back("/usr/lib/i386-linux-gnu");
171 else if(architecture->match_name("pc-64-linux"))
173 system_path.push_back("/lib/x86_64-linux-gnu");
174 system_path.push_back("/usr/lib/x86_64-linux-gnu");
178 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/lib");
182 Task *GnuLinker::run(const Target &) const
184 throw logic_error("GnuLinker should not be run directly");
188 GnuLinker::Linker::Linker(GnuLinker &p, const string &ct):
192 if(compiler_tag=="CC")
193 set_command("gcc", true);
194 else if(compiler_tag=="CXX")
195 set_command("g++", true);
197 throw invalid_argument("GnuLinker::Linker::Linker");
200 string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
202 string result = FS::basename(executable->get_path());
204 if(binfo.libmode<=BuildInfo::STATIC)
210 if(!binfo.libs.empty())
213 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
218 void GnuLinker::Linker::do_prepare()
221 build_info = parent.get_build_info();
222 system_path = parent.get_system_path();
224 Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
225 if(dynamic_cast<GnuCompiler *>(&compiler))
228 executable = compiler.get_executable();
232 Task *GnuLinker::Linker::run(const Target &target) const
234 const Binary &bin = dynamic_cast<const Binary &>(target);
237 argv.push_back(executable->get_path().str());
239 FS::Path work_dir = bin.get_component()->get_package().get_source_directory();
241 if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
243 argv.push_back("-shared");
244 argv.push_back("-fPIC");
245 if(architecture->get_system()!="windows" && !shlib->get_soname().empty())
247 if(architecture->get_system()=="darwin")
249 argv.push_back("-install_name");
250 argv.push_back(shlib->get_soname());
252 const string &ver = shlib->get_package()->get_version();
253 const string &if_ver = shlib->get_package()->get_interface_version();
254 if(!ver.empty() && !if_ver.empty())
256 argv.push_back("-current_version");
258 argv.push_back("-compatibility_version");
259 argv.push_back(if_ver);
263 argv.push_back("-Wl,-soname,"+shlib->get_soname());
268 target.collect_build_info(binfo);
270 const FS::Path &sysroot = binfo.sysroot;
272 argv.push_back("--sysroot="+sysroot.str());
274 FS::Path lib_dir = builder.get_prefix()/"lib";
275 if(binfo.rpath_mode==BuildInfo::ABSOLUTE)
276 argv.push_back("-Wl,-rpath,"+lib_dir.str());
279 if(binfo.rpath_mode==BuildInfo::RELATIVE)
280 argv.push_back("-Wl,-rpath,$ORIGIN/../lib");
281 argv.push_back("-Wl,-rpath-link,"+lib_dir.str());
284 for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
285 argv.push_back("-L"+i->str());
287 argv.push_back("-s");
288 if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
289 argv.push_back("-pthread");
291 const Architecture &native_arch = builder.get_native_arch();
292 if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
293 argv.push_back(format("-m%d", architecture->get_bits()));
295 argv.push_back("-o");
296 argv.push_back(relative(bin.get_path(), work_dir).str());
298 for(BuildInfo::WordList::const_iterator i=binfo.keep_symbols.begin(); i!=binfo.keep_symbols.end(); ++i)
299 argv.push_back("-u"+*i);
301 bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
303 if(architecture->get_system()=="windows" && architecture->get_cross_prefix().find("mingw")!=string::npos)
304 argv.push_back("-lmingw32");
306 const Target::Dependencies &depends = target.get_dependencies();
307 for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
309 FileTarget *file = dynamic_cast<FileTarget *>(*i);
310 Target *tgt = (*i)->get_real_target();
312 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
313 argv.push_back(relative(obj->get_path(), work_dir).str());
314 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
315 argv.push_back((file?file:stlib)->get_path().str());
316 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
318 argv.push_back("-l"+shlib->get_libname());
319 static_link_ok = false;
321 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
323 shlib = imp->get_shared_library();
325 argv.push_back("-l"+shlib->get_libname());
327 argv.push_back((file?file:imp)->get_path().str());
328 static_link_ok = false;
332 for(BuildInfo::WordList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i)
333 if(i->size()>10 && !i->compare(i->size()-10, 10, ".framework"))
335 argv.push_back("-framework");
336 argv.push_back(i->substr(0, i->size()-10));
340 argv.push_back("-static");
343 if(compiler_tag=="CXX")
345 BuildInfo::LibModeMap::const_iterator i = binfo.libmodes.find("stdc++");
346 if(i!=binfo.libmodes.end() && i->second<=BuildInfo::STATIC)
347 argv.push_back("-static-libstdc++");
350 if(architecture->get_system()=="windows")
351 argv.push_back("-Wl,--enable-auto-import");
354 return new ExternalTask(argv, work_dir);