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"
16 #include "jarsigner.h"
21 // TODO Mark problems somewhere instead of throwing exceptions
23 unsigned parse_version(const std::string &version_str)
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]);
35 AndroidDevKit::AndroidDevKit(Builder &b, const string &type, const FS::Path &default_path):
38 string var = format("ANDROID_%s_ROOT", type);
39 if(const char *value = getenv(var.c_str()))
41 else if(!default_path.empty() && FS::exists(default_path))
45 builder.get_logger().log("problems", format("Android %s not found", type));
49 FS::Path platforms_dir = root/"platforms";
50 if(!FS::exists(platforms_dir))
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]*$");
56 for(vector<string>::const_iterator i=platforms.begin(); i!=platforms.end(); ++i)
57 supported_api_levels.insert(lexical_cast<unsigned>(i->substr(8)));
60 void AndroidDevKit::select_api_level(unsigned api)
62 if(!supported_api_levels.count(api))
63 throw invalid_argument("AndroidDevKit::select_api_level");
69 AndroidSdk::AndroidSdk(Builder &b):
70 AndroidDevKit(b, "SDK")
72 find_build_tools_dir();
75 void AndroidSdk::find_build_tools_dir()
77 FS::Path bt_dir = root/"build-tools";
78 if(!FS::exists(bt_dir))
80 builder.get_logger().log("problems", "Android build-tools not found");
84 builder.get_logger().log("files", format("Traversing %s", bt_dir.str()));
85 vector<string> tool_versions = list_files(bt_dir);
88 unsigned latest_version = 0;
89 for(vector<string>::const_iterator i=tool_versions.begin(); i!=tool_versions.end(); ++i)
91 unsigned version = parse_version(*i);
92 if(version>latest_version)
95 latest_version = version;
101 builder.get_logger().log("problems", "Android build-tools not found");
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()));
109 void AndroidSdk::init_api_level(unsigned api)
111 platform_jar = root/"platforms"/format("android-%d", api)/"android.jar";
115 AndroidNdk::AndroidNdk(Builder &b, const Architecture &a, const AndroidSdk &sdk):
116 AndroidDevKit(b, "NDK", create_default_path(sdk)),
121 FS::Path csr = root/"sysroot";
124 common_sysroot = csr;
125 builder.get_logger().log("tools", format("Android NDK common sysroot is %s", common_sysroot));
129 find_toolchain_dir();
132 FS::Path AndroidNdk::create_default_path(const AndroidSdk &sdk)
134 if(sdk.get_root_dir().empty())
136 return sdk.get_root_dir()/"ndk-bundle";
139 void AndroidNdk::find_toolchain_dir()
144 FS::Path toolchains_dir = root/"toolchains";
145 if(!FS::exists(toolchains_dir))
147 builder.get_logger().log("problems", "Android NDK toolchains not found");
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);
155 string use_toolchain;
156 unsigned latest_version = 0;
157 for(vector<string>::const_iterator i=toolchains.begin(); i!=toolchains.end(); ++i)
159 string version_str = i->substr(prefix.size());
160 string compiler = "gcc";
161 if(!isdigit(version_str[0]))
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);
172 unsigned version = parse_version(version_str);
173 if(version>latest_version)
176 latest_version = version;
180 if(use_toolchain.empty())
182 builder.get_logger().log("problems", "No applicable Android NDK toolchains found");
186 const Architecture &native_arch = builder.get_native_arch();
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));
194 builder.get_logger().log("problems", "No applicable Android NDK toolchains found");
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()));
202 void AndroidNdk::init_api_level(unsigned api)
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)
209 string use_arch = architecture.best_match(platform_archs);
213 builder.get_logger().log("problems", "No matching Android NDK platform found");
217 platform_sysroot = platform_archs_dir/("arch-"+use_arch);
218 builder.get_logger().log("tools", format("Android NDK platform sysroot is %s", platform_sysroot));
222 AndroidTools::AndroidTools(Builder &builder, const Architecture &arch):
224 ndk(builder, arch, sdk)
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))
235 sdk.select_api_level(highest_common);
236 ndk.select_api_level(highest_common);
239 builder.get_logger().log("problems", "No usable Android platforms found");
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));