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, const FS::Path &sysroot):
27 input_suffixes.push_back(".o");
28 input_suffixes.push_back(".a");
30 processing_unit = COMPONENT;
33 build_info.sysroot = sysroot;
35 default_linker = new Linker(*this, "CC");
36 cxx_linker = new Linker(*this, "CXX");
39 GnuLinker::~GnuLinker()
41 delete default_linker;
45 Target *GnuLinker::create_target(const list<Target *> &sources, const string &arg)
48 throw invalid_argument("GnuLinker::create_target");
49 list<ObjectFile *> objs;
50 Linker *linker = default_linker;
51 for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
53 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
56 if(obj->get_tool()->get_tag()=="CXX")
60 throw invalid_argument("GnuLinker::create_target");
63 const Component &comp = *objs.front()->get_component();
67 SharedLibrary *shlib = new SharedLibrary(builder, comp, objs);
68 if(architecture->get_system()=="windows")
70 Tool &dlltool = builder.get_toolchain().get_tool("DLL");
71 dlltool.create_target(*shlib);
76 bin = new Executable(builder, comp, objs);
77 bin->set_tool(*linker);
81 Target *GnuLinker::create_install(Target &target) const
83 if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(&target))
85 Tool © = builder.get_toolchain().get_tool("CP");
86 InstalledFile *inst_tgt = dynamic_cast<InstalledFile *>(copy.create_target(target));
87 if(architecture->get_system()=="windows")
88 builder.get_build_graph().add_installed_target(*shlib->get_import_library());
91 const Pattern &pattern = architecture->get_shared_library_patterns().front();
92 string link_name = pattern.apply(shlib->get_libname());
93 if(link_name!=FS::basename(inst_tgt->get_path()))
94 inst_tgt->set_symlink(link_name);
102 void GnuLinker::do_prepare()
104 bool path_found = false;
105 const FS::Path &sysroot = build_info.sysroot;
107 Tool &compiler = builder.get_toolchain().get_tool("CC");
108 if(dynamic_cast<GnuCompiler *>(&compiler))
111 FileTarget *compiler_exe = compiler.get_executable();
114 ExternalTask::Arguments argv;
115 argv.push_back(compiler_exe->get_path().str());
116 argv.push_back("-Wl,--verbose");
117 argv.push_back("-nostdlib");
119 argv.push_back("--sysroot="+sysroot.str());
121 builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
124 string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
125 string::size_type start = 0;
126 while(start<output.size())
128 string::size_type search_dir = output.find("SEARCH_DIR(\"", start);
129 if(search_dir==string::npos)
133 string::size_type end = output.find("\");", search_dir);
134 if(end==string::npos)
138 if(!output.compare(search_dir, 2, "=/"))
147 path /= output.substr(search_dir, end-search_dir);
148 builder.get_logger().log("tools", format("Got %s system path: %s", tag, path));
149 system_path.push_back(path);
162 builder.get_logger().log("tools", format("No %s system path found, using defaults", tag));
164 system_path.push_back(sysroot/"usr/lib");
165 else if(architecture->is_native())
167 system_path.push_back("/lib");
168 system_path.push_back("/usr/lib");
169 if(architecture->match_name("pc-32-linux"))
171 system_path.push_back("/lib/i386-linux-gnu");
172 system_path.push_back("/usr/lib/i386-linux-gnu");
174 else if(architecture->match_name("pc-64-linux"))
176 system_path.push_back("/lib/x86_64-linux-gnu");
177 system_path.push_back("/usr/lib/x86_64-linux-gnu");
181 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/lib");
185 Task *GnuLinker::run(const Target &) const
187 throw logic_error("GnuLinker should not be run directly");
191 GnuLinker::Linker::Linker(GnuLinker &p, const string &ct):
195 if(compiler_tag=="CC")
196 set_command("gcc", true);
197 else if(compiler_tag=="CXX")
198 set_command("g++", true);
200 throw invalid_argument("GnuLinker::Linker::Linker");
203 string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
205 string result = FS::basename(executable->get_path());
207 if(binfo.libmode<=BuildInfo::STATIC)
213 if(!binfo.libs.empty())
216 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
221 void GnuLinker::Linker::do_prepare()
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 for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
274 argv.push_back("-L"+i->str());
276 argv.push_back("-s");
277 if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
278 argv.push_back("-pthread");
280 const Architecture &native_arch = builder.get_native_arch();
281 if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
282 argv.push_back(format("-m%d", architecture->get_bits()));
284 argv.push_back("-o");
285 argv.push_back(relative(bin.get_path(), work_dir).str());
287 for(BuildInfo::WordList::const_iterator i=binfo.keep_symbols.begin(); i!=binfo.keep_symbols.end(); ++i)
288 argv.push_back("-u"+*i);
290 bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
292 if(architecture->get_system()=="windows" && architecture->get_cross_prefix().find("mingw")!=string::npos)
293 argv.push_back("-lmingw32");
295 const Target::Dependencies &depends = target.get_dependencies();
296 for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
298 FileTarget *file = dynamic_cast<FileTarget *>(*i);
299 Target *tgt = (*i)->get_real_target();
301 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
302 argv.push_back(relative(obj->get_path(), work_dir).str());
303 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
304 argv.push_back((file?file:stlib)->get_path().str());
305 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
307 argv.push_back("-l"+shlib->get_libname());
308 static_link_ok = false;
310 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
312 shlib = imp->get_shared_library();
314 argv.push_back("-l"+shlib->get_libname());
316 argv.push_back((file?file:imp)->get_path().str());
317 static_link_ok = false;
321 for(BuildInfo::WordList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i)
322 if(i->size()>10 && !i->compare(i->size()-10, 10, ".framework"))
324 argv.push_back("-framework");
325 argv.push_back(i->substr(0, i->size()-10));
329 argv.push_back("-static");
330 else if(architecture->get_system()=="windows")
331 argv.push_back("-Wl,--enable-auto-import");
333 return new ExternalTask(argv, work_dir);