]> git.tdb.fi Git - builder.git/blob - source/gnulinker.cpp
cc0a50d3ace1ef23db89835b4ad240bce64e4956
[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, const FS::Path &sysroot):
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         if(!sysroot.empty())
33         {
34                 build_info.sysroot = sysroot;
35                 system_path.push_back(sysroot/"usr"/"lib");
36         }
37         else if(architecture->is_native())
38         {
39                 system_path.push_back("/lib");
40                 system_path.push_back("/usr/lib");
41                 if(architecture->match_name("pc-32-linux"))
42                         system_path.push_back("/usr/lib/i386-linux-gnu");
43                 else if(architecture->match_name("pc-64-linux"))
44                         system_path.push_back("/usr/lib/x86_64-linux-gnu");
45         }
46         else
47                 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/lib");
48
49         default_linker = new Linker(*this, "CC");
50         cxx_linker = new Linker(*this, "CXX");
51 }
52
53 GnuLinker::~GnuLinker()
54 {
55         delete default_linker;
56         delete cxx_linker;
57 }
58
59 Target *GnuLinker::create_target(const list<Target *> &sources, const string &arg)
60 {
61         if(sources.empty())
62                 throw invalid_argument("GnuLinker::create_target");
63         list<ObjectFile *> objs;
64         Linker *linker = default_linker;
65         for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
66         {
67                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
68                 {
69                         objs.push_back(obj);
70                         if(obj->get_tool()->get_tag()=="CXX")
71                                 linker = cxx_linker;
72                 }
73                 else
74                         throw invalid_argument("GnuLinker::create_target");
75         }
76
77         const Component &comp = *objs.front()->get_component();
78         Binary *bin = 0;
79         if(arg=="shared")
80         {
81                 SharedLibrary *shlib = new SharedLibrary(builder, comp, objs);
82                 if(architecture->get_system()=="windows")
83                 {
84                         Tool &dlltool = builder.get_toolchain().get_tool("DLL");
85                         dlltool.create_target(*shlib);
86                 }
87                 bin = shlib;
88         }
89         else
90                 bin = new Executable(builder, comp, objs);
91         bin->set_tool(*linker);
92         return bin;
93 }
94
95 Target *GnuLinker::create_install(Target &target) const
96 {
97         if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(&target))
98         {
99                 Tool &copy = builder.get_toolchain().get_tool("CP");
100                 InstalledFile *inst_tgt = dynamic_cast<InstalledFile *>(copy.create_target(target));
101                 if(architecture->get_system()=="windows")
102                         builder.get_build_graph().add_installed_target(*shlib->get_import_library());
103                 else
104                 {
105                         const Pattern &pattern = architecture->get_shared_library_patterns().front();
106                         string link_name = pattern.apply(shlib->get_libname());
107                         if(link_name!=FS::basename(inst_tgt->get_path()))
108                                 inst_tgt->set_symlink(link_name);
109                 }
110                 return inst_tgt;
111         }
112         else
113                 return 0;
114 }
115
116 Task *GnuLinker::run(const Target &) const
117 {
118         throw logic_error("GnuLinker should not be run directly");
119 }
120
121
122 GnuLinker::Linker::Linker(GnuLinker &p, const string &ct):
123         SubTool(p),
124         compiler_tag(ct)
125 {
126         if(compiler_tag=="CC")
127                 set_command("gcc", true);
128         else if(compiler_tag=="CXX")
129                 set_command("g++", true);
130         else
131                 throw invalid_argument("GnuLinker::Linker::Linker");
132 }
133
134 Target *GnuLinker::Linker::create_target(const list<Target *> &sources, const string &arg)
135 {
136         return parent.create_target(sources, arg);
137 }
138
139 Target *GnuLinker::Linker::create_install(Target &target) const
140 {
141         return parent.create_install(target);
142 }
143
144 string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
145 {
146         string result = FS::basename(executable->get_path());
147         result += ',';
148         if(binfo.libmode<=BuildInfo::STATIC)
149                 result += 't';
150         else
151                 result += 'd';
152         if(binfo.strip)
153                 result += 's';
154         if(!binfo.libs.empty())
155         {
156                 result += ",l";
157                 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
158         }
159         return result;
160 }
161
162 void GnuLinker::Linker::do_prepare()
163 {
164         Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
165         if(dynamic_cast<GnuCompiler *>(&compiler))
166         {
167                 compiler.prepare();
168                 executable = compiler.get_executable();
169         }
170 }
171
172 Task *GnuLinker::Linker::run(const Target &target) const
173 {
174         const Binary &bin = dynamic_cast<const Binary &>(target);
175
176         vector<string> argv;
177         argv.push_back(executable->get_path().str());
178
179         FS::Path work_dir = bin.get_component()->get_package().get_source_directory();
180
181         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
182         {
183                 argv.push_back("-shared");
184                 argv.push_back("-fPIC");
185                 if(architecture->get_system()!="windows" && !shlib->get_soname().empty())
186                 {
187                         if(architecture->get_system()=="darwin")
188                         {
189                                 argv.push_back("-install_name");
190                                 argv.push_back(shlib->get_soname());
191
192                                 const string &ver = shlib->get_package()->get_version();
193                                 const string &if_ver = shlib->get_package()->get_interface_version();
194                                 if(!ver.empty() && !if_ver.empty())
195                                 {
196                                         argv.push_back("-current_version");
197                                         argv.push_back(ver);
198                                         argv.push_back("-compatibility_version");
199                                         argv.push_back(if_ver);
200                                 }
201                         }
202                         else
203                                 argv.push_back("-Wl,-soname,"+shlib->get_soname());
204                 }
205         }
206
207         BuildInfo binfo;
208         target.collect_build_info(binfo);
209
210         const FS::Path &sysroot = binfo.sysroot;
211         if(!sysroot.empty())
212                 argv.push_back("--sysroot="+sysroot.str());
213
214         for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
215                 argv.push_back("-L"+i->str());
216         if(binfo.strip)
217                 argv.push_back("-s");
218         if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
219                 argv.push_back("-pthread");
220
221         const Architecture &native_arch = builder.get_native_arch();
222         if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
223                 argv.push_back(format("-m%d", architecture->get_bits()));
224
225         argv.push_back("-o");
226         argv.push_back(relative(bin.get_path(), work_dir).str());
227
228         for(BuildInfo::WordList::const_iterator i=binfo.keep_symbols.begin(); i!=binfo.keep_symbols.end(); ++i)
229                 argv.push_back("-u"+*i);
230
231         bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
232
233         if(architecture->get_system()=="windows" && architecture->get_cross_prefix().find("mingw")!=string::npos)
234                 argv.push_back("-lmingw32");
235
236         const Target::Dependencies &depends = target.get_dependencies();
237         for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
238         {
239                 FileTarget *file = dynamic_cast<FileTarget *>(*i);
240                 Target *tgt = (*i)->get_real_target();
241
242                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
243                         argv.push_back(relative(obj->get_path(), work_dir).str());
244                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
245                         argv.push_back((file?file:stlib)->get_path().str());
246                 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
247                 {
248                         argv.push_back("-l"+shlib->get_libname());
249                         static_link_ok = false;
250                 }
251                 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
252                 {
253                         shlib = imp->get_shared_library();
254                         if(shlib)
255                                 argv.push_back("-l"+shlib->get_libname());
256                         else
257                                 argv.push_back((file?file:imp)->get_path().str());
258                         static_link_ok = false;
259                 }
260         }
261
262         for(BuildInfo::WordList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i)
263                 if(i->size()>10 && !i->compare(i->size()-10, 10, ".framework"))
264                 {
265                         argv.push_back("-framework");
266                         argv.push_back(i->substr(0, i->size()-10));
267                 }
268
269         if(static_link_ok)
270                 argv.push_back("-static");
271         else if(architecture->get_system()=="windows")
272                 argv.push_back("-Wl,--enable-auto-import");
273
274         return new ExternalTask(argv, work_dir);
275 }