]> git.tdb.fi Git - builder.git/blob - source/gnulinker.cpp
Use dynamic_cast to reference when incorrect type is not acceptable
[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(Target *s: sources)
49         {
50                 ObjectFile &obj = dynamic_cast<ObjectFile &>(*s);
51                 objs.push_back(&obj);
52                 if(obj.get_tool()->get_tag()=="CXX")
53                         linker = cxx_linker;
54         }
55
56         const Component &comp = *objs.front()->get_component();
57         Binary *bin = 0;
58         if(arg=="shared")
59         {
60                 SharedLibrary *shlib = new SharedLibrary(builder, comp, objs);
61                 if(architecture->get_system()=="windows")
62                 {
63                         Tool &dlltool = builder.get_toolchain().get_tool("DLL");
64                         dlltool.create_target(*shlib);
65                 }
66                 bin = shlib;
67         }
68         else
69                 bin = new Executable(builder, comp, objs);
70         bin->set_tool(*linker);
71         return bin;
72 }
73
74 Target *GnuLinker::create_install(Target &target) const
75 {
76         if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(&target))
77         {
78                 Tool &copy = builder.get_toolchain().get_tool("CP");
79                 InstalledFile *inst_tgt = dynamic_cast<InstalledFile *>(copy.create_target(target));
80                 if(architecture->get_system()=="windows")
81                         builder.get_build_graph().add_installed_target(*shlib->get_import_library());
82                 else
83                 {
84                         string link_name = architecture->create_filename<SharedLibrary>(shlib->get_libname());
85                         if(link_name!=FS::basename(inst_tgt->get_path()))
86                                 inst_tgt->set_symlink(link_name);
87                 }
88                 return inst_tgt;
89         }
90         else
91                 return 0;
92 }
93
94 void GnuLinker::do_prepare()
95 {
96         bool path_found = false;
97         const FS::Path &sysroot = build_info.sysroot;
98
99         Tool &compiler = builder.get_toolchain().get_tool("CC");
100         if(dynamic_cast<GnuCompiler *>(&compiler))
101         {
102                 compiler.prepare();
103                 FileTarget *compiler_exe = compiler.get_executable();
104                 if(compiler_exe)
105                 {
106                         ExternalTask::Arguments argv;
107                         argv.push_back(compiler_exe->get_path().str());
108                         argv.push_back("-Wl,--verbose");
109                         argv.push_back("-nostdlib");
110                         if(!sysroot.empty())
111                                 argv.push_back("--sysroot="+sysroot.str());
112
113                         builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
114                         try
115                         {
116                                 string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
117                                 string::size_type start = 0;
118                                 while(start<output.size())
119                                 {
120                                         string::size_type search_dir = output.find("SEARCH_DIR(\"", start);
121                                         if(search_dir==string::npos)
122                                                 break;
123
124                                         search_dir += 12;
125                                         string::size_type end = output.find("\");", search_dir);
126                                         if(end==string::npos)
127                                                 break;
128
129                                         FS::Path path;
130                                         if(!output.compare(search_dir, 2, "=/"))
131                                         {
132                                                 search_dir += 2;
133                                                 if(sysroot.empty())
134                                                         path = "/";
135                                                 else
136                                                         path = sysroot;
137                                         }
138
139                                         path /= output.substr(search_dir, end-search_dir);
140                                         builder.get_logger().log("tools", format("Got %s system path: %s", tag, path));
141                                         system_path.push_back(path);
142                                         path_found = true;
143
144                                         start = end+3;
145                                 }
146                         }
147                         catch(...)
148                         { }
149                 }
150         }
151
152         if(!path_found)
153         {
154                 builder.get_logger().log("tools", format("No %s system path found, using defaults", tag));
155                 if(!sysroot.empty())
156                         system_path.push_back(sysroot/"usr/lib");
157                 else if(architecture->is_native())
158                 {
159                         system_path.push_back("/lib");
160                         system_path.push_back("/usr/lib");
161                         if(architecture->match_name("pc-32-linux"))
162                         {
163                                 system_path.push_back("/lib/i386-linux-gnu");
164                                 system_path.push_back("/usr/lib/i386-linux-gnu");
165                         }
166                         else if(architecture->match_name("pc-64-linux"))
167                         {
168                                 system_path.push_back("/lib/x86_64-linux-gnu");
169                                 system_path.push_back("/usr/lib/x86_64-linux-gnu");
170                         }
171                 }
172                 else
173                         system_path.push_back(format("/usr/%s/lib", architecture->get_cross_prefix()));
174         }
175 }
176
177 Task *GnuLinker::run(const Target &) const
178 {
179         throw logic_error("GnuLinker should not be run directly");
180 }
181
182
183 GnuLinker::Linker::Linker(GnuLinker &p, const string &ct):
184         SubTool(p),
185         compiler_tag(ct)
186 {
187         if(compiler_tag=="CC")
188                 set_command("gcc", true);
189         else if(compiler_tag=="CXX")
190                 set_command("g++", true);
191         else
192                 throw invalid_argument("GnuLinker::Linker::Linker");
193 }
194
195 string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
196 {
197         string result = Tool::create_build_signature(binfo);
198         result += ',';
199         if(binfo.libmode<=BuildInfo::STATIC)
200                 result += 't';
201         else
202                 result += 'd';
203         if(binfo.strip)
204                 result += 's';
205         if(!binfo.libs.empty())
206         {
207                 result += ",l";
208                 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
209         }
210         return result;
211 }
212
213 void GnuLinker::Linker::do_prepare()
214 {
215         parent.prepare();
216         build_info = parent.get_build_info();
217         system_path = parent.get_system_path();
218
219         Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
220         if(dynamic_cast<GnuCompiler *>(&compiler))
221         {
222                 compiler.prepare();
223                 executable = compiler.get_executable();
224         }
225 }
226
227 Task *GnuLinker::Linker::run(const Target &target) const
228 {
229         const Binary &bin = dynamic_cast<const Binary &>(target);
230
231         ExternalTask::Arguments argv;
232         argv.push_back(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(architecture->get_system()!="windows" && !shlib->get_soname().empty())
241                 {
242                         if(architecture->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         target.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 && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
284                 argv.push_back("-pthread");
285
286         const Architecture &native_arch = builder.get_native_arch();
287         if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
288                 argv.push_back(format("-m%d", architecture->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: target.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(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(architecture->get_system()=="windows")
342                         argv.push_back("-Wl,--enable-auto-import");
343         }
344
345         return new ExternalTask(argv, work_dir);
346 }