]> git.tdb.fi Git - builder.git/blob - source/apkbuilder.cpp
Redesign how tools are run
[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 {
20         set_command("jar");
21         set_run(_run);
22 }
23
24 Target *ApkBuilder::create_target(const vector<Target *> &sources, const string &)
25 {
26         AndroidResourceBundle *resource_bundle = 0;
27         vector<FileTarget *> other_files;
28         other_files.reserve(sources.size());
29         for(Target *s: sources)
30         {
31                 if(AndroidResourceBundle *r = dynamic_cast<AndroidResourceBundle *>(s))
32                         resource_bundle = r;
33                 else if(FileTarget *f = dynamic_cast<FileTarget *>(s))
34                         other_files.push_back(f);
35         }
36         AndroidPackageFile *apk = new AndroidPackageFile(builder, *resource_bundle->get_component(), *resource_bundle, other_files);
37         apk->set_tool(*this);
38         return apk;
39 }
40
41 void ApkBuilder::do_prepare()
42 {
43         jarsigner = &builder.get_toolchain().get_tool("JSGN");
44         jarsigner->prepare();
45 }
46
47 Task *ApkBuilder::_run(const AndroidPackageFile &apk)
48 {
49         const ApkBuilder &tool = dynamic_cast<const ApkBuilder &>(*apk.get_tool());
50
51         ExternalTask::Arguments argv;
52         argv.push_back(tool.get_executable()->get_path().str());
53         argv.push_back("u");
54
55         FS::Path input_path;
56         vector<FS::Path> files;
57         files.reserve(apk.get_dependencies().size());
58         for(Target *d: apk.get_dependencies())
59         {
60                 FileTarget *file = dynamic_cast<FileTarget *>(d);
61                 Target *real = d->get_real_target();
62
63                 if(dynamic_cast<AndroidResourceBundle *>(real))
64                         input_path = file->get_path();
65                 else if(real->get_package()==apk.get_package())
66                         files.push_back(file->get_path());
67         }
68
69         FS::Path work_dir = FS::dirname(input_path);
70
71         for(const FS::Path &f: files)
72                 argv.push_back(FS::relative(f, work_dir).str());
73
74         ExternalTask *task = new ExternalTask(argv, work_dir);
75         task->set_stdin(FS::basename(input_path));
76         task->set_stdout(FS::relative(apk.get_path(), work_dir));
77         ChainedTask *chain = new ChainedTask(task);
78         chain->add_task(tool.jarsigner->run(apk));
79         return chain;
80 }