1#!/usr/bin/env python
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import argparse
17import os
18import shutil
19import sys
20
21
22def Main():
23  parser = argparse.ArgumentParser()
24  parser.add_argument('--verbose', '-v', action='store_true')
25  parser.add_argument('--pid', help='(optional) save pid into given file')
26  args = parser.parse_args()
27
28  root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
29
30  if sys.platform.startswith('linux'):
31    emulator_root = os.path.join(
32      root_dir, 'buildtools', 'emulator', 'linux-x86_64')
33    emulator_path = os.path.join(emulator_root, 'qemu', 'linux-x86_64')
34  else:
35    emulator_root = os.path.join(
36      root_dir, 'buildtools', 'emulator', 'darwin-x86_64')
37    emulator_path = os.path.join(emulator_root, 'qemu', 'darwin-x86_64')
38
39  aosp_path = os.path.join(root_dir, 'buildtools', 'aosp-arm')
40
41  env = {
42      # Travis CI doesn't set this and causes the emulator to fallback in
43      # 32-bit mode with a "Cannot decide host bitness because $SHELL" error.
44      'SHELL': '/bin/bash',
45      'LD_LIBRARY_PATH': os.path.join(emulator_root, 'lib64', 'qt', 'lib'),
46      'DYLD_LIBRARY_PATH': os.path.join(emulator_root, 'lib64', 'qt', 'lib'),
47  }
48  emulator_bin = os.path.join(emulator_path, 'qemu-system-armel')
49  emulator_args = ['-no-window', '-no-snapshot',  '-gpu', 'off', '-no-accel',
50                   '-sysdir', aosp_path,
51                   '-system', os.path.join(aosp_path, 'system-qemu.img'),
52                   '-kernel', os.path.join(aosp_path, 'kernel-ranchu'),
53                   '-ramdisk', os.path.join(aosp_path, 'ramdisk.img'),
54                   '-vendor', os.path.join(aosp_path, 'vendor-qemu.img'),
55                   '-data', os.path.join(aosp_path, 'userdata-qemu.img')]
56  print '\n'.join('='.join(x) for x in env.items())
57  print ' '.join([emulator_bin] + emulator_args)
58  if args.pid:
59    with open(args.pid, 'w') as f:
60      f.write(str(os.getpid()))
61  os.execve(emulator_bin, [emulator_bin] + emulator_args, env)
62
63
64if __name__ == '__main__':
65  sys.exit(Main())
66