sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 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. |
Henrik Kjellander | 90fd7d8 | 2017-05-09 08:30:10 +0200 | [diff] [blame] | 14 | ./tools_webrtc/android/build_aar.py |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 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 | - armeabi-v7a/ |
| 22 | - libjingle_peerconnection_so.so |
| 23 | - x86/ |
| 24 | - libjingle_peerconnection_so.so |
| 25 | """ |
| 26 | |
| 27 | import argparse |
| 28 | import logging |
| 29 | import os |
| 30 | import shutil |
| 31 | import subprocess |
| 32 | import sys |
| 33 | import tempfile |
| 34 | import zipfile |
| 35 | |
| 36 | |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 37 | SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0])) |
Henrik Kjellander | ec57e05 | 2017-10-17 21:36:01 +0200 | [diff] [blame] | 38 | 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] | 39 | DEFAULT_ARCHS = ['armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'] |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 40 | NEEDED_SO_FILES = ['libjingle_peerconnection_so.so'] |
Henrik Kjellander | 03ec4f8 | 2017-09-27 16:07:40 +0200 | [diff] [blame] | 41 | JAR_FILE = 'lib.java/sdk/android/libwebrtc.jar' |
| 42 | MANIFEST_FILE = 'sdk/android/AndroidManifest.xml' |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 43 | TARGETS = [ |
Henrik Kjellander | 03ec4f8 | 2017-09-27 16:07:40 +0200 | [diff] [blame] | 44 | 'sdk/android:libwebrtc', |
| 45 | 'sdk/android:libjingle_peerconnection_so', |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 46 | ] |
| 47 | |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 48 | sys.path.append(os.path.join(SCRIPT_DIR, '..', 'libs')) |
| 49 | from generate_licenses import LicenseBuilder |
| 50 | |
Henrik Kjellander | ec57e05 | 2017-10-17 21:36:01 +0200 | [diff] [blame] | 51 | sys.path.append(os.path.join(SRC_DIR, 'build')) |
| 52 | import find_depot_tools |
| 53 | |
| 54 | |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 55 | |
| 56 | def _ParseArgs(): |
| 57 | parser = argparse.ArgumentParser(description='libwebrtc.aar generator.') |
korniltsev.anatoly | 0b510a9 | 2017-09-05 08:12:30 -0700 | [diff] [blame] | 58 | parser.add_argument('--build-dir', |
| 59 | help='Build dir. By default will create and use temporary dir.') |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 60 | parser.add_argument('--output', default='libwebrtc.aar', |
| 61 | help='Output file of the script.') |
kjellander | 6b3fcfd | 2017-02-07 01:11:06 -0800 | [diff] [blame] | 62 | parser.add_argument('--arch', default=DEFAULT_ARCHS, nargs='*', |
| 63 | help='Architectures to build. Defaults to %(default)s.') |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 64 | parser.add_argument('--use-goma', action='store_true', default=False, |
| 65 | help='Use goma.') |
| 66 | parser.add_argument('--verbose', action='store_true', default=False, |
| 67 | help='Debug logging.') |
kjellander | 6b3fcfd | 2017-02-07 01:11:06 -0800 | [diff] [blame] | 68 | parser.add_argument('--extra-gn-args', default=[], nargs='*', |
Yura Yaroshevich | be7b88c | 2018-03-13 13:12:56 +0300 | [diff] [blame] | 69 | help="""Additional GN arguments to be used during Ninja generation. |
| 70 | These are applied after any other arguments and will |
| 71 | override any values defined by the script.""") |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 72 | return parser.parse_args() |
| 73 | |
| 74 | |
| 75 | def _RunGN(args): |
Henrik Kjellander | ec57e05 | 2017-10-17 21:36:01 +0200 | [diff] [blame] | 76 | cmd = [sys.executable, |
| 77 | os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py')] |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 78 | cmd.extend(args) |
| 79 | logging.debug('Running: %r', cmd) |
| 80 | subprocess.check_call(cmd) |
| 81 | |
| 82 | |
| 83 | def _RunNinja(output_directory, args): |
Henrik Kjellander | ec57e05 | 2017-10-17 21:36:01 +0200 | [diff] [blame] | 84 | cmd = [os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'ninja'), |
| 85 | '-C', output_directory] |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 86 | cmd.extend(args) |
| 87 | logging.debug('Running: %r', cmd) |
| 88 | subprocess.check_call(cmd) |
| 89 | |
| 90 | |
| 91 | def _EncodeForGN(value): |
| 92 | """Encodes value as a GN literal.""" |
| 93 | if type(value) is str: |
| 94 | return '"' + value + '"' |
| 95 | elif type(value) is bool: |
| 96 | return repr(value).lower() |
| 97 | else: |
| 98 | return repr(value) |
| 99 | |
| 100 | |
korniltsev.anatoly | 0b510a9 | 2017-09-05 08:12:30 -0700 | [diff] [blame] | 101 | def _GetOutputDirectory(build_dir, arch): |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 102 | """Returns the GN output directory for the target architecture.""" |
korniltsev.anatoly | 0b510a9 | 2017-09-05 08:12:30 -0700 | [diff] [blame] | 103 | return os.path.join(build_dir, arch) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 104 | |
| 105 | |
| 106 | def _GetTargetCpu(arch): |
| 107 | """Returns target_cpu for the GN build with the given architecture.""" |
| 108 | if arch in ['armeabi', 'armeabi-v7a']: |
| 109 | return 'arm' |
sakal | 423f106 | 2017-04-07 05:10:15 -0700 | [diff] [blame] | 110 | elif arch == 'arm64-v8a': |
| 111 | return 'arm64' |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 112 | elif arch == 'x86': |
| 113 | return 'x86' |
sakal | 423f106 | 2017-04-07 05:10:15 -0700 | [diff] [blame] | 114 | elif arch == 'x86_64': |
| 115 | return 'x64' |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 116 | else: |
| 117 | raise Exception('Unknown arch: ' + arch) |
| 118 | |
| 119 | |
| 120 | def _GetArmVersion(arch): |
| 121 | """Returns arm_version for the GN build with the given architecture.""" |
| 122 | if arch == 'armeabi': |
| 123 | return 6 |
| 124 | elif arch == 'armeabi-v7a': |
| 125 | return 7 |
sakal | 423f106 | 2017-04-07 05:10:15 -0700 | [diff] [blame] | 126 | elif arch in ['arm64-v8a', 'x86', 'x86_64']: |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 127 | return None |
| 128 | else: |
| 129 | raise Exception('Unknown arch: ' + arch) |
| 130 | |
| 131 | |
korniltsev.anatoly | 0b510a9 | 2017-09-05 08:12:30 -0700 | [diff] [blame] | 132 | def Build(build_dir, arch, use_goma, extra_gn_args): |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 133 | """Generates target architecture using GN and builds it using ninja.""" |
| 134 | logging.info('Building: %s', arch) |
korniltsev.anatoly | 0b510a9 | 2017-09-05 08:12:30 -0700 | [diff] [blame] | 135 | output_directory = _GetOutputDirectory(build_dir, arch) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 136 | gn_args = { |
| 137 | 'target_os': 'android', |
| 138 | 'is_debug': False, |
| 139 | 'is_component_build': False, |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 140 | 'rtc_include_tests': False, |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 141 | 'target_cpu': _GetTargetCpu(arch), |
| 142 | 'use_goma': use_goma |
| 143 | } |
| 144 | arm_version = _GetArmVersion(arch) |
| 145 | if arm_version: |
| 146 | gn_args['arm_version'] = arm_version |
| 147 | gn_args_str = '--args=' + ' '.join([ |
kjellander | 6b3fcfd | 2017-02-07 01:11:06 -0800 | [diff] [blame] | 148 | k + '=' + _EncodeForGN(v) for k, v in gn_args.items()] + extra_gn_args) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 149 | |
| 150 | _RunGN(['gen', output_directory, gn_args_str]) |
| 151 | |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 152 | ninja_args = TARGETS[:] |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 153 | if use_goma: |
sakal | a53d4e7 | 2017-02-07 06:19:20 -0800 | [diff] [blame] | 154 | ninja_args.extend(['-j', '200']) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 155 | _RunNinja(output_directory, ninja_args) |
| 156 | |
| 157 | |
korniltsev.anatoly | 0b510a9 | 2017-09-05 08:12:30 -0700 | [diff] [blame] | 158 | def CollectCommon(aar_file, build_dir, arch): |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 159 | """Collects architecture independent files into the .aar-archive.""" |
| 160 | logging.info('Collecting common files.') |
korniltsev.anatoly | 0b510a9 | 2017-09-05 08:12:30 -0700 | [diff] [blame] | 161 | output_directory = _GetOutputDirectory(build_dir, arch) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 162 | aar_file.write(MANIFEST_FILE, 'AndroidManifest.xml') |
| 163 | aar_file.write(os.path.join(output_directory, JAR_FILE), 'classes.jar') |
| 164 | |
| 165 | |
korniltsev.anatoly | 0b510a9 | 2017-09-05 08:12:30 -0700 | [diff] [blame] | 166 | def Collect(aar_file, build_dir, arch): |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 167 | """Collects architecture specific files into the .aar-archive.""" |
| 168 | logging.info('Collecting: %s', arch) |
korniltsev.anatoly | 0b510a9 | 2017-09-05 08:12:30 -0700 | [diff] [blame] | 169 | output_directory = _GetOutputDirectory(build_dir, arch) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 170 | |
| 171 | abi_dir = os.path.join('jni', arch) |
| 172 | for so_file in NEEDED_SO_FILES: |
| 173 | aar_file.write(os.path.join(output_directory, so_file), |
| 174 | os.path.join(abi_dir, so_file)) |
| 175 | |
| 176 | |
korniltsev.anatoly | 0b510a9 | 2017-09-05 08:12:30 -0700 | [diff] [blame] | 177 | def GenerateLicenses(output_dir, build_dir, archs): |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 178 | builder = LicenseBuilder( |
korniltsev.anatoly | 0b510a9 | 2017-09-05 08:12:30 -0700 | [diff] [blame] | 179 | [_GetOutputDirectory(build_dir, arch) for arch in archs], TARGETS) |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 180 | builder.GenerateLicenseText(output_dir) |
| 181 | |
| 182 | |
Sami Kalliomäki | dbb15a7 | 2017-10-05 16:15:02 +0200 | [diff] [blame] | 183 | def BuildAar(archs, output_file, use_goma=False, extra_gn_args=None, |
| 184 | ext_build_dir=None): |
| 185 | extra_gn_args = extra_gn_args or [] |
| 186 | build_dir = ext_build_dir if ext_build_dir else tempfile.mkdtemp() |
| 187 | |
| 188 | for arch in archs: |
| 189 | Build(build_dir, arch, use_goma, extra_gn_args) |
| 190 | |
| 191 | with zipfile.ZipFile(output_file, 'w') as aar_file: |
| 192 | # Architecture doesn't matter here, arbitrarily using the first one. |
| 193 | CollectCommon(aar_file, build_dir, archs[0]) |
| 194 | for arch in archs: |
| 195 | Collect(aar_file, build_dir, arch) |
| 196 | |
| 197 | license_dir = os.path.dirname(os.path.realpath(output_file)) |
| 198 | GenerateLicenses(license_dir, build_dir, archs) |
| 199 | |
| 200 | if not ext_build_dir: |
| 201 | shutil.rmtree(build_dir, True) |
| 202 | |
| 203 | |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 204 | def main(): |
| 205 | args = _ParseArgs() |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 206 | logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO) |
| 207 | |
Sami Kalliomäki | dbb15a7 | 2017-10-05 16:15:02 +0200 | [diff] [blame] | 208 | BuildAar(args.arch, args.output, args.use_goma, args.extra_gn_args, |
| 209 | args.build_dir) |
sakal | a4a7538 | 2017-01-24 01:25:50 -0800 | [diff] [blame] | 210 | |
| 211 | |
| 212 | if __name__ == '__main__': |
| 213 | sys.exit(main()) |