OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 | |
3 # Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | |
4 # | |
5 # Use of this source code is governed by a BSD-style license | |
6 # that can be found in the LICENSE file in the root of the source | |
7 # tree. An additional intellectual property rights grant can be found | |
8 # in the file PATENTS. All contributing project authors may | |
9 # be found in the AUTHORS file in the root of the source tree. | |
10 | |
11 """Script to generate libwebrtc.aar for distribution. | |
12 | |
13 The script has to be run from the root src folder. | |
14 ./webrtc/build/android/build_aar.py | |
15 | |
16 .aar-file is just a zip-archive containing the files of the library. The file | |
17 structure generated by this script looks like this: | |
18 - AndroidManifest.xml | |
19 - classes.jar | |
20 - libs/ | |
21 - armeabi-v7a/ | |
22 - libjingle_peerconnection_so.so | |
23 - x86/ | |
24 - libjingle_peerconnection_so.so | |
25 """ | |
26 | |
27 import argparse | |
28 import logging | |
29 import os | |
30 import shutil | |
31 import subprocess | |
32 import sys | |
33 import tempfile | |
34 import zipfile | |
35 | |
36 | |
37 DEFAULT_ARCHS = ['armeabi-v7a', 'x86'] | |
38 NEEDED_SO_FILES = ['libjingle_peerconnection_so.so'] | |
39 JAR_FILE = 'lib.java/webrtc/sdk/android/libwebrtc.jar' | |
40 MANIFEST_FILE = 'webrtc/sdk/android/AndroidManifest.xml' | |
41 TARGETS = [ | |
42 'webrtc/sdk/android:libwebrtc', | |
43 'webrtc/sdk/android:libjingle_peerconnection_so', | |
44 ] | |
45 | |
46 | |
47 def _ParseArgs(): | |
48 parser = argparse.ArgumentParser(description='libwebrtc.aar generator.') | |
49 parser.add_argument('--output', default='libwebrtc.aar', | |
50 help='Output file of the script.') | |
51 parser.add_argument('--arch', action='append', default=[], | |
52 help='Architectures to build. Defaults to ' + str(DEFAULT_ARCHS)) | |
53 parser.add_argument('--use-goma', action='store_true', default=False, | |
54 help='Use goma.') | |
55 parser.add_argument('--verbose', action='store_true', default=False, | |
56 help='Debug logging.') | |
57 return parser.parse_args() | |
58 | |
59 | |
60 def _RunGN(args): | |
61 cmd = ['gn'] | |
62 cmd.extend(args) | |
63 logging.debug('Running: %r', cmd) | |
64 subprocess.check_call(cmd) | |
65 | |
66 | |
67 def _RunNinja(output_directory, args): | |
68 cmd = ['ninja', '-C', output_directory] | |
69 cmd.extend(args) | |
70 logging.debug('Running: %r', cmd) | |
71 subprocess.check_call(cmd) | |
72 | |
73 | |
74 def _EncodeForGN(value): | |
75 """Encodes value as a GN literal.""" | |
76 if type(value) is str: | |
77 return '"' + value + '"' | |
78 elif type(value) is bool: | |
79 return repr(value).lower() | |
80 else: | |
81 return repr(value) | |
82 | |
83 | |
84 def _GetOutputDirectory(tmp_dir, arch): | |
85 """Returns the GN output directory for the target architecture.""" | |
86 return os.path.join(tmp_dir, arch) | |
87 | |
88 | |
89 def _GetTargetCpu(arch): | |
90 """Returns target_cpu for the GN build with the given architecture.""" | |
91 if arch in ['armeabi', 'armeabi-v7a']: | |
92 return 'arm' | |
93 elif arch == 'x86': | |
94 return 'x86' | |
95 else: | |
96 raise Exception('Unknown arch: ' + arch) | |
97 | |
98 | |
99 def _GetArmVersion(arch): | |
100 """Returns arm_version for the GN build with the given architecture.""" | |
101 if arch == 'armeabi': | |
102 return 6 | |
103 elif arch == 'armeabi-v7a': | |
104 return 7 | |
105 elif arch == 'x86': | |
106 return None | |
107 else: | |
108 raise Exception('Unknown arch: ' + arch) | |
109 | |
110 | |
111 def Build(tmp_dir, arch, use_goma): | |
112 """Generates target architecture using GN and builds it using ninja.""" | |
113 logging.info('Building: %s', arch) | |
114 output_directory = _GetOutputDirectory(tmp_dir, arch) | |
115 gn_args = { | |
116 'target_os': 'android', | |
117 'is_debug': False, | |
118 'is_component_build': False, | |
119 'target_cpu': _GetTargetCpu(arch), | |
120 'use_goma': use_goma | |
121 } | |
122 arm_version = _GetArmVersion(arch) | |
123 if arm_version: | |
124 gn_args['arm_version'] = arm_version | |
125 gn_args_str = '--args=' + ' '.join([ | |
126 k + '=' + _EncodeForGN(v) for k, v in gn_args.items()]) | |
127 | |
128 _RunGN(['gen', output_directory, gn_args_str]) | |
129 | |
130 ninja_args = TARGETS | |
131 if use_goma: | |
132 ninja_args.extend(['-j', '1024']) | |
133 _RunNinja(output_directory, ninja_args) | |
134 | |
135 | |
136 def CollectCommon(aar_file, tmp_dir, arch): | |
137 """Collects architecture independent files into the .aar-archive.""" | |
138 logging.info('Collecting common files.') | |
139 output_directory = _GetOutputDirectory(tmp_dir, arch) | |
140 aar_file.write(MANIFEST_FILE, 'AndroidManifest.xml') | |
141 aar_file.write(os.path.join(output_directory, JAR_FILE), 'classes.jar') | |
142 | |
143 | |
144 def Collect(aar_file, tmp_dir, arch): | |
145 """Collects architecture specific files into the .aar-archive.""" | |
146 logging.info('Collecting: %s', arch) | |
147 output_directory = _GetOutputDirectory(tmp_dir, arch) | |
148 | |
149 abi_dir = os.path.join('jni', arch) | |
150 for so_file in NEEDED_SO_FILES: | |
151 aar_file.write(os.path.join(output_directory, so_file), | |
152 os.path.join(abi_dir, so_file)) | |
153 | |
154 | |
155 def main(): | |
156 args = _ParseArgs() | |
157 if not args.arch: | |
158 args.arch = DEFAULT_ARCHS | |
159 | |
160 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO) | |
161 | |
162 tmp_dir = tempfile.mkdtemp() | |
163 | |
164 for arch in args.arch: | |
165 Build(tmp_dir, arch, args.use_goma) | |
166 | |
167 with zipfile.ZipFile(args.output, 'w') as aar_file: | |
168 # Architecture doesn't matter here, arbitrarily using the first one. | |
169 CollectCommon(aar_file, tmp_dir, args.arch[0]) | |
170 for arch in args.arch: | |
171 Collect(aar_file, tmp_dir, arch) | |
172 | |
173 shutil.rmtree(tmp_dir, True) | |
174 | |
175 | |
176 if __name__ == '__main__': | |
177 sys.exit(main()) | |
OLD | NEW |