3 This file is part of builder
4 Copyright © 2006-2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/fs/dir.h>
9 #include <msp/fs/utils.h>
11 #include "component.h"
12 #include "executable.h"
15 #include "objectfile.h"
17 #include "sharedlibrary.h"
18 #include "staticlibrary.h"
19 #include "systemlibrary.h"
24 Link::Link(Builder &b, const Binary &bin):
27 const Component &comp = bin.get_component();
29 work_dir = comp.get_package().get_source();
31 //XXX Determine whether to use g++ or gcc
33 argv.push_back(builder.get_current_arch().get_tool(tool));
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");
40 if(const SharedLibrary *lib = dynamic_cast<const SharedLibrary *>(&bin))
41 if(!lib->get_soname().empty())
42 argv.push_back("-Wl,-soname,"+lib->get_soname());
44 const BuildInfo &binfo = comp.get_build_info();
45 for(list<string>::const_iterator i=binfo.ldflags.begin(); i!=binfo.ldflags.end(); ++i)
47 for(list<string>::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
48 argv.push_back("-L"+*i);
51 argv.push_back(relative(bin.get_path(), work_dir).str());
52 const TargetList &deps = bin.get_depends();
53 for(TargetList::const_iterator i=deps.begin(); i!=deps.end(); ++i)
55 Target *tgt = (*i)->get_real_target();
57 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
58 argv.push_back(relative(obj->get_path(), work_dir).str());
59 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
60 argv.push_back(stlib->get_path().str());
61 else if(Library *lib = dynamic_cast<Library *>(tgt))
62 argv.push_back("-l"+lib->get_libname());
65 FS::Path binpath = bin.get_path();
66 if(!builder.get_dry_run())
67 FS::mkpath(FS::dirname(binpath), 0755);
69 announce(comp.get_package().get_name(), tool, relative(binpath, work_dir).str().substr(2));