]> git.tdb.fi Git - builder.git/blob - source/gnuarchiver.cpp
Add a utility function for setting the executable for a Tool
[builder.git] / source / gnuarchiver.cpp
1 #include <msp/fs/dir.h>
2 #include <msp/fs/stat.h>
3 #include <msp/fs/utils.h>
4 #include <stdexcept>
5 #include "builder.h"
6 #include "component.h"
7 #include "externaltask.h"
8 #include "gnuarchiver.h"
9 #include "objectfile.h"
10 #include "sourcepackage.h"
11 #include "staticlibrary.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 GnuArchiver::GnuArchiver(Builder &b, const Architecture &a):
17         Tool(b, a, "AR")
18 {
19         input_suffixes.push_back(".o");
20 }
21
22 Target *GnuArchiver::create_target(const list<Target *> &sources, const string &)
23 {
24         if(sources.empty())
25                 throw invalid_argument("GnuArchiver::create_target");
26
27         list<ObjectFile *> objs;
28         for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
29         {
30                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
31                         objs.push_back(obj);
32                 else
33                         throw invalid_argument("GnuArchiver::create_target");
34         }
35
36         const Component &comp = *objs.front()->get_component();
37         StaticLibrary *lib = new StaticLibrary(builder, comp, objs);
38         lib->set_tool(*this);
39         return lib;
40 }
41
42 void GnuArchiver::do_prepare()
43 {
44         set_executable("ar", true);
45 }
46
47 Task *GnuArchiver::run(const Target &target) const
48 {
49         const StaticLibrary &lib = dynamic_cast<const StaticLibrary &>(target);
50         const Component &comp = *lib.get_component();
51
52         vector<string> argv;
53         argv.push_back(executable->get_path().str());
54         argv.push_back("rc");
55
56         FS::Path work_dir = comp.get_package().get_source_directory();
57
58         argv.push_back(relative(lib.get_path(), work_dir).str());
59
60         const Target::Dependencies &deps = lib.get_dependencies();
61         for(Target::Dependencies::const_iterator i=deps.begin(); i!=deps.end(); ++i)
62                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
63                         argv.push_back(relative(obj->get_path(), work_dir).str());
64
65         return new ExternalTask(argv, work_dir);
66 }