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 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(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
284 argv.push_back("-L"+i->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(BuildInfo::WordList::const_iterator i=binfo.keep_symbols.begin(); i!=binfo.keep_symbols.end(); ++i)
298 argv.push_back("-u"+*i);
300 bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
302 const Target::Dependencies &depends = target.get_dependencies();
303 for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
305 FileTarget *file = dynamic_cast<FileTarget *>(*i);
306 Target *tgt = (*i)->get_real_target();
308 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
309 argv.push_back(relative(obj->get_path(), work_dir).str());
310 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
311 argv.push_back((file?file:stlib)->get_path().str());
312 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
314 argv.push_back("-l"+shlib->get_libname());
315 static_link_ok = false;
317 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
319 shlib = imp->get_shared_library();
321 argv.push_back("-l"+shlib->get_libname());
323 argv.push_back((file?file:imp)->get_path().str());
324 static_link_ok = false;
328 for(BuildInfo::WordList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i)
329 if(i->size()>10 && !i->compare(i->size()-10, 10, ".framework"))
331 argv.push_back("-framework");
332 argv.push_back(i->substr(0, i->size()-10));
336 argv.push_back("-static");
339 if(compiler_tag=="CXX")
341 BuildInfo::LibModeMap::const_iterator i = binfo.libmodes.find("stdc++");
342 if(i!=binfo.libmodes.end() && i->second<=BuildInfo::STATIC)
343 argv.push_back("-static-libstdc++");
346 if(architecture->get_system()=="windows")
347 argv.push_back("-Wl,--enable-auto-import");
350 return new ExternalTask(argv, work_dir);