]> git.tdb.fi Git - builder.git/blob - source/androidassetpackagingtool.cpp
Slight updates to Readme
[builder.git] / source / androidassetpackagingtool.cpp
1 #include <msp/fs/utils.h>
2 #include "androidassetpackagingtool.h"
3 #include "androidmanifestfile.h"
4 #include "androidresourcebundle.h"
5 #include "androidtools.h"
6 #include "component.h"
7 #include "externaltask.h"
8 #include "sourcepackage.h"
9
10 using namespace std;
11 using namespace Msp;
12
13 AndroidAssetPackagingTool::AndroidAssetPackagingTool(Builder &b, const AndroidSdk &s):
14         Tool(b, "AAPT"),
15         sdk(s)
16 {
17         if(sdk.get_root_dir().empty())
18                 problems.push_back("Android SDK not found");
19         else if(sdk.get_build_tools_dir().empty())
20                 problems.push_back("Android build-tools not found");
21         else
22                 set_command((sdk.get_build_tools_dir()/"aapt").str());
23
24         if(sdk.get_platform_jar().empty())
25                 problems.push_back("Android platform not found");
26 }
27
28 Target *AndroidAssetPackagingTool::create_target(const list<Target *> &sources, const string &)
29 {
30         AndroidManifestFile *manifest = 0;
31         list<FileTarget *> resources;
32         for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
33         {
34                 if(AndroidManifestFile *m = dynamic_cast<AndroidManifestFile *>(*i))
35                         manifest = m;
36                 else if(FileTarget *f = dynamic_cast<FileTarget *>(*i))
37                         resources.push_back(f);
38         }
39
40         if(!manifest)
41                 throw invalid_argument("AndroidAssetPackagingTool::create_target");
42
43         AndroidResourceBundle *res = new AndroidResourceBundle(builder, *manifest->get_component(), *manifest, resources);
44         res->set_tool(*this);
45         return res;
46 }
47
48 Task *AndroidAssetPackagingTool::run(const Target &tgt) const
49 {
50         const AndroidResourceBundle &res = dynamic_cast<const AndroidResourceBundle &>(tgt);
51
52         ExternalTask::Arguments argv;
53         argv.push_back(executable->get_path().str());
54         argv.push_back("package");
55
56         FS::Path work_dir = res.get_component()->get_package().get_source_directory();
57
58         argv.push_back("-I");
59         argv.push_back(sdk.get_platform_jar().str());
60
61         argv.push_back("-F");
62         argv.push_back(FS::relative(res.get_path(), work_dir).str());
63
64         const Target::Dependencies &depends = res.get_dependencies();
65         list<FS::Path> resource_dirs;
66         for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
67         {
68                 FileTarget *file = dynamic_cast<FileTarget *>(*i);
69                 Target *real = (*i)->get_real_target();
70
71                 if(dynamic_cast<AndroidManifestFile *>(real))
72                 {
73                         argv.push_back("-M");
74                         argv.push_back(FS::relative(file->get_path(), work_dir).str());
75                 }
76                 else if(real->get_package()==res.get_package())
77                 {
78                         const FS::Path &path = file->get_path();
79                         resource_dirs.push_back(path.subpath(0, path.size()-2));
80                 }
81         }
82
83         set<string> seen_dirs;
84         for(list<FS::Path>::const_iterator i=resource_dirs.begin(); i!=resource_dirs.end(); ++i)
85                 if(seen_dirs.insert(i->str()).second)
86                 {
87                         argv.push_back("-S");
88                         argv.push_back(FS::relative(*i, work_dir).str());
89                 }
90
91         return new ExternalTask(argv, work_dir);
92 }