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(Target *s: sources)
50 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(s))
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 string link_name = architecture->create_filename<SharedLibrary>(shlib->get_libname());
89 if(link_name!=FS::basename(inst_tgt->get_path()))
90 inst_tgt->set_symlink(link_name);
98 void GnuLinker::do_prepare()
100 bool path_found = false;
101 const FS::Path &sysroot = build_info.sysroot;
103 Tool &compiler = builder.get_toolchain().get_tool("CC");
104 if(dynamic_cast<GnuCompiler *>(&compiler))
107 FileTarget *compiler_exe = compiler.get_executable();
110 ExternalTask::Arguments argv;
111 argv.push_back(compiler_exe->get_path().str());
112 argv.push_back("-Wl,--verbose");
113 argv.push_back("-nostdlib");
115 argv.push_back("--sysroot="+sysroot.str());
117 builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
120 string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
121 string::size_type start = 0;
122 while(start<output.size())
124 string::size_type search_dir = output.find("SEARCH_DIR(\"", start);
125 if(search_dir==string::npos)
129 string::size_type end = output.find("\");", search_dir);
130 if(end==string::npos)
134 if(!output.compare(search_dir, 2, "=/"))
143 path /= output.substr(search_dir, end-search_dir);
144 builder.get_logger().log("tools", format("Got %s system path: %s", tag, path));
145 system_path.push_back(path);
158 builder.get_logger().log("tools", format("No %s system path found, using defaults", tag));
160 system_path.push_back(sysroot/"usr/lib");
161 else if(architecture->is_native())
163 system_path.push_back("/lib");
164 system_path.push_back("/usr/lib");
165 if(architecture->match_name("pc-32-linux"))
167 system_path.push_back("/lib/i386-linux-gnu");
168 system_path.push_back("/usr/lib/i386-linux-gnu");
170 else if(architecture->match_name("pc-64-linux"))
172 system_path.push_back("/lib/x86_64-linux-gnu");
173 system_path.push_back("/usr/lib/x86_64-linux-gnu");
177 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/lib");
181 Task *GnuLinker::run(const Target &) const
183 throw logic_error("GnuLinker should not be run directly");
187 GnuLinker::Linker::Linker(GnuLinker &p, const string &ct):
191 if(compiler_tag=="CC")
192 set_command("gcc", true);
193 else if(compiler_tag=="CXX")
194 set_command("g++", true);
196 throw invalid_argument("GnuLinker::Linker::Linker");
199 string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
201 string result = FS::basename(executable->get_path());
203 if(binfo.libmode<=BuildInfo::STATIC)
209 if(!binfo.libs.empty())
212 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
217 void GnuLinker::Linker::do_prepare()
220 build_info = parent.get_build_info();
221 system_path = parent.get_system_path();
223 Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
224 if(dynamic_cast<GnuCompiler *>(&compiler))
227 executable = compiler.get_executable();
231 Task *GnuLinker::Linker::run(const Target &target) const
233 const Binary &bin = dynamic_cast<const Binary &>(target);
236 argv.push_back(executable->get_path().str());
238 FS::Path work_dir = bin.get_component()->get_package().get_source_directory();
240 if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
242 argv.push_back("-shared");
243 argv.push_back("-fPIC");
244 if(architecture->get_system()!="windows" && !shlib->get_soname().empty())
246 if(architecture->get_system()=="darwin")
248 argv.push_back("-install_name");
249 argv.push_back(shlib->get_soname());
251 const string &ver = shlib->get_package()->get_version();
252 const string &if_ver = shlib->get_package()->get_interface_version();
253 if(!ver.empty() && !if_ver.empty())
255 argv.push_back("-current_version");
257 argv.push_back("-compatibility_version");
258 argv.push_back(if_ver);
262 argv.push_back("-Wl,-soname,"+shlib->get_soname());
267 target.collect_build_info(binfo);
269 const FS::Path &sysroot = binfo.sysroot;
271 argv.push_back("--sysroot="+sysroot.str());
273 FS::Path lib_dir = builder.get_prefix()/"lib";
274 if(binfo.rpath_mode==BuildInfo::ABSOLUTE)
275 argv.push_back("-Wl,-rpath,"+lib_dir.str());
278 if(binfo.rpath_mode==BuildInfo::RELATIVE)
279 argv.push_back("-Wl,-rpath,$ORIGIN/../lib");
280 argv.push_back("-Wl,-rpath-link,"+lib_dir.str());
283 for(const FS::Path &p: binfo.libpath)
284 argv.push_back("-L"+p.str());
286 argv.push_back("-s");
287 if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
288 argv.push_back("-pthread");
290 const Architecture &native_arch = builder.get_native_arch();
291 if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
292 argv.push_back(format("-m%d", architecture->get_bits()));
294 argv.push_back("-o");
295 argv.push_back(relative(bin.get_path(), work_dir).str());
297 for(const string &s: binfo.keep_symbols)
298 argv.push_back("-u"+s);
300 bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
302 for(Target *d: target.get_dependencies())
304 FileTarget *file = dynamic_cast<FileTarget *>(d);
305 Target *tgt = d->get_real_target();
307 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
308 argv.push_back(relative(obj->get_path(), work_dir).str());
309 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
310 argv.push_back((file?file:stlib)->get_path().str());
311 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
313 argv.push_back("-l"+shlib->get_libname());
314 static_link_ok = false;
316 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
318 shlib = imp->get_shared_library();
320 argv.push_back("-l"+shlib->get_libname());
322 argv.push_back((file?file:imp)->get_path().str());
323 static_link_ok = false;
327 for(const string &l: binfo.libs)
328 if(l.size()>10 && !l.compare(l.size()-10, 10, ".framework"))
330 argv.push_back("-framework");
331 argv.push_back(l.substr(0, l.size()-10));
335 argv.push_back("-static");
338 if(compiler_tag=="CXX")
340 BuildInfo::LibModeMap::const_iterator i = binfo.libmodes.find("stdc++");
341 if(i!=binfo.libmodes.end() && i->second<=BuildInfo::STATIC)
342 argv.push_back("-static-libstdc++");
345 if(architecture->get_system()=="windows")
346 argv.push_back("-Wl,--enable-auto-import");
349 return new ExternalTask(argv, work_dir);