]> git.tdb.fi Git - builder.git/blob - source/gnuarchiver.cpp
Save caches before starting the build
[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(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 string GnuArchiver::create_build_signature(const BuildInfo &) const
45 {
46         return FS::basename(executable->get_path());
47 }
48
49 Task *GnuArchiver::run(const Target &target) const
50 {
51         const StaticLibrary &lib = dynamic_cast<const StaticLibrary &>(target);
52         const Component &comp = *lib.get_component();
53
54         vector<string> argv;
55         argv.push_back(executable->get_path().str());
56         argv.push_back("rc");
57
58         FS::Path work_dir = comp.get_package().get_source_directory();
59
60         argv.push_back(relative(lib.get_path(), work_dir).str());
61
62         const Target::Dependencies &deps = lib.get_dependencies();
63         for(Target::Dependencies::const_iterator i=deps.begin(); i!=deps.end(); ++i)
64                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
65                         argv.push_back(relative(obj->get_path(), work_dir).str());
66
67         return new ExternalTask(argv, work_dir);
68 }