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