]> git.tdb.fi Git - builder.git/blob - source/gnulinker.cpp
adc3efcfcf6ad72d933b8e401836cbd935e34091
[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, const FS::Path &sysroot):
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         if(!sysroot.empty())
33                 build_info.sysroot = sysroot;
34
35         default_linker = new Linker(*this, "CC");
36         cxx_linker = new Linker(*this, "CXX");
37 }
38
39 GnuLinker::~GnuLinker()
40 {
41         delete default_linker;
42         delete cxx_linker;
43 }
44
45 Target *GnuLinker::create_target(const list<Target *> &sources, const string &arg)
46 {
47         if(sources.empty())
48                 throw invalid_argument("GnuLinker::create_target");
49         list<ObjectFile *> objs;
50         Linker *linker = default_linker;
51         for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
52         {
53                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
54                 {
55                         objs.push_back(obj);
56                         if(obj->get_tool()->get_tag()=="CXX")
57                                 linker = cxx_linker;
58                 }
59                 else
60                         throw invalid_argument("GnuLinker::create_target");
61         }
62
63         const Component &comp = *objs.front()->get_component();
64         Binary *bin = 0;
65         if(arg=="shared")
66         {
67                 SharedLibrary *shlib = new SharedLibrary(builder, comp, objs);
68                 if(architecture->get_system()=="windows")
69                 {
70                         Tool &dlltool = builder.get_toolchain().get_tool("DLL");
71                         dlltool.create_target(*shlib);
72                 }
73                 bin = shlib;
74         }
75         else
76                 bin = new Executable(builder, comp, objs);
77         bin->set_tool(*linker);
78         return bin;
79 }
80
81 Target *GnuLinker::create_install(Target &target) const
82 {
83         if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(&target))
84         {
85                 Tool &copy = builder.get_toolchain().get_tool("CP");
86                 InstalledFile *inst_tgt = dynamic_cast<InstalledFile *>(copy.create_target(target));
87                 if(architecture->get_system()=="windows")
88                         builder.get_build_graph().add_installed_target(*shlib->get_import_library());
89                 else
90                 {
91                         const Pattern &pattern = architecture->get_shared_library_patterns().front();
92                         string link_name = pattern.apply(shlib->get_libname());
93                         if(link_name!=FS::basename(inst_tgt->get_path()))
94                                 inst_tgt->set_symlink(link_name);
95                 }
96                 return inst_tgt;
97         }
98         else
99                 return 0;
100 }
101
102 void GnuLinker::do_prepare()
103 {
104         bool path_found = false;
105         const FS::Path &sysroot = build_info.sysroot;
106
107         Tool &compiler = builder.get_toolchain().get_tool("CC");
108         if(dynamic_cast<GnuCompiler *>(&compiler))
109         {
110                 compiler.prepare();
111                 FileTarget *compiler_exe = compiler.get_executable();
112                 if(compiler_exe)
113                 {
114                         ExternalTask::Arguments argv;
115                         argv.push_back(compiler_exe->get_path().str());
116                         argv.push_back("-Wl,--verbose");
117                         argv.push_back("-nostdlib");
118                         if(!sysroot.empty())
119                                 argv.push_back("--sysroot="+sysroot.str());
120
121                         builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
122                         try
123                         {
124                                 string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
125                                 string::size_type start = 0;
126                                 while(start<output.size())
127                                 {
128                                         string::size_type search_dir = output.find("SEARCH_DIR(\"", start);
129                                         if(search_dir==string::npos)
130                                                 break;
131
132                                         search_dir += 12;
133                                         string::size_type end = output.find("\");", search_dir);
134                                         if(end==string::npos)
135                                                 break;
136
137                                         FS::Path path;
138                                         if(!output.compare(search_dir, 2, "=/"))
139                                         {
140                                                 search_dir += 2;
141                                                 if(sysroot.empty())
142                                                         path = "/";
143                                                 else
144                                                         path = sysroot;
145                                         }
146
147                                         path /= output.substr(search_dir, end-search_dir);
148                                         builder.get_logger().log("tools", format("Got %s system path: %s", tag, path));
149                                         system_path.push_back(path);
150                                         path_found = true;
151
152                                         start = end+3;
153                                 }
154                         }
155                         catch(...)
156                         { }
157                 }
158         }
159
160         if(!path_found)
161         {
162                 builder.get_logger().log("tools", format("No %s system path found, using defaults", tag));
163                 if(!sysroot.empty())
164                         system_path.push_back(sysroot/"usr/lib");
165                 else if(architecture->is_native())
166                 {
167                         system_path.push_back("/lib");
168                         system_path.push_back("/usr/lib");
169                         if(architecture->match_name("pc-32-linux"))
170                         {
171                                 system_path.push_back("/lib/i386-linux-gnu");
172                                 system_path.push_back("/usr/lib/i386-linux-gnu");
173                         }
174                         else if(architecture->match_name("pc-64-linux"))
175                         {
176                                 system_path.push_back("/lib/x86_64-linux-gnu");
177                                 system_path.push_back("/usr/lib/x86_64-linux-gnu");
178                         }
179                 }
180                 else
181                         system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/lib");
182         }
183 }
184
185 Task *GnuLinker::run(const Target &) const
186 {
187         throw logic_error("GnuLinker should not be run directly");
188 }
189
190
191 GnuLinker::Linker::Linker(GnuLinker &p, const string &ct):
192         SubTool(p),
193         compiler_tag(ct)
194 {
195         if(compiler_tag=="CC")
196                 set_command("gcc", true);
197         else if(compiler_tag=="CXX")
198                 set_command("g++", true);
199         else
200                 throw invalid_argument("GnuLinker::Linker::Linker");
201 }
202
203 string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
204 {
205         string result = FS::basename(executable->get_path());
206         result += ',';
207         if(binfo.libmode<=BuildInfo::STATIC)
208                 result += 't';
209         else
210                 result += 'd';
211         if(binfo.strip)
212                 result += 's';
213         if(!binfo.libs.empty())
214         {
215                 result += ",l";
216                 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
217         }
218         return result;
219 }
220
221 void GnuLinker::Linker::do_prepare()
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         for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
274                 argv.push_back("-L"+i->str());
275         if(binfo.strip)
276                 argv.push_back("-s");
277         if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
278                 argv.push_back("-pthread");
279
280         const Architecture &native_arch = builder.get_native_arch();
281         if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
282                 argv.push_back(format("-m%d", architecture->get_bits()));
283
284         argv.push_back("-o");
285         argv.push_back(relative(bin.get_path(), work_dir).str());
286
287         for(BuildInfo::WordList::const_iterator i=binfo.keep_symbols.begin(); i!=binfo.keep_symbols.end(); ++i)
288                 argv.push_back("-u"+*i);
289
290         bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
291
292         if(architecture->get_system()=="windows" && architecture->get_cross_prefix().find("mingw")!=string::npos)
293                 argv.push_back("-lmingw32");
294
295         const Target::Dependencies &depends = target.get_dependencies();
296         for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
297         {
298                 FileTarget *file = dynamic_cast<FileTarget *>(*i);
299                 Target *tgt = (*i)->get_real_target();
300
301                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
302                         argv.push_back(relative(obj->get_path(), work_dir).str());
303                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
304                         argv.push_back((file?file:stlib)->get_path().str());
305                 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
306                 {
307                         argv.push_back("-l"+shlib->get_libname());
308                         static_link_ok = false;
309                 }
310                 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
311                 {
312                         shlib = imp->get_shared_library();
313                         if(shlib)
314                                 argv.push_back("-l"+shlib->get_libname());
315                         else
316                                 argv.push_back((file?file:imp)->get_path().str());
317                         static_link_ok = false;
318                 }
319         }
320
321         for(BuildInfo::WordList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i)
322                 if(i->size()>10 && !i->compare(i->size()-10, 10, ".framework"))
323                 {
324                         argv.push_back("-framework");
325                         argv.push_back(i->substr(0, i->size()-10));
326                 }
327
328         if(static_link_ok)
329                 argv.push_back("-static");
330         else if(architecture->get_system()=="windows")
331                 argv.push_back("-Wl,--enable-auto-import");
332
333         return new ExternalTask(argv, work_dir);
334 }