]> git.tdb.fi Git - builder.git/blob - source/gnulinker.cpp
Get rid of the global TargetList typedef
[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 "builder.h"
6 #include "component.h"
7 #include "executable.h"
8 #include "externaltask.h"
9 #include "gnucxxcompiler.h"
10 #include "gnulinker.h"
11 #include "objectfile.h"
12 #include "sharedlibrary.h"
13 #include "sourcepackage.h"
14 #include "staticlibrary.h"
15
16 using namespace std;
17 using namespace Msp;
18
19 GnuLinker::GnuLinker(Builder &b):
20         Tool(b, "LINK")
21 {
22         input_suffixes.push_back(".o");
23         input_suffixes.push_back(".a");
24 }
25
26 Target *GnuLinker::create_target(const list<Target *> &sources, const std::string &arg) const
27 {
28         if(sources.empty())
29                 throw invalid_argument("GnuLinker::create_target");
30         list<ObjectFile *> objs;
31         for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
32         {
33                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
34                         objs.push_back(obj);
35                 else
36                         throw invalid_argument("GnuLinker::create_target");
37         }
38
39         const Component &comp = objs.front()->get_component();
40         Binary *bin = 0;
41         if(arg=="shared")
42                 bin = new SharedLibrary(builder, comp, objs);
43         else
44                 bin = new Executable(builder, comp, objs);
45         bin->set_tool(*this);
46         return bin;
47 }
48
49 Task *GnuLinker::run(const Target &target) const
50 {
51         const Binary &bin = dynamic_cast<const Binary &>(target);
52         const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin);
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(dynamic_cast<const GnuCxxCompiler *>(tool))
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(shlib)
71         {
72                 argv.push_back("-shared");
73                 if(!shlib->get_soname().empty())
74                         argv.push_back("-Wl,-soname,"+shlib->get_soname());
75         }
76         else if(comp.get_package().get_library_mode()==ALL_STATIC)
77                 argv.push_back("-static");
78
79         const BuildInfo &binfo = comp.get_build_info();
80         for(list<string>::const_iterator i=binfo.ldflags.begin(); i!=binfo.ldflags.end(); ++i)
81                 argv.push_back(*i);
82         for(list<string>::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
83                 argv.push_back("-L"+*i);
84
85         FS::Path work_dir = comp.get_package().get_source();
86
87         argv.push_back("-o");
88         argv.push_back(relative(bin.get_path(), work_dir).str());
89         for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
90         {
91                 Target *tgt = (*i)->get_real_target();
92
93                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
94                         argv.push_back(relative(obj->get_path(), work_dir).str());
95                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
96                         argv.push_back(stlib->get_path().str());
97                 else if(Library *lib = dynamic_cast<Library *>(tgt))
98                         argv.push_back("-l"+lib->get_libname());
99         }
100
101         if(!builder.get_dry_run())
102                 FS::mkpath(FS::dirname(bin.get_path()), 0755);
103
104         return new ExternalTask(argv, work_dir);
105 }