]> git.tdb.fi Git - builder.git/blob - source/gnulinker.cpp
Split GnuLinker into sub-tools and select the appropriate one in create_target
[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 "gnucxxcompiler.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 std::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         if(compiler_tag=="CC")
87                 command = "gcc";
88         else if(compiler_tag=="CXX")
89                 command = "g++";
90         else
91                 throw invalid_argument("GnuLinker::Linker::Linker");
92 }
93
94 Target *GnuLinker::Linker::create_target(const list<Target *> &sources, const string &arg) const
95 {
96         return parent.create_target(sources, arg);
97 }
98
99 Task *GnuLinker::Linker::run(const Target &target) const
100 {
101         const Binary &bin = dynamic_cast<const Binary &>(target);
102
103         vector<string> argv;
104         argv.push_back(command);
105
106         const Component &comp = *bin.get_component();
107
108         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
109         {
110                 argv.push_back("-shared");
111                 argv.push_back("-fPIC");
112                 if(!shlib->get_soname().empty())
113                         argv.push_back("-Wl,-soname,"+shlib->get_soname());
114         }
115         else if(comp.get_package().get_library_mode()==ALL_STATIC)
116                 argv.push_back("-static");
117
118         const BuildInfo &binfo = comp.get_build_info();
119         for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
120                 argv.push_back("-L"+i->str());
121         if(binfo.strip)
122                 argv.push_back("-s");
123         if(binfo.threads)
124                 argv.push_back("-pthread");
125
126         const Architecture &arch = builder.get_current_arch();
127         const Architecture &native_arch = builder.get_native_arch();
128         if(arch.get_bits()!=native_arch.get_bits())
129                 argv.push_back(format("-m%d", arch.get_bits()));
130
131         FS::Path work_dir = comp.get_package().get_source();
132
133         argv.push_back("-o");
134         argv.push_back(relative(bin.get_path(), work_dir).str());
135
136         const Target::Dependencies &depends = target.get_depends();
137         for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
138         {
139                 Target *tgt = (*i)->get_real_target();
140
141                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
142                         argv.push_back(relative(obj->get_path(), work_dir).str());
143                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
144                         argv.push_back(stlib->get_path().str());
145                 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
146                         argv.push_back("-l"+shlib->get_libname());
147         }
148
149         if(!builder.get_dry_run())
150                 FS::mkpath(FS::dirname(bin.get_path()), 0755);
151
152         return new ExternalTask(argv, work_dir);
153 }