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