Primiano Tucci | d775045 | 2017-09-29 14:38:51 +0100 | [diff] [blame^] | 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 | |
| 16 | import argparse |
| 17 | import os |
| 18 | import shutil |
| 19 | import sys |
| 20 | |
| 21 | |
| 22 | def 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 | parser.add_argument('image', help='arm|arm64 (see //build/android_emulators)') |
| 27 | args = parser.parse_args() |
| 28 | |
| 29 | root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 30 | ini = os.path.join(root_dir, 'build', 'android_emulators', args.image + '.ini') |
| 31 | assert os.path.exists(ini), 'File not found: ' + ini |
| 32 | |
| 33 | avd_dir = os.path.join(root_dir, 'buildtools', 'emulator_images') |
| 34 | if not os.path.exists(avd_dir): |
| 35 | os.makedirs(avd_dir) |
| 36 | |
| 37 | emu_img_dir = os.path.join(avd_dir, args.image + '.avd') |
| 38 | if not os.path.exists(emu_img_dir): |
| 39 | os.makedirs(emu_img_dir) |
| 40 | shutil.copyfile(ini, os.path.join(emu_img_dir, 'config.ini')) |
| 41 | |
| 42 | with open(os.path.join(avd_dir, args.image + '.ini'), 'w') as f: |
| 43 | f.write('path=' + emu_img_dir) |
| 44 | |
| 45 | sdk_dir = os.path.join(root_dir, 'buildtools', 'android_sdk') |
| 46 | env = { |
| 47 | # Travis CI doesn't set this and causes the emulator to fallback in |
| 48 | # 32-bit mode with a "Cannot decide host bitness because $SHELL" error. |
| 49 | 'SHELL': '/bin/bash', |
| 50 | 'ANDROID_EMULATOR_DEBUG': '1' if args.verbose else '0', |
| 51 | 'ANDROID_SDK_ROOT': sdk_dir, |
| 52 | 'ANDROID_AVD_HOME': avd_dir, |
| 53 | 'DYLD_LIBRARY_PATH': os.path.join(sdk_dir, 'tools', 'lib64', 'qt', 'lib'), |
| 54 | } |
| 55 | emulator_bin = os.path.join(sdk_dir, 'tools', 'emulator') |
| 56 | emulator_args = ['-no-window', '-no-snapshot', '-gpu', 'off', '-wipe-data', |
| 57 | '-avd', args.image] |
| 58 | print '\n'.join('='.join(x) for x in env.items()) |
| 59 | print ' '.join([emulator_bin] + emulator_args) |
| 60 | if args.pid: |
| 61 | with open(args.pid, 'w') as f: |
| 62 | f.write(str(os.getpid())) |
| 63 | os.execve(emulator_bin, [emulator_bin] + emulator_args, env) |
| 64 | |
| 65 | |
| 66 | if __name__ == '__main__': |
| 67 | sys.exit(Main()) |