• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 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"""Configures devil for use in chromium."""
6
7import os
8import sys
9
10from pylib import constants
11from pylib.constants import host_paths
12
13if host_paths.DEVIL_PATH not in sys.path:
14  sys.path.insert(1, host_paths.DEVIL_PATH)
15
16from devil import devil_env
17from devil.android.ndk import abis
18
19_BUILD_DIR = os.path.join(constants.DIR_SOURCE_ROOT, 'build')
20if _BUILD_DIR not in sys.path:
21  sys.path.insert(1, _BUILD_DIR)
22
23import gn_helpers
24
25_DEVIL_CONFIG = os.path.abspath(
26    os.path.join(os.path.dirname(__file__), 'devil_chromium.json'))
27
28_DEVIL_BUILD_PRODUCT_DEPS = {
29    'devil_util_device': [
30        {
31            'platform': 'android',
32            'arch': abis.ARM,
33            'path_components': ['devil_util_dist'],
34        },
35        {
36            'platform': 'android',
37            'arch': abis.ARM_64,
38            'path_components': ['devil_util_dist'],
39        },
40        {
41            'platform': 'android',
42            'arch': abis.X86,
43            'path_components': ['devil_util_dist'],
44        },
45        {
46            'platform': 'android',
47            'arch': abis.X86_64,
48            'path_components': ['devil_util_dist'],
49        },
50    ],
51    'devil_util_host': [
52        {
53            'platform': 'linux2',
54            'arch': 'x86_64',
55            'path_components': ['devil_util_host'],
56        },
57    ],
58    'chromium_commands': [{
59        'platform':
60        'linux2',
61        'arch':
62        'x86_64',
63        'path_components': ['lib.java', 'chromium_commands.dex.jar'],
64    }],
65    'forwarder_device': [
66        {
67            'platform': 'android',
68            'arch': abis.ARM,
69            'path_components': ['forwarder_dist'],
70        },
71        {
72            'platform': 'android',
73            'arch': abis.ARM_64,
74            'path_components': ['forwarder_dist'],
75        },
76        {
77            'platform': 'android',
78            'arch': 'mips',
79            'path_components': ['forwarder_dist'],
80        },
81        {
82            'platform': 'android',
83            'arch': 'mips64',
84            'path_components': ['forwarder_dist'],
85        },
86        {
87            'platform': 'android',
88            'arch': abis.X86,
89            'path_components': ['forwarder_dist'],
90        },
91        {
92            'platform': 'android',
93            'arch': abis.X86_64,
94            'path_components': ['forwarder_dist'],
95        },
96    ],
97    'forwarder_host': [
98        {
99            'platform': 'linux2',
100            'arch': 'x86_64',
101            'path_components': ['host_forwarder'],
102        },
103    ],
104}
105
106
107def _UseLocalBuildProducts(output_directory, devil_dynamic_config):
108  output_directory = os.path.abspath(output_directory)
109  devil_dynamic_config['dependencies'] = {
110      dep_name: {
111          'file_info': {
112              '%s_%s' % (dep_config['platform'], dep_config['arch']): {
113                  'local_paths': [
114                      os.path.join(output_directory,
115                                   *dep_config['path_components']),
116                  ],
117              }
118              for dep_config in dep_configs
119          }
120      }
121      for dep_name, dep_configs in _DEVIL_BUILD_PRODUCT_DEPS.items()
122  }
123
124
125def _BuildWithChromium():
126  """Returns value of gclient's |build_with_chromium|."""
127  gni_path = os.path.join(_BUILD_DIR, 'config', 'gclient_args.gni')
128  if not os.path.exists(gni_path):
129    return False
130  with open(gni_path) as f:
131    data = f.read()
132  args = gn_helpers.FromGNArgs(data)
133  return args.get('build_with_chromium', False)
134
135
136def Initialize(output_directory=None,
137               custom_deps=None,
138               adb_path=None,
139               use_local_devil_tools=False):
140  """Initializes devil with chromium's binaries and third-party libraries.
141
142  This includes:
143    - Libraries:
144      - the android SDK ("android_sdk")
145    - Build products:
146      - host & device forwarder binaries
147          ("forwarder_device" and "forwarder_host")
148      - host & device devil_util binaries
149          ("devil_util_device" and "devil_util_host")
150
151  Args:
152    output_directory: An optional path to the output directory. If not set,
153      no built dependencies are configured.
154    custom_deps: An optional dictionary specifying custom dependencies.
155      This should be of the form:
156
157        {
158          'dependency_name': {
159            'platform': 'path',
160            ...
161          },
162          ...
163        }
164    adb_path: An optional path to use for the adb binary. If not set, this uses
165      the adb binary provided by the Android SDK.
166    use_local_devil_tools: Use locally built versions of devil_util,
167      forwarder_dist, etc.
168  """
169  build_with_chromium = _BuildWithChromium()
170
171  devil_dynamic_config = {
172    'config_type': 'BaseConfig',
173    'dependencies': {},
174  }
175  if use_local_devil_tools:
176    # Non-chromium users of chromium's //build directory fetch build products
177    # from google storage rather than use locally built copies. Chromium uses
178    # locally-built copies so that changes to the tools can be easily tested.
179    _UseLocalBuildProducts(output_directory, devil_dynamic_config)
180
181  if custom_deps:
182    devil_dynamic_config['dependencies'].update(custom_deps)
183  if adb_path:
184    devil_dynamic_config['dependencies'].update({
185      'adb': {
186        'file_info': {
187          devil_env.GetPlatform(): {
188            'local_paths': [adb_path]
189          }
190        }
191      }
192    })
193
194  config_files = [_DEVIL_CONFIG] if build_with_chromium else None
195  devil_env.config.Initialize(configs=[devil_dynamic_config],
196                              config_files=config_files)
197