xref: /aosp_15_r20/external/angle/build/android/pylib/utils/device_dependencies.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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
5import glob
6import os
7import posixpath
8import re
9
10from pylib import constants
11
12_EXCLUSIONS = [
13    # Misc files that exist to document directories
14    re.compile(r'.*METADATA'),
15    re.compile(r'.*OWNERS'),
16    re.compile(r'.*\.md'),
17    re.compile(r'.*\.crx'),  # Chrome extension zip files.
18    re.compile(r'.*/\.git.*'),  # Any '.git*' directories/files.
19    re.compile(r'.*\.so'),  # Libraries packed into .apk.
20    re.compile(r'.*Mojo.*manifest\.json'),  # Some source_set()s pull these in.
21    re.compile(r'.*\.py'),  # Some test_support targets include python deps.
22    re.compile(r'.*\.apk'),  # Should be installed separately.
23    re.compile(r'.*\.jar'),  # Never need java intermediates.
24    re.compile(r'.*\.crx'),  # Used by download_from_google_storage.
25    re.compile(r'.*\.wpr'),  # Web-page-relay files needed only on host.
26    re.compile(r'.*lib.java/.*'),  # Never need java intermediates.
27
28    # Test filter files:
29    re.compile(r'.*/testing/buildbot/filters/.*'),
30
31    # Chrome external extensions config file.
32    re.compile(r'.*external_extensions\.json'),
33
34    # v8's blobs and icu data get packaged into APKs.
35    re.compile(r'.*snapshot_blob.*\.bin'),
36    re.compile(r'.*icudtl\.bin'),
37
38    # Scripts that are needed by swarming, but not on devices:
39    re.compile(r'.*llvm-symbolizer'),
40    re.compile(r'.*md5sum_(?:bin|dist)'),
41    re.compile(r'.*/development/scripts/stack'),
42    re.compile(r'.*/build/android/pylib/symbols'),
43    re.compile(r'.*/build/android/stacktrace'),
44
45    # Required for java deobfuscation on the host:
46    re.compile(r'.*build/android/stacktrace/.*'),
47    re.compile(r'.*third_party/jdk/.*'),
48    re.compile(r'.*third_party/proguard/.*'),
49
50    # Our tests don't need these.
51    re.compile(r'.*/devtools-frontend/.*front_end/.*'),
52
53    # Build artifacts:
54    re.compile(r'.*\.stamp'),
55    re.compile(r'.*\.pak\.info'),
56    re.compile(r'.*\.build_config.json'),
57    re.compile(r'.*\.incremental\.json'),
58]
59
60
61def _FilterDataDeps(abs_host_files):
62  exclusions = _EXCLUSIONS + [
63      re.compile(os.path.join(constants.GetOutDirectory(), 'bin'))
64  ]
65  return [p for p in abs_host_files if not any(r.match(p) for r in exclusions)]
66
67
68def DevicePathComponentsFor(host_path, output_directory=None):
69  """Returns the device path components for a given host path.
70
71  This returns the device path as a list of joinable path components,
72  with None as the first element to indicate that the path should be
73  rooted at $EXTERNAL_STORAGE.
74
75  e.g., given
76
77    '$RUNTIME_DEPS_ROOT_DIR/foo/bar/baz.txt'
78
79  this would return
80
81    [None, 'foo', 'bar', 'baz.txt']
82
83  This handles a couple classes of paths differently than it otherwise would:
84    - All .pak files get mapped to top-level paks/
85    - All other dependencies get mapped to the top level directory
86        - If a file is not in the output directory then it's relative path to
87          the output directory will start with .. strings, so we remove those
88          and then the path gets mapped to the top-level directory
89        - If a file is in the output directory then the relative path to the
90          output directory gets mapped to the top-level directory
91
92  e.g. given
93
94    '$RUNTIME_DEPS_ROOT_DIR/out/Release/icu_fake_dir/icudtl.dat'
95
96  this would return
97
98    [None, 'icu_fake_dir', 'icudtl.dat']
99
100  Args:
101    host_path: The absolute path to the host file.
102  Returns:
103    A list of device path components.
104  """
105  output_directory = output_directory or constants.GetOutDirectory()
106  if (host_path.startswith(output_directory) and
107      os.path.splitext(host_path)[1] == '.pak'):
108    return [None, 'paks', os.path.basename(host_path)]
109
110  rel_host_path = os.path.relpath(host_path, output_directory)
111
112  device_path_components = [None]
113  p = rel_host_path
114  while p:
115    p, d = os.path.split(p)
116    # The relative path from the output directory to a file under the runtime
117    # deps root directory may start with multiple .. strings, so they need to
118    # be skipped.
119    if d and d != os.pardir:
120      device_path_components.insert(1, d)
121  return device_path_components
122
123
124def GetDataDependencies(runtime_deps_path):
125  """Returns a list of device data dependencies.
126
127  Args:
128    runtime_deps_path: A str path to the .runtime_deps file.
129  Returns:
130    A list of (host_path, device_path) tuples.
131  """
132  if not runtime_deps_path:
133    return []
134
135  with open(runtime_deps_path, 'r') as runtime_deps_file:
136    # .runtime_deps can contain duplicates.
137    rel_host_files = sorted({l.strip() for l in runtime_deps_file if l})
138
139  output_directory = constants.GetOutDirectory()
140  abs_host_files = [
141      os.path.abspath(os.path.join(output_directory, r))
142      for r in rel_host_files]
143  filtered_abs_host_files = _FilterDataDeps(abs_host_files)
144  # TODO(crbug.com/40533647): Filter out host executables, and investigate
145  # whether other files could be filtered as well.
146  return [(f, DevicePathComponentsFor(f, output_directory))
147          for f in filtered_abs_host_files]
148
149
150def SubstituteDeviceRootSingle(device_path, device_root):
151  if not device_path:
152    return device_root
153  if isinstance(device_path, list):
154    return posixpath.join(*(p if p else device_root for p in device_path))
155  return device_path
156
157
158def SubstituteDeviceRoot(host_device_tuples, device_root):
159  return [(h, SubstituteDeviceRootSingle(d, device_root))
160          for h, d in host_device_tuples]
161
162
163def ExpandDataDependencies(host_device_tuples):
164  ret = []
165  for h, d in host_device_tuples:
166    if os.path.isdir(h):
167      for subpath in glob.glob(f'{h}/**', recursive=True):
168        if not os.path.isdir(subpath):
169          new_part = subpath[len(h):]
170          ret.append((subpath, d + new_part))
171    else:
172      ret.append((h, d))
173  return ret
174