]> git.tdb.fi Git - builder.git/blob - source/gnulinker.cpp
Use a cross prefix for the new unified GnuLinker driver
[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", true);
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(ToolData &tool) const
102 {
103         bool path_found = false;
104         const FS::Path &sysroot = tool.build_info.sysroot;
105         const std::string &tool_tag = static_cast<Tool &>(tool).get_tag();
106
107         const FileTarget *exe = static_cast<Tool &>(tool).get_executable();
108         if(exe)
109         {
110                 ExternalTask::Arguments argv;
111                 argv.push_back(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", "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", "Got %s system path: %s", tool_tag, path);
145                                 tool.system_path.push_back(path);
146                                 path_found = true;
147
148                                 start = end+3;
149                         }
150                 }
151                 catch(...)
152                 { }
153         }
154
155         if(!path_found)
156         {
157                 builder.get_logger().log("tools", "No %s system path found, using defaults", tool_tag);
158                 if(!sysroot.empty())
159                         tool.system_path.push_back(sysroot/"usr/lib");
160                 else if(architecture->is_native())
161                 {
162                         tool.system_path.push_back("/lib");
163                         tool.system_path.push_back("/usr/lib");
164                         if(architecture->match_name("pc-32-linux"))
165                         {
166                                 tool.system_path.push_back("/lib/i386-linux-gnu");
167                                 tool.system_path.push_back("/usr/lib/i386-linux-gnu");
168                         }
169                         else if(architecture->match_name("pc-64-linux"))
170                         {
171                                 tool.system_path.push_back("/lib/x86_64-linux-gnu");
172                                 tool.system_path.push_back("/usr/lib/x86_64-linux-gnu");
173                         }
174                 }
175                 else
176                         tool.system_path.push_back(format("/usr/%s/lib", architecture->get_cross_prefix()));
177         }
178 }
179
180 Task *GnuLinker::_run(const Binary &bin)
181 {
182         const Tool &tool = *bin.get_tool();
183         const Builder &builder = tool.get_builder();
184         const Architecture &arch = *tool.get_architecture();
185
186         ExternalTask::Arguments argv;
187         argv.push_back(tool.get_executable()->get_path().str());
188
189         FS::Path work_dir = bin.get_component()->get_package().get_source_directory();
190
191         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
192         {
193                 argv.push_back("-shared");
194                 argv.push_back("-fPIC");
195                 if(arch.get_system()!="windows" && !shlib->get_soname().empty())
196                 {
197                         if(arch.get_system()=="darwin")
198                         {
199                                 argv.push_back("-install_name");
200                                 argv.push_back(shlib->get_soname());
201
202                                 const string &ver = shlib->get_package()->get_version();
203                                 const string &if_ver = shlib->get_package()->get_interface_version();
204                                 if(!ver.empty() && !if_ver.empty())
205                                 {
206                                         argv.push_back("-current_version");
207                                         argv.push_back(ver);
208                                         argv.push_back("-compatibility_version");
209                                         argv.push_back(if_ver);
210                                 }
211                         }
212                         else
213                                 argv.push_back("-Wl,-soname,"+shlib->get_soname());
214                 }
215         }
216
217         BuildInfo binfo;
218         bin.collect_build_info(binfo);
219
220         const FS::Path &sysroot = binfo.sysroot;
221         if(!sysroot.empty())
222                 argv.push_back("--sysroot="+sysroot.str());
223
224         FS::Path lib_dir = builder.get_prefix()/"lib";
225         if(binfo.rpath_mode==BuildInfo::ABSOLUTE)
226                 argv.push_back("-Wl,-rpath,"+lib_dir.str());
227         else
228         {
229                 if(binfo.rpath_mode==BuildInfo::RELATIVE)
230                         argv.push_back("-Wl,-rpath,$ORIGIN/../lib");
231                 argv.push_back("-Wl,-rpath-link,"+lib_dir.str());
232         }
233
234         for(const FS::Path &p: binfo.libpath)
235                 argv.push_back("-L"+p.str());
236         if(binfo.strip)
237                 argv.push_back("-s");
238         if(binfo.threads && arch.get_system()!="windows" && arch.get_system()!="darwin")
239                 argv.push_back("-pthread");
240
241         const Architecture &native_arch = builder.get_native_arch();
242         if(arch.is_native() && arch.get_bits()!=native_arch.get_bits())
243                 argv.push_back(format("-m%d", arch.get_bits()));
244
245         argv.push_back("-o");
246         argv.push_back(relative(bin.get_path(), work_dir).str());
247
248         for(const string &s: binfo.keep_symbols)
249                 argv.push_back("-u"+s);
250
251         bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
252
253         bool has_cplusplus = false;
254         for(Target *d: bin.get_dependencies())
255         {
256                 FileTarget *file = dynamic_cast<FileTarget *>(d);
257                 Target *tgt = d->get_real_target();
258
259                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
260                 {
261                         argv.push_back(relative(obj->get_path(), work_dir).str());
262                         if(obj->get_tool()->get_tag()=="CXX")
263                                 has_cplusplus = true;
264                 }
265                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
266                         argv.push_back((file?file:stlib)->get_path().str());
267                 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
268                 {
269                         argv.push_back("-l"+shlib->get_libname());
270                         static_link_ok = false;
271                 }
272                 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
273                 {
274                         shlib = imp->get_shared_library();
275                         if(shlib)
276                                 argv.push_back("-l"+shlib->get_libname());
277                         else
278                                 argv.push_back((file?file:imp)->get_path().str());
279                         static_link_ok = false;
280                 }
281         }
282
283         for(const string &l: binfo.libs)
284                 if(l.size()>10 && !l.compare(l.size()-10, 10, ".framework"))
285                 {
286                         argv.push_back("-framework");
287                         argv.push_back(l.substr(0, l.size()-10));
288                 }
289
290         if(has_cplusplus)
291                 argv.push_back("-lstdc++");
292
293         if(static_link_ok)
294                 argv.push_back("-static");
295         else
296         {
297                 if(has_cplusplus)
298                 {
299                         auto i = binfo.libmodes.find("stdc++");
300                         if(i!=binfo.libmodes.end() && i->second<=BuildInfo::STATIC)
301                                 argv.push_back("-static-libstdc++");
302                 }
303
304                 if(arch.get_system()=="windows")
305                         argv.push_back("-Wl,--enable-auto-import");
306         }
307
308         return new ExternalTask(argv, work_dir);
309 }