]> git.tdb.fi Git - builder.git/blob - source/link.cpp
Migrate from msppath to mspfs
[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/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 Executable &exe):
25         ExternalAction(b)
26 {
27         const Component &comp=exe.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(exe.get_name(), work_dir).str());
48         const TargetList &deps=exe.get_depends();
49         for(TargetList::const_iterator i=deps.begin(); i!=deps.end(); ++i)
50         {
51                 Target *tgt=*i;
52                 if(dynamic_cast<Install *>(tgt))
53                         tgt=tgt->get_depends().front();
54
55                 if(dynamic_cast<ObjectFile *>(tgt))
56                         argv.push_back(relative((*i)->get_name(), work_dir).str());
57                 else if(SharedLibrary *shlib=dynamic_cast<SharedLibrary *>(tgt))
58                         argv.push_back("-l"+shlib->get_libname());
59                 else if(dynamic_cast<StaticLibrary *>(tgt))
60                         argv.push_back((*i)->get_name());
61                 else if(SystemLibrary *syslib=dynamic_cast<SystemLibrary *>(tgt))
62                         argv.push_back("-l"+syslib->get_libname());
63         }
64
65         FS::Path epath=exe.get_name();
66         if(!builder.get_dry_run())
67                 FS::mkpath(FS::dirname(epath), 0755);
68
69         announce(comp.get_package().get_name(), tool, relative(epath, work_dir).str());
70
71         launch();
72 }