]> git.tdb.fi Git - builder.git/blob - source/link.cpp
Add Symlink target and associated action
[builder.git] / source / link.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/fs/dir.h>
9 #include <msp/fs/utils.h>
10 #include "builder.h"
11 #include "component.h"
12 #include "executable.h"
13 #include "install.h"
14 #include "link.h"
15 #include "objectfile.h"
16 #include "package.h"
17 #include "sharedlibrary.h"
18 #include "staticlibrary.h"
19 #include "systemlibrary.h"
20
21 using namespace std;
22 using namespace Msp;
23
24 Link::Link(Builder &b, const Binary &bin):
25         ExternalAction(b)
26 {
27         const Component &comp = bin.get_component();
28
29         work_dir = comp.get_package().get_source();
30
31         //XXX Determine whether to use g++ or gcc
32         string tool = "LXX";
33         argv.push_back(builder.get_current_arch().get_tool(tool));
34
35         if(comp.get_type()==Component::LIBRARY || comp.get_type()==Component::MODULE)
36                 argv.push_back("-shared");
37         else if(comp.get_package().get_library_mode()==ALL_STATIC)
38                 argv.push_back("-static");
39
40         if(const SharedLibrary *lib = dynamic_cast<const SharedLibrary *>(&bin))
41                 if(!lib->get_soname().empty())
42                         argv.push_back("-Wl,-soname,"+lib->get_soname());
43
44         const BuildInfo &binfo = comp.get_build_info();
45         for(list<string>::const_iterator i=binfo.ldflags.begin(); i!=binfo.ldflags.end(); ++i)
46                 argv.push_back(*i);
47         for(list<string>::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
48                 argv.push_back("-L"+*i);
49
50         argv.push_back("-o");
51         argv.push_back(relative(bin.get_path(), work_dir).str());
52         const TargetList &deps = bin.get_depends();
53         for(TargetList::const_iterator i=deps.begin(); i!=deps.end(); ++i)
54         {
55                 Target *tgt = (*i)->get_real_target();
56
57                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
58                         argv.push_back(relative(obj->get_path(), work_dir).str());
59                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
60                         argv.push_back(stlib->get_path().str());
61                 else if(Library *lib = dynamic_cast<Library *>(tgt))
62                         argv.push_back("-l"+lib->get_libname());
63         }
64
65         FS::Path binpath = bin.get_path();
66         if(!builder.get_dry_run())
67                 FS::mkpath(FS::dirname(binpath), 0755);
68
69         announce(comp.get_package().get_name(), tool, relative(binpath, work_dir).str().substr(2));
70
71         launch();
72 }