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