3 #include <msp/fs/dir.h>
4 #include <msp/fs/utils.h>
7 #include "executable.h"
8 #include "externaltask.h"
9 #include "gnucxxcompiler.h"
10 #include "gnulinker.h"
11 #include "objectfile.h"
12 #include "sharedlibrary.h"
13 #include "sourcepackage.h"
14 #include "staticlibrary.h"
19 GnuLinker::GnuLinker(Builder &b):
22 input_suffixes.push_back(".o");
23 input_suffixes.push_back(".a");
26 Target *GnuLinker::create_target(const list<Target *> &sources, const std::string &arg) const
29 throw invalid_argument("GnuLinker::create_target");
30 list<ObjectFile *> objs;
31 for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
33 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
36 throw invalid_argument("GnuLinker::create_target");
39 const Component &comp = objs.front()->get_component();
42 bin = new SharedLibrary(builder, comp, objs);
44 bin = new Executable(builder, comp, objs);
49 Task *GnuLinker::run(const Target &target) const
51 const Binary &bin = dynamic_cast<const Binary &>(target);
52 const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin);
54 string command = "gcc";
56 const list<Target *> &depends = target.get_depends();
57 for(list<Target *>::const_iterator i=depends.begin(); i!=depends.end(); ++i)
58 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
60 const Tool *tool = obj->get_tool();
61 if(dynamic_cast<const GnuCxxCompiler *>(tool))
66 argv.push_back(command);
68 const Component &comp = bin.get_component();
72 argv.push_back("-shared");
73 if(!shlib->get_soname().empty())
74 argv.push_back("-Wl,-soname,"+shlib->get_soname());
76 else if(comp.get_package().get_library_mode()==ALL_STATIC)
77 argv.push_back("-static");
79 const BuildInfo &binfo = comp.get_build_info();
80 for(list<string>::const_iterator i=binfo.ldflags.begin(); i!=binfo.ldflags.end(); ++i)
82 for(list<string>::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
83 argv.push_back("-L"+*i);
85 FS::Path work_dir = comp.get_package().get_source();
88 argv.push_back(relative(bin.get_path(), work_dir).str());
89 for(TargetList::const_iterator i=depends.begin(); i!=depends.end(); ++i)
91 Target *tgt = (*i)->get_real_target();
93 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
94 argv.push_back(relative(obj->get_path(), work_dir).str());
95 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
96 argv.push_back(stlib->get_path().str());
97 else if(Library *lib = dynamic_cast<Library *>(tgt))
98 argv.push_back("-l"+lib->get_libname());
101 if(!builder.get_dry_run())
102 FS::mkpath(FS::dirname(bin.get_path()), 0755);
104 return new ExternalTask(argv, work_dir);