3 #include <msp/fs/dir.h>
4 #include <msp/fs/utils.h>
5 #include <msp/strings/format.h>
8 #include "executable.h"
9 #include "externaltask.h"
10 #include "gnucxxcompiler.h"
11 #include "gnulinker.h"
12 #include "objectfile.h"
13 #include "sharedlibrary.h"
14 #include "sourcepackage.h"
15 #include "staticlibrary.h"
20 GnuLinker::GnuLinker(Builder &b):
23 input_suffixes.push_back(".o");
24 input_suffixes.push_back(".a");
27 Target *GnuLinker::create_target(const list<Target *> &sources, const std::string &arg) const
30 throw invalid_argument("GnuLinker::create_target");
31 list<ObjectFile *> objs;
32 for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
34 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
37 throw invalid_argument("GnuLinker::create_target");
40 const Component &comp = objs.front()->get_component();
43 bin = new SharedLibrary(builder, comp, objs);
45 bin = new Executable(builder, comp, objs);
50 Task *GnuLinker::run(const Target &target) const
52 const Binary &bin = dynamic_cast<const Binary &>(target);
53 const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin);
55 string command = "gcc";
57 const Target::Dependencies &depends = target.get_depends();
58 for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
59 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
61 const Tool *tool = obj->get_tool();
62 if(dynamic_cast<const GnuCxxCompiler *>(tool))
67 argv.push_back(command);
69 const Component &comp = bin.get_component();
73 argv.push_back("-shared");
74 if(!shlib->get_soname().empty())
75 argv.push_back("-Wl,-soname,"+shlib->get_soname());
77 else if(comp.get_package().get_library_mode()==ALL_STATIC)
78 argv.push_back("-static");
80 const BuildInfo &binfo = comp.get_build_info();
81 for(list<string>::const_iterator i=binfo.ldflags.begin(); i!=binfo.ldflags.end(); ++i)
83 for(list<string>::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
84 argv.push_back("-L"+*i);
86 const Architecture &arch = builder.get_current_arch();
87 const Architecture &native_arch = builder.get_native_arch();
88 if(arch.get_bits()!=native_arch.get_bits())
89 argv.push_back(format("-m%d", arch.get_bits()));
91 FS::Path work_dir = comp.get_package().get_source();
94 argv.push_back(relative(bin.get_path(), work_dir).str());
95 for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
97 Target *tgt = (*i)->get_real_target();
99 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
100 argv.push_back(relative(obj->get_path(), work_dir).str());
101 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
102 argv.push_back(stlib->get_path().str());
103 else if(Library *lib = dynamic_cast<Library *>(tgt))
104 argv.push_back("-l"+lib->get_libname());
107 if(!builder.get_dry_run())
108 FS::mkpath(FS::dirname(bin.get_path()), 0755);
110 return new ExternalTask(argv, work_dir);