]> git.tdb.fi Git - builder.git/blob - plugins/gnu/gnulinker.cpp
Refactor transitive dependencies to work on all targets
[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
41         if(arg=="import")
42         {
43                 if(sources.size()!=1)
44                         throw invalid_argument("GnuLinker::create_target");
45                 SharedLibrary &shlib = dynamic_cast<SharedLibrary &>(*sources.front());
46                 if(architecture->get_system()!="windows")
47                         return 0;
48
49                 Tool &dlltool = builder.get_toolchain().get_tool("DLL");
50                 return dlltool.create_target(shlib);
51         }
52
53         vector<ObjectFile *> objs;
54         objs.reserve(sources.size());
55         for(Target *s: sources)
56                 objs.push_back(&dynamic_cast<ObjectFile &>(*s));
57
58         const Component &comp = *objs.front()->get_component();
59         Binary *bin = 0;
60         if(arg=="shared")
61                 bin = new SharedLibrary(builder, comp, objs);
62         else
63                 bin = new Executable(builder, comp, objs);
64         bin->set_tool(*this);
65         return bin;
66 }
67
68 Target *GnuLinker::create_install(Target &target) const
69 {
70         if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(&target))
71         {
72                 Tool &copy = builder.get_toolchain().get_tool("CP");
73                 InstalledFile *inst_tgt = dynamic_cast<InstalledFile *>(copy.create_target(target));
74                 if(architecture->get_system()!="windows")
75                 {
76                         string link_name = architecture->create_filename<SharedLibrary>(shlib->get_libname());
77                         if(link_name!=FS::basename(inst_tgt->get_path()))
78                                 inst_tgt->set_symlink(link_name);
79                 }
80                 return inst_tgt;
81         }
82         else
83                 return 0;
84 }
85
86 string GnuLinker::create_build_signature(const BuildInfo &binfo) const
87 {
88         string result = Tool::create_build_signature(binfo);
89         result += ',';
90         if(binfo.libmode<=BuildInfo::STATIC)
91                 result += 't';
92         else
93                 result += 'd';
94         if(binfo.strip)
95                 result += 's';
96         if(!binfo.libs.empty())
97         {
98                 result += ",l";
99                 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
100         }
101         return result;
102 }
103
104 void GnuLinker::do_prepare(ToolData &tool) const
105 {
106         bool path_found = false;
107         const FS::Path &sysroot = tool.build_info.sysroot;
108         const std::string &tool_tag = static_cast<Tool &>(tool).get_tag();
109
110         const FileTarget *exe = static_cast<Tool &>(tool).get_executable();
111         if(exe)
112         {
113                 ExternalTask::Arguments argv;
114                 argv.push_back(exe->get_path().str());
115                 argv.push_back("-v");
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", "Running %s", join(argv.begin(), argv.end()));
122                 try
123                 {
124                         string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
125
126                         string::size_type lib_path = output.find("LIBRARY_PATH=");
127                         if(lib_path!=string::npos)
128                         {
129                                 string::size_type newline = output.find('\n', lib_path);
130                                 for(const string &p: split(output.substr(lib_path+13, newline-lib_path-13), ':'))
131                                 {
132                                         FS::Path path = strip(p);
133                                         if(!any_equals(tool.system_path, path))
134                                         {
135                                                 builder.get_logger().log("tools", "Got %s frontend system path: %s", tool_tag, path);
136                                                 tool.system_path.push_back(path);
137                                         }
138                                         path_found = true;
139                                 }
140                         }
141
142                         string::size_type start = 0;
143                         while(start<output.size())
144                         {
145                                 string::size_type search_dir = output.find("SEARCH_DIR(\"", start);
146                                 if(search_dir==string::npos)
147                                         break;
148
149                                 search_dir += 12;
150                                 string::size_type end = output.find("\");", search_dir);
151                                 if(end==string::npos)
152                                         break;
153
154                                 FS::Path path;
155                                 if(!output.compare(search_dir, 2, "=/"))
156                                 {
157                                         search_dir += 2;
158                                         if(sysroot.empty())
159                                                 path = "/";
160                                         else
161                                                 path = sysroot;
162                                 }
163
164                                 path /= output.substr(search_dir, end-search_dir);
165                                 if(!any_equals(tool.system_path, path))
166                                 {
167                                         builder.get_logger().log("tools", "Got %s implicit system path: %s", tool_tag, path);
168                                         tool.system_path.push_back(path);
169                                 }
170                                 path_found = true;
171
172                                 start = end+3;
173                         }
174                 }
175                 catch(...)
176                 { }
177         }
178
179         if(!path_found)
180         {
181                 builder.get_logger().log("tools", "No %s system path found, using defaults", tool_tag);
182                 if(!sysroot.empty())
183                         tool.system_path.push_back(sysroot/"usr/lib");
184                 else if(architecture->is_native())
185                 {
186                         tool.system_path.push_back("/lib");
187                         tool.system_path.push_back("/usr/lib");
188                         if(architecture->match_name("pc-32-linux"))
189                         {
190                                 tool.system_path.push_back("/lib/i386-linux-gnu");
191                                 tool.system_path.push_back("/usr/lib/i386-linux-gnu");
192                         }
193                         else if(architecture->match_name("pc-64-linux"))
194                         {
195                                 tool.system_path.push_back("/lib/x86_64-linux-gnu");
196                                 tool.system_path.push_back("/usr/lib/x86_64-linux-gnu");
197                         }
198                 }
199                 else
200                         tool.system_path.push_back(format("/usr/%s/lib", architecture->get_cross_prefix()));
201         }
202 }
203
204 ExternalTask::Arguments GnuLinker::_run(const Binary &bin, FS::Path &work_dir)
205 {
206         const Tool &tool = *bin.get_tool();
207         const Builder &builder = tool.get_builder();
208         const Architecture &arch = *tool.get_architecture();
209
210         ExternalTask::Arguments argv;
211         argv.push_back(tool.get_executable()->get_path().str());
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(static_link_ok)
313                 argv.push_back("-static");
314         else
315         {
316                 if(has_cplusplus)
317                 {
318                         auto i = binfo.libmodes.find("stdc++");
319                         if(i!=binfo.libmodes.end() && i->second<=BuildInfo::STATIC)
320                                 argv.push_back("-static-libstdc++");
321                 }
322         }
323
324         return argv;
325 }