Thanks to visit codestin.com
Credit goes to webrtc.googlesource.com

blob: 2fbce97c6008c6f37111756449623c60a0f4b74e [file] [log] [blame]
Christoffer Jansson4e8a7732022-02-08 08:01:121#!/usr/bin/env vpython3
sakala4a75382017-01-24 09:25:502
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.
sakala4a75382017-01-24 09:25:5010"""Script to generate libwebrtc.aar for distribution.
11
12The script has to be run from the root src folder.
Henrik Kjellander90fd7d82017-05-09 06:30:1013./tools_webrtc/android/build_aar.py
sakala4a75382017-01-24 09:25:5014
15.aar-file is just a zip-archive containing the files of the library. The file
16structure generated by this script looks like this:
17 - AndroidManifest.xml
18 - classes.jar
19 - libs/
20 - armeabi-v7a/
21 - libjingle_peerconnection_so.so
22 - x86/
23 - libjingle_peerconnection_so.so
24"""
25
26import argparse
27import logging
28import os
Junji Watanabe41150c52025-06-25 13:16:4529import pathlib
sakala4a75382017-01-24 09:25:5030import subprocess
31import sys
sakala4a75382017-01-24 09:25:5032import zipfile
33
sakal67e414c2017-09-05 07:16:1534SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
Henrik Kjellanderec57e052017-10-17 19:36:0135SRC_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
Junji Watanabe41150c52025-06-25 13:16:4536DEFAULT_BUILD_DIR = os.path.join(SRC_DIR, 'out_aar')
sakal423f1062017-04-07 12:10:1537DEFAULT_ARCHS = ['armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64']
sakala4a75382017-01-24 09:25:5038NEEDED_SO_FILES = ['libjingle_peerconnection_so.so']
Henrik Kjellander03ec4f82017-09-27 14:07:4039JAR_FILE = 'lib.java/sdk/android/libwebrtc.jar'
40MANIFEST_FILE = 'sdk/android/AndroidManifest.xml'
sakala4a75382017-01-24 09:25:5041TARGETS = [
Mirko Bonadei8cc66952020-10-30 09:13:4542 'sdk/android:libwebrtc',
43 'sdk/android:libjingle_peerconnection_so',
sakala4a75382017-01-24 09:25:5044]
45
sakal67e414c2017-09-05 07:16:1546sys.path.append(os.path.join(SCRIPT_DIR, '..', 'libs'))
47from generate_licenses import LicenseBuilder
48
Henrik Kjellanderec57e052017-10-17 19:36:0149sys.path.append(os.path.join(SRC_DIR, 'build'))
50import find_depot_tools
51
52
sakala4a75382017-01-24 09:25:5053def _ParseArgs():
Christoffer Dewerin81a91172024-04-08 07:26:4154 parser = argparse.ArgumentParser(description='libwebrtc.aar generator.')
55 parser.add_argument(
56 '--build-dir',
57 type=os.path.abspath,
Junji Watanabe41150c52025-06-25 13:16:4558 default=DEFAULT_BUILD_DIR,
Christoffer Dewerin81a91172024-04-08 07:26:4159 help='Build dir. By default will create and use temporary dir.')
60 parser.add_argument('--output',
61 default='libwebrtc.aar',
62 type=os.path.abspath,
63 help='Output file of the script.')
64 parser.add_argument(
65 '--arch',
66 default=DEFAULT_ARCHS,
67 nargs='*',
68 help='Architectures to build. Defaults to %(default)s.')
Christoffer Dewerin81a91172024-04-08 07:26:4169 parser.add_argument('--use-remoteexec',
70 action='store_true',
71 default=False,
72 help='Use RBE.')
73 parser.add_argument('--use-unstripped-libs',
74 action='store_true',
75 default=False,
76 help='Use unstripped .so files within libwebrtc.aar')
77 parser.add_argument('--verbose',
78 action='store_true',
79 default=False,
80 help='Debug logging.')
81 parser.add_argument(
82 '--extra-gn-args',
83 default=[],
84 nargs='*',
85 help="""Additional GN arguments to be used during Ninja generation.
Yura Yaroshevichf517f112018-05-24 13:48:0286 These are passed to gn inside `--args` switch and
87 applied after any other arguments and will
88 override any values defined by the script.
89 Example of building debug aar file:
90 build_aar.py --extra-gn-args='is_debug=true'""")
Christoffer Dewerin81a91172024-04-08 07:26:4191 parser.add_argument(
92 '--extra-ninja-switches',
93 default=[],
94 nargs='*',
95 help="""Additional Ninja switches to be used during compilation.
Yura Yaroshevichf517f112018-05-24 13:48:0296 These are applied after any other Ninja switches.
97 Example of enabling verbose Ninja output:
98 build_aar.py --extra-ninja-switches='-v'""")
Christoffer Dewerin81a91172024-04-08 07:26:4199 parser.add_argument(
100 '--extra-gn-switches',
101 default=[],
102 nargs='*',
103 help="""Additional GN switches to be used during compilation.
Yura Yaroshevichf517f112018-05-24 13:48:02104 These are applied after any other GN switches.
105 Example of enabling verbose GN output:
106 build_aar.py --extra-gn-switches='-v'""")
Christoffer Dewerin81a91172024-04-08 07:26:41107 return parser.parse_args()
sakala4a75382017-01-24 09:25:50108
109
110def _RunGN(args):
Christoffer Dewerin81a91172024-04-08 07:26:41111 cmd = [
112 sys.executable,
113 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py')
114 ]
115 cmd.extend(args)
116 logging.debug('Running: %r', cmd)
117 subprocess.check_call(cmd)
sakala4a75382017-01-24 09:25:50118
119
120def _RunNinja(output_directory, args):
Christoffer Dewerin81a91172024-04-08 07:26:41121 cmd = [
Junji Watanabe5d165b12025-05-15 05:34:41122 os.path.join(SRC_DIR, 'third_party', 'siso', 'cipd', 'siso'),
123 'ninja',
124 '-C',
125 output_directory,
Christoffer Dewerin81a91172024-04-08 07:26:41126 ]
127 cmd.extend(args)
128 logging.debug('Running: %r', cmd)
129 subprocess.check_call(cmd)
sakala4a75382017-01-24 09:25:50130
131
132def _EncodeForGN(value):
Christoffer Dewerin81a91172024-04-08 07:26:41133 """Encodes value as a GN literal."""
134 if isinstance(value, str):
135 return '"' + value + '"'
136 if isinstance(value, bool):
137 return repr(value).lower()
138 return repr(value)
sakala4a75382017-01-24 09:25:50139
140
korniltsev.anatoly0b510a92017-09-05 15:12:30141def _GetOutputDirectory(build_dir, arch):
Christoffer Dewerin81a91172024-04-08 07:26:41142 """Returns the GN output directory for the target architecture."""
143 return os.path.join(build_dir, arch)
sakala4a75382017-01-24 09:25:50144
145
146def _GetTargetCpu(arch):
Christoffer Dewerin81a91172024-04-08 07:26:41147 """Returns target_cpu for the GN build with the given architecture."""
148 if arch in ['armeabi', 'armeabi-v7a']:
149 return 'arm'
150 if arch == 'arm64-v8a':
151 return 'arm64'
152 if arch == 'x86':
153 return 'x86'
154 if arch == 'x86_64':
155 return 'x64'
156 raise Exception('Unknown arch: ' + arch)
sakala4a75382017-01-24 09:25:50157
158
159def _GetArmVersion(arch):
Christoffer Dewerin81a91172024-04-08 07:26:41160 """Returns arm_version for the GN build with the given architecture."""
161 if arch == 'armeabi':
162 return 6
163 if arch == 'armeabi-v7a':
164 return 7
165 if arch in ['arm64-v8a', 'x86', 'x86_64']:
166 return None
167 raise Exception('Unknown arch: ' + arch)
sakala4a75382017-01-24 09:25:50168
169
Jeremy Leconte0b496cc2024-05-16 08:14:06170def Build(build_dir, arch, use_remoteexec, extra_gn_args, extra_gn_switches,
171 extra_ninja_switches):
Christoffer Dewerin81a91172024-04-08 07:26:41172 """Generates target architecture using GN and builds it using ninja."""
173 logging.info('Building: %s', arch)
174 output_directory = _GetOutputDirectory(build_dir, arch)
175 gn_args = {
176 'target_os': 'android',
177 'is_debug': False,
178 'is_component_build': False,
179 'rtc_include_tests': False,
180 'target_cpu': _GetTargetCpu(arch),
Christoffer Dewerincd43baf2025-04-04 14:40:26181 'android_static_analysis': "off",
Junji Watanabe5d165b12025-05-15 05:34:41182 'use_siso': True,
Christoffer Dewerin81a91172024-04-08 07:26:41183 }
Junji Watanabe5d165b12025-05-15 05:34:41184 if use_remoteexec:
185 gn_args.update({
186 'use_remoteexec': True,
187 'use_reclient': False,
188 })
Christoffer Dewerin81a91172024-04-08 07:26:41189 arm_version = _GetArmVersion(arch)
190 if arm_version:
191 gn_args['arm_version'] = arm_version
192 gn_args_str = '--args=' + ' '.join(
193 [k + '=' + _EncodeForGN(v)
194 for k, v in gn_args.items()] + extra_gn_args)
sakala4a75382017-01-24 09:25:50195
Christoffer Dewerin81a91172024-04-08 07:26:41196 gn_args_list = ['gen', output_directory, gn_args_str]
197 gn_args_list.extend(extra_gn_switches)
198 _RunGN(gn_args_list)
sakala4a75382017-01-24 09:25:50199
Christoffer Dewerin81a91172024-04-08 07:26:41200 ninja_args = TARGETS[:]
Jeremy Leconte0b496cc2024-05-16 08:14:06201 if use_remoteexec:
Junji Watanabe5d165b12025-05-15 05:34:41202 ninja_args.extend(['-remote_jobs', '200'])
Christoffer Dewerin81a91172024-04-08 07:26:41203 ninja_args.extend(extra_ninja_switches)
204 _RunNinja(output_directory, ninja_args)
sakala4a75382017-01-24 09:25:50205
206
korniltsev.anatoly0b510a92017-09-05 15:12:30207def CollectCommon(aar_file, build_dir, arch):
Christoffer Dewerin81a91172024-04-08 07:26:41208 """Collects architecture independent files into the .aar-archive."""
209 logging.info('Collecting common files.')
210 output_directory = _GetOutputDirectory(build_dir, arch)
211 aar_file.write(MANIFEST_FILE, 'AndroidManifest.xml')
212 aar_file.write(os.path.join(output_directory, JAR_FILE), 'classes.jar')
sakala4a75382017-01-24 09:25:50213
214
Yura Yaroshevichb0a57d82022-08-03 09:44:04215def Collect(aar_file, build_dir, arch, unstripped):
Christoffer Dewerin81a91172024-04-08 07:26:41216 """Collects architecture specific files into the .aar-archive."""
217 logging.info('Collecting: %s', arch)
218 output_directory = _GetOutputDirectory(build_dir, arch)
sakala4a75382017-01-24 09:25:50219
Christoffer Dewerin81a91172024-04-08 07:26:41220 abi_dir = os.path.join('jni', arch)
221 for so_file in NEEDED_SO_FILES:
222 source_so_file = os.path.join("lib.unstripped",
223 so_file) if unstripped else so_file
224 aar_file.write(os.path.join(output_directory, source_so_file),
225 os.path.join(abi_dir, so_file))
sakala4a75382017-01-24 09:25:50226
227
korniltsev.anatoly0b510a92017-09-05 15:12:30228def GenerateLicenses(output_dir, build_dir, archs):
Christoffer Dewerin81a91172024-04-08 07:26:41229 builder = LicenseBuilder(
230 [_GetOutputDirectory(build_dir, arch) for arch in archs], TARGETS)
231 builder.generate_license_text(output_dir)
sakal67e414c2017-09-05 07:16:15232
233
Junji Watanabe41150c52025-06-25 13:16:45234def BuildAar(build_dir,
235 archs,
Mirko Bonadei8cc66952020-10-30 09:13:45236 output_file,
Junji Watanabed6c7ee72022-08-31 06:40:26237 use_remoteexec=False,
Mirko Bonadei8cc66952020-10-30 09:13:45238 extra_gn_args=None,
Mirko Bonadei8cc66952020-10-30 09:13:45239 extra_gn_switches=None,
Yura Yaroshevichb0a57d82022-08-03 09:44:04240 extra_ninja_switches=None,
241 unstripped=False):
Christoffer Dewerin81a91172024-04-08 07:26:41242 extra_gn_args = extra_gn_args or []
243 extra_gn_switches = extra_gn_switches or []
244 extra_ninja_switches = extra_ninja_switches or []
Sami Kalliomäkidbb15a72017-10-05 14:15:02245
Sami Kalliomäkidbb15a72017-10-05 14:15:02246 for arch in archs:
Jeremy Leconte0b496cc2024-05-16 08:14:06247 Build(build_dir, arch, use_remoteexec, extra_gn_args,
Christoffer Dewerin81a91172024-04-08 07:26:41248 extra_gn_switches, extra_ninja_switches)
Sami Kalliomäkidbb15a72017-10-05 14:15:02249
Christoffer Dewerin81a91172024-04-08 07:26:41250 with zipfile.ZipFile(output_file, 'w') as aar_file:
251 # Architecture doesn't matter here, arbitrarily using the first one.
252 CollectCommon(aar_file, build_dir, archs[0])
253 for arch in archs:
254 Collect(aar_file, build_dir, arch, unstripped)
Sami Kalliomäkidbb15a72017-10-05 14:15:02255
Christoffer Dewerin81a91172024-04-08 07:26:41256 license_dir = os.path.dirname(os.path.realpath(output_file))
257 GenerateLicenses(license_dir, build_dir, archs)
258
Sami Kalliomäkidbb15a72017-10-05 14:15:02259
sakala4a75382017-01-24 09:25:50260def main():
Christoffer Dewerin81a91172024-04-08 07:26:41261 args = _ParseArgs()
262 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
sakala4a75382017-01-24 09:25:50263
Junji Watanabe41150c52025-06-25 13:16:45264 if not pathlib.Path(args.build_dir).is_relative_to(SRC_DIR):
265 logging.error('--build-dir must be under %s.', SRC_DIR)
266 return 1
267
268 return BuildAar(args.build_dir, args.arch, args.output,
269 args.use_remoteexec, args.extra_gn_args,
270 args.extra_gn_switches, args.extra_ninja_switches,
271 args.use_unstripped_libs)
sakala4a75382017-01-24 09:25:50272
273
274if __name__ == '__main__':
Christoffer Dewerin81a91172024-04-08 07:26:41275 sys.exit(main())