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