]> git.tdb.fi Git - builder.git/blob - source/apkbuilder.cpp
Replace basic for loops with range-based loops or algorithms
[builder.git] / source / apkbuilder.cpp
1 #include <msp/fs/utils.h>
2 #include "androidpackagefile.h"
3 #include "androidresourcebundle.h"
4 #include "apkbuilder.h"
5 #include "builder.h"
6 #include "chainedtask.h"
7 #include "component.h"
8 #include "externaltask.h"
9 #include "filetarget.h"
10 #include "sourcepackage.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 // TODO Separate jar into its own tool and have this one just chain the two
16
17 ApkBuilder::ApkBuilder(Builder &b):
18         Tool(b, "APK"),
19         jarsigner(0)
20 {
21         set_command("jar");
22 }
23
24 Target *ApkBuilder::create_target(const list<Target *> &sources, const string &)
25 {
26         AndroidResourceBundle *resource_bundle = 0;
27         list<FileTarget *> other_files;
28         for(Target *s: sources)
29         {
30                 if(AndroidResourceBundle *r = dynamic_cast<AndroidResourceBundle *>(s))
31                         resource_bundle = r;
32                 else if(FileTarget *f = dynamic_cast<FileTarget *>(s))
33                         other_files.push_back(f);
34         }
35         AndroidPackageFile *apk = new AndroidPackageFile(builder, *resource_bundle->get_component(), *resource_bundle, other_files);
36         apk->set_tool(*this);
37         return apk;
38 }
39
40 void ApkBuilder::do_prepare()
41 {
42         jarsigner = &builder.get_toolchain().get_tool("JSGN");
43         jarsigner->prepare();
44 }
45
46 Task *ApkBuilder::run(const Target &tgt) const
47 {
48         const AndroidPackageFile &apk = dynamic_cast<const AndroidPackageFile &>(tgt);
49
50         ExternalTask::Arguments argv;
51         argv.push_back(executable->get_path().str());
52         argv.push_back("u");
53
54         FS::Path input_path;
55         list<FS::Path> files;
56         for(Target *d: apk.get_dependencies())
57         {
58                 FileTarget *file = dynamic_cast<FileTarget *>(d);
59                 Target *real = d->get_real_target();
60
61                 if(dynamic_cast<AndroidResourceBundle *>(real))
62                         input_path = file->get_path();
63                 else if(real->get_package()==apk.get_package())
64                         files.push_back(file->get_path());
65         }
66
67         FS::Path work_dir = FS::dirname(input_path);
68
69         for(const FS::Path &f: files)
70                 argv.push_back(FS::relative(f, work_dir).str());
71
72         ExternalTask *task = new ExternalTask(argv, work_dir);
73         task->set_stdin(FS::basename(input_path));
74         task->set_stdout(FS::relative(apk.get_path(), work_dir));
75         ChainedTask *chain = new ChainedTask(task);
76         chain->add_task(jarsigner->run(tgt));
77         return chain;
78 }