]> git.tdb.fi Git - builder.git/blob - source/link.cpp
b165bad72550900f75c9e82400a86dd91f0816d7
[builder.git] / source / link.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2009  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         const BuildInfo &binfo=comp.get_build_info();
41         for(list<string>::const_iterator i=binfo.ldflags.begin(); i!=binfo.ldflags.end(); ++i)
42                 argv.push_back(*i);
43         for(list<string>::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
44                 argv.push_back("-L"+*i);
45
46         argv.push_back("-o");
47         argv.push_back(relative(bin.get_path(), work_dir).str());
48         const TargetList &deps=bin.get_depends();
49         for(TargetList::const_iterator i=deps.begin(); i!=deps.end(); ++i)
50         {
51                 Target *tgt=*i;
52                 if(Install *inst=dynamic_cast<Install *>(tgt))
53                         tgt=&inst->get_source();
54
55                 if(ObjectFile *obj=dynamic_cast<ObjectFile *>(tgt))
56                         argv.push_back(relative(obj->get_path(), work_dir).str());
57                 else if(StaticLibrary *stlib=dynamic_cast<StaticLibrary *>(tgt))
58                         argv.push_back(stlib->get_path().str());
59                 else if(Library *lib=dynamic_cast<Library *>(tgt))
60                         argv.push_back("-l"+lib->get_libname());
61         }
62
63         FS::Path binpath=bin.get_path();
64         if(!builder.get_dry_run())
65                 FS::mkpath(FS::dirname(binpath), 0755);
66
67         announce(comp.get_package().get_name(), tool, relative(binpath, work_dir).str());
68
69         launch();
70 }