]> git.tdb.fi Git - builder.git/blob - source/gnulinker.cpp
Remove extraneous std:: qualifiers
[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 "builder.h"
7 #include "component.h"
8 #include "executable.h"
9 #include "externaltask.h"
10 #include "gnucompiler.h"
11 #include "gnulinker.h"
12 #include "objectfile.h"
13 #include "sharedlibrary.h"
14 #include "sourcepackage.h"
15 #include "staticlibrary.h"
16
17 using namespace std;
18 using namespace Msp;
19
20 GnuLinker::GnuLinker(Builder &b):
21         Tool(b, "LINK")
22 {
23         input_suffixes.push_back(".o");
24         input_suffixes.push_back(".a");
25
26         const Architecture &arch = builder.get_current_arch();
27         if(arch.is_native())
28         {
29                 system_path.push_back("/lib");
30                 system_path.push_back("/usr/lib");
31                 if(arch.match_name("pc-32-linux"))
32                         system_path.push_back("/usr/lib/i386-linux-gnu");
33                 else if(arch.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/"+arch.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                 executable = builder.get_vfs().find_binary(command);
99         }
100 }
101
102 Target *GnuLinker::Linker::create_target(const list<Target *> &sources, const string &arg) const
103 {
104         return parent.create_target(sources, arg);
105 }
106
107 Task *GnuLinker::Linker::run(const Target &target) const
108 {
109         const Binary &bin = dynamic_cast<const Binary &>(target);
110
111         vector<string> argv;
112         argv.push_back(executable->get_path().str());
113
114         const Component &comp = *bin.get_component();
115
116         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
117         {
118                 argv.push_back("-shared");
119                 argv.push_back("-fPIC");
120                 if(!shlib->get_soname().empty())
121                         argv.push_back("-Wl,-soname,"+shlib->get_soname());
122         }
123         else if(comp.get_package().get_library_mode()==ALL_STATIC)
124                 argv.push_back("-static");
125
126         const BuildInfo &binfo = comp.get_build_info();
127         for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
128                 argv.push_back("-L"+i->str());
129         if(binfo.strip)
130                 argv.push_back("-s");
131         if(binfo.threads)
132                 argv.push_back("-pthread");
133
134         const Architecture &arch = builder.get_current_arch();
135         const Architecture &native_arch = builder.get_native_arch();
136         if(arch.get_bits()!=native_arch.get_bits())
137                 argv.push_back(format("-m%d", arch.get_bits()));
138
139         FS::Path work_dir = comp.get_package().get_source();
140
141         argv.push_back("-o");
142         argv.push_back(relative(bin.get_path(), work_dir).str());
143
144         const Target::Dependencies &depends = target.get_depends();
145         for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
146         {
147                 Target *tgt = (*i)->get_real_target();
148
149                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
150                         argv.push_back(relative(obj->get_path(), work_dir).str());
151                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
152                         argv.push_back(stlib->get_path().str());
153                 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
154                         argv.push_back("-l"+shlib->get_libname());
155         }
156
157         if(!builder.get_dry_run())
158                 FS::mkpath(FS::dirname(bin.get_path()), 0755);
159
160         return new ExternalTask(argv, work_dir);
161 }