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