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