]> git.tdb.fi Git - builder.git/blob - source/gnulinker.cpp
Have the linker specify symlinks for installed libraries
[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 "externaltask.h"
11 #include "gnucompiler.h"
12 #include "gnulinker.h"
13 #include "installedfile.h"
14 #include "objectfile.h"
15 #include "sharedlibrary.h"
16 #include "sourcepackage.h"
17 #include "staticlibrary.h"
18
19 using namespace std;
20 using namespace Msp;
21
22 GnuLinker::GnuLinker(Builder &b, const Architecture &a):
23         Tool(b, a, "LINK")
24 {
25         input_suffixes.push_back(".o");
26         input_suffixes.push_back(".a");
27
28         if(architecture->is_native())
29         {
30                 system_path.push_back("/lib");
31                 system_path.push_back("/usr/lib");
32                 if(architecture->match_name("pc-32-linux"))
33                         system_path.push_back("/usr/lib/i386-linux-gnu");
34                 else if(architecture->match_name("pc-64-linux"))
35                         system_path.push_back("/usr/lib/x86_64-linux-gnu");
36         }
37         else
38                 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/lib");
39
40         default_linker = new Linker(*this, "CC");
41         cxx_linker = new Linker(*this, "CXX");
42 }
43
44 GnuLinker::~GnuLinker()
45 {
46         delete default_linker;
47         delete cxx_linker;
48 }
49
50 Target *GnuLinker::create_target(const list<Target *> &sources, const string &arg) const
51 {
52         if(sources.empty())
53                 throw invalid_argument("GnuLinker::create_target");
54         list<ObjectFile *> objs;
55         Linker *linker = default_linker;
56         for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
57         {
58                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
59                 {
60                         objs.push_back(obj);
61                         if(obj->get_tool()->get_tag()=="CXX")
62                                 linker = cxx_linker;
63                 }
64                 else
65                         throw invalid_argument("GnuLinker::create_target");
66         }
67
68         const Component &comp = *objs.front()->get_component();
69         Binary *bin = 0;
70         if(arg=="shared")
71                 bin = new SharedLibrary(builder, comp, objs);
72         else
73                 bin = new Executable(builder, comp, objs);
74         bin->set_tool(*linker);
75         return bin;
76 }
77
78 Target *GnuLinker::create_install(Target &target) const
79 {
80         if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(&target))
81         {
82                 const Tool &copy = builder.get_toolchain().get_tool("CP");
83                 InstalledFile *inst_tgt = dynamic_cast<InstalledFile *>(copy.create_target(target));
84                 const Pattern &pattern = architecture->get_shared_library_patterns().front();
85                 inst_tgt->set_symlink(pattern.apply(shlib->get_libname()));
86                 return inst_tgt;
87         }
88         else
89                 return 0;
90 }
91
92 Task *GnuLinker::run(const Target &) const
93 {
94         throw logic_error("GnuLinker should not be run directly");
95 }
96
97
98 GnuLinker::Linker::Linker(GnuLinker &p, const string &compiler_tag):
99         SubTool(p)
100 {
101         const Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
102         if(dynamic_cast<const GnuCompiler *>(&compiler))
103                 executable = compiler.get_executable();
104         else
105         {
106                 string command;
107                 if(compiler_tag=="CC")
108                         command = "gcc";
109                 else if(compiler_tag=="CXX")
110                         command = "g++";
111                 else
112                         throw invalid_argument("GnuLinker::Linker::Linker");
113                 if(architecture->is_cross())
114                         command = format("%s-%s", architecture->get_cross_prefix(), command);
115                 executable = builder.get_vfs().find_binary(command);
116         }
117 }
118
119 Target *GnuLinker::Linker::create_target(const list<Target *> &sources, const string &arg) const
120 {
121         return parent.create_target(sources, arg);
122 }
123
124 Target *GnuLinker::Linker::create_install(Target &target) const
125 {
126         return parent.create_install(target);
127 }
128
129 string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
130 {
131         string result = FS::basename(executable->get_path());
132         result += ',';
133         if(binfo.libmode<=BuildInfo::STATIC)
134                 result += 't';
135         else
136                 result += 'd';
137         if(binfo.strip)
138                 result += 's';
139         result += ",l";
140         result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
141         return result;
142 }
143
144 Task *GnuLinker::Linker::run(const Target &target) const
145 {
146         const Binary &bin = dynamic_cast<const Binary &>(target);
147
148         vector<string> argv;
149         argv.push_back(executable->get_path().str());
150
151         const Component &comp = *bin.get_component();
152
153         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
154         {
155                 argv.push_back("-shared");
156                 argv.push_back("-fPIC");
157                 if(!shlib->get_soname().empty())
158                         argv.push_back("-Wl,-soname,"+shlib->get_soname());
159         }
160
161         const BuildInfo &binfo = comp.get_build_info();
162         for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
163                 argv.push_back("-L"+i->str());
164         if(binfo.strip)
165                 argv.push_back("-s");
166         if(binfo.threads)
167                 argv.push_back("-pthread");
168
169         const Architecture &native_arch = builder.get_native_arch();
170         if(architecture->get_bits()!=native_arch.get_bits())
171                 argv.push_back(format("-m%d", architecture->get_bits()));
172
173         FS::Path work_dir = comp.get_package().get_source_directory();
174
175         argv.push_back("-o");
176         argv.push_back(relative(bin.get_path(), work_dir).str());
177
178         bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
179
180         const Target::Dependencies &depends = target.get_dependencies();
181         for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
182         {
183                 Target *tgt = (*i)->get_real_target();
184
185                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
186                         argv.push_back(relative(obj->get_path(), work_dir).str());
187                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
188                         argv.push_back(stlib->get_path().str());
189                 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
190                 {
191                         argv.push_back("-l"+shlib->get_libname());
192                         static_link_ok = false;
193                 }
194         }
195
196         if(static_link_ok)
197                 argv.push_back("-static");
198         else if(architecture->get_system()=="windows")
199                 argv.push_back("-Wl,--enable-auto-import");
200
201         return new ExternalTask(argv, work_dir);
202 }