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