1# Copyright 2016 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import contextlib 6import os 7import sys 8 9from pylib import constants 10 11DIR_SOURCE_ROOT = os.environ.get( 12 'CHECKOUT_SOURCE_ROOT', 13 os.path.abspath(os.path.join(os.path.dirname(__file__), 14 os.pardir, os.pardir, os.pardir, os.pardir))) 15 16BUILD_COMMON_PATH = os.path.join( 17 DIR_SOURCE_ROOT, 'build', 'util', 'lib', 'common') 18 19# third-party libraries 20ANDROID_PLATFORM_DEVELOPMENT_SCRIPTS_PATH = os.path.join( 21 DIR_SOURCE_ROOT, 'third_party', 'android_platform', 'development', 22 'scripts') 23DEVIL_PATH = os.path.join( 24 DIR_SOURCE_ROOT, 'third_party', 'catapult', 'devil') 25PYMOCK_PATH = os.path.join( 26 DIR_SOURCE_ROOT, 'third_party', 'pymock') 27 28@contextlib.contextmanager 29def SysPath(path, position=None): 30 if position is None: 31 sys.path.append(path) 32 else: 33 sys.path.insert(position, path) 34 try: 35 yield 36 finally: 37 if sys.path[-1] == path: 38 sys.path.pop() 39 else: 40 sys.path.remove(path) 41 42 43# Map of CPU architecture name to (toolchain_name, binprefix) pairs. 44# TODO(digit): Use the build_vars.txt file generated by gn. 45_TOOL_ARCH_MAP = { 46 'arm': ('arm-linux-androideabi-4.9', 'arm-linux-androideabi'), 47 'arm64': ('aarch64-linux-android-4.9', 'aarch64-linux-android'), 48 'x86': ('x86-4.9', 'i686-linux-android'), 49 'x86_64': ('x86_64-4.9', 'x86_64-linux-android'), 50 'x64': ('x86_64-4.9', 'x86_64-linux-android'), 51 'mips': ('mipsel-linux-android-4.9', 'mipsel-linux-android'), 52} 53 54# Cache used to speed up the results of ToolPath() 55# Maps (arch, tool_name) pairs to fully qualified program paths. 56# Useful because ToolPath() is called repeatedly for demangling C++ symbols. 57_cached_tool_paths = {} 58 59 60def ToolPath(tool, cpu_arch): 61 """Return a fully qualifed path to an arch-specific toolchain program. 62 63 Args: 64 tool: Unprefixed toolchain program name (e.g. 'objdump') 65 cpu_arch: Target CPU architecture (e.g. 'arm64') 66 Returns: 67 Fully qualified path (e.g. ..../aarch64-linux-android-objdump') 68 Raises: 69 Exception if the toolchain could not be found. 70 """ 71 tool_path = _cached_tool_paths.get((tool, cpu_arch)) 72 if tool_path: 73 return tool_path 74 75 toolchain_source, toolchain_prefix = _TOOL_ARCH_MAP.get( 76 cpu_arch, (None, None)) 77 if not toolchain_source: 78 raise Exception('Could not find tool chain for ' + cpu_arch) 79 80 toolchain_subdir = ( 81 'toolchains/%s/prebuilt/linux-x86_64/bin' % toolchain_source) 82 83 tool_path = os.path.join(constants.ANDROID_NDK_ROOT, 84 toolchain_subdir, 85 toolchain_prefix + '-' + tool) 86 87 _cached_tool_paths[(tool, cpu_arch)] = tool_path 88 return tool_path 89 90 91def GetAaptPath(): 92 """Returns the path to the 'aapt' executable.""" 93 return os.path.join(constants.ANDROID_SDK_TOOLS, 'aapt') 94