1 #include <msp/fs/dir.h>
2 #include <msp/fs/stat.h>
3 #include <msp/fs/utils.h>
4 #include <msp/strings/format.h>
8 #include "externaltask.h"
9 #include "gnuarchiver.h"
10 #include "objectfile.h"
11 #include "sourcepackage.h"
12 #include "staticlibrary.h"
17 GnuArchiver::GnuArchiver(Builder &b, const Architecture &a):
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);
25 input_suffixes.push_back(".o");
28 Target *GnuArchiver::create_target(const list<Target *> &sources, const string &) const
31 throw invalid_argument("GnuArchiver::create_target");
33 list<ObjectFile *> objs;
34 for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
36 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
39 throw invalid_argument("GnuArchiver::create_target");
42 const Component &comp = *objs.front()->get_component();
43 StaticLibrary *lib = new StaticLibrary(builder, comp, objs);
48 Task *GnuArchiver::run(const Target &target) const
50 const StaticLibrary &lib = dynamic_cast<const StaticLibrary &>(target);
51 const Component &comp = *lib.get_component();
54 argv.push_back(executable->get_path().str());
57 FS::Path work_dir = comp.get_package().get_source();
59 argv.push_back(relative(lib.get_path(), work_dir).str());
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());
66 if(!builder.get_dry_run())
68 FS::mkpath(FS::dirname(lib.get_path()), 0755);
69 if(FS::exists(lib.get_path()))
70 FS::unlink(lib.get_path());
73 return new ExternalTask(argv, work_dir);