]> git.tdb.fi Git - builder.git/blob - androidtools.cpp
93b7bfac7a05d941f128ab55fcf05c088736e51d
[builder.git] / androidtools.cpp
1 #include <cstdlib>
2 #include <msp/fs/dir.h>
3 #include <msp/fs/stat.h>
4 #include <msp/strings/format.h>
5 #include <msp/strings/lexicalcast.h>
6 #include <msp/strings/utils.h>
7 #include "androidarchiver.h"
8 #include "androidassetpackagingtool.h"
9 #include "androidcompiler.h"
10 #include "androidlinker.h"
11 #include "androidmanifestgenerator.h"
12 #include "androidtools.h"
13 #include "apkbuilder.h"
14 #include "architecture.h"
15 #include "builder.h"
16 #include "jarsigner.h"
17
18 using namespace std;
19 using namespace Msp;
20
21 // TODO Mark problems somewhere instead of throwing exceptions
22
23 unsigned parse_version(const std::string &version_str)
24 {
25         vector<string> version_parts = split(version_str, '.');
26         unsigned version = lexical_cast<unsigned>(version_parts[0])<<16;
27         if(version_parts.size()>1)
28                 version += lexical_cast<unsigned>(version_parts[1])<<8;
29         if(version_parts.size()>2)
30                 version += lexical_cast<unsigned>(version_parts[2]);
31         return version;
32 }
33
34
35 AndroidDevKit::AndroidDevKit(Builder &b, const string &type, const FS::Path &default_path):
36         builder(b)
37 {
38         string var = format("ANDROID_%s_ROOT", type);
39         if(const char *value = getenv(var.c_str()))
40                 root = value;
41         else if(!default_path.empty() && FS::exists(default_path))
42                 root = default_path;
43         else
44         {
45                 builder.get_logger().log("problems", format("Android %s not found", type));
46                 return;
47         }
48
49         FS::Path platforms_dir = root/"platforms";
50         if(!FS::exists(platforms_dir))
51                 return;
52
53         builder.get_logger().log("files", format("Traversing %s", platforms_dir.str()));
54         vector<string> platforms = list_filtered(platforms_dir, "^android-[1-9][0-9]*$");
55
56         for(vector<string>::const_iterator i=platforms.begin(); i!=platforms.end(); ++i)
57                 supported_api_levels.insert(lexical_cast<unsigned>(i->substr(8)));
58 }
59
60 void AndroidDevKit::select_api_level(unsigned api)
61 {
62         if(!supported_api_levels.count(api))
63                 throw invalid_argument("AndroidDevKit::select_api_level");
64
65         init_api_level(api);
66 }
67
68
69 AndroidSdk::AndroidSdk(Builder &b):
70         AndroidDevKit(b, "SDK")
71 {
72         find_build_tools_dir();
73 }
74
75 void AndroidSdk::find_build_tools_dir()
76 {
77         FS::Path bt_dir = root/"build-tools";
78         if(!FS::exists(bt_dir))
79         {
80                 builder.get_logger().log("problems", "Android build-tools not found");
81                 return;
82         }
83
84         builder.get_logger().log("files", format("Traversing %s", bt_dir.str()));
85         vector<string> tool_versions = list_files(bt_dir);
86
87         string use_tools;
88         unsigned latest_version = 0;
89         for(vector<string>::const_iterator i=tool_versions.begin(); i!=tool_versions.end(); ++i)
90         {
91                 unsigned version = parse_version(*i);
92                 if(version>latest_version)
93                 {
94                         use_tools = *i;
95                         latest_version = version;
96                 }
97         }
98
99         if(use_tools.empty())
100         {
101                 builder.get_logger().log("problems", "Android build-tools not found");
102                 return;
103         }
104
105         build_tools_dir = bt_dir/use_tools;
106         builder.get_logger().log("tools", format("Android build-tools found in %s", build_tools_dir.str()));
107 }
108
109 void AndroidSdk::init_api_level(unsigned api)
110 {
111         platform_jar = root/"platforms"/format("android-%d", api)/"android.jar";
112 }
113
114
115 AndroidNdk::AndroidNdk(Builder &b, const Architecture &a, const AndroidSdk &sdk):
116         AndroidDevKit(b, "NDK", create_default_path(sdk)),
117         architecture(a)
118 {
119         if(!root.empty())
120         {
121                 FS::Path csr = root/"sysroot";
122                 if(FS::exists(csr))
123                 {
124                         common_sysroot = csr;
125                         builder.get_logger().log("tools", format("Android NDK common sysroot is %s", common_sysroot));
126                 }
127         }
128
129         find_toolchain_dir();
130 }
131
132 FS::Path AndroidNdk::create_default_path(const AndroidSdk &sdk)
133 {
134         if(sdk.get_root_dir().empty())
135                 return FS::Path();
136         return sdk.get_root_dir()/"ndk-bundle";
137 }
138
139 void AndroidNdk::find_toolchain_dir()
140 {
141         if(root.empty())
142                 return;
143
144         FS::Path toolchains_dir = root/"toolchains";
145         if(!FS::exists(toolchains_dir))
146         {
147                 builder.get_logger().log("problems", "Android NDK toolchains not found");
148                 return;
149         }
150
151         builder.get_logger().log("files", format("Traversing %s", toolchains_dir.str()));
152         string prefix = architecture.get_cross_prefix()+"-";
153         vector<string> toolchains = list_filtered(toolchains_dir, "^"+prefix);
154
155         string use_toolchain;
156         unsigned latest_version = 0;
157         for(vector<string>::const_iterator i=toolchains.begin(); i!=toolchains.end(); ++i)
158         {
159                 string version_str = i->substr(prefix.size());
160                 string compiler = "gcc";
161                 if(!isdigit(version_str[0]))
162                 {
163                         unsigned j;
164                         for(j=1; (j<version_str.size() && !isdigit(version_str[j])); ++j) ;
165                         compiler = version_str.substr(0, j);
166                         version_str = version_str.substr(j);
167                 }
168
169                 if(compiler!="gcc")
170                         continue;
171
172                 unsigned version = parse_version(version_str);
173                 if(version>latest_version)
174                 {
175                         use_toolchain = *i;
176                         latest_version = version;
177                 }
178         }
179
180         if(use_toolchain.empty())
181         {
182                 builder.get_logger().log("problems", "No applicable Android NDK toolchains found");
183                 return;
184         }
185
186         const Architecture &native_arch = builder.get_native_arch();
187
188         FS::Path tc_archs_dir = toolchains_dir/use_toolchain/"prebuilt";
189         builder.get_logger().log("files", format("Traversing %s", tc_archs_dir.str()));
190         string use_arch = native_arch.best_match(list_files(tc_archs_dir));
191
192         if(use_arch.empty())
193         {
194                 builder.get_logger().log("problems", "No applicable Android NDK toolchains found");
195                 return;
196         }
197
198         bin_dir = toolchains_dir/use_toolchain/"prebuilt"/use_arch/"bin";
199         builder.get_logger().log("tools", format("Android NDK toolchain binaries found in %s", bin_dir.str()));
200 }
201
202 void AndroidNdk::init_api_level(unsigned api)
203 {
204         FS::Path platform_archs_dir = root/"platforms"/format("android-%d", api);
205         builder.get_logger().log("files", format("Traversing %s", platform_archs_dir.str()));
206         vector<string> platform_archs = list_filtered(platform_archs_dir, "^arch-");
207         for(vector<string>::iterator i=platform_archs.begin(); i!=platform_archs.end(); ++i)
208                 i->erase(0, 5);
209         string use_arch = architecture.best_match(platform_archs);
210
211         if(use_arch.empty())
212         {
213                 builder.get_logger().log("problems", "No matching Android NDK platform found");
214                 return;
215         }
216
217         platform_sysroot = platform_archs_dir/("arch-"+use_arch);
218         builder.get_logger().log("tools", format("Android NDK platform sysroot is %s", platform_sysroot));
219 }
220
221
222 AndroidTools::AndroidTools(Builder &builder, const Architecture &arch):
223         sdk(builder),
224         ndk(builder, arch, sdk)
225 {
226         const set<unsigned> &sdk_api_levels = sdk.get_supported_api_levels();
227         const set<unsigned> &ndk_api_levels = ndk.get_supported_api_levels();
228         unsigned highest_common = 0;
229         for(set<unsigned>::const_reverse_iterator i=sdk_api_levels.rbegin(); (!highest_common && i!=sdk_api_levels.rend()); ++i)
230                 if(ndk_api_levels.count(*i))
231                         highest_common = *i;
232
233         if(highest_common)
234         {
235                 sdk.select_api_level(highest_common);
236                 ndk.select_api_level(highest_common);
237         }
238         else
239                 builder.get_logger().log("problems", "No usable Android platforms found");
240
241         add_tool(new AndroidCompiler(builder, arch, "CC", ndk));
242         add_tool(new AndroidCompiler(builder, arch, "CXX", ndk));
243         add_tool(new AndroidLinker(builder, arch, ndk));
244         add_tool(new AndroidArchiver(builder, arch, ndk));
245         add_tool(new AndroidManifestGenerator(builder));
246         add_tool(new AndroidAssetPackagingTool(builder, sdk));
247         add_tool(new ApkBuilder(builder));
248         add_tool(new JarSigner(builder));
249 }