OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2013 The Chromium Authors. All rights reserved. | 3 # Copyright 2013 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Creates symlinks to native libraries for an APK. | 7 """Creates symlinks to native libraries for an APK. |
8 | 8 |
9 The native libraries should have previously been pushed to the device (in | 9 The native libraries should have previously been pushed to the device (in |
10 options.target_dir). This script then creates links in an apk's lib/ folder to | 10 options.target_dir). This script then creates links in an apk's lib/ folder to |
11 those native libraries. | 11 those native libraries. |
12 """ | 12 """ |
13 | 13 |
14 import json | 14 import json |
15 import optparse | 15 import optparse |
16 import os | 16 import os |
17 import sys | 17 import sys |
18 | 18 |
| 19 from util import build_device |
19 from util import build_utils | 20 from util import build_utils |
20 from util import md5_check | 21 from util import md5_check |
21 | 22 |
22 BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__), '..') | 23 BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__), '..') |
23 sys.path.append(BUILD_ANDROID_DIR) | 24 sys.path.append(BUILD_ANDROID_DIR) |
24 | 25 |
25 from pylib import android_commands | |
26 from pylib.utils import apk_helper | 26 from pylib.utils import apk_helper |
27 | 27 |
28 def RunShellCommand(adb, cmd): | 28 def RunShellCommand(device, cmd): |
29 output = adb.RunShellCommand(cmd) | 29 output = device.RunShellCommand(cmd) |
30 | 30 |
31 if output: | 31 if output: |
32 raise Exception( | 32 raise Exception( |
33 'Unexpected output running command: ' + cmd + '\n' + | 33 'Unexpected output running command: ' + cmd + '\n' + |
34 '\n'.join(output)) | 34 '\n'.join(output)) |
35 | 35 |
36 | 36 |
37 def CreateSymlinkScript(options): | 37 def CreateSymlinkScript(options): |
38 libraries = build_utils.ReadJson(options.libraries_json) | 38 libraries = build_utils.ReadJson(options.libraries_json) |
39 | 39 |
40 link_cmd = ( | 40 link_cmd = ( |
41 'rm $APK_LIBRARIES_DIR/%(lib_basename)s > /dev/null 2>&1 \n' | 41 'rm $APK_LIBRARIES_DIR/%(lib_basename)s > /dev/null 2>&1 \n' |
42 'ln -s $STRIPPED_LIBRARIES_DIR/%(lib_basename)s ' | 42 'ln -s $STRIPPED_LIBRARIES_DIR/%(lib_basename)s ' |
43 '$APK_LIBRARIES_DIR/%(lib_basename)s \n' | 43 '$APK_LIBRARIES_DIR/%(lib_basename)s \n' |
44 ) | 44 ) |
45 | 45 |
46 script = '#!/bin/sh \n' | 46 script = '#!/bin/sh \n' |
47 | 47 |
48 for lib in libraries: | 48 for lib in libraries: |
49 script += link_cmd % { 'lib_basename': lib } | 49 script += link_cmd % { 'lib_basename': lib } |
50 | 50 |
51 with open(options.script_host_path, 'w') as scriptfile: | 51 with open(options.script_host_path, 'w') as scriptfile: |
52 scriptfile.write(script) | 52 scriptfile.write(script) |
53 | 53 |
54 | 54 |
55 def TriggerSymlinkScript(options): | 55 def TriggerSymlinkScript(options): |
| 56 device = build_device.GetBuildDeviceFromPath( |
| 57 options.build_device_configuration) |
| 58 if not device: |
| 59 return |
| 60 |
56 apk_package = apk_helper.GetPackageName(options.apk) | 61 apk_package = apk_helper.GetPackageName(options.apk) |
57 apk_libraries_dir = '/data/data/%s/lib' % apk_package | 62 apk_libraries_dir = '/data/data/%s/lib' % apk_package |
58 | 63 |
59 adb = android_commands.AndroidCommands() | |
60 device_dir = os.path.dirname(options.script_device_path) | 64 device_dir = os.path.dirname(options.script_device_path) |
61 mkdir_cmd = ('if [ ! -e %(dir)s ]; then mkdir -p %(dir)s; fi ' % | 65 mkdir_cmd = ('if [ ! -e %(dir)s ]; then mkdir -p %(dir)s; fi ' % |
62 { 'dir': device_dir }) | 66 { 'dir': device_dir }) |
63 RunShellCommand(adb, mkdir_cmd) | 67 RunShellCommand(device, mkdir_cmd) |
64 adb.PushIfNeeded(options.script_host_path, options.script_device_path) | 68 device.PushIfNeeded(options.script_host_path, options.script_device_path) |
65 | 69 |
66 trigger_cmd = ( | 70 trigger_cmd = ( |
67 'APK_LIBRARIES_DIR=%(apk_libraries_dir)s; ' | 71 'APK_LIBRARIES_DIR=%(apk_libraries_dir)s; ' |
68 'STRIPPED_LIBRARIES_DIR=%(target_dir)s; ' | 72 'STRIPPED_LIBRARIES_DIR=%(target_dir)s; ' |
69 '. %(script_device_path)s' | 73 '. %(script_device_path)s' |
70 ) % { | 74 ) % { |
71 'apk_libraries_dir': apk_libraries_dir, | 75 'apk_libraries_dir': apk_libraries_dir, |
72 'target_dir': options.target_dir, | 76 'target_dir': options.target_dir, |
73 'script_device_path': options.script_device_path | 77 'script_device_path': options.script_device_path |
74 } | 78 } |
75 RunShellCommand(adb, trigger_cmd) | 79 RunShellCommand(device, trigger_cmd) |
76 | 80 |
77 | 81 |
78 def main(argv): | 82 def main(argv): |
79 if not build_utils.IsDeviceReady(): | |
80 build_utils.PrintBigWarning( | |
81 'Zero (or multiple) devices attached. Skipping creating symlinks.') | |
82 return | |
83 | |
84 parser = optparse.OptionParser() | 83 parser = optparse.OptionParser() |
85 parser.add_option('--apk', help='Path to the apk.') | 84 parser.add_option('--apk', help='Path to the apk.') |
86 parser.add_option('--script-host-path', | 85 parser.add_option('--script-host-path', |
87 help='Path on the host for the symlink script.') | 86 help='Path on the host for the symlink script.') |
88 parser.add_option('--script-device-path', | 87 parser.add_option('--script-device-path', |
89 help='Path on the device to push the created symlink script.') | 88 help='Path on the device to push the created symlink script.') |
90 parser.add_option('--libraries-json', | 89 parser.add_option('--libraries-json', |
91 help='Path to the json list of native libraries.') | 90 help='Path to the json list of native libraries.') |
92 parser.add_option('--target-dir', | 91 parser.add_option('--target-dir', |
93 help='Device directory that contains the target libraries for symlinks.') | 92 help='Device directory that contains the target libraries for symlinks.') |
94 parser.add_option('--stamp', help='Path to touch on success.') | 93 parser.add_option('--stamp', help='Path to touch on success.') |
| 94 parser.add_option('--build-device-configuration', |
| 95 help='Path to build device configuration.') |
95 options, _ = parser.parse_args() | 96 options, _ = parser.parse_args() |
96 | 97 |
97 required_options = ['apk', 'libraries_json', 'script_host_path', | 98 required_options = ['apk', 'libraries_json', 'script_host_path', |
98 'script_device_path', 'target_dir'] | 99 'script_device_path', 'target_dir'] |
99 build_utils.CheckOptions(options, parser, required=required_options) | 100 build_utils.CheckOptions(options, parser, required=required_options) |
100 | 101 |
101 CreateSymlinkScript(options) | 102 CreateSymlinkScript(options) |
102 TriggerSymlinkScript(options) | 103 TriggerSymlinkScript(options) |
103 | 104 |
104 if options.stamp: | 105 if options.stamp: |
105 build_utils.Touch(options.stamp) | 106 build_utils.Touch(options.stamp) |
106 | 107 |
107 | 108 |
108 if __name__ == '__main__': | 109 if __name__ == '__main__': |
109 sys.exit(main(sys.argv)) | 110 sys.exit(main(sys.argv)) |
OLD | NEW |