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