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", "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", "Got %s system path: %s", tag, path);
142 system_path.push_back(path);
155 builder.get_logger().log("tools", "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()));
179 GnuLinker::Linker::Linker(GnuLinker &p, const string &ct):
183 if(compiler_tag=="CC")
184 set_command("gcc", true);
185 else if(compiler_tag=="CXX")
186 set_command("g++", true);
188 throw invalid_argument("GnuLinker::Linker::Linker");
193 string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
195 string result = Tool::create_build_signature(binfo);
197 if(binfo.libmode<=BuildInfo::STATIC)
203 if(!binfo.libs.empty())
206 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
211 void GnuLinker::Linker::do_prepare()
214 build_info = parent.get_build_info();
215 system_path = parent.get_system_path();
217 Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
218 if(dynamic_cast<GnuCompiler *>(&compiler))
221 executable = compiler.get_executable();
225 Task *GnuLinker::Linker::_run(const Binary &bin)
227 const Linker &tool = dynamic_cast<const Linker &>(*bin.get_tool());
228 const Builder &builder = tool.get_builder();
229 const Architecture &arch = *tool.get_architecture();
231 ExternalTask::Arguments argv;
232 argv.push_back(tool.get_executable()->get_path().str());
234 FS::Path work_dir = bin.get_component()->get_package().get_source_directory();
236 if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
238 argv.push_back("-shared");
239 argv.push_back("-fPIC");
240 if(arch.get_system()!="windows" && !shlib->get_soname().empty())
242 if(arch.get_system()=="darwin")
244 argv.push_back("-install_name");
245 argv.push_back(shlib->get_soname());
247 const string &ver = shlib->get_package()->get_version();
248 const string &if_ver = shlib->get_package()->get_interface_version();
249 if(!ver.empty() && !if_ver.empty())
251 argv.push_back("-current_version");
253 argv.push_back("-compatibility_version");
254 argv.push_back(if_ver);
258 argv.push_back("-Wl,-soname,"+shlib->get_soname());
263 bin.collect_build_info(binfo);
265 const FS::Path &sysroot = binfo.sysroot;
267 argv.push_back("--sysroot="+sysroot.str());
269 FS::Path lib_dir = builder.get_prefix()/"lib";
270 if(binfo.rpath_mode==BuildInfo::ABSOLUTE)
271 argv.push_back("-Wl,-rpath,"+lib_dir.str());
274 if(binfo.rpath_mode==BuildInfo::RELATIVE)
275 argv.push_back("-Wl,-rpath,$ORIGIN/../lib");
276 argv.push_back("-Wl,-rpath-link,"+lib_dir.str());
279 for(const FS::Path &p: binfo.libpath)
280 argv.push_back("-L"+p.str());
282 argv.push_back("-s");
283 if(binfo.threads && arch.get_system()!="windows" && arch.get_system()!="darwin")
284 argv.push_back("-pthread");
286 const Architecture &native_arch = builder.get_native_arch();
287 if(arch.is_native() && arch.get_bits()!=native_arch.get_bits())
288 argv.push_back(format("-m%d", arch.get_bits()));
290 argv.push_back("-o");
291 argv.push_back(relative(bin.get_path(), work_dir).str());
293 for(const string &s: binfo.keep_symbols)
294 argv.push_back("-u"+s);
296 bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
298 for(Target *d: bin.get_dependencies())
300 FileTarget *file = dynamic_cast<FileTarget *>(d);
301 Target *tgt = d->get_real_target();
303 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
304 argv.push_back(relative(obj->get_path(), work_dir).str());
305 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
306 argv.push_back((file?file:stlib)->get_path().str());
307 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
309 argv.push_back("-l"+shlib->get_libname());
310 static_link_ok = false;
312 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
314 shlib = imp->get_shared_library();
316 argv.push_back("-l"+shlib->get_libname());
318 argv.push_back((file?file:imp)->get_path().str());
319 static_link_ok = false;
323 for(const string &l: binfo.libs)
324 if(l.size()>10 && !l.compare(l.size()-10, 10, ".framework"))
326 argv.push_back("-framework");
327 argv.push_back(l.substr(0, l.size()-10));
331 argv.push_back("-static");
334 if(tool.compiler_tag=="CXX")
336 auto i = binfo.libmodes.find("stdc++");
337 if(i!=binfo.libmodes.end() && i->second<=BuildInfo::STATIC)
338 argv.push_back("-static-libstdc++");
341 if(arch.get_system()=="windows")
342 argv.push_back("-Wl,--enable-auto-import");
345 return new ExternalTask(argv, work_dir);