]> git.tdb.fi Git - builder.git/blob - source/gnuarchiver.cpp
Store a target representing the executable in each tool
[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):
17         Tool(b, "AR")
18 {
19         executable = builder.get_vfs().find_binary("ar");
20
21         input_suffixes.push_back(".o");
22 }
23
24 Target *GnuArchiver::create_target(const list<Target *> &sources, const string &) const
25 {
26         if(sources.empty())
27                 throw invalid_argument("GnuArchiver::create_target");
28
29         list<ObjectFile *> objs;
30         for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
31         {
32                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
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();
54
55         argv.push_back(relative(lib.get_path(), work_dir).str());
56
57         const Target::Dependencies &deps = lib.get_depends();
58         for(Target::Dependencies::const_iterator i=deps.begin(); i!=deps.end(); ++i)
59                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
60                         argv.push_back(relative(obj->get_path(), work_dir).str());
61
62         if(!builder.get_dry_run())
63         {
64                 FS::mkpath(FS::dirname(lib.get_path()), 0755);
65                 if(FS::exists(lib.get_path()))
66                         FS::unlink(lib.get_path());
67         }
68
69         return new ExternalTask(argv, work_dir);
70 }