OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python3 | |
kjellander_webrtc
2017/01/23 14:39:31
Prefer #!/usr/bin/env python
Especially Python 3
sakal
2017/01/23 15:29:03
Done.
| |
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 # Run from root src folder: ./webrtc/build/android/build_aar.py | |
kjellander_webrtc
2017/01/23 14:39:30
Add a docstring that briefly explains what this sc
kjellander_webrtc
2017/01/23 14:39:31
Is it a requirement to run this from the src/ fold
sakal
2017/01/23 15:29:03
Done.
sakal
2017/01/23 15:29:03
Do you have an eloquent method to check the folder
| |
12 | |
13 import argparse | |
14 import logging | |
15 import os | |
16 import subprocess | |
17 import sys | |
18 import zipfile | |
19 | |
20 DEFAULT_ARCHS = ['armeabi-v7a', 'x86'] | |
21 NEEDED_SO_FILES = [ | |
22 'libc++_shared.so', | |
kjellander_webrtc
2017/01/23 14:39:31
Is there a way around hardcoding these into the sc
sakal
2017/01/23 15:29:03
Actually, it seems we only need libjingle_peerconn
| |
23 'libboringssl.cr.so', | |
24 'libjingle_peerconnection_so.so', | |
25 'libprotobuf_lite.cr.so', | |
26 ] | |
27 JAR_FILE = 'lib.java/webrtc/sdk/android/libwebrtc.jar' | |
28 MANIFEST_FILE = 'webrtc/sdk/android/AndroidManifest.xml' | |
29 TARGETS = [ | |
30 'build/android:cpplib_stripped', | |
31 'webrtc/sdk/android:libwebrtc', | |
32 'webrtc/sdk/android:libjingle_peerconnection_so' | |
33 ] | |
34 TMP_OUTPUT = '/tmp/webrtc_android_aar_build/' | |
kjellander_webrtc
2017/01/23 14:39:31
Please use Python's tempfile module to create a un
sakal
2017/01/23 15:29:03
Done.
| |
35 | |
36 parser = argparse.ArgumentParser(description='libwebrtc.aar generator.') | |
kjellander_webrtc
2017/01/23 14:39:31
Refactor line 36-44 into a _ParseArgs function tha
sakal
2017/01/23 15:29:03
Done.
| |
37 parser.add_argument('--output', default='libwebrtc.aar', | |
38 help='Output file of the scipt.') | |
39 parser.add_argument('--arch', action='append', default=[], | |
kjellander_webrtc
2017/01/23 14:39:31
Can't you set default=DEFAULT_ARCHS here?
sakal
2017/01/23 15:29:03
No, additional arch parameters would be just be ap
| |
40 help='Architectures to build. Defaults to ' + str(DEFAULT_ARCHS)) | |
41 parser.add_argument('--use-goma', action='store_true', default=False, | |
42 help='Use goma.') | |
43 parser.add_argument('--debug', action='store_true', default=False, | |
kjellander_webrtc
2017/01/23 14:39:31
I prefer --verbose, but I won't force you.
sakal
2017/01/23 15:29:03
Done.
| |
44 help='Debug logging.') | |
45 | |
46 def RunGN(args): | |
kjellander_webrtc
2017/01/23 14:39:31
Use two blank lines before top-level statements/fu
sakal
2017/01/23 15:29:03
Done.
| |
47 cmd = ['gn'] | |
48 cmd.extend(args) | |
49 logging.debug('Running: %r', cmd) | |
50 subprocess.check_call(cmd) | |
51 | |
52 def RunNinja(output_directory, args): | |
53 cmd = ['ninja', '-C', output_directory] | |
54 cmd.extend(args) | |
55 logging.debug('Running: %r', cmd) | |
56 subprocess.check_call(cmd) | |
57 | |
58 def EncodeForGN(value): | |
59 """Encodes value as a GN literal.""" | |
60 if type(value) is str: | |
61 return '"' + value + '"' | |
62 elif type(value) is bool: | |
63 return repr(value).lower() | |
64 else: | |
65 return repr(value) | |
66 | |
67 def GetOutputDirectory(arch): | |
68 """Returns the GN output directory for the target architecture.""" | |
69 return TMP_OUTPUT + arch | |
70 | |
71 def GetTargetCpu(arch): | |
72 """Returns target_cpu for the GN build with the given architecture.""" | |
73 if arch in ['armeabi', 'armeabi-v7a']: | |
74 return 'arm' | |
75 elif arch == 'x86': | |
76 return 'x86' | |
77 else: | |
78 raise Exception('Unknown arch: ' + arch) | |
79 | |
80 def GetArmVersion(arch): | |
81 """Returns arm_version for the GN build with the given architecture.""" | |
82 if arch == 'armeabi': | |
83 return 6 | |
84 elif arch == 'armeabi-v7a': | |
85 return 7 | |
86 elif arch == 'x86': | |
87 return None | |
88 else: | |
89 raise Exception('Unknown arch: ' + arch) | |
90 | |
91 def Build(arch, use_goma): | |
92 """Generates target architecture using GN and builds it using ninja.""" | |
93 logging.info('Building: ' + arch) | |
kjellander_webrtc
2017/01/23 14:39:31
Use string formatters:
logging.info('Building: %s'
sakal
2017/01/23 15:29:03
Done.
| |
94 output_directory = GetOutputDirectory(arch) | |
95 gn_args = { | |
96 'target_os': 'android', | |
97 'target_cpu': GetTargetCpu(arch), | |
98 'use_goma': use_goma | |
99 } | |
100 arm_version = GetArmVersion(arch) | |
101 if arm_version: | |
102 gn_args['arm_version'] = arm_version | |
103 gn_args_str = '--args=' + ' '.join([ | |
104 k + '=' + EncodeForGN(v) for k, v in gn_args.items()]) | |
105 | |
106 RunGN(['gen', output_directory, gn_args_str]) | |
107 | |
108 ninja_args = TARGETS | |
109 if use_goma: | |
110 ninja_args.extend(['-j', '1024']) | |
111 RunNinja(output_directory, ninja_args) | |
112 | |
113 def CollectCommon(aar, arch): | |
kjellander_webrtc
2017/01/23 14:39:30
aar -> aar_file to make it more clear what this ob
sakal
2017/01/23 15:29:03
Done.
| |
114 """Collects architecture independent files into the .aar-archive.""" | |
115 logging.info('Collecting common files.') | |
116 output_directory = GetOutputDirectory(arch) | |
117 aar.write(MANIFEST_FILE, 'AndroidManifest.xml') | |
118 aar.write(os.path.join(output_directory, JAR_FILE), 'classes.jar') | |
119 | |
120 def Collect(aar, arch): | |
121 """Collects architecture specific files into the .aar-archive.""" | |
122 logging.info('Collecting: ' + arch) | |
123 output_directory = GetOutputDirectory(arch) | |
124 | |
125 jni_dir = 'jni' | |
kjellander_webrtc
2017/01/23 14:39:31
Remove this variable, it doesn't add anything usef
sakal
2017/01/23 15:29:03
Done.
| |
126 abi_dir = os.path.join(jni_dir, arch) | |
127 | |
128 for so_file in NEEDED_SO_FILES: | |
129 aar.write(os.path.join(output_directory, so_file), | |
130 os.path.join(abi_dir, so_file)) | |
kjellander_webrtc
2017/01/23 14:39:31
+ indentation to alight with the above parenthesis
sakal
2017/01/23 15:29:03
Done.
| |
131 | |
132 def main(): | |
133 args = parser.parse_args() | |
134 if not args.arch: | |
kjellander_webrtc
2017/01/23 14:39:31
Add a parser.error message if len(args.arch) < 1.
sakal
2017/01/23 15:29:03
Wouldn't that be the case when using the default a
kjellander_webrtc
2017/01/24 06:47:00
Yeah, this assumed it was possible to use default=
| |
135 args.arch = DEFAULT_ARCHS | |
kjellander_webrtc
2017/01/23 14:39:31
Can't you set this at line 39 instead?
sakal
2017/01/23 15:29:03
No, see above.
| |
136 | |
137 logging.basicConfig(level=logging.INFO if not args.debug else logging.DEBUG) | |
kjellander_webrtc
2017/01/23 14:39:30
level=logging.DEBUG if args.debug else logging.INF
sakal
2017/01/23 15:29:03
Done.
| |
138 | |
139 for arch in args.arch: | |
140 Build(arch, args.use_goma) | |
141 | |
142 with zipfile.ZipFile(args.output, 'w') as aar: | |
143 CollectCommon(aar, args.arch[0]) | |
kjellander_webrtc
2017/01/23 14:39:31
This makes me wonder what different it makes which
sakal
2017/01/23 15:29:03
There is no difference. The common outputs are the
kjellander_webrtc
2017/01/24 06:47:00
Please add a comment explaining that to make it ob
| |
144 for arch in args.arch: | |
145 Collect(aar, arch) | |
146 | |
147 if __name__ == '__main__': | |
148 sys.exit(main()) | |
OLD | NEW |