]> git.tdb.fi Git - builder.git/blob - source/gnulinker.cpp
31440976e917d31a43c8d9d701ff057a0573a740
[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", "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", "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", "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
179 GnuLinker::Linker::Linker(GnuLinker &p, const string &ct):
180         SubTool(p),
181         compiler_tag(ct)
182 {
183         if(compiler_tag=="CC")
184                 set_command("gcc", true);
185         else if(compiler_tag=="CXX")
186                 set_command("g++", true);
187         else
188                 throw invalid_argument("GnuLinker::Linker::Linker");
189
190         set_run(_run);
191 }
192
193 string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
194 {
195         string result = Tool::create_build_signature(binfo);
196         result += ',';
197         if(binfo.libmode<=BuildInfo::STATIC)
198                 result += 't';
199         else
200                 result += 'd';
201         if(binfo.strip)
202                 result += 's';
203         if(!binfo.libs.empty())
204         {
205                 result += ",l";
206                 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
207         }
208         return result;
209 }
210
211 void GnuLinker::Linker::do_prepare()
212 {
213         parent.prepare();
214         build_info = parent.get_build_info();
215         system_path = parent.get_system_path();
216
217         Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
218         if(dynamic_cast<GnuCompiler *>(&compiler))
219         {
220                 compiler.prepare();
221                 executable = compiler.get_executable();
222         }
223 }
224
225 Task *GnuLinker::Linker::_run(const Binary &bin)
226 {
227         const Linker &tool = dynamic_cast<const Linker &>(*bin.get_tool());
228         const Builder &builder = tool.get_builder();
229         const Architecture &arch = *tool.get_architecture();
230
231         ExternalTask::Arguments argv;
232         argv.push_back(tool.get_executable()->get_path().str());
233
234         FS::Path work_dir = bin.get_component()->get_package().get_source_directory();
235
236         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
237         {
238                 argv.push_back("-shared");
239                 argv.push_back("-fPIC");
240                 if(arch.get_system()!="windows" && !shlib->get_soname().empty())
241                 {
242                         if(arch.get_system()=="darwin")
243                         {
244                                 argv.push_back("-install_name");
245                                 argv.push_back(shlib->get_soname());
246
247                                 const string &ver = shlib->get_package()->get_version();
248                                 const string &if_ver = shlib->get_package()->get_interface_version();
249                                 if(!ver.empty() && !if_ver.empty())
250                                 {
251                                         argv.push_back("-current_version");
252                                         argv.push_back(ver);
253                                         argv.push_back("-compatibility_version");
254                                         argv.push_back(if_ver);
255                                 }
256                         }
257                         else
258                                 argv.push_back("-Wl,-soname,"+shlib->get_soname());
259                 }
260         }
261
262         BuildInfo binfo;
263         bin.collect_build_info(binfo);
264
265         const FS::Path &sysroot = binfo.sysroot;
266         if(!sysroot.empty())
267                 argv.push_back("--sysroot="+sysroot.str());
268
269         FS::Path lib_dir = builder.get_prefix()/"lib";
270         if(binfo.rpath_mode==BuildInfo::ABSOLUTE)
271                 argv.push_back("-Wl,-rpath,"+lib_dir.str());
272         else
273         {
274                 if(binfo.rpath_mode==BuildInfo::RELATIVE)
275                         argv.push_back("-Wl,-rpath,$ORIGIN/../lib");
276                 argv.push_back("-Wl,-rpath-link,"+lib_dir.str());
277         }
278
279         for(const FS::Path &p: binfo.libpath)
280                 argv.push_back("-L"+p.str());
281         if(binfo.strip)
282                 argv.push_back("-s");
283         if(binfo.threads && arch.get_system()!="windows" && arch.get_system()!="darwin")
284                 argv.push_back("-pthread");
285
286         const Architecture &native_arch = builder.get_native_arch();
287         if(arch.is_native() && arch.get_bits()!=native_arch.get_bits())
288                 argv.push_back(format("-m%d", arch.get_bits()));
289
290         argv.push_back("-o");
291         argv.push_back(relative(bin.get_path(), work_dir).str());
292
293         for(const string &s: binfo.keep_symbols)
294                 argv.push_back("-u"+s);
295
296         bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
297
298         for(Target *d: bin.get_dependencies())
299         {
300                 FileTarget *file = dynamic_cast<FileTarget *>(d);
301                 Target *tgt = d->get_real_target();
302
303                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
304                         argv.push_back(relative(obj->get_path(), work_dir).str());
305                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
306                         argv.push_back((file?file:stlib)->get_path().str());
307                 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
308                 {
309                         argv.push_back("-l"+shlib->get_libname());
310                         static_link_ok = false;
311                 }
312                 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
313                 {
314                         shlib = imp->get_shared_library();
315                         if(shlib)
316                                 argv.push_back("-l"+shlib->get_libname());
317                         else
318                                 argv.push_back((file?file:imp)->get_path().str());
319                         static_link_ok = false;
320                 }
321         }
322
323         for(const string &l: binfo.libs)
324                 if(l.size()>10 && !l.compare(l.size()-10, 10, ".framework"))
325                 {
326                         argv.push_back("-framework");
327                         argv.push_back(l.substr(0, l.size()-10));
328                 }
329
330         if(static_link_ok)
331                 argv.push_back("-static");
332         else
333         {
334                 if(tool.compiler_tag=="CXX")
335                 {
336                         auto i = binfo.libmodes.find("stdc++");
337                         if(i!=binfo.libmodes.end() && i->second<=BuildInfo::STATIC)
338                                 argv.push_back("-static-libstdc++");
339                 }
340
341                 if(arch.get_system()=="windows")
342                         argv.push_back("-Wl,--enable-auto-import");
343         }
344
345         return new ExternalTask(argv, work_dir);
346 }