]> git.tdb.fi Git - builder.git/blob - source/gnulinker.cpp
Redesign GnuLinker to work without subtools
[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         set_command("gcc");
33         set_run(_run);
34 }
35
36 Target *GnuLinker::create_target(const vector<Target *> &sources, const string &arg)
37 {
38         if(sources.empty())
39                 throw invalid_argument("GnuLinker::create_target");
40         vector<ObjectFile *> objs;
41         objs.reserve(sources.size());
42         for(Target *s: sources)
43                 objs.push_back(&dynamic_cast<ObjectFile &>(*s));
44
45         const Component &comp = *objs.front()->get_component();
46         Binary *bin = 0;
47         if(arg=="shared")
48         {
49                 SharedLibrary *shlib = new SharedLibrary(builder, comp, objs);
50                 if(architecture->get_system()=="windows")
51                 {
52                         Tool &dlltool = builder.get_toolchain().get_tool("DLL");
53                         dlltool.create_target(*shlib);
54                 }
55                 bin = shlib;
56         }
57         else
58                 bin = new Executable(builder, comp, objs);
59         bin->set_tool(*this);
60         return bin;
61 }
62
63 Target *GnuLinker::create_install(Target &target) const
64 {
65         if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(&target))
66         {
67                 Tool &copy = builder.get_toolchain().get_tool("CP");
68                 InstalledFile *inst_tgt = dynamic_cast<InstalledFile *>(copy.create_target(target));
69                 if(architecture->get_system()=="windows")
70                         builder.get_build_graph().add_installed_target(*shlib->get_import_library());
71                 else
72                 {
73                         string link_name = architecture->create_filename<SharedLibrary>(shlib->get_libname());
74                         if(link_name!=FS::basename(inst_tgt->get_path()))
75                                 inst_tgt->set_symlink(link_name);
76                 }
77                 return inst_tgt;
78         }
79         else
80                 return 0;
81 }
82
83 string GnuLinker::create_build_signature(const BuildInfo &binfo) const
84 {
85         string result = Tool::create_build_signature(binfo);
86         result += ',';
87         if(binfo.libmode<=BuildInfo::STATIC)
88                 result += 't';
89         else
90                 result += 'd';
91         if(binfo.strip)
92                 result += 's';
93         if(!binfo.libs.empty())
94         {
95                 result += ",l";
96                 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
97         }
98         return result;
99 }
100
101 void GnuLinker::do_prepare()
102 {
103         bool path_found = false;
104         const FS::Path &sysroot = build_info.sysroot;
105
106         if(executable)
107         {
108                 ExternalTask::Arguments argv;
109                 argv.push_back(executable->get_path().str());
110                 argv.push_back("-Wl,--verbose");
111                 argv.push_back("-nostdlib");
112                 if(!sysroot.empty())
113                         argv.push_back("--sysroot="+sysroot.str());
114
115                 builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
116                 try
117                 {
118                         string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
119                         string::size_type start = 0;
120                         while(start<output.size())
121                         {
122                                 string::size_type search_dir = output.find("SEARCH_DIR(\"", start);
123                                 if(search_dir==string::npos)
124                                         break;
125
126                                 search_dir += 12;
127                                 string::size_type end = output.find("\");", search_dir);
128                                 if(end==string::npos)
129                                         break;
130
131                                 FS::Path path;
132                                 if(!output.compare(search_dir, 2, "=/"))
133                                 {
134                                         search_dir += 2;
135                                         if(sysroot.empty())
136                                                 path = "/";
137                                         else
138                                                 path = sysroot;
139                                 }
140
141                                 path /= output.substr(search_dir, end-search_dir);
142                                 builder.get_logger().log("tools", "Got %s system path: %s", tag, path);
143                                 system_path.push_back(path);
144                                 path_found = true;
145
146                                 start = end+3;
147                         }
148                 }
149                 catch(...)
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 Task *GnuLinker::_run(const Binary &bin)
179 {
180         const Tool &tool = *bin.get_tool();
181         const Builder &builder = tool.get_builder();
182         const Architecture &arch = *tool.get_architecture();
183
184         ExternalTask::Arguments argv;
185         argv.push_back(tool.get_executable()->get_path().str());
186
187         FS::Path work_dir = bin.get_component()->get_package().get_source_directory();
188
189         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
190         {
191                 argv.push_back("-shared");
192                 argv.push_back("-fPIC");
193                 if(arch.get_system()!="windows" && !shlib->get_soname().empty())
194                 {
195                         if(arch.get_system()=="darwin")
196                         {
197                                 argv.push_back("-install_name");
198                                 argv.push_back(shlib->get_soname());
199
200                                 const string &ver = shlib->get_package()->get_version();
201                                 const string &if_ver = shlib->get_package()->get_interface_version();
202                                 if(!ver.empty() && !if_ver.empty())
203                                 {
204                                         argv.push_back("-current_version");
205                                         argv.push_back(ver);
206                                         argv.push_back("-compatibility_version");
207                                         argv.push_back(if_ver);
208                                 }
209                         }
210                         else
211                                 argv.push_back("-Wl,-soname,"+shlib->get_soname());
212                 }
213         }
214
215         BuildInfo binfo;
216         bin.collect_build_info(binfo);
217
218         const FS::Path &sysroot = binfo.sysroot;
219         if(!sysroot.empty())
220                 argv.push_back("--sysroot="+sysroot.str());
221
222         FS::Path lib_dir = builder.get_prefix()/"lib";
223         if(binfo.rpath_mode==BuildInfo::ABSOLUTE)
224                 argv.push_back("-Wl,-rpath,"+lib_dir.str());
225         else
226         {
227                 if(binfo.rpath_mode==BuildInfo::RELATIVE)
228                         argv.push_back("-Wl,-rpath,$ORIGIN/../lib");
229                 argv.push_back("-Wl,-rpath-link,"+lib_dir.str());
230         }
231
232         for(const FS::Path &p: binfo.libpath)
233                 argv.push_back("-L"+p.str());
234         if(binfo.strip)
235                 argv.push_back("-s");
236         if(binfo.threads && arch.get_system()!="windows" && arch.get_system()!="darwin")
237                 argv.push_back("-pthread");
238
239         const Architecture &native_arch = builder.get_native_arch();
240         if(arch.is_native() && arch.get_bits()!=native_arch.get_bits())
241                 argv.push_back(format("-m%d", arch.get_bits()));
242
243         argv.push_back("-o");
244         argv.push_back(relative(bin.get_path(), work_dir).str());
245
246         for(const string &s: binfo.keep_symbols)
247                 argv.push_back("-u"+s);
248
249         bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
250
251         bool has_cplusplus = false;
252         for(Target *d: bin.get_dependencies())
253         {
254                 FileTarget *file = dynamic_cast<FileTarget *>(d);
255                 Target *tgt = d->get_real_target();
256
257                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
258                 {
259                         argv.push_back(relative(obj->get_path(), work_dir).str());
260                         if(obj->get_tool()->get_tag()=="CXX")
261                                 has_cplusplus = true;
262                 }
263                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
264                         argv.push_back((file?file:stlib)->get_path().str());
265                 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
266                 {
267                         argv.push_back("-l"+shlib->get_libname());
268                         static_link_ok = false;
269                 }
270                 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
271                 {
272                         shlib = imp->get_shared_library();
273                         if(shlib)
274                                 argv.push_back("-l"+shlib->get_libname());
275                         else
276                                 argv.push_back((file?file:imp)->get_path().str());
277                         static_link_ok = false;
278                 }
279         }
280
281         for(const string &l: binfo.libs)
282                 if(l.size()>10 && !l.compare(l.size()-10, 10, ".framework"))
283                 {
284                         argv.push_back("-framework");
285                         argv.push_back(l.substr(0, l.size()-10));
286                 }
287
288         if(has_cplusplus)
289                 argv.push_back("-lstdc++");
290
291         if(static_link_ok)
292                 argv.push_back("-static");
293         else
294         {
295                 if(has_cplusplus)
296                 {
297                         auto i = binfo.libmodes.find("stdc++");
298                         if(i!=binfo.libmodes.end() && i->second<=BuildInfo::STATIC)
299                                 argv.push_back("-static-libstdc++");
300                 }
301
302                 if(arch.get_system()=="windows")
303                         argv.push_back("-Wl,--enable-auto-import");
304         }
305
306         return new ExternalTask(argv, work_dir);
307 }