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 vector<Target *> &sources, const string &arg)
45 throw invalid_argument("GnuLinker::create_target");
46 vector<ObjectFile *> objs;
47 objs.reserve(sources.size());
48 Linker *linker = default_linker;
49 for(Target *s: sources)
51 ObjectFile &obj = dynamic_cast<ObjectFile &>(*s);
53 if(obj.get_tool()->get_tag()=="CXX")
57 const Component &comp = *objs.front()->get_component();
61 SharedLibrary *shlib = new SharedLibrary(builder, comp, objs);
62 if(architecture->get_system()=="windows")
64 Tool &dlltool = builder.get_toolchain().get_tool("DLL");
65 dlltool.create_target(*shlib);
70 bin = new Executable(builder, comp, objs);
71 bin->set_tool(*linker);
75 Target *GnuLinker::create_install(Target &target) const
77 if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(&target))
79 Tool © = builder.get_toolchain().get_tool("CP");
80 InstalledFile *inst_tgt = dynamic_cast<InstalledFile *>(copy.create_target(target));
81 if(architecture->get_system()=="windows")
82 builder.get_build_graph().add_installed_target(*shlib->get_import_library());
85 string link_name = architecture->create_filename<SharedLibrary>(shlib->get_libname());
86 if(link_name!=FS::basename(inst_tgt->get_path()))
87 inst_tgt->set_symlink(link_name);
95 void GnuLinker::do_prepare()
97 bool path_found = false;
98 const FS::Path &sysroot = build_info.sysroot;
100 Tool &compiler = builder.get_toolchain().get_tool("CC");
101 if(dynamic_cast<GnuCompiler *>(&compiler))
104 FileTarget *compiler_exe = compiler.get_executable();
107 ExternalTask::Arguments argv;
108 argv.push_back(compiler_exe->get_path().str());
109 argv.push_back("-Wl,--verbose");
110 argv.push_back("-nostdlib");
112 argv.push_back("--sysroot="+sysroot.str());
114 builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
117 string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
118 string::size_type start = 0;
119 while(start<output.size())
121 string::size_type search_dir = output.find("SEARCH_DIR(\"", start);
122 if(search_dir==string::npos)
126 string::size_type end = output.find("\");", search_dir);
127 if(end==string::npos)
131 if(!output.compare(search_dir, 2, "=/"))
140 path /= output.substr(search_dir, end-search_dir);
141 builder.get_logger().log("tools", format("Got %s system path: %s", tag, path));
142 system_path.push_back(path);
155 builder.get_logger().log("tools", format("No %s system path found, using defaults", tag));
157 system_path.push_back(sysroot/"usr/lib");
158 else if(architecture->is_native())
160 system_path.push_back("/lib");
161 system_path.push_back("/usr/lib");
162 if(architecture->match_name("pc-32-linux"))
164 system_path.push_back("/lib/i386-linux-gnu");
165 system_path.push_back("/usr/lib/i386-linux-gnu");
167 else if(architecture->match_name("pc-64-linux"))
169 system_path.push_back("/lib/x86_64-linux-gnu");
170 system_path.push_back("/usr/lib/x86_64-linux-gnu");
174 system_path.push_back(format("/usr/%s/lib", architecture->get_cross_prefix()));
178 Task *GnuLinker::run(const Target &) const
180 throw logic_error("GnuLinker should not be run directly");
184 GnuLinker::Linker::Linker(GnuLinker &p, const string &ct):
188 if(compiler_tag=="CC")
189 set_command("gcc", true);
190 else if(compiler_tag=="CXX")
191 set_command("g++", true);
193 throw invalid_argument("GnuLinker::Linker::Linker");
196 string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
198 string result = Tool::create_build_signature(binfo);
200 if(binfo.libmode<=BuildInfo::STATIC)
206 if(!binfo.libs.empty())
209 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
214 void GnuLinker::Linker::do_prepare()
217 build_info = parent.get_build_info();
218 system_path = parent.get_system_path();
220 Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
221 if(dynamic_cast<GnuCompiler *>(&compiler))
224 executable = compiler.get_executable();
228 Task *GnuLinker::Linker::run(const Target &target) const
230 const Binary &bin = dynamic_cast<const Binary &>(target);
232 ExternalTask::Arguments argv;
233 argv.push_back(executable->get_path().str());
235 FS::Path work_dir = bin.get_component()->get_package().get_source_directory();
237 if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
239 argv.push_back("-shared");
240 argv.push_back("-fPIC");
241 if(architecture->get_system()!="windows" && !shlib->get_soname().empty())
243 if(architecture->get_system()=="darwin")
245 argv.push_back("-install_name");
246 argv.push_back(shlib->get_soname());
248 const string &ver = shlib->get_package()->get_version();
249 const string &if_ver = shlib->get_package()->get_interface_version();
250 if(!ver.empty() && !if_ver.empty())
252 argv.push_back("-current_version");
254 argv.push_back("-compatibility_version");
255 argv.push_back(if_ver);
259 argv.push_back("-Wl,-soname,"+shlib->get_soname());
264 target.collect_build_info(binfo);
266 const FS::Path &sysroot = binfo.sysroot;
268 argv.push_back("--sysroot="+sysroot.str());
270 FS::Path lib_dir = builder.get_prefix()/"lib";
271 if(binfo.rpath_mode==BuildInfo::ABSOLUTE)
272 argv.push_back("-Wl,-rpath,"+lib_dir.str());
275 if(binfo.rpath_mode==BuildInfo::RELATIVE)
276 argv.push_back("-Wl,-rpath,$ORIGIN/../lib");
277 argv.push_back("-Wl,-rpath-link,"+lib_dir.str());
280 for(const FS::Path &p: binfo.libpath)
281 argv.push_back("-L"+p.str());
283 argv.push_back("-s");
284 if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
285 argv.push_back("-pthread");
287 const Architecture &native_arch = builder.get_native_arch();
288 if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
289 argv.push_back(format("-m%d", architecture->get_bits()));
291 argv.push_back("-o");
292 argv.push_back(relative(bin.get_path(), work_dir).str());
294 for(const string &s: binfo.keep_symbols)
295 argv.push_back("-u"+s);
297 bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
299 for(Target *d: target.get_dependencies())
301 FileTarget *file = dynamic_cast<FileTarget *>(d);
302 Target *tgt = d->get_real_target();
304 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
305 argv.push_back(relative(obj->get_path(), work_dir).str());
306 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
307 argv.push_back((file?file:stlib)->get_path().str());
308 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
310 argv.push_back("-l"+shlib->get_libname());
311 static_link_ok = false;
313 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
315 shlib = imp->get_shared_library();
317 argv.push_back("-l"+shlib->get_libname());
319 argv.push_back((file?file:imp)->get_path().str());
320 static_link_ok = false;
324 for(const string &l: binfo.libs)
325 if(l.size()>10 && !l.compare(l.size()-10, 10, ".framework"))
327 argv.push_back("-framework");
328 argv.push_back(l.substr(0, l.size()-10));
332 argv.push_back("-static");
335 if(compiler_tag=="CXX")
337 auto i = binfo.libmodes.find("stdc++");
338 if(i!=binfo.libmodes.end() && i->second<=BuildInfo::STATIC)
339 argv.push_back("-static-libstdc++");
342 if(architecture->get_system()=="windows")
343 argv.push_back("-Wl,--enable-auto-import");
346 return new ExternalTask(argv, work_dir);