]> git.tdb.fi Git - builder.git/blob - source/gnuarchiver.cpp
Redesign the library mode system
[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 <msp/strings/format.h>
5 #include <stdexcept>
6 #include "builder.h"
7 #include "component.h"
8 #include "externaltask.h"
9 #include "gnuarchiver.h"
10 #include "objectfile.h"
11 #include "sourcepackage.h"
12 #include "staticlibrary.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 GnuArchiver::GnuArchiver(Builder &b, const Architecture &a):
18         Tool(b, a, "AR")
19 {
20         string command = "ar";
21         if(architecture->is_cross())
22                 command = format("%s-%s", architecture->get_cross_prefix(), command);
23         executable = builder.get_vfs().find_binary(command);
24
25         input_suffixes.push_back(".o");
26 }
27
28 Target *GnuArchiver::create_target(const list<Target *> &sources, const string &) const
29 {
30         if(sources.empty())
31                 throw invalid_argument("GnuArchiver::create_target");
32
33         list<ObjectFile *> objs;
34         for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
35         {
36                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
37                         objs.push_back(obj);
38                 else
39                         throw invalid_argument("GnuArchiver::create_target");
40         }
41
42         const Component &comp = *objs.front()->get_component();
43         StaticLibrary *lib = new StaticLibrary(builder, comp, objs);
44         lib->set_tool(*this);
45         return lib;
46 }
47
48 Task *GnuArchiver::run(const Target &target) const
49 {
50         const StaticLibrary &lib = dynamic_cast<const StaticLibrary &>(target);
51         const Component &comp = *lib.get_component();
52
53         vector<string> argv;
54         argv.push_back(executable->get_path().str());
55         argv.push_back("rc");
56
57         FS::Path work_dir = comp.get_package().get_source();
58
59         argv.push_back(relative(lib.get_path(), work_dir).str());
60
61         const Target::Dependencies &deps = lib.get_depends();
62         for(Target::Dependencies::const_iterator i=deps.begin(); i!=deps.end(); ++i)
63                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
64                         argv.push_back(relative(obj->get_path(), work_dir).str());
65
66         if(!builder.get_dry_run())
67         {
68                 FS::mkpath(FS::dirname(lib.get_path()), 0755);
69                 if(FS::exists(lib.get_path()))
70                         FS::unlink(lib.get_path());
71         }
72
73         return new ExternalTask(argv, work_dir);
74 }