]> git.tdb.fi Git - builder.git/blob - source/gnuarchiver.cpp
Redesign how tools are run
[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         set_command("ar", true);
20         input_suffixes.push_back(".o");
21         processing_unit = COMPONENT;
22         set_run(_run);
23 }
24
25 Target *GnuArchiver::create_target(const vector<Target *> &sources, const string &)
26 {
27         if(sources.empty())
28                 throw invalid_argument("GnuArchiver::create_target");
29
30         vector<ObjectFile *> objs;
31         objs.reserve(sources.size());
32         for(Target *s: sources)
33                 objs.push_back(&dynamic_cast<ObjectFile &>(*s));
34
35         const Component &comp = *objs.front()->get_component();
36         StaticLibrary *lib = new StaticLibrary(builder, comp, objs);
37         lib->set_tool(*this);
38         return lib;
39 }
40
41 Task *GnuArchiver::_run(const StaticLibrary &lib)
42 {
43         const Tool &tool = *lib.get_tool();
44         const Component &comp = *lib.get_component();
45
46         vector<string> argv;
47         argv.push_back(tool.get_executable()->get_path().str());
48         argv.push_back("rc");
49
50         FS::Path work_dir = comp.get_package().get_source_directory();
51
52         argv.push_back(relative(lib.get_path(), work_dir).str());
53
54         for(Target *d: lib.get_dependencies())
55                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(d))
56                         argv.push_back(relative(obj->get_path(), work_dir).str());
57
58         return new ExternalTask(argv, work_dir);
59 }