]> git.tdb.fi Git - builder.git/blob - source/gnulinker.cpp
Gather more system path information for GnuLinker
[builder.git] / source / gnulinker.cpp
1 #include <stdexcept>
2 #include <vector>
3 #include <msp/core/algorithm.h>
4 #include <msp/fs/dir.h>
5 #include <msp/fs/utils.h>
6 #include <msp/strings/format.h>
7 #include <msp/strings/utils.h>
8 #include "builder.h"
9 #include "component.h"
10 #include "executable.h"
11 #include "exportdefinitions.h"
12 #include "externaltask.h"
13 #include "gnucompiler.h"
14 #include "gnulinker.h"
15 #include "importlibrary.h"
16 #include "installedfile.h"
17 #include "objectfile.h"
18 #include "sharedlibrary.h"
19 #include "sourcepackage.h"
20 #include "staticlibrary.h"
21
22 using namespace std;
23 using namespace Msp;
24
25 GnuLinker::GnuLinker(Builder &b, const Architecture &a):
26         Tool(b, &a, "LINK")
27 {
28         input_suffixes.push_back(".o");
29         input_suffixes.push_back(".a");
30
31         processing_unit = COMPONENT;
32
33         set_command("gcc", true);
34         set_run(_run);
35 }
36
37 Target *GnuLinker::create_target(const vector<Target *> &sources, const string &arg)
38 {
39         if(sources.empty())
40                 throw invalid_argument("GnuLinker::create_target");
41         vector<ObjectFile *> objs;
42         objs.reserve(sources.size());
43         for(Target *s: sources)
44                 objs.push_back(&dynamic_cast<ObjectFile &>(*s));
45
46         const Component &comp = *objs.front()->get_component();
47         Binary *bin = 0;
48         if(arg=="shared")
49         {
50                 SharedLibrary *shlib = new SharedLibrary(builder, comp, objs);
51                 if(architecture->get_system()=="windows")
52                 {
53                         Tool &dlltool = builder.get_toolchain().get_tool("DLL");
54                         dlltool.create_target(*shlib);
55                 }
56                 bin = shlib;
57         }
58         else
59                 bin = new Executable(builder, comp, objs);
60         bin->set_tool(*this);
61         return bin;
62 }
63
64 Target *GnuLinker::create_install(Target &target) const
65 {
66         if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(&target))
67         {
68                 Tool &copy = builder.get_toolchain().get_tool("CP");
69                 InstalledFile *inst_tgt = dynamic_cast<InstalledFile *>(copy.create_target(target));
70                 if(architecture->get_system()=="windows")
71                         builder.get_build_graph().add_installed_target(*shlib->get_import_library());
72                 else
73                 {
74                         string link_name = architecture->create_filename<SharedLibrary>(shlib->get_libname());
75                         if(link_name!=FS::basename(inst_tgt->get_path()))
76                                 inst_tgt->set_symlink(link_name);
77                 }
78                 return inst_tgt;
79         }
80         else
81                 return 0;
82 }
83
84 string GnuLinker::create_build_signature(const BuildInfo &binfo) const
85 {
86         string result = Tool::create_build_signature(binfo);
87         result += ',';
88         if(binfo.libmode<=BuildInfo::STATIC)
89                 result += 't';
90         else
91                 result += 'd';
92         if(binfo.strip)
93                 result += 's';
94         if(!binfo.libs.empty())
95         {
96                 result += ",l";
97                 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
98         }
99         return result;
100 }
101
102 void GnuLinker::do_prepare(ToolData &tool) const
103 {
104         bool path_found = false;
105         const FS::Path &sysroot = tool.build_info.sysroot;
106         const std::string &tool_tag = static_cast<Tool &>(tool).get_tag();
107
108         const FileTarget *exe = static_cast<Tool &>(tool).get_executable();
109         if(exe)
110         {
111                 ExternalTask::Arguments argv;
112                 argv.push_back(exe->get_path().str());
113                 argv.push_back("-v");
114                 argv.push_back("-Wl,--verbose");
115                 argv.push_back("-nostdlib");
116                 if(!sysroot.empty())
117                         argv.push_back("--sysroot="+sysroot.str());
118
119                 builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
120                 try
121                 {
122                         string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
123
124                         string::size_type lib_path = output.find("LIBRARY_PATH=");
125                         if(lib_path!=string::npos)
126                         {
127                                 string::size_type newline = output.find('\n', lib_path);
128                                 for(const string &p: split(output.substr(lib_path+13, newline-lib_path-13), ':'))
129                                 {
130                                         FS::Path path = strip(p);
131                                         if(!any_equals(tool.system_path, path))
132                                         {
133                                                 builder.get_logger().log("tools", "Got %s frontend system path: %s", tool_tag, path);
134                                                 tool.system_path.push_back(path);
135                                         }
136                                         path_found = true;
137                                 }
138                         }
139
140                         string::size_type start = 0;
141                         while(start<output.size())
142                         {
143                                 string::size_type search_dir = output.find("SEARCH_DIR(\"", start);
144                                 if(search_dir==string::npos)
145                                         break;
146
147                                 search_dir += 12;
148                                 string::size_type end = output.find("\");", search_dir);
149                                 if(end==string::npos)
150                                         break;
151
152                                 FS::Path path;
153                                 if(!output.compare(search_dir, 2, "=/"))
154                                 {
155                                         search_dir += 2;
156                                         if(sysroot.empty())
157                                                 path = "/";
158                                         else
159                                                 path = sysroot;
160                                 }
161
162                                 path /= output.substr(search_dir, end-search_dir);
163                                 if(!any_equals(tool.system_path, path))
164                                 {
165                                         builder.get_logger().log("tools", "Got %s implicit system path: %s", tool_tag, path);
166                                         tool.system_path.push_back(path);
167                                 }
168                                 path_found = true;
169
170                                 start = end+3;
171                         }
172                 }
173                 catch(...)
174                 { }
175         }
176
177         if(!path_found)
178         {
179                 builder.get_logger().log("tools", "No %s system path found, using defaults", tool_tag);
180                 if(!sysroot.empty())
181                         tool.system_path.push_back(sysroot/"usr/lib");
182                 else if(architecture->is_native())
183                 {
184                         tool.system_path.push_back("/lib");
185                         tool.system_path.push_back("/usr/lib");
186                         if(architecture->match_name("pc-32-linux"))
187                         {
188                                 tool.system_path.push_back("/lib/i386-linux-gnu");
189                                 tool.system_path.push_back("/usr/lib/i386-linux-gnu");
190                         }
191                         else if(architecture->match_name("pc-64-linux"))
192                         {
193                                 tool.system_path.push_back("/lib/x86_64-linux-gnu");
194                                 tool.system_path.push_back("/usr/lib/x86_64-linux-gnu");
195                         }
196                 }
197                 else
198                         tool.system_path.push_back(format("/usr/%s/lib", architecture->get_cross_prefix()));
199         }
200 }
201
202 Task *GnuLinker::_run(const Binary &bin)
203 {
204         const Tool &tool = *bin.get_tool();
205         const Builder &builder = tool.get_builder();
206         const Architecture &arch = *tool.get_architecture();
207
208         ExternalTask::Arguments argv;
209         argv.push_back(tool.get_executable()->get_path().str());
210
211         FS::Path work_dir = bin.get_component()->get_package().get_source_directory();
212
213         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
214         {
215                 argv.push_back("-shared");
216                 argv.push_back("-fPIC");
217                 if(arch.get_system()!="windows" && !shlib->get_soname().empty())
218                 {
219                         if(arch.get_system()=="darwin")
220                         {
221                                 argv.push_back("-install_name");
222                                 argv.push_back(shlib->get_soname());
223
224                                 const string &ver = shlib->get_package()->get_version();
225                                 const string &if_ver = shlib->get_package()->get_interface_version();
226                                 if(!ver.empty() && !if_ver.empty())
227                                 {
228                                         argv.push_back("-current_version");
229                                         argv.push_back(ver);
230                                         argv.push_back("-compatibility_version");
231                                         argv.push_back(if_ver);
232                                 }
233                         }
234                         else
235                                 argv.push_back("-Wl,-soname,"+shlib->get_soname());
236                 }
237         }
238
239         BuildInfo binfo;
240         bin.collect_build_info(binfo);
241
242         const FS::Path &sysroot = binfo.sysroot;
243         if(!sysroot.empty())
244                 argv.push_back("--sysroot="+sysroot.str());
245
246         FS::Path lib_dir = builder.get_prefix()/"lib";
247         if(binfo.rpath_mode==BuildInfo::ABSOLUTE)
248                 argv.push_back("-Wl,-rpath,"+lib_dir.str());
249         else
250         {
251                 if(binfo.rpath_mode==BuildInfo::RELATIVE)
252                         argv.push_back("-Wl,-rpath,$ORIGIN/../lib");
253                 argv.push_back("-Wl,-rpath-link,"+lib_dir.str());
254         }
255
256         for(const FS::Path &p: binfo.libpath)
257                 argv.push_back("-L"+p.str());
258         if(binfo.strip)
259                 argv.push_back("-s");
260         if(binfo.threads && arch.get_system()!="windows" && arch.get_system()!="darwin")
261                 argv.push_back("-pthread");
262
263         const Architecture &native_arch = builder.get_native_arch();
264         if(arch.is_native() && arch.get_bits()!=native_arch.get_bits())
265                 argv.push_back(format("-m%d", arch.get_bits()));
266
267         argv.push_back("-o");
268         argv.push_back(relative(bin.get_path(), work_dir).str());
269
270         for(const string &s: binfo.keep_symbols)
271                 argv.push_back("-u"+s);
272
273         bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
274
275         bool has_cplusplus = false;
276         for(Target *d: bin.get_dependencies())
277         {
278                 FileTarget *file = dynamic_cast<FileTarget *>(d);
279                 Target *tgt = d->get_real_target();
280
281                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
282                 {
283                         argv.push_back(relative(obj->get_path(), work_dir).str());
284                         if(obj->get_tool()->get_tag()=="CXX")
285                                 has_cplusplus = true;
286                 }
287                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
288                         argv.push_back((file?file:stlib)->get_path().str());
289                 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
290                 {
291                         argv.push_back("-l"+shlib->get_libname());
292                         static_link_ok = false;
293                 }
294                 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
295                 {
296                         shlib = imp->get_shared_library();
297                         if(shlib)
298                                 argv.push_back("-l"+shlib->get_libname());
299                         else
300                                 argv.push_back((file?file:imp)->get_path().str());
301                         static_link_ok = false;
302                 }
303         }
304
305         for(const string &l: binfo.libs)
306                 if(l.size()>10 && !l.compare(l.size()-10, 10, ".framework"))
307                 {
308                         argv.push_back("-framework");
309                         argv.push_back(l.substr(0, l.size()-10));
310                 }
311
312         if(has_cplusplus)
313                 argv.push_back("-lstdc++");
314
315         if(static_link_ok)
316                 argv.push_back("-static");
317         else
318         {
319                 if(has_cplusplus)
320                 {
321                         auto i = binfo.libmodes.find("stdc++");
322                         if(i!=binfo.libmodes.end() && i->second<=BuildInfo::STATIC)
323                                 argv.push_back("-static-libstdc++");
324                 }
325
326                 if(arch.get_system()=="windows")
327                         argv.push_back("-Wl,--enable-auto-import");
328         }
329
330         return new ExternalTask(argv, work_dir);
331 }