|
27 | 27 | RECOMMENDED_NDK_API, RECOMMENDED_TARGET_API)
|
28 | 28 |
|
29 | 29 |
|
| 30 | +def get_cython_path(): |
| 31 | + for cython_fn in ("cython", "cython3", "cython2", "cython-2.7"): |
| 32 | + cython = sh.which(cython_fn) |
| 33 | + if cython: |
| 34 | + return cython |
| 35 | + raise BuildInterruptingException('No cython binary found.') |
| 36 | + |
| 37 | + |
| 38 | +def get_ndk_platform_dir(ndk_dir, ndk_api, arch): |
| 39 | + ndk_platform_dir_exists = True |
| 40 | + platform_dir = arch.platform_dir |
| 41 | + ndk_platform = join( |
| 42 | + ndk_dir, |
| 43 | + 'platforms', |
| 44 | + 'android-{}'.format(ndk_api), |
| 45 | + platform_dir) |
| 46 | + if not exists(ndk_platform): |
| 47 | + warning("ndk_platform doesn't exist: {}".format(ndk_platform)) |
| 48 | + ndk_platform_dir_exists = False |
| 49 | + return ndk_platform, ndk_platform_dir_exists |
| 50 | + |
| 51 | + |
| 52 | +def get_toolchain_versions(ndk_dir, arch): |
| 53 | + toolchain_versions = [] |
| 54 | + toolchain_path_exists = True |
| 55 | + toolchain_prefix = arch.toolchain_prefix |
| 56 | + toolchain_path = join(ndk_dir, 'toolchains') |
| 57 | + if isdir(toolchain_path): |
| 58 | + toolchain_contents = glob.glob('{}/{}-*'.format(toolchain_path, |
| 59 | + toolchain_prefix)) |
| 60 | + toolchain_versions = [split(path)[-1][len(toolchain_prefix) + 1:] |
| 61 | + for path in toolchain_contents] |
| 62 | + else: |
| 63 | + warning('Could not find toolchain subdirectory!') |
| 64 | + toolchain_path_exists = False |
| 65 | + return toolchain_versions, toolchain_path_exists |
| 66 | + |
| 67 | + |
| 68 | +def get_targets(sdk_dir): |
| 69 | + if exists(join(sdk_dir, 'tools', 'bin', 'avdmanager')): |
| 70 | + avdmanager = sh.Command(join(sdk_dir, 'tools', 'bin', 'avdmanager')) |
| 71 | + targets = avdmanager('list', 'target').stdout.decode('utf-8').split('\n') |
| 72 | + elif exists(join(sdk_dir, 'tools', 'android')): |
| 73 | + android = sh.Command(join(sdk_dir, 'tools', 'android')) |
| 74 | + targets = android('list').stdout.decode('utf-8').split('\n') |
| 75 | + else: |
| 76 | + raise BuildInterruptingException( |
| 77 | + 'Could not find `android` or `sdkmanager` binaries in Android SDK', |
| 78 | + instructions='Make sure the path to the Android SDK is correct') |
| 79 | + return targets |
| 80 | + |
| 81 | + |
| 82 | +def get_available_apis(sdk_dir): |
| 83 | + targets = get_targets(sdk_dir) |
| 84 | + apis = [s for s in targets if re.match(r'^ *API level: ', s)] |
| 85 | + apis = [re.findall(r'[0-9]+', s) for s in apis] |
| 86 | + apis = [int(s[0]) for s in apis if s] |
| 87 | + return apis |
| 88 | + |
| 89 | + |
30 | 90 | class Context(object):
|
31 | 91 | '''A build context. If anything will be built, an instance this class
|
32 | 92 | will be instantiated and used to hold all the build state.'''
|
@@ -238,20 +298,7 @@ def prepare_build_environment(self,
|
238 | 298 | self.android_api = android_api
|
239 | 299 |
|
240 | 300 | check_target_api(android_api, self.archs[0].arch)
|
241 |
| - |
242 |
| - if exists(join(sdk_dir, 'tools', 'bin', 'avdmanager')): |
243 |
| - avdmanager = sh.Command(join(sdk_dir, 'tools', 'bin', 'avdmanager')) |
244 |
| - targets = avdmanager('list', 'target').stdout.decode('utf-8').split('\n') |
245 |
| - elif exists(join(sdk_dir, 'tools', 'android')): |
246 |
| - android = sh.Command(join(sdk_dir, 'tools', 'android')) |
247 |
| - targets = android('list').stdout.decode('utf-8').split('\n') |
248 |
| - else: |
249 |
| - raise BuildInterruptingException( |
250 |
| - 'Could not find `android` or `sdkmanager` binaries in Android SDK', |
251 |
| - instructions='Make sure the path to the Android SDK is correct') |
252 |
| - apis = [s for s in targets if re.match(r'^ *API level: ', s)] |
253 |
| - apis = [re.findall(r'[0-9]+', s) for s in apis] |
254 |
| - apis = [int(s[0]) for s in apis if s] |
| 301 | + apis = get_available_apis(self.sdk_dir) |
255 | 302 | info('Available Android APIs are ({})'.format(
|
256 | 303 | ', '.join(map(str, apis))))
|
257 | 304 | if android_api in apis:
|
@@ -327,46 +374,21 @@ def prepare_build_environment(self,
|
327 | 374 | if not self.ccache:
|
328 | 375 | info('ccache is missing, the build will not be optimized in the '
|
329 | 376 | 'future.')
|
330 |
| - for cython_fn in ("cython", "cython3", "cython2", "cython-2.7"): |
331 |
| - cython = sh.which(cython_fn) |
332 |
| - if cython: |
333 |
| - self.cython = cython |
334 |
| - break |
335 |
| - else: |
336 |
| - raise BuildInterruptingException('No cython binary found.') |
337 |
| - if not self.cython: |
338 |
| - ok = False |
339 |
| - warning("Missing requirement: cython is not installed") |
| 377 | + self.cython = get_cython_path() |
340 | 378 |
|
341 | 379 | # This would need to be changed if supporting multiarch APKs
|
342 | 380 | arch = self.archs[0]
|
343 |
| - platform_dir = arch.platform_dir |
344 | 381 | toolchain_prefix = arch.toolchain_prefix
|
345 |
| - toolchain_version = None |
346 |
| - self.ndk_platform = join( |
347 |
| - self.ndk_dir, |
348 |
| - 'platforms', |
349 |
| - 'android-{}'.format(self.ndk_api), |
350 |
| - platform_dir) |
351 |
| - if not exists(self.ndk_platform): |
352 |
| - warning('ndk_platform doesn\'t exist: {}'.format( |
353 |
| - self.ndk_platform)) |
354 |
| - ok = False |
| 382 | + self.ndk_platform, ndk_platform_dir_exists = get_ndk_platform_dir( |
| 383 | + self.ndk_dir, self.ndk_api, arch) |
| 384 | + ok = ok and ndk_platform_dir_exists |
355 | 385 |
|
356 | 386 | py_platform = sys.platform
|
357 | 387 | if py_platform in ['linux2', 'linux3']:
|
358 | 388 | py_platform = 'linux'
|
359 |
| - |
360 |
| - toolchain_versions = [] |
361 |
| - toolchain_path = join(self.ndk_dir, 'toolchains') |
362 |
| - if isdir(toolchain_path): |
363 |
| - toolchain_contents = glob.glob('{}/{}-*'.format(toolchain_path, |
364 |
| - toolchain_prefix)) |
365 |
| - toolchain_versions = [split(path)[-1][len(toolchain_prefix) + 1:] |
366 |
| - for path in toolchain_contents] |
367 |
| - else: |
368 |
| - warning('Could not find toolchain subdirectory!') |
369 |
| - ok = False |
| 389 | + toolchain_versions, toolchain_path_exists = get_toolchain_versions( |
| 390 | + self.ndk_dir, arch) |
| 391 | + ok = ok and toolchain_path_exists |
370 | 392 | toolchain_versions.sort()
|
371 | 393 |
|
372 | 394 | toolchain_versions_gcc = []
|
|
0 commit comments