summaryrefslogtreecommitdiffstats
path: root/build/android/install_emulator_deps.py
blob: acd2093b1fcfb83e17ca776dd0a8197c7c4a16a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Installs deps for using SDK emulator for testing.

The script will download the SDK and system images, if they are not present, and
install and enable KVM, if virtualization has been enabled in the BIOS.
"""


import logging
import optparse
import os
import re
import sys

import devil_chromium
from devil.utils import cmd_helper
from devil.utils import run_tests_helper
from pylib import constants
from pylib import pexpect

# Android API level
DEFAULT_ANDROID_API_LEVEL = constants.ANDROID_SDK_VERSION
# Android ABI/Arch
DEFAULT_ABI = 'x86'

# Default Time out for downloading SDK component
DOWNLOAD_SYSTEM_IMAGE_TIMEOUT = 30
DOWNLOAD_SDK_PLATFORM_TIMEOUT = 60

def CheckSDK():
  """Check if SDK is already installed.

  Returns:
    True if the emulator SDK directory (src/android_emulator_sdk/) exists.
  """
  return os.path.exists(constants.ANDROID_SDK_ROOT)


def CheckSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL, google=False):
  """Check if the "SDK Platform" for the specified API level is installed.
     This is necessary in order for the emulator to run when the target
     is specified.

  Args:
    abi: target abi, x86 or arm
    api_level: the Android API level to check; defaults to the latest API.
    google: use Google build system image instead of AOSP build

  Returns:
    True if the platform is already installed.
  """
  android_binary = os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'android')
  if google:
    pattern = re.compile('id: [0-9]+ or "Google Inc.:Google APIs:%s"' %
                         api_level)
  else:
    pattern = re.compile('id: [0-9]+ or "android-%d"' % api_level)

  try:
    exit_code, stdout = cmd_helper.GetCmdStatusAndOutput(
        [android_binary, 'list'])
    if exit_code != 0:
      raise Exception('\'android list\' command failed')
    for line in stdout.split('\n'):
      if pattern.match(line):
        return True
    return False
  except OSError:
    logging.exception('Unable to execute \'android list\'')
    return False


def CheckSystemImage(abi, api_level=DEFAULT_ANDROID_API_LEVEL, google=False):
  """Check if Android system images have been installed.

  Args:
    abi: target abi, x86 or arm
    api_level: the Android API level to check for; defaults to the latest API.
    google: use Google build system image instead of AOSP build

  Returns:
    True if x86 image has been previously downloaded.
  """
  api_target = 'android-%d' % api_level
  system_image_root = os.path.join(constants.ANDROID_SDK_ROOT,
                                   'system-images', api_target)
  if abi == 'x86':
    if google:
      return os.path.exists(os.path.join(system_image_root, 'google_apis',
                                         'x86'))
    else:
      return os.path.exists(os.path.join(system_image_root, 'default', 'x86'))
  elif abi == 'arm':
    if google:
      return os.path.exists(os.path.join(system_image_root, 'google_apis',
                                         'armeabi-v7a'))
    else:
      return os.path.exists(os.path.join(system_image_root, 'default',
                                         'armeabi-v7a'))
  else:
    raise Exception("abi option invalid")

def CheckKVM():
  """Quickly check whether KVM is enabled.

  Returns:
    True iff /dev/kvm exists (Linux only).
  """
  return os.path.exists('/dev/kvm')

def RunKvmOk():
  """Run kvm-ok as root to check that KVM is properly enabled after installation
     of the required packages.

  Returns:
    True iff KVM is enabled (/dev/kvm exists). On failure, returns False
    but also print detailed information explaining why KVM isn't enabled
    (e.g. CPU doesn't support it, or BIOS disabled it).
  """
  try:
    # Note: kvm-ok is in /usr/sbin, so always use 'sudo' to run it.
    return not cmd_helper.RunCmd(['sudo', 'kvm-ok'])
  except OSError:
    logging.info('kvm-ok not installed')
    return False


def InstallKVM():
  """Installs KVM packages."""
  rc = cmd_helper.RunCmd(['sudo', 'apt-get', 'install', 'kvm'])
  if rc:
    logging.critical('ERROR: Did not install KVM. Make sure hardware '
                     'virtualization is enabled in BIOS (i.e. Intel VT-x or '
                     'AMD SVM).')
  # TODO(navabi): Use modprobe kvm-amd on AMD processors.
  rc = cmd_helper.RunCmd(['sudo', 'modprobe', 'kvm-intel'])
  if rc:
    logging.critical('ERROR: Did not add KVM module to Linux Kernel. Make sure '
                     'hardware virtualization is enabled in BIOS.')
  # Now check to ensure KVM acceleration can be used.
  if not RunKvmOk():
    logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware '
                     'virtualization is enabled in BIOS (i.e. Intel VT-x or '
                     'AMD SVM).')


def UpdateSDK(api_level, package_name, package_pattern, timeout):
  """This function update SDK with a filter index.

  Args:
    api_level: the Android API level to download for.
    package_name: logging name of package that is being updated.
    package_pattern: the pattern to match the filter index from.
    timeout: the amount of time wait for update command.
  """
  android_binary = os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'android')

  list_sdk_repo_command = [android_binary, 'list', 'sdk', '--all']

  exit_code, stdout = cmd_helper.GetCmdStatusAndOutput(list_sdk_repo_command)

  if exit_code != 0:
    raise Exception('\'android list sdk --all\' command return %d' % exit_code)

  for line in stdout.split('\n'):
    match = package_pattern.match(line)
    if match:
      index = match.group(1)
      logging.info('package %s corresponds to %s with api level %d',
                   index, package_name, api_level)
      update_command = [android_binary, 'update', 'sdk', '--no-ui', '--all',
                         '--filter', index]
      update_command_str = ' '.join(update_command)
      logging.info('running update command: %s', update_command_str)
      update_process = pexpect.spawn(update_command_str)

      if update_process.expect('Do you accept the license') != 0:
        raise Exception('License agreement check failed')
      update_process.sendline('y')
      if update_process.expect(
        'Done. 1 package installed.', timeout=timeout) == 0:
        logging.info('Successfully installed %s for API level %d',
                      package_name, api_level)
        return
      else:
        raise Exception('Failed to install platform update')
  raise Exception('Could not find android-%d update for the SDK!' % api_level)

def GetSystemImage(abi, api_level=DEFAULT_ANDROID_API_LEVEL, google=False):
  """Download system image files

  Args:
    abi: target abi, x86 or arm
    api_level: the Android API level to download for.
    google: use Google build system image instead of AOSP build
  """
  logging.info('Download x86 system image directory into sdk directory.')

  if abi == 'x86':
    if google:
      package_name = 'Google Intel x86 Atom System Image'
      pattern = re.compile(
         r'\s*([0-9]+)- Google APIs Intel x86 Atom System Image, Google Inc.'
        ' API %d.*' % api_level)
    else:
      package_name = 'Intel x86 system image'
      pattern = re.compile(
        r'\s*([0-9]+)- Intel x86 Atom System Image, Android API %d.*'
        % api_level)
  elif abi == 'arm':
    if google:
      package_name = 'Google arm system image'
      pattern = re.compile(
        r'\s*([0-9]+)- Google APIs ARM EABI v7a System Image, Google Inc. API '
        '%d.*' % api_level)
    else:
      package_name = 'Android arm system image'
      pattern = re.compile(
        r'\s*([0-9]+)- ARM EABI v7a System Image, Android API %d.*' % api_level)
  else:
    raise Exception('abi option is invalid')

  UpdateSDK(api_level, package_name, pattern, DOWNLOAD_SYSTEM_IMAGE_TIMEOUT)

def GetSDKPlatform(api_level=DEFAULT_ANDROID_API_LEVEL, google=False):
  """Update the SDK to include the platform specified.

  Args:
    api_level: the Android API level to download
    google: use Google build system image instead of AOSP build
  """
  logging.info('Download SDK Platform directory into sdk directory.')

  platform_package_pattern = re.compile(
      r'\s*([0-9]+)- SDK Platform Android [\.,0-9]+, API %d.*' % api_level)

  UpdateSDK(api_level, 'SDK Platform', platform_package_pattern,
            DOWNLOAD_SDK_PLATFORM_TIMEOUT)

  if google:
    google_api_package_pattern = re.compile(
      r'\s*([0-9]+)- Google APIs, Android API %d.*' % api_level)
    UpdateSDK(api_level, 'Google APIs', google_api_package_pattern,
              DOWNLOAD_SDK_PLATFORM_TIMEOUT)


def main(argv):
  opt_parser = optparse.OptionParser(
      description='Install dependencies for running the Android emulator')
  opt_parser.add_option('--abi',
                        dest='abi',
                        help='The targeted abi for emulator system image',
                        type='string',
                        default=DEFAULT_ABI)
  opt_parser.add_option('--api-level',
                        dest='api_level',
                        help=('The API level (e.g., 19 for Android 4.4) to '
                              'ensure is available'),
                        type='int',
                        default=DEFAULT_ANDROID_API_LEVEL)
  opt_parser.add_option('-v',
                        dest='verbosity',
                        default=1,
                        action='count',
                        help='Verbose level (multiple times for more)')
  opt_parser.add_option('--google',
                        dest='google',
                        action='store_true',
                        default=False,
                        help='Install Google System Image instead of AOSP')

  options, _ = opt_parser.parse_args(argv[1:])

  run_tests_helper.SetLogLevel(verbose_count=options.verbosity)

  devil_chromium.Initialize()

  # Calls below will download emulator SDK and/or system images only if needed.
  if CheckSDK():
    logging.info('android_emulator_sdk/ exists')
  else:
    logging.critical('ERROR: Emulator SDK not installed in %s'
                     , constants.ANDROID_SDK_ROOT)
    return 1

  # Check target. The target has to be installed in order to run the emulator.
  if CheckSDKPlatform(options.api_level, options.google):
    logging.info('SDK platform %s %s android-%d already present, skipping.',
                 'Google' if options.google else 'AOSP', options.abi,
                 options.api_level)
  else:
    logging.info('SDK platform %s %s android-%d not present, installing.',
                 'Google' if options.google else 'AOSP', options.abi,
                 options.api_level)
    GetSDKPlatform(options.api_level, options.google)

  # Download the system image needed
  if CheckSystemImage(options.abi, options.api_level, options.google):
    logging.info('system image for %s %s android-%d already present, skipping.',
                 'Google' if options.google else 'AOSP', options.abi,
                 options.api_level)
  else:
    GetSystemImage(options.abi, options.api_level, options.google)

  # Make sure KVM packages are installed and enabled.
  if options.abi == 'x86':
    if CheckKVM():
      logging.info('KVM already installed and enabled.')
    else:
      logging.warning('KVM is not installed or enabled.')


if __name__ == '__main__':
  sys.exit(main(sys.argv))