def _FirstBootAtBuildTimeOnly()

in tools/android/emulator/unified_launcher.py [0:0]


def _FirstBootAtBuildTimeOnly(
    system_images,
    skin,
    density,
    memory,
    output_dir,
    source_properties,
    vm_size,
    default_properties,
    qemu_gdb_port,
    enable_single_step,
    emulator_tmp_dir,
    boot_time_apks,
    mini_boot):
  """Performs first, cold boot of a system image.

  This is only used at build time. Blaze boots up device in this mode, then
  shuts it down; output of that build action are the disk images. First boot
  of the device is special, slow and weird so we don't want to do that at
  "runtime". Network and all other services are functional after second boot.

  ONLY in this mode we ignore all display and openGL related flags passed to
  unified_launcher (assumption is the bazel will never actually pass them:
  and ONLY in this mode we use the no-op rendering mode "emulator -no-window"
  because 1) it is enough for building the disk images and 2) we want to make
  buildtime action as lightweight and non-flaky as possible (which Xvfb is not)

  Args:
    system_images: a list of system images all in the same directory for boot
      and other support files.
    skin: the screen resolution of the emulated device.
    density: the pixel density.
    memory: the ram of the device.
    output_dir: where to place the fully booted images and snapshot.
    source_properties: an optional dictionary of source.properties for this
      system.
    vm_size: the vmheap per android application.
    default_properties: an optional dictionary of default boot properties to
      set.
    qemu_gdb_port: emulator will start with gdb debugging listening on the port
    enable_single_step: emulator will start in stopped state for debug
      session
    emulator_tmp_dir: temporary directory where system_images/sockets/ramdisk
      files are placed while starting the emulator
    boot_time_apks: apks to install at boot time.
    mini_boot: should the device be booted up in a minimalistic mode.
  """

  sysdir = _FindSystemImagesDir(system_images)
  sysimg_path = (
      _ExtractSuffixFile(system_images, 'system.img.tar.gz') or
      _ExtractSuffixFile(system_images, 'system.img'))
  dataimg_path = (_ExtractSuffixFile(system_images, 'userdata.img.tar.gz') or
                  _ExtractSuffixFile(system_images, 'userdata.img'))
  vendor_img_path = (_ExtractSuffixFile(system_images, 'vendor.img.tar.gz') or
                     _ExtractSuffixFile(system_images, 'vendor.img'))

  encryptionkey_img_path = _ExtractSuffixFile(system_images,
                                              'encryptionkey.img')
  advanced_features_ini = _ExtractSuffixFile(system_images,
                                             'advancedFeatures.ini')
  build_prop_path = _ExtractSuffixFile(system_images, 'build.prop')

  data_files = _ExtractDataFiles(system_images)

  # If this file is present, then the ramdisk was patched as part of a previous
  # build action and we can just use it as is.
  modified_ramdisk_path = _ExtractPrefixFile(system_images, 'modified_ramdisk_')

  device = emulated_device.EmulatedDevice(
      android_platform=_MakeAndroidPlatform(),
      qemu_gdb_port=qemu_gdb_port,
      enable_single_step=enable_single_step,
      source_properties=source_properties,
      mini_boot=mini_boot,
      use_waterfall=FLAGS.use_h2o or FLAGS.use_waterfall,
      forward_bin=FLAGS.forward_bin,
      ports_bin=FLAGS.ports_bin)
  device.delete_temp_on_exit = False  # we will do it ourselves.

  device.Configure(
      sysdir,
      skin,
      memory,
      density,
      vm_size,
      source_properties=source_properties,
      default_properties=default_properties,
      kvm_present=_IsKvmPresent(),
      system_image_path=sysimg_path,
      data_image_path=dataimg_path,
      vendor_img_path=vendor_img_path,
      encryptionkey_img_path=encryptionkey_img_path,
      advanced_features_ini=advanced_features_ini,
      build_prop_path=build_prop_path,
      data_files=data_files)

  device.StartDevice(enable_display=False,  # Will be ignored.
                     start_vnc_on_port=0,  # Will be ignored.
                     emulator_tmp_dir=emulator_tmp_dir,
                     save_snapshot=FLAGS.save_snapshot,
                     modified_ramdisk_path=modified_ramdisk_path)

  try:
    device.LogToDevice('Device booted.')
    for apk in boot_time_apks:
      try:
        device.InstallApk(apk)
      except Exception as error:
        device.KillEmulator()
        raise error
    # Wait for system being idle for 4 minutes before shutting down.
    if FLAGS.save_snapshot:
      idle = emulated_device.IdleStatus(device=device)
      for _ in range(40):
        time.sleep(6)
        load = idle.RecentMaxLoad(15)
        if load < 0.1:
          logging.info('Emulator is idle now.')
          break
    _StopDeviceAndOutputState(device, output_dir)
  finally:
    device.CleanUp()