Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(408)

Side by Side Diff: webrtc/build/android/build_aar.py

Issue 2653533004: Android: Script for building libwebrtc.aar. (Closed)
Patch Set: Changes according to kjellander's comments. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | webrtc/sdk/android/AndroidManifest.xml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 - libjingle_peerconnection_so.so
22 """
23
24 import argparse
25 import logging
26 import os
27 import shutil
28 import subprocess
29 import sys
30 import tempfile
31 import zipfile
32
33
34 DEFAULT_ARCHS = ['armeabi-v7a', 'x86']
35 NEEDED_SO_FILES = [
kjellander_webrtc 2017/01/24 06:47:00 nit: put the list contents on one line
sakal 2017/01/24 08:40:28 Done.
36 'libjingle_peerconnection_so.so',
37 ]
38 JAR_FILE = 'lib.java/webrtc/sdk/android/libwebrtc.jar'
39 MANIFEST_FILE = 'webrtc/sdk/android/AndroidManifest.xml'
40 TARGETS = [
41 'webrtc/sdk/android:libwebrtc',
42 'webrtc/sdk/android:libjingle_peerconnection_so',
43 ]
44
45
46 def _ParseArgs():
47 parser = argparse.ArgumentParser(description='libwebrtc.aar generator.')
48 parser.add_argument('--output', default='libwebrtc.aar',
49 help='Output file of the scipt.')
50 parser.add_argument('--arch', action='append', default=[],
51 help='Architectures to build. Defaults to ' + str(DEFAULT_ARCHS))
52 parser.add_argument('--use-goma', action='store_true', default=False,
53 help='Use goma.')
54 parser.add_argument('--verbose', action='store_true', default=False,
55 help='Debug logging.')
56 return parser.parse_args()
57
58
59 def _RunGN(args):
60 cmd = ['gn']
61 cmd.extend(args)
62 logging.debug('Running: %r', cmd)
63 subprocess.check_call(cmd)
64
65
66 def _RunNinja(output_directory, args):
67 cmd = ['ninja', '-C', output_directory]
68 cmd.extend(args)
69 logging.debug('Running: %r', cmd)
70 subprocess.check_call(cmd)
71
72
73 def _EncodeForGN(value):
74 """Encodes value as a GN literal."""
75 if type(value) is str:
76 return '"' + value + '"'
77 elif type(value) is bool:
78 return repr(value).lower()
79 else:
80 return repr(value)
81
82
83 def _GetOutputDirectory(tmp_dir, arch):
84 """Returns the GN output directory for the target architecture."""
85 return os.path.join(tmp_dir, arch)
86
87
88 def _GetTargetCpu(arch):
89 """Returns target_cpu for the GN build with the given architecture."""
90 if arch in ['armeabi', 'armeabi-v7a']:
91 return 'arm'
92 elif arch == 'x86':
93 return 'x86'
94 else:
95 raise Exception('Unknown arch: ' + arch)
96
97
98 def _GetArmVersion(arch):
99 """Returns arm_version for the GN build with the given architecture."""
100 if arch == 'armeabi':
101 return 6
102 elif arch == 'armeabi-v7a':
103 return 7
104 elif arch == 'x86':
105 return None
106 else:
107 raise Exception('Unknown arch: ' + arch)
108
109
110 def Build(tmp_dir, arch, use_goma):
111 """Generates target architecture using GN and builds it using ninja."""
112 logging.info('Building: %s', arch)
113 output_directory = _GetOutputDirectory(tmp_dir, arch)
114 gn_args = {
115 'target_os': 'android',
116 'is_debug': False,
117 'is_component_build': False,
118 'target_cpu': _GetTargetCpu(arch),
119 'use_goma': use_goma
120 }
121 arm_version = _GetArmVersion(arch)
122 if arm_version:
123 gn_args['arm_version'] = arm_version
124 gn_args_str = '--args=' + ' '.join([
125 k + '=' + _EncodeForGN(v) for k, v in gn_args.items()])
126
127 _RunGN(['gen', output_directory, gn_args_str])
128
129 ninja_args = TARGETS
130 if use_goma:
131 ninja_args.extend(['-j', '1024'])
132 _RunNinja(output_directory, ninja_args)
133
134
135 def CollectCommon(aar_file, tmp_dir, arch):
136 """Collects architecture independent files into the .aar-archive."""
137 logging.info('Collecting common files.')
138 output_directory = _GetOutputDirectory(tmp_dir, arch)
139 aar_file.write(MANIFEST_FILE, 'AndroidManifest.xml')
140 aar_file.write(os.path.join(output_directory, JAR_FILE), 'classes.jar')
141
142
143 def Collect(aar_file, tmp_dir, arch):
144 """Collects architecture specific files into the .aar-archive."""
145 logging.info('Collecting: %s', arch)
146 output_directory = _GetOutputDirectory(tmp_dir, arch)
147
148 abi_dir = os.path.join('jni', arch)
149 for so_file in NEEDED_SO_FILES:
150 aar_file.write(os.path.join(output_directory, so_file),
151 os.path.join(abi_dir, so_file))
152
153
154 def main():
155 args = _ParseArgs()
156 if not args.arch:
157 args.arch = DEFAULT_ARCHS
158
159 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
160
161 tmp_dir = tempfile.mkdtemp()
162
163 for arch in args.arch:
164 Build(tmp_dir, arch, args.use_goma)
165
166 with zipfile.ZipFile(args.output, 'w') as aar_file:
167 CollectCommon(aar_file, tmp_dir, args.arch[0])
168 for arch in args.arch:
169 Collect(aar_file, tmp_dir, arch)
170
171 shutil.rmtree(tmp_dir, True)
172
173
174 if __name__ == '__main__':
175 sys.exit(main())
OLDNEW
« no previous file with comments | « no previous file | webrtc/sdk/android/AndroidManifest.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698