]> git.tdb.fi Git - builder.git/blob - source/gnulinker.cpp
a93f809bfa1df263531d157d063ddf35ab12c441
[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 string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
135 {
136         string result = FS::basename(executable->get_path());
137         result += ',';
138         if(binfo.libmode<=BuildInfo::STATIC)
139                 result += 't';
140         else
141                 result += 'd';
142         if(binfo.strip)
143                 result += 's';
144         if(!binfo.libs.empty())
145         {
146                 result += ",l";
147                 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
148         }
149         return result;
150 }
151
152 void GnuLinker::Linker::do_prepare()
153 {
154         Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
155         if(dynamic_cast<GnuCompiler *>(&compiler))
156         {
157                 compiler.prepare();
158                 executable = compiler.get_executable();
159         }
160 }
161
162 Task *GnuLinker::Linker::run(const Target &target) const
163 {
164         const Binary &bin = dynamic_cast<const Binary &>(target);
165
166         vector<string> argv;
167         argv.push_back(executable->get_path().str());
168
169         FS::Path work_dir = bin.get_component()->get_package().get_source_directory();
170
171         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
172         {
173                 argv.push_back("-shared");
174                 argv.push_back("-fPIC");
175                 if(architecture->get_system()!="windows" && !shlib->get_soname().empty())
176                 {
177                         if(architecture->get_system()=="darwin")
178                         {
179                                 argv.push_back("-install_name");
180                                 argv.push_back(shlib->get_soname());
181
182                                 const string &ver = shlib->get_package()->get_version();
183                                 const string &if_ver = shlib->get_package()->get_interface_version();
184                                 if(!ver.empty() && !if_ver.empty())
185                                 {
186                                         argv.push_back("-current_version");
187                                         argv.push_back(ver);
188                                         argv.push_back("-compatibility_version");
189                                         argv.push_back(if_ver);
190                                 }
191                         }
192                         else
193                                 argv.push_back("-Wl,-soname,"+shlib->get_soname());
194                 }
195         }
196
197         BuildInfo binfo;
198         target.collect_build_info(binfo);
199
200         const FS::Path &sysroot = binfo.sysroot;
201         if(!sysroot.empty())
202                 argv.push_back("--sysroot="+sysroot.str());
203
204         for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
205                 argv.push_back("-L"+i->str());
206         if(binfo.strip)
207                 argv.push_back("-s");
208         if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
209                 argv.push_back("-pthread");
210
211         const Architecture &native_arch = builder.get_native_arch();
212         if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
213                 argv.push_back(format("-m%d", architecture->get_bits()));
214
215         argv.push_back("-o");
216         argv.push_back(relative(bin.get_path(), work_dir).str());
217
218         for(BuildInfo::WordList::const_iterator i=binfo.keep_symbols.begin(); i!=binfo.keep_symbols.end(); ++i)
219                 argv.push_back("-u"+*i);
220
221         bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
222
223         if(architecture->get_system()=="windows" && architecture->get_cross_prefix().find("mingw")!=string::npos)
224                 argv.push_back("-lmingw32");
225
226         const Target::Dependencies &depends = target.get_dependencies();
227         for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
228         {
229                 FileTarget *file = dynamic_cast<FileTarget *>(*i);
230                 Target *tgt = (*i)->get_real_target();
231
232                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
233                         argv.push_back(relative(obj->get_path(), work_dir).str());
234                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
235                         argv.push_back((file?file:stlib)->get_path().str());
236                 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
237                 {
238                         argv.push_back("-l"+shlib->get_libname());
239                         static_link_ok = false;
240                 }
241                 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
242                 {
243                         shlib = imp->get_shared_library();
244                         if(shlib)
245                                 argv.push_back("-l"+shlib->get_libname());
246                         else
247                                 argv.push_back((file?file:imp)->get_path().str());
248                         static_link_ok = false;
249                 }
250         }
251
252         for(BuildInfo::WordList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i)
253                 if(i->size()>10 && !i->compare(i->size()-10, 10, ".framework"))
254                 {
255                         argv.push_back("-framework");
256                         argv.push_back(i->substr(0, i->size()-10));
257                 }
258
259         if(static_link_ok)
260                 argv.push_back("-static");
261         else if(architecture->get_system()=="windows")
262                 argv.push_back("-Wl,--enable-auto-import");
263
264         return new ExternalTask(argv, work_dir);
265 }