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