]> git.tdb.fi Git - builder.git/blob - source/gnuarchiver.cpp
742278de668653f17895abab2e6d003fc9698e16
[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 }
23
24 Target *GnuArchiver::create_target(const list<Target *> &sources, const string &)
25 {
26         if(sources.empty())
27                 throw invalid_argument("GnuArchiver::create_target");
28
29         list<ObjectFile *> objs;
30         for(Target *s: sources)
31         {
32                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(s))
33                         objs.push_back(obj);
34                 else
35                         throw invalid_argument("GnuArchiver::create_target");
36         }
37
38         const Component &comp = *objs.front()->get_component();
39         StaticLibrary *lib = new StaticLibrary(builder, comp, objs);
40         lib->set_tool(*this);
41         return lib;
42 }
43
44 Task *GnuArchiver::run(const Target &target) const
45 {
46         const StaticLibrary &lib = dynamic_cast<const StaticLibrary &>(target);
47         const Component &comp = *lib.get_component();
48
49         vector<string> argv;
50         argv.push_back(executable->get_path().str());
51         argv.push_back("rc");
52
53         FS::Path work_dir = comp.get_package().get_source_directory();
54
55         argv.push_back(relative(lib.get_path(), work_dir).str());
56
57         for(Target *d: lib.get_dependencies())
58                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(d))
59                         argv.push_back(relative(obj->get_path(), work_dir).str());
60
61         return new ExternalTask(argv, work_dir);
62 }