]> git.tdb.fi Git - builder.git/blob - source/gnulinker.cpp
The mingw port of gcc doesn't recognize -pthread
[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
120 Target *GnuLinker::Linker::create_target(const list<Target *> &sources, const string &arg)
121 {
122         return parent.create_target(sources, arg);
123 }
124
125 Target *GnuLinker::Linker::create_install(Target &target) const
126 {
127         return parent.create_install(target);
128 }
129
130 string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
131 {
132         string result = FS::basename(executable->get_path());
133         result += ',';
134         if(binfo.libmode<=BuildInfo::STATIC)
135                 result += 't';
136         else
137                 result += 'd';
138         if(binfo.strip)
139                 result += 's';
140         result += ",l";
141         result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
142         return result;
143 }
144
145 void GnuLinker::Linker::do_prepare()
146 {
147         Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
148         if(dynamic_cast<GnuCompiler *>(&compiler))
149         {
150                 compiler.prepare();
151                 executable = compiler.get_executable();
152         }
153         else
154         {
155                 string command;
156                 if(compiler_tag=="CC")
157                         command = "gcc";
158                 else if(compiler_tag=="CXX")
159                         command = "g++";
160                 else
161                         throw invalid_argument("GnuLinker::Linker::Linker");
162
163                 set_executable(command, true);
164         }
165 }
166
167 Task *GnuLinker::Linker::run(const Target &target) const
168 {
169         const Binary &bin = dynamic_cast<const Binary &>(target);
170
171         vector<string> argv;
172         argv.push_back(executable->get_path().str());
173
174         const Component &comp = *bin.get_component();
175
176         FS::Path work_dir = comp.get_package().get_source_directory();
177
178         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
179         {
180                 argv.push_back("-shared");
181                 argv.push_back("-fPIC");
182                 if(architecture->get_system()!="windows" && !shlib->get_soname().empty())
183                         argv.push_back("-Wl,-soname,"+shlib->get_soname());
184         }
185
186         const BuildInfo &binfo = comp.get_build_info();
187         for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
188                 argv.push_back("-L"+i->str());
189         if(binfo.strip)
190                 argv.push_back("-s");
191         if(binfo.threads && architecture->get_system()!="windows")
192                 argv.push_back("-pthread");
193
194         const Architecture &native_arch = builder.get_native_arch();
195         if(architecture->get_bits()!=native_arch.get_bits())
196                 argv.push_back(format("-m%d", architecture->get_bits()));
197
198         argv.push_back("-o");
199         argv.push_back(relative(bin.get_path(), work_dir).str());
200
201         bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
202
203         const Target::Dependencies &depends = target.get_dependencies();
204         for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
205         {
206                 FileTarget *file = dynamic_cast<FileTarget *>(*i);
207                 Target *tgt = (*i)->get_real_target();
208
209                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
210                         argv.push_back(relative(obj->get_path(), work_dir).str());
211                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
212                         argv.push_back((file?file:stlib)->get_path().str());
213                 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
214                 {
215                         argv.push_back("-l"+shlib->get_libname());
216                         static_link_ok = false;
217                 }
218                 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
219                 {
220                         shlib = imp->get_shared_library();
221                         if(shlib)
222                                 argv.push_back("-l"+shlib->get_libname());
223                         else
224                                 argv.push_back((file?file:imp)->get_path().str());
225                         static_link_ok = false;
226                 }
227         }
228
229         if(static_link_ok)
230                 argv.push_back("-static");
231         else if(architecture->get_system()=="windows")
232                 argv.push_back("-Wl,--enable-auto-import");
233
234         return new ExternalTask(argv, work_dir);
235 }