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