Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 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. |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 10 | """Script to generate libwebrtc.aar for distribution. |
| 11 | |
| 12 | The script has to be run from the root src folder. |
Henrik Kjellander | 90fd7d8 | 2017-05-09 08:30:10 +0200 | [diff] [blame] | 13 | ./tools_webrtc/android/build_aar.py |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 14 | |
| 15 | .aar-file is just a zip-archive containing the files of the library. The file |
| 16 | structure 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 | |
| 26 | import argparse |
| 27 | import logging |
| 28 | import os |
| 29 | import shutil |
| 30 | import subprocess |
| 31 | import sys |
| 32 | import tempfile |
| 33 | import zipfile |
| 34 | |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 35 | SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0])) |
Henrik Kjellander | ec57e05 | 2017-10-17 21:36:01 +0200 | [diff] [blame] | 36 | SRC_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir)) |
sakal | 423f106 | 2017-04-07 05:10:15 -0700 | [diff] [blame] | 37 | DEFAULT_ARCHS = ['armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'] |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 38 | NEEDED_SO_FILES = ['libjingle_peerconnection_so.so'] |
Henrik Kjellander | 03ec4f8 | 2017-09-27 16:07:40 +0200 | [diff] [blame] | 39 | JAR_FILE = 'lib.java/sdk/android/libwebrtc.jar' |
| 40 | MANIFEST_FILE = 'sdk/android/AndroidManifest.xml' |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 41 | TARGETS = [ |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 42 | 'sdk/android:libwebrtc', |
| 43 | 'sdk/android:libjingle_peerconnection_so', |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 44 | ] |
| 45 | |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 46 | sys.path.append(os.path.join(SCRIPT_DIR, '..', 'libs')) |
| 47 | from generate_licenses import LicenseBuilder |
| 48 | |
Henrik Kjellander | ec57e05 | 2017-10-17 21:36:01 +0200 | [diff] [blame] | 49 | sys.path.append(os.path.join(SRC_DIR, 'build')) |
| 50 | import find_depot_tools |
| 51 | |
| 52 | |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 53 | def _ParseArgs(): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 54 | 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.') |
Yura Yaroshevich | b0a57d8 | 2022-08-03 11:44:04 +0200 | [diff] [blame^] | 71 | parser.add_argument('--use-unstripped-libs', |
| 72 | action='store_true', |
| 73 | default=False, |
| 74 | help='Use unstripped .so files within libwebrtc.aar') |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 75 | parser.add_argument('--verbose', |
| 76 | action='store_true', |
| 77 | default=False, |
| 78 | help='Debug logging.') |
| 79 | parser.add_argument( |
| 80 | '--extra-gn-args', |
| 81 | default=[], |
| 82 | nargs='*', |
| 83 | help="""Additional GN arguments to be used during Ninja generation. |
Yura Yaroshevich | f517f11 | 2018-05-24 16:48:02 +0300 | [diff] [blame] | 84 | These are passed to gn inside `--args` switch and |
| 85 | applied after any other arguments and will |
| 86 | override any values defined by the script. |
| 87 | Example of building debug aar file: |
| 88 | build_aar.py --extra-gn-args='is_debug=true'""") |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 89 | parser.add_argument( |
| 90 | '--extra-ninja-switches', |
| 91 | default=[], |
| 92 | nargs='*', |
| 93 | help="""Additional Ninja switches to be used during compilation. |
Yura Yaroshevich | f517f11 | 2018-05-24 16:48:02 +0300 | [diff] [blame] | 94 | These are applied after any other Ninja switches. |
| 95 | Example of enabling verbose Ninja output: |
| 96 | build_aar.py --extra-ninja-switches='-v'""") |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 97 | parser.add_argument( |
| 98 | '--extra-gn-switches', |
| 99 | default=[], |
| 100 | nargs='*', |
| 101 | help="""Additional GN switches to be used during compilation. |
Yura Yaroshevich | f517f11 | 2018-05-24 16:48:02 +0300 | [diff] [blame] | 102 | These are applied after any other GN switches. |
| 103 | Example of enabling verbose GN output: |
| 104 | build_aar.py --extra-gn-switches='-v'""") |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 105 | return parser.parse_args() |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 106 | |
| 107 | |
| 108 | def _RunGN(args): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 109 | cmd = [ |
| 110 | sys.executable, |
| 111 | os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py') |
| 112 | ] |
| 113 | cmd.extend(args) |
| 114 | logging.debug('Running: %r', cmd) |
| 115 | subprocess.check_call(cmd) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 116 | |
| 117 | |
| 118 | def _RunNinja(output_directory, args): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 119 | cmd = [ |
| 120 | os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'ninja'), '-C', |
| 121 | output_directory |
| 122 | ] |
| 123 | cmd.extend(args) |
| 124 | logging.debug('Running: %r', cmd) |
| 125 | subprocess.check_call(cmd) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 126 | |
| 127 | |
| 128 | def _EncodeForGN(value): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 129 | """Encodes value as a GN literal.""" |
| 130 | if isinstance(value, str): |
| 131 | return '"' + value + '"' |
| 132 | if isinstance(value, bool): |
| 133 | return repr(value).lower() |
| 134 | return repr(value) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 135 | |
| 136 | |
korniltsev.anatoly | 0b510a9 | 2017-09-05 08:12:30 -0700 | [diff] [blame] | 137 | def _GetOutputDirectory(build_dir, arch): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 138 | """Returns the GN output directory for the target architecture.""" |
| 139 | return os.path.join(build_dir, arch) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 140 | |
| 141 | |
| 142 | def _GetTargetCpu(arch): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 143 | """Returns target_cpu for the GN build with the given architecture.""" |
| 144 | if arch in ['armeabi', 'armeabi-v7a']: |
| 145 | return 'arm' |
| 146 | if arch == 'arm64-v8a': |
| 147 | return 'arm64' |
| 148 | if arch == 'x86': |
| 149 | return 'x86' |
| 150 | if arch == 'x86_64': |
| 151 | return 'x64' |
| 152 | raise Exception('Unknown arch: ' + arch) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 153 | |
| 154 | |
| 155 | def _GetArmVersion(arch): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 156 | """Returns arm_version for the GN build with the given architecture.""" |
| 157 | if arch == 'armeabi': |
| 158 | return 6 |
| 159 | if arch == 'armeabi-v7a': |
| 160 | return 7 |
| 161 | if arch in ['arm64-v8a', 'x86', 'x86_64']: |
| 162 | return None |
| 163 | raise Exception('Unknown arch: ' + arch) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 164 | |
| 165 | |
Yura Yaroshevich | f517f11 | 2018-05-24 16:48:02 +0300 | [diff] [blame] | 166 | def Build(build_dir, arch, use_goma, extra_gn_args, extra_gn_switches, |
| 167 | extra_ninja_switches): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 168 | """Generates target architecture using GN and builds it using ninja.""" |
| 169 | logging.info('Building: %s', arch) |
| 170 | output_directory = _GetOutputDirectory(build_dir, arch) |
| 171 | gn_args = { |
| 172 | 'target_os': 'android', |
| 173 | 'is_debug': False, |
| 174 | 'is_component_build': False, |
| 175 | 'rtc_include_tests': False, |
| 176 | 'target_cpu': _GetTargetCpu(arch), |
| 177 | 'use_goma': use_goma |
| 178 | } |
| 179 | arm_version = _GetArmVersion(arch) |
| 180 | if arm_version: |
| 181 | gn_args['arm_version'] = arm_version |
| 182 | gn_args_str = '--args=' + ' '.join( |
| 183 | [k + '=' + _EncodeForGN(v) for k, v in gn_args.items()] + extra_gn_args) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 184 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 185 | gn_args_list = ['gen', output_directory, gn_args_str] |
| 186 | gn_args_list.extend(extra_gn_switches) |
| 187 | _RunGN(gn_args_list) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 188 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 189 | ninja_args = TARGETS[:] |
| 190 | if use_goma: |
| 191 | ninja_args.extend(['-j', '200']) |
| 192 | ninja_args.extend(extra_ninja_switches) |
| 193 | _RunNinja(output_directory, ninja_args) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 194 | |
| 195 | |
korniltsev.anatoly | 0b510a9 | 2017-09-05 08:12:30 -0700 | [diff] [blame] | 196 | def CollectCommon(aar_file, build_dir, arch): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 197 | """Collects architecture independent files into the .aar-archive.""" |
| 198 | logging.info('Collecting common files.') |
| 199 | output_directory = _GetOutputDirectory(build_dir, arch) |
| 200 | aar_file.write(MANIFEST_FILE, 'AndroidManifest.xml') |
| 201 | aar_file.write(os.path.join(output_directory, JAR_FILE), 'classes.jar') |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 202 | |
| 203 | |
Yura Yaroshevich | b0a57d8 | 2022-08-03 11:44:04 +0200 | [diff] [blame^] | 204 | def Collect(aar_file, build_dir, arch, unstripped): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 205 | """Collects architecture specific files into the .aar-archive.""" |
| 206 | logging.info('Collecting: %s', arch) |
| 207 | output_directory = _GetOutputDirectory(build_dir, arch) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 208 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 209 | abi_dir = os.path.join('jni', arch) |
| 210 | for so_file in NEEDED_SO_FILES: |
Yura Yaroshevich | b0a57d8 | 2022-08-03 11:44:04 +0200 | [diff] [blame^] | 211 | source_so_file = os.path.join("lib.unstripped", |
| 212 | so_file) if unstripped else so_file |
| 213 | aar_file.write(os.path.join(output_directory, source_so_file), |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 214 | os.path.join(abi_dir, so_file)) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 215 | |
| 216 | |
korniltsev.anatoly | 0b510a9 | 2017-09-05 08:12:30 -0700 | [diff] [blame] | 217 | def GenerateLicenses(output_dir, build_dir, archs): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 218 | builder = LicenseBuilder( |
| 219 | [_GetOutputDirectory(build_dir, arch) for arch in archs], TARGETS) |
| 220 | builder.GenerateLicenseText(output_dir) |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 221 | |
| 222 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 223 | def BuildAar(archs, |
| 224 | output_file, |
| 225 | use_goma=False, |
| 226 | extra_gn_args=None, |
| 227 | ext_build_dir=None, |
| 228 | extra_gn_switches=None, |
Yura Yaroshevich | b0a57d8 | 2022-08-03 11:44:04 +0200 | [diff] [blame^] | 229 | extra_ninja_switches=None, |
| 230 | unstripped=False): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 231 | extra_gn_args = extra_gn_args or [] |
| 232 | extra_gn_switches = extra_gn_switches or [] |
| 233 | extra_ninja_switches = extra_ninja_switches or [] |
| 234 | build_dir = ext_build_dir if ext_build_dir else tempfile.mkdtemp() |
Sami Kalliomäki | dbb15a7 | 2017-10-05 16:15:02 +0200 | [diff] [blame] | 235 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 236 | for arch in archs: |
| 237 | Build(build_dir, arch, use_goma, extra_gn_args, extra_gn_switches, |
| 238 | extra_ninja_switches) |
| 239 | |
| 240 | with zipfile.ZipFile(output_file, 'w') as aar_file: |
| 241 | # Architecture doesn't matter here, arbitrarily using the first one. |
| 242 | CollectCommon(aar_file, build_dir, archs[0]) |
Sami Kalliomäki | dbb15a7 | 2017-10-05 16:15:02 +0200 | [diff] [blame] | 243 | for arch in archs: |
Yura Yaroshevich | b0a57d8 | 2022-08-03 11:44:04 +0200 | [diff] [blame^] | 244 | Collect(aar_file, build_dir, arch, unstripped) |
Sami Kalliomäki | dbb15a7 | 2017-10-05 16:15:02 +0200 | [diff] [blame] | 245 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 246 | license_dir = os.path.dirname(os.path.realpath(output_file)) |
| 247 | GenerateLicenses(license_dir, build_dir, archs) |
Sami Kalliomäki | dbb15a7 | 2017-10-05 16:15:02 +0200 | [diff] [blame] | 248 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 249 | if not ext_build_dir: |
| 250 | shutil.rmtree(build_dir, True) |
Sami Kalliomäki | dbb15a7 | 2017-10-05 16:15:02 +0200 | [diff] [blame] | 251 | |
| 252 | |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 253 | def main(): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 254 | args = _ParseArgs() |
| 255 | logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 256 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 257 | BuildAar(args.arch, args.output, args.use_goma, args.extra_gn_args, |
Yura Yaroshevich | b0a57d8 | 2022-08-03 11:44:04 +0200 | [diff] [blame^] | 258 | args.build_dir, args.extra_gn_switches, args.extra_ninja_switches, |
| 259 | args.use_unstripped_libs) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 260 | |
| 261 | |
| 262 | if __name__ == '__main__': |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame] | 263 | sys.exit(main()) |