]> git.tdb.fi Git - builder.git/blob - source/gnuarchiver.cpp
Use dynamic_cast to reference when incorrect type is not acceptable
[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                 objs.push_back(&dynamic_cast<ObjectFile &>(*s));
32
33         const Component &comp = *objs.front()->get_component();
34         StaticLibrary *lib = new StaticLibrary(builder, comp, objs);
35         lib->set_tool(*this);
36         return lib;
37 }
38
39 Task *GnuArchiver::run(const Target &target) const
40 {
41         const StaticLibrary &lib = dynamic_cast<const StaticLibrary &>(target);
42         const Component &comp = *lib.get_component();
43
44         vector<string> argv;
45         argv.push_back(executable->get_path().str());
46         argv.push_back("rc");
47
48         FS::Path work_dir = comp.get_package().get_source_directory();
49
50         argv.push_back(relative(lib.get_path(), work_dir).str());
51
52         for(Target *d: lib.get_dependencies())
53                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(d))
54                         argv.push_back(relative(obj->get_path(), work_dir).str());
55
56         return new ExternalTask(argv, work_dir);
57 }