]> git.tdb.fi Git - builder.git/blob - source/gnulinker.cpp
Use Path objects to store include and library paths
[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
40 Target *GnuLinker::create_target(const list<Target *> &sources, const std::string &arg) const
41 {
42         if(sources.empty())
43                 throw invalid_argument("GnuLinker::create_target");
44         list<ObjectFile *> objs;
45         for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
46         {
47                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
48                         objs.push_back(obj);
49                 else
50                         throw invalid_argument("GnuLinker::create_target");
51         }
52
53         const Component &comp = objs.front()->get_component();
54         Binary *bin = 0;
55         if(arg=="shared")
56                 bin = new SharedLibrary(builder, comp, objs);
57         else
58                 bin = new Executable(builder, comp, objs);
59         bin->set_tool(*this);
60         return bin;
61 }
62
63 Task *GnuLinker::run(const Target &target) const
64 {
65         const Binary &bin = dynamic_cast<const Binary &>(target);
66
67         string command = "gcc";
68
69         const Target::Dependencies &depends = target.get_depends();
70         for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
71                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
72                 {
73                         const Tool *tool = obj->get_tool();
74                         if(tool->get_tag()=="CXX")
75                                 command = "g++";
76                 }
77
78         vector<string> argv;
79         argv.push_back(command);
80
81         const Component &comp = *bin.get_component();
82
83         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
84         {
85                 argv.push_back("-shared");
86                 argv.push_back("-fPIC");
87                 if(!shlib->get_soname().empty())
88                         argv.push_back("-Wl,-soname,"+shlib->get_soname());
89         }
90         else if(comp.get_package().get_library_mode()==ALL_STATIC)
91                 argv.push_back("-static");
92
93         const BuildInfo &binfo = comp.get_build_info();
94         for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
95                 argv.push_back("-L"+i->str());
96         if(binfo.strip)
97                 argv.push_back("-s");
98         if(binfo.threads)
99                 argv.push_back("-pthread");
100
101         const Architecture &arch = builder.get_current_arch();
102         const Architecture &native_arch = builder.get_native_arch();
103         if(arch.get_bits()!=native_arch.get_bits())
104                 argv.push_back(format("-m%d", arch.get_bits()));
105
106         FS::Path work_dir = comp.get_package().get_source();
107
108         argv.push_back("-o");
109         argv.push_back(relative(bin.get_path(), work_dir).str());
110         for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
111         {
112                 Target *tgt = (*i)->get_real_target();
113
114                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
115                         argv.push_back(relative(obj->get_path(), work_dir).str());
116                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
117                         argv.push_back(stlib->get_path().str());
118                 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
119                         argv.push_back("-l"+shlib->get_libname());
120         }
121
122         if(!builder.get_dry_run())
123                 FS::mkpath(FS::dirname(bin.get_path()), 0755);
124
125         return new ExternalTask(argv, work_dir);
126 }