]> git.tdb.fi Git - builder.git/blob - plugins/gnu/gnulinker.cpp
Remove automatic export/import of symbols from GNU tools on Windows
[builder.git] / plugins / gnu / gnulinker.cpp
1 #include <stdexcept>
2 #include <vector>
3 #include <msp/builder/builder.h>
4 #include <msp/builder/component.h>
5 #include <msp/builder/executable.h>
6 #include <msp/builder/exportdefinitions.h>
7 #include <msp/builder/importlibrary.h>
8 #include <msp/builder/installedfile.h>
9 #include <msp/builder/objectfile.h>
10 #include <msp/builder/sharedlibrary.h>
11 #include <msp/builder/sourcepackage.h>
12 #include <msp/builder/staticlibrary.h>
13 #include <msp/core/algorithm.h>
14 #include <msp/fs/dir.h>
15 #include <msp/fs/utils.h>
16 #include <msp/strings/format.h>
17 #include <msp/strings/utils.h>
18 #include "gnucompiler.h"
19 #include "gnulinker.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_external(_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("-v");
113                 argv.push_back("-Wl,--verbose");
114                 argv.push_back("-nostdlib");
115                 if(!sysroot.empty())
116                         argv.push_back("--sysroot="+sysroot.str());
117
118                 builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
119                 try
120                 {
121                         string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
122
123                         string::size_type lib_path = output.find("LIBRARY_PATH=");
124                         if(lib_path!=string::npos)
125                         {
126                                 string::size_type newline = output.find('\n', lib_path);
127                                 for(const string &p: split(output.substr(lib_path+13, newline-lib_path-13), ':'))
128                                 {
129                                         FS::Path path = strip(p);
130                                         if(!any_equals(tool.system_path, path))
131                                         {
132                                                 builder.get_logger().log("tools", "Got %s frontend system path: %s", tool_tag, path);
133                                                 tool.system_path.push_back(path);
134                                         }
135                                         path_found = true;
136                                 }
137                         }
138
139                         string::size_type start = 0;
140                         while(start<output.size())
141                         {
142                                 string::size_type search_dir = output.find("SEARCH_DIR(\"", start);
143                                 if(search_dir==string::npos)
144                                         break;
145
146                                 search_dir += 12;
147                                 string::size_type end = output.find("\");", search_dir);
148                                 if(end==string::npos)
149                                         break;
150
151                                 FS::Path path;
152                                 if(!output.compare(search_dir, 2, "=/"))
153                                 {
154                                         search_dir += 2;
155                                         if(sysroot.empty())
156                                                 path = "/";
157                                         else
158                                                 path = sysroot;
159                                 }
160
161                                 path /= output.substr(search_dir, end-search_dir);
162                                 if(!any_equals(tool.system_path, path))
163                                 {
164                                         builder.get_logger().log("tools", "Got %s implicit system path: %s", tool_tag, path);
165                                         tool.system_path.push_back(path);
166                                 }
167                                 path_found = true;
168
169                                 start = end+3;
170                         }
171                 }
172                 catch(...)
173                 { }
174         }
175
176         if(!path_found)
177         {
178                 builder.get_logger().log("tools", "No %s system path found, using defaults", tool_tag);
179                 if(!sysroot.empty())
180                         tool.system_path.push_back(sysroot/"usr/lib");
181                 else if(architecture->is_native())
182                 {
183                         tool.system_path.push_back("/lib");
184                         tool.system_path.push_back("/usr/lib");
185                         if(architecture->match_name("pc-32-linux"))
186                         {
187                                 tool.system_path.push_back("/lib/i386-linux-gnu");
188                                 tool.system_path.push_back("/usr/lib/i386-linux-gnu");
189                         }
190                         else if(architecture->match_name("pc-64-linux"))
191                         {
192                                 tool.system_path.push_back("/lib/x86_64-linux-gnu");
193                                 tool.system_path.push_back("/usr/lib/x86_64-linux-gnu");
194                         }
195                 }
196                 else
197                         tool.system_path.push_back(format("/usr/%s/lib", architecture->get_cross_prefix()));
198         }
199 }
200
201 ExternalTask::Arguments GnuLinker::_run(const Binary &bin, FS::Path &work_dir)
202 {
203         const Tool &tool = *bin.get_tool();
204         const Builder &builder = tool.get_builder();
205         const Architecture &arch = *tool.get_architecture();
206
207         ExternalTask::Arguments argv;
208         argv.push_back(tool.get_executable()->get_path().str());
209
210         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
211         {
212                 argv.push_back("-shared");
213                 argv.push_back("-fPIC");
214                 if(arch.get_system()!="windows" && !shlib->get_soname().empty())
215                 {
216                         if(arch.get_system()=="darwin")
217                         {
218                                 argv.push_back("-install_name");
219                                 argv.push_back(shlib->get_soname());
220
221                                 const string &ver = shlib->get_package()->get_version();
222                                 const string &if_ver = shlib->get_package()->get_interface_version();
223                                 if(!ver.empty() && !if_ver.empty())
224                                 {
225                                         argv.push_back("-current_version");
226                                         argv.push_back(ver);
227                                         argv.push_back("-compatibility_version");
228                                         argv.push_back(if_ver);
229                                 }
230                         }
231                         else
232                                 argv.push_back("-Wl,-soname,"+shlib->get_soname());
233                 }
234         }
235
236         BuildInfo binfo;
237         bin.collect_build_info(binfo);
238
239         const FS::Path &sysroot = binfo.sysroot;
240         if(!sysroot.empty())
241                 argv.push_back("--sysroot="+sysroot.str());
242
243         FS::Path lib_dir = builder.get_prefix()/"lib";
244         if(binfo.rpath_mode==BuildInfo::ABSOLUTE)
245                 argv.push_back("-Wl,-rpath,"+lib_dir.str());
246         else
247         {
248                 if(binfo.rpath_mode==BuildInfo::RELATIVE)
249                         argv.push_back("-Wl,-rpath,$ORIGIN/../lib");
250                 argv.push_back("-Wl,-rpath-link,"+lib_dir.str());
251         }
252
253         for(const FS::Path &p: binfo.libpath)
254                 argv.push_back("-L"+p.str());
255         if(binfo.strip)
256                 argv.push_back("-s");
257         if(binfo.threads && arch.get_system()!="windows" && arch.get_system()!="darwin")
258                 argv.push_back("-pthread");
259
260         const Architecture &native_arch = builder.get_native_arch();
261         if(arch.is_native() && arch.get_bits()!=native_arch.get_bits())
262                 argv.push_back(format("-m%d", arch.get_bits()));
263
264         argv.push_back("-o");
265         argv.push_back(relative(bin.get_path(), work_dir).str());
266
267         for(const string &s: binfo.keep_symbols)
268                 argv.push_back("-u"+s);
269
270         bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
271
272         bool has_cplusplus = false;
273         for(Target *d: bin.get_dependencies())
274         {
275                 FileTarget *file = dynamic_cast<FileTarget *>(d);
276                 Target *tgt = d->get_real_target();
277
278                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
279                 {
280                         argv.push_back(relative(obj->get_path(), work_dir).str());
281                         if(obj->get_tool()->get_tag()=="CXX")
282                                 has_cplusplus = true;
283                 }
284                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
285                         argv.push_back((file?file:stlib)->get_path().str());
286                 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
287                 {
288                         argv.push_back("-l"+shlib->get_libname());
289                         static_link_ok = false;
290                 }
291                 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
292                 {
293                         shlib = imp->get_shared_library();
294                         if(shlib)
295                                 argv.push_back("-l"+shlib->get_libname());
296                         else
297                                 argv.push_back((file?file:imp)->get_path().str());
298                         static_link_ok = false;
299                 }
300         }
301
302         for(const string &l: binfo.libs)
303                 if(l.size()>10 && !l.compare(l.size()-10, 10, ".framework"))
304                 {
305                         argv.push_back("-framework");
306                         argv.push_back(l.substr(0, l.size()-10));
307                 }
308
309         if(static_link_ok)
310                 argv.push_back("-static");
311         else
312         {
313                 if(has_cplusplus)
314                 {
315                         auto i = binfo.libmodes.find("stdc++");
316                         if(i!=binfo.libmodes.end() && i->second<=BuildInfo::STATIC)
317                                 argv.push_back("-static-libstdc++");
318                 }
319         }
320
321         return argv;
322 }