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