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