]> git.tdb.fi Git - builder.git/blob - source/gnuarchiver.cpp
Refactor the use of external tasks in tools
[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 "gnuarchiver.h"
8 #include "objectfile.h"
9 #include "sourcepackage.h"
10 #include "staticlibrary.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 GnuArchiver::GnuArchiver(Builder &b, const Architecture &a):
16         Tool(b, &a, "AR")
17 {
18         set_command("ar", true);
19         input_suffixes.push_back(".o");
20         processing_unit = COMPONENT;
21         set_run_external(_run);
22 }
23
24 Target *GnuArchiver::create_target(const vector<Target *> &sources, const string &)
25 {
26         if(sources.empty())
27                 throw invalid_argument("GnuArchiver::create_target");
28
29         vector<ObjectFile *> objs;
30         objs.reserve(sources.size());
31         for(Target *s: sources)
32                 objs.push_back(&dynamic_cast<ObjectFile &>(*s));
33
34         const Component &comp = *objs.front()->get_component();
35         StaticLibrary *lib = new StaticLibrary(builder, comp, objs);
36         lib->set_tool(*this);
37         return lib;
38 }
39
40 ExternalTask::Arguments GnuArchiver::_run(const StaticLibrary &lib, FS::Path &work_dir)
41 {
42         const Tool &tool = *lib.get_tool();
43
44         vector<string> argv;
45         argv.push_back(tool.get_executable()->get_path().str());
46         argv.push_back("rc");
47
48         argv.push_back(relative(lib.get_path(), work_dir).str());
49
50         for(Target *d: lib.get_dependencies())
51                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(d))
52                         argv.push_back(relative(obj->get_path(), work_dir).str());
53
54         return argv;
55 }