]> git.tdb.fi Git - builder.git/blob - source/gnulinker.cpp
Use tool tag to detect C++ files rather than a specific tool class
[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
54         string command = "gcc";
55
56         const Target::Dependencies &depends = target.get_depends();
57         for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
58                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
59                 {
60                         const Tool *tool = obj->get_tool();
61                         if(tool->get_tag()=="CXX")
62                                 command = "g++";
63                 }
64
65         vector<string> argv;
66         argv.push_back(command);
67
68         const Component &comp = *bin.get_component();
69
70         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
71         {
72                 argv.push_back("-shared");
73                 argv.push_back("-fPIC");
74                 if(!shlib->get_soname().empty())
75                         argv.push_back("-Wl,-soname,"+shlib->get_soname());
76         }
77         else if(comp.get_package().get_library_mode()==ALL_STATIC)
78                 argv.push_back("-static");
79
80         const BuildInfo &binfo = comp.get_build_info();
81         for(list<string>::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
82                 argv.push_back("-L"+*i);
83         if(binfo.strip)
84                 argv.push_back("-s");
85         if(binfo.threads)
86                 argv.push_back("-pthread");
87
88         const Architecture &arch = builder.get_current_arch();
89         const Architecture &native_arch = builder.get_native_arch();
90         if(arch.get_bits()!=native_arch.get_bits())
91                 argv.push_back(format("-m%d", arch.get_bits()));
92
93         FS::Path work_dir = comp.get_package().get_source();
94
95         argv.push_back("-o");
96         argv.push_back(relative(bin.get_path(), work_dir).str());
97         for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
98         {
99                 Target *tgt = (*i)->get_real_target();
100
101                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
102                         argv.push_back(relative(obj->get_path(), work_dir).str());
103                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
104                         argv.push_back(stlib->get_path().str());
105                 else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
106                         argv.push_back("-l"+shlib->get_libname());
107         }
108
109         if(!builder.get_dry_run())
110                 FS::mkpath(FS::dirname(bin.get_path()), 0755);
111
112         return new ExternalTask(argv, work_dir);
113 }