blob: 42a902cafd31e480f35cfb837cff2dd21a8c558e [file] [log] [blame]
Christoffer Jansson4e8a7732022-02-08 09:01:12 +01001#!/usr/bin/env vpython3
sakala4a75382017-01-24 01:25:50 -08002
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 01:25:50 -080010"""Script to generate libwebrtc.aar for distribution.
11
12The script has to be run from the root src folder.
Henrik Kjellander90fd7d82017-05-09 08:30:10 +020013./tools_webrtc/android/build_aar.py
sakala4a75382017-01-24 01:25:50 -080014
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
29import shutil
30import subprocess
31import sys
32import tempfile
33import zipfile
34
sakal67e414c2017-09-05 00:16:15 -070035SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
Henrik Kjellanderec57e052017-10-17 21:36:01 +020036SRC_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
sakal423f1062017-04-07 05:10:15 -070037DEFAULT_ARCHS = ['armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64']
sakala4a75382017-01-24 01:25:50 -080038NEEDED_SO_FILES = ['libjingle_peerconnection_so.so']
Henrik Kjellander03ec4f82017-09-27 16:07:40 +020039JAR_FILE = 'lib.java/sdk/android/libwebrtc.jar'
40MANIFEST_FILE = 'sdk/android/AndroidManifest.xml'
sakala4a75382017-01-24 01:25:50 -080041TARGETS = [
Mirko Bonadei8cc66952020-10-30 10:13:45 +010042 'sdk/android:libwebrtc',
43 'sdk/android:libjingle_peerconnection_so',
sakala4a75382017-01-24 01:25:50 -080044]
45
sakal67e414c2017-09-05 00:16:15 -070046sys.path.append(os.path.join(SCRIPT_DIR, '..', 'libs'))
47from generate_licenses import LicenseBuilder
48
Henrik Kjellanderec57e052017-10-17 21:36:01 +020049sys.path.append(os.path.join(SRC_DIR, 'build'))
50import find_depot_tools
51
52
sakala4a75382017-01-24 01:25:50 -080053def _ParseArgs():
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010054 parser = argparse.ArgumentParser(description='libwebrtc.aar generator.')
55 parser.add_argument(
56 '--build-dir',
57 type=os.path.abspath,
58 help='Build dir. By default will create and use temporary dir.')
59 parser.add_argument('--output',
60 default='libwebrtc.aar',
61 type=os.path.abspath,
62 help='Output file of the script.')
63 parser.add_argument('--arch',
64 default=DEFAULT_ARCHS,
65 nargs='*',
66 help='Architectures to build. Defaults to %(default)s.')
67 parser.add_argument('--use-goma',
68 action='store_true',
69 default=False,
70 help='Use goma.')
Junji Watanabe1a7dd712022-08-31 12:59:21 +090071 parser.add_argument('--use-remoteexec',
72 action='store_true',
73 default=False,
74 help='Use RBE.')
Yura Yaroshevichb0a57d82022-08-03 11:44:04 +020075 parser.add_argument('--use-unstripped-libs',
76 action='store_true',
77 default=False,
78 help='Use unstripped .so files within libwebrtc.aar')
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010079 parser.add_argument('--verbose',
80 action='store_true',
81 default=False,
82 help='Debug logging.')
83 parser.add_argument(
84 '--extra-gn-args',
85 default=[],
86 nargs='*',
87 help="""Additional GN arguments to be used during Ninja generation.
Yura Yaroshevichf517f112018-05-24 16:48:02 +030088 These are passed to gn inside `--args` switch and
89 applied after any other arguments and will
90 override any values defined by the script.
91 Example of building debug aar file:
92 build_aar.py --extra-gn-args='is_debug=true'""")
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010093 parser.add_argument(
94 '--extra-ninja-switches',
95 default=[],
96 nargs='*',
97 help="""Additional Ninja switches to be used during compilation.
Yura Yaroshevichf517f112018-05-24 16:48:02 +030098 These are applied after any other Ninja switches.
99 Example of enabling verbose Ninja output:
100 build_aar.py --extra-ninja-switches='-v'""")
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100101 parser.add_argument(
102 '--extra-gn-switches',
103 default=[],
104 nargs='*',
105 help="""Additional GN switches to be used during compilation.
Yura Yaroshevichf517f112018-05-24 16:48:02 +0300106 These are applied after any other GN switches.
107 Example of enabling verbose GN output:
108 build_aar.py --extra-gn-switches='-v'""")
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100109 return parser.parse_args()
sakala4a75382017-01-24 01:25:50 -0800110
111
112def _RunGN(args):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100113 cmd = [
114 sys.executable,
115 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py')
116 ]
117 cmd.extend(args)
118 logging.debug('Running: %r', cmd)
119 subprocess.check_call(cmd)
sakala4a75382017-01-24 01:25:50 -0800120
121
122def _RunNinja(output_directory, args):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100123 cmd = [
124 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'ninja'), '-C',
125 output_directory
126 ]
127 cmd.extend(args)
128 logging.debug('Running: %r', cmd)
129 subprocess.check_call(cmd)
sakala4a75382017-01-24 01:25:50 -0800130
131
132def _EncodeForGN(value):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100133 """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 01:25:50 -0800139
140
korniltsev.anatoly0b510a92017-09-05 08:12:30 -0700141def _GetOutputDirectory(build_dir, arch):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100142 """Returns the GN output directory for the target architecture."""
143 return os.path.join(build_dir, arch)
sakala4a75382017-01-24 01:25:50 -0800144
145
146def _GetTargetCpu(arch):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100147 """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 01:25:50 -0800157
158
159def _GetArmVersion(arch):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100160 """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 01:25:50 -0800168
169
Junji Watanabed6c7ee72022-08-31 15:40:26 +0900170def Build(build_dir, arch, use_goma, use_remoteexec, extra_gn_args,
Junji Watanabe1a7dd712022-08-31 12:59:21 +0900171 extra_gn_switches, extra_ninja_switches):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100172 """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),
Junji Watanabe1a7dd712022-08-31 12:59:21 +0900181 'use_goma': use_goma,
Junji Watanabed6c7ee72022-08-31 15:40:26 +0900182 'use_remoteexec': use_remoteexec,
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100183 }
184 arm_version = _GetArmVersion(arch)
185 if arm_version:
186 gn_args['arm_version'] = arm_version
187 gn_args_str = '--args=' + ' '.join(
188 [k + '=' + _EncodeForGN(v) for k, v in gn_args.items()] + extra_gn_args)
sakala4a75382017-01-24 01:25:50 -0800189
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100190 gn_args_list = ['gen', output_directory, gn_args_str]
191 gn_args_list.extend(extra_gn_switches)
192 _RunGN(gn_args_list)
sakala4a75382017-01-24 01:25:50 -0800193
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100194 ninja_args = TARGETS[:]
Junji Watanabed6c7ee72022-08-31 15:40:26 +0900195 if use_goma or use_remoteexec:
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100196 ninja_args.extend(['-j', '200'])
197 ninja_args.extend(extra_ninja_switches)
198 _RunNinja(output_directory, ninja_args)
sakala4a75382017-01-24 01:25:50 -0800199
200
korniltsev.anatoly0b510a92017-09-05 08:12:30 -0700201def CollectCommon(aar_file, build_dir, arch):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100202 """Collects architecture independent files into the .aar-archive."""
203 logging.info('Collecting common files.')
204 output_directory = _GetOutputDirectory(build_dir, arch)
205 aar_file.write(MANIFEST_FILE, 'AndroidManifest.xml')
206 aar_file.write(os.path.join(output_directory, JAR_FILE), 'classes.jar')
sakala4a75382017-01-24 01:25:50 -0800207
208
Yura Yaroshevichb0a57d82022-08-03 11:44:04 +0200209def Collect(aar_file, build_dir, arch, unstripped):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100210 """Collects architecture specific files into the .aar-archive."""
211 logging.info('Collecting: %s', arch)
212 output_directory = _GetOutputDirectory(build_dir, arch)
sakala4a75382017-01-24 01:25:50 -0800213
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100214 abi_dir = os.path.join('jni', arch)
215 for so_file in NEEDED_SO_FILES:
Yura Yaroshevichb0a57d82022-08-03 11:44:04 +0200216 source_so_file = os.path.join("lib.unstripped",
217 so_file) if unstripped else so_file
218 aar_file.write(os.path.join(output_directory, source_so_file),
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100219 os.path.join(abi_dir, so_file))
sakala4a75382017-01-24 01:25:50 -0800220
221
korniltsev.anatoly0b510a92017-09-05 08:12:30 -0700222def GenerateLicenses(output_dir, build_dir, archs):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100223 builder = LicenseBuilder(
224 [_GetOutputDirectory(build_dir, arch) for arch in archs], TARGETS)
225 builder.GenerateLicenseText(output_dir)
sakal67e414c2017-09-05 00:16:15 -0700226
227
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100228def BuildAar(archs,
229 output_file,
230 use_goma=False,
Junji Watanabed6c7ee72022-08-31 15:40:26 +0900231 use_remoteexec=False,
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100232 extra_gn_args=None,
233 ext_build_dir=None,
234 extra_gn_switches=None,
Yura Yaroshevichb0a57d82022-08-03 11:44:04 +0200235 extra_ninja_switches=None,
236 unstripped=False):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100237 extra_gn_args = extra_gn_args or []
238 extra_gn_switches = extra_gn_switches or []
239 extra_ninja_switches = extra_ninja_switches or []
240 build_dir = ext_build_dir if ext_build_dir else tempfile.mkdtemp()
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200241
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100242 for arch in archs:
Junji Watanabed6c7ee72022-08-31 15:40:26 +0900243 Build(build_dir, arch, use_goma, use_remoteexec, extra_gn_args,
Junji Watanabe1a7dd712022-08-31 12:59:21 +0900244 extra_gn_switches, extra_ninja_switches)
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100245
246 with zipfile.ZipFile(output_file, 'w') as aar_file:
247 # Architecture doesn't matter here, arbitrarily using the first one.
248 CollectCommon(aar_file, build_dir, archs[0])
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200249 for arch in archs:
Yura Yaroshevichb0a57d82022-08-03 11:44:04 +0200250 Collect(aar_file, build_dir, arch, unstripped)
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200251
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100252 license_dir = os.path.dirname(os.path.realpath(output_file))
253 GenerateLicenses(license_dir, build_dir, archs)
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200254
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100255 if not ext_build_dir:
256 shutil.rmtree(build_dir, True)
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200257
258
sakala4a75382017-01-24 01:25:50 -0800259def main():
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100260 args = _ParseArgs()
261 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
sakala4a75382017-01-24 01:25:50 -0800262
Junji Watanabed6c7ee72022-08-31 15:40:26 +0900263 BuildAar(args.arch, args.output, args.use_goma, args.use_remoteexec,
Junji Watanabe1a7dd712022-08-31 12:59:21 +0900264 args.extra_gn_args, args.build_dir, args.extra_gn_switches,
265 args.extra_ninja_switches, args.use_unstripped_libs)
sakala4a75382017-01-24 01:25:50 -0800266
267
268if __name__ == '__main__':
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100269 sys.exit(main())