]> git.tdb.fi Git - builder.git/blob - source/gnulinker.cpp
Add target and tools for compiling Objective-C sources
[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         result += ",l";
148         result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
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         const Component &comp = *bin.get_component();
170
171         FS::Path work_dir = comp.get_package().get_source_directory();
172
173         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
174         {
175                 argv.push_back("-shared");
176                 argv.push_back("-fPIC");
177                 if(architecture->get_system()!="windows" && !shlib->get_soname().empty())
178                 {
179                         if(architecture->get_system()=="darwin")
180                         {
181                                 argv.push_back("-install_name");
182                                 argv.push_back(shlib->get_soname());
183
184                                 const string &ver = shlib->get_package()->get_version();
185                                 const string &if_ver = shlib->get_package()->get_interface_version();
186                                 if(!ver.empty() && !if_ver.empty())
187                                 {
188                                         argv.push_back("-current_version");
189                                         argv.push_back(ver);
190                                         argv.push_back("-compatibility_version");
191                                         argv.push_back(if_ver);
192                                 }
193                         }
194                         else
195                                 argv.push_back("-Wl,-soname,"+shlib->get_soname());
196                 }
197         }
198
199         const BuildInfo &binfo = comp.get_build_info();
200         for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
201                 argv.push_back("-L"+i->str());
202         if(binfo.strip)
203                 argv.push_back("-s");
204         if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
205                 argv.push_back("-pthread");
206
207         const Architecture &native_arch = builder.get_native_arch();
208         if(architecture->get_bits()!=native_arch.get_bits())
209                 argv.push_back(format("-m%d", architecture->get_bits()));
210
211         argv.push_back("-o");
212         argv.push_back(relative(bin.get_path(), work_dir).str());
213
214         bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
215         bool need_l_objc = false;
216
217         const Target::Dependencies &depends = target.get_dependencies();
218         for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
219         {
220                 FileTarget *file = dynamic_cast<FileTarget *>(*i);
221                 Target *tgt = (*i)->get_real_target();
222
223                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
224                 {
225                         argv.push_back(relative(obj->get_path(), work_dir).str());
226                         /* XXX This is a hack.  A more generic way is needed for tools to pass
227                         information down the chain. */
228                         if(obj->get_tool()->get_tag()=="OBJC")
229                                 need_l_objc = true;
230                 }
231                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
232                         argv.push_back((file?file:stlib)->get_path().str());
233                 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
234                 {
235                         argv.push_back("-l"+shlib->get_libname());
236                         static_link_ok = false;
237                 }
238                 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
239                 {
240                         shlib = imp->get_shared_library();
241                         if(shlib)
242                                 argv.push_back("-l"+shlib->get_libname());
243                         else
244                                 argv.push_back((file?file:imp)->get_path().str());
245                         static_link_ok = false;
246                 }
247         }
248
249         if(need_l_objc)
250                 argv.push_back("-lobjc");
251         if(static_link_ok)
252                 argv.push_back("-static");
253         else if(architecture->get_system()=="windows")
254                 argv.push_back("-Wl,--enable-auto-import");
255
256         return new ExternalTask(argv, work_dir);
257 }