]> git.tdb.fi Git - builder.git/blob - source/gnulinker.cpp
Remove the -lmingw32 hack
[builder.git] / source / gnulinker.cpp
1 #include <stdexcept>
2 #include <vector>
3 #include <msp/fs/dir.h>
4 #include <msp/fs/utils.h>
5 #include <msp/strings/format.h>
6 #include <msp/strings/utils.h>
7 #include "builder.h"
8 #include "component.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"
20
21 using namespace std;
22 using namespace Msp;
23
24 GnuLinker::GnuLinker(Builder &b, const Architecture &a):
25         Tool(b, a, "LINK")
26 {
27         input_suffixes.push_back(".o");
28         input_suffixes.push_back(".a");
29
30         processing_unit = COMPONENT;
31
32         default_linker = new Linker(*this, "CC");
33         cxx_linker = new Linker(*this, "CXX");
34 }
35
36 GnuLinker::~GnuLinker()
37 {
38         delete default_linker;
39         delete cxx_linker;
40 }
41
42 Target *GnuLinker::create_target(const list<Target *> &sources, const string &arg)
43 {
44         if(sources.empty())
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)
49         {
50                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
51                 {
52                         objs.push_back(obj);
53                         if(obj->get_tool()->get_tag()=="CXX")
54                                 linker = cxx_linker;
55                 }
56                 else
57                         throw invalid_argument("GnuLinker::create_target");
58         }
59
60         const Component &comp = *objs.front()->get_component();
61         Binary *bin = 0;
62         if(arg=="shared")
63         {
64                 SharedLibrary *shlib = new SharedLibrary(builder, comp, objs);
65                 if(architecture->get_system()=="windows")
66                 {
67                         Tool &dlltool = builder.get_toolchain().get_tool("DLL");
68                         dlltool.create_target(*shlib);
69                 }
70                 bin = shlib;
71         }
72         else
73                 bin = new Executable(builder, comp, objs);
74         bin->set_tool(*linker);
75         return bin;
76 }
77
78 Target *GnuLinker::create_install(Target &target) const
79 {
80         if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(&target))
81         {
82                 Tool &copy = 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());
86                 else
87                 {
88                         const Pattern &pattern = architecture->get_shared_library_patterns().front();
89                         string link_name = pattern.apply(shlib->get_libname());
90                         if(link_name!=FS::basename(inst_tgt->get_path()))
91                                 inst_tgt->set_symlink(link_name);
92                 }
93                 return inst_tgt;
94         }
95         else
96                 return 0;
97 }
98
99 void GnuLinker::do_prepare()
100 {
101         bool path_found = false;
102         const FS::Path &sysroot = build_info.sysroot;
103
104         Tool &compiler = builder.get_toolchain().get_tool("CC");
105         if(dynamic_cast<GnuCompiler *>(&compiler))
106         {
107                 compiler.prepare();
108                 FileTarget *compiler_exe = compiler.get_executable();
109                 if(compiler_exe)
110                 {
111                         ExternalTask::Arguments argv;
112                         argv.push_back(compiler_exe->get_path().str());
113                         argv.push_back("-Wl,--verbose");
114                         argv.push_back("-nostdlib");
115                         if(!sysroot.empty())
116                                 argv.push_back("--sysroot="+sysroot.str());
117
118                         builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
119                         try
120                         {
121                                 string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
122                                 string::size_type start = 0;
123                                 while(start<output.size())
124                                 {
125                                         string::size_type search_dir = output.find("SEARCH_DIR(\"", start);
126                                         if(search_dir==string::npos)
127                                                 break;
128
129                                         search_dir += 12;
130                                         string::size_type end = output.find("\");", search_dir);
131                                         if(end==string::npos)
132                                                 break;
133
134                                         FS::Path path;
135                                         if(!output.compare(search_dir, 2, "=/"))
136                                         {
137                                                 search_dir += 2;
138                                                 if(sysroot.empty())
139                                                         path = "/";
140                                                 else
141                                                         path = sysroot;
142                                         }
143
144                                         path /= output.substr(search_dir, end-search_dir);
145                                         builder.get_logger().log("tools", format("Got %s system path: %s", tag, path));
146                                         system_path.push_back(path);
147                                         path_found = true;
148
149                                         start = end+3;
150                                 }
151                         }
152                         catch(...)
153                         { }
154                 }
155         }
156
157         if(!path_found)
158         {
159                 builder.get_logger().log("tools", format("No %s system path found, using defaults", tag));
160                 if(!sysroot.empty())
161                         system_path.push_back(sysroot/"usr/lib");
162                 else if(architecture->is_native())
163                 {
164                         system_path.push_back("/lib");
165                         system_path.push_back("/usr/lib");
166                         if(architecture->match_name("pc-32-linux"))
167                         {
168                                 system_path.push_back("/lib/i386-linux-gnu");
169                                 system_path.push_back("/usr/lib/i386-linux-gnu");
170                         }
171                         else if(architecture->match_name("pc-64-linux"))
172                         {
173                                 system_path.push_back("/lib/x86_64-linux-gnu");
174                                 system_path.push_back("/usr/lib/x86_64-linux-gnu");
175                         }
176                 }
177                 else
178                         system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/lib");
179         }
180 }
181
182 Task *GnuLinker::run(const Target &) const
183 {
184         throw logic_error("GnuLinker should not be run directly");
185 }
186
187
188 GnuLinker::Linker::Linker(GnuLinker &p, const string &ct):
189         SubTool(p),
190         compiler_tag(ct)
191 {
192         if(compiler_tag=="CC")
193                 set_command("gcc", true);
194         else if(compiler_tag=="CXX")
195                 set_command("g++", true);
196         else
197                 throw invalid_argument("GnuLinker::Linker::Linker");
198 }
199
200 string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
201 {
202         string result = FS::basename(executable->get_path());
203         result += ',';
204         if(binfo.libmode<=BuildInfo::STATIC)
205                 result += 't';
206         else
207                 result += 'd';
208         if(binfo.strip)
209                 result += 's';
210         if(!binfo.libs.empty())
211         {
212                 result += ",l";
213                 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
214         }
215         return result;
216 }
217
218 void GnuLinker::Linker::do_prepare()
219 {
220         parent.prepare();
221         build_info = parent.get_build_info();
222         system_path = parent.get_system_path();
223
224         Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
225         if(dynamic_cast<GnuCompiler *>(&compiler))
226         {
227                 compiler.prepare();
228                 executable = compiler.get_executable();
229         }
230 }
231
232 Task *GnuLinker::Linker::run(const Target &target) const
233 {
234         const Binary &bin = dynamic_cast<const Binary &>(target);
235
236         vector<string> argv;
237         argv.push_back(executable->get_path().str());
238
239         FS::Path work_dir = bin.get_component()->get_package().get_source_directory();
240
241         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
242         {
243                 argv.push_back("-shared");
244                 argv.push_back("-fPIC");
245                 if(architecture->get_system()!="windows" && !shlib->get_soname().empty())
246                 {
247                         if(architecture->get_system()=="darwin")
248                         {
249                                 argv.push_back("-install_name");
250                                 argv.push_back(shlib->get_soname());
251
252                                 const string &ver = shlib->get_package()->get_version();
253                                 const string &if_ver = shlib->get_package()->get_interface_version();
254                                 if(!ver.empty() && !if_ver.empty())
255                                 {
256                                         argv.push_back("-current_version");
257                                         argv.push_back(ver);
258                                         argv.push_back("-compatibility_version");
259                                         argv.push_back(if_ver);
260                                 }
261                         }
262                         else
263                                 argv.push_back("-Wl,-soname,"+shlib->get_soname());
264                 }
265         }
266
267         BuildInfo binfo;
268         target.collect_build_info(binfo);
269
270         const FS::Path &sysroot = binfo.sysroot;
271         if(!sysroot.empty())
272                 argv.push_back("--sysroot="+sysroot.str());
273
274         FS::Path lib_dir = builder.get_prefix()/"lib";
275         if(binfo.rpath_mode==BuildInfo::ABSOLUTE)
276                 argv.push_back("-Wl,-rpath,"+lib_dir.str());
277         else
278         {
279                 if(binfo.rpath_mode==BuildInfo::RELATIVE)
280                         argv.push_back("-Wl,-rpath,$ORIGIN/../lib");
281                 argv.push_back("-Wl,-rpath-link,"+lib_dir.str());
282         }
283
284         for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
285                 argv.push_back("-L"+i->str());
286         if(binfo.strip)
287                 argv.push_back("-s");
288         if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
289                 argv.push_back("-pthread");
290
291         const Architecture &native_arch = builder.get_native_arch();
292         if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
293                 argv.push_back(format("-m%d", architecture->get_bits()));
294
295         argv.push_back("-o");
296         argv.push_back(relative(bin.get_path(), work_dir).str());
297
298         for(BuildInfo::WordList::const_iterator i=binfo.keep_symbols.begin(); i!=binfo.keep_symbols.end(); ++i)
299                 argv.push_back("-u"+*i);
300
301         bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
302
303         const Target::Dependencies &depends = target.get_dependencies();
304         for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
305         {
306                 FileTarget *file = dynamic_cast<FileTarget *>(*i);
307                 Target *tgt = (*i)->get_real_target();
308
309                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
310                         argv.push_back(relative(obj->get_path(), work_dir).str());
311                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
312                         argv.push_back((file?file:stlib)->get_path().str());
313                 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
314                 {
315                         argv.push_back("-l"+shlib->get_libname());
316                         static_link_ok = false;
317                 }
318                 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
319                 {
320                         shlib = imp->get_shared_library();
321                         if(shlib)
322                                 argv.push_back("-l"+shlib->get_libname());
323                         else
324                                 argv.push_back((file?file:imp)->get_path().str());
325                         static_link_ok = false;
326                 }
327         }
328
329         for(BuildInfo::WordList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i)
330                 if(i->size()>10 && !i->compare(i->size()-10, 10, ".framework"))
331                 {
332                         argv.push_back("-framework");
333                         argv.push_back(i->substr(0, i->size()-10));
334                 }
335
336         if(static_link_ok)
337                 argv.push_back("-static");
338         else
339         {
340                 if(compiler_tag=="CXX")
341                 {
342                         BuildInfo::LibModeMap::const_iterator i = binfo.libmodes.find("stdc++");
343                         if(i!=binfo.libmodes.end() && i->second<=BuildInfo::STATIC)
344                                 argv.push_back("-static-libstdc++");
345                 }
346
347                 if(architecture->get_system()=="windows")
348                         argv.push_back("-Wl,--enable-auto-import");
349         }
350
351         return new ExternalTask(argv, work_dir);
352 }