blob: 9fc4bb0f392731c4929da49a956ff7159d56b3bc [file] [log] [blame]
sakala4a75382017-01-24 01:25:50 -08001#!/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.
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():
Mirko Bonadei8cc66952020-10-30 10:13:45 +010054 parser = argparse.ArgumentParser(description='libwebrtc.aar generator.')
55 parser.add_argument(
56 '--build-dir',
Yura Yaroshevich686ad4f2021-02-10 22:32:20 +030057 type=os.path.abspath,
Mirko Bonadei8cc66952020-10-30 10:13:45 +010058 help='Build dir. By default will create and use temporary dir.')
59 parser.add_argument('--output',
60 default='libwebrtc.aar',
Yura Yaroshevich686ad4f2021-02-10 22:32:20 +030061 type=os.path.abspath,
Mirko Bonadei8cc66952020-10-30 10:13:45 +010062 help='Output file of the script.')
63 parser.add_argument(
64 '--arch',
65 default=DEFAULT_ARCHS,
66 nargs='*',
67 help='Architectures to build. Defaults to %(default)s.')
68 parser.add_argument('--use-goma',
69 action='store_true',
70 default=False,
71 help='Use goma.')
72 parser.add_argument('--verbose',
73 action='store_true',
74 default=False,
75 help='Debug logging.')
76 parser.add_argument(
77 '--extra-gn-args',
78 default=[],
79 nargs='*',
80 help="""Additional GN arguments to be used during Ninja generation.
Yura Yaroshevichf517f112018-05-24 16:48:02 +030081 These are passed to gn inside `--args` switch and
82 applied after any other arguments and will
83 override any values defined by the script.
84 Example of building debug aar file:
85 build_aar.py --extra-gn-args='is_debug=true'""")
Mirko Bonadei8cc66952020-10-30 10:13:45 +010086 parser.add_argument(
87 '--extra-ninja-switches',
88 default=[],
89 nargs='*',
90 help="""Additional Ninja switches to be used during compilation.
Yura Yaroshevichf517f112018-05-24 16:48:02 +030091 These are applied after any other Ninja switches.
92 Example of enabling verbose Ninja output:
93 build_aar.py --extra-ninja-switches='-v'""")
Mirko Bonadei8cc66952020-10-30 10:13:45 +010094 parser.add_argument(
95 '--extra-gn-switches',
96 default=[],
97 nargs='*',
98 help="""Additional GN switches to be used during compilation.
Yura Yaroshevichf517f112018-05-24 16:48:02 +030099 These are applied after any other GN switches.
100 Example of enabling verbose GN output:
101 build_aar.py --extra-gn-switches='-v'""")
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100102 return parser.parse_args()
sakala4a75382017-01-24 01:25:50 -0800103
104
105def _RunGN(args):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100106 cmd = [
107 sys.executable,
108 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py')
109 ]
110 cmd.extend(args)
111 logging.debug('Running: %r', cmd)
112 subprocess.check_call(cmd)
sakala4a75382017-01-24 01:25:50 -0800113
114
115def _RunNinja(output_directory, args):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100116 cmd = [
117 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'ninja'), '-C',
118 output_directory
119 ]
120 cmd.extend(args)
121 logging.debug('Running: %r', cmd)
122 subprocess.check_call(cmd)
sakala4a75382017-01-24 01:25:50 -0800123
124
125def _EncodeForGN(value):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100126 """Encodes value as a GN literal."""
127 if isinstance(value, str):
128 return '"' + value + '"'
129 elif isinstance(value, bool):
130 return repr(value).lower()
131 else:
132 return repr(value)
sakala4a75382017-01-24 01:25:50 -0800133
134
korniltsev.anatoly0b510a92017-09-05 08:12:30 -0700135def _GetOutputDirectory(build_dir, arch):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100136 """Returns the GN output directory for the target architecture."""
137 return os.path.join(build_dir, arch)
sakala4a75382017-01-24 01:25:50 -0800138
139
140def _GetTargetCpu(arch):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100141 """Returns target_cpu for the GN build with the given architecture."""
142 if arch in ['armeabi', 'armeabi-v7a']:
143 return 'arm'
144 elif arch == 'arm64-v8a':
145 return 'arm64'
146 elif arch == 'x86':
147 return 'x86'
148 elif arch == 'x86_64':
149 return 'x64'
150 else:
151 raise Exception('Unknown arch: ' + arch)
sakala4a75382017-01-24 01:25:50 -0800152
153
154def _GetArmVersion(arch):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100155 """Returns arm_version for the GN build with the given architecture."""
156 if arch == 'armeabi':
157 return 6
158 elif arch == 'armeabi-v7a':
159 return 7
160 elif arch in ['arm64-v8a', 'x86', 'x86_64']:
161 return None
162 else:
163 raise Exception('Unknown arch: ' + arch)
sakala4a75382017-01-24 01:25:50 -0800164
165
Yura Yaroshevichf517f112018-05-24 16:48:02 +0300166def Build(build_dir, arch, use_goma, extra_gn_args, extra_gn_switches,
167 extra_ninja_switches):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100168 """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)
184 for k, v in gn_args.items()] + extra_gn_args)
sakala4a75382017-01-24 01:25:50 -0800185
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100186 gn_args_list = ['gen', output_directory, gn_args_str]
187 gn_args_list.extend(extra_gn_switches)
188 _RunGN(gn_args_list)
sakala4a75382017-01-24 01:25:50 -0800189
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100190 ninja_args = TARGETS[:]
191 if use_goma:
192 ninja_args.extend(['-j', '200'])
193 ninja_args.extend(extra_ninja_switches)
194 _RunNinja(output_directory, ninja_args)
sakala4a75382017-01-24 01:25:50 -0800195
196
korniltsev.anatoly0b510a92017-09-05 08:12:30 -0700197def CollectCommon(aar_file, build_dir, arch):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100198 """Collects architecture independent files into the .aar-archive."""
199 logging.info('Collecting common files.')
200 output_directory = _GetOutputDirectory(build_dir, arch)
201 aar_file.write(MANIFEST_FILE, 'AndroidManifest.xml')
202 aar_file.write(os.path.join(output_directory, JAR_FILE), 'classes.jar')
sakala4a75382017-01-24 01:25:50 -0800203
204
korniltsev.anatoly0b510a92017-09-05 08:12:30 -0700205def Collect(aar_file, build_dir, arch):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100206 """Collects architecture specific files into the .aar-archive."""
207 logging.info('Collecting: %s', arch)
208 output_directory = _GetOutputDirectory(build_dir, arch)
sakala4a75382017-01-24 01:25:50 -0800209
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100210 abi_dir = os.path.join('jni', arch)
211 for so_file in NEEDED_SO_FILES:
212 aar_file.write(os.path.join(output_directory, so_file),
213 os.path.join(abi_dir, so_file))
sakala4a75382017-01-24 01:25:50 -0800214
215
korniltsev.anatoly0b510a92017-09-05 08:12:30 -0700216def GenerateLicenses(output_dir, build_dir, archs):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100217 builder = LicenseBuilder(
218 [_GetOutputDirectory(build_dir, arch) for arch in archs], TARGETS)
219 builder.GenerateLicenseText(output_dir)
sakal67e414c2017-09-05 00:16:15 -0700220
221
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100222def BuildAar(archs,
223 output_file,
224 use_goma=False,
225 extra_gn_args=None,
226 ext_build_dir=None,
227 extra_gn_switches=None,
Yura Yaroshevichf517f112018-05-24 16:48:02 +0300228 extra_ninja_switches=None):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100229 extra_gn_args = extra_gn_args or []
230 extra_gn_switches = extra_gn_switches or []
231 extra_ninja_switches = extra_ninja_switches or []
232 build_dir = ext_build_dir if ext_build_dir else tempfile.mkdtemp()
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200233
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200234 for arch in archs:
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100235 Build(build_dir, arch, use_goma, extra_gn_args, extra_gn_switches,
236 extra_ninja_switches)
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200237
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100238 with zipfile.ZipFile(output_file, 'w') as aar_file:
239 # Architecture doesn't matter here, arbitrarily using the first one.
240 CollectCommon(aar_file, build_dir, archs[0])
241 for arch in archs:
242 Collect(aar_file, build_dir, arch)
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200243
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100244 license_dir = os.path.dirname(os.path.realpath(output_file))
245 GenerateLicenses(license_dir, build_dir, archs)
246
247 if not ext_build_dir:
248 shutil.rmtree(build_dir, True)
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200249
250
sakala4a75382017-01-24 01:25:50 -0800251def main():
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100252 args = _ParseArgs()
253 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
sakala4a75382017-01-24 01:25:50 -0800254
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100255 BuildAar(args.arch, args.output, args.use_goma, args.extra_gn_args,
256 args.build_dir, args.extra_gn_switches, args.extra_ninja_switches)
sakala4a75382017-01-24 01:25:50 -0800257
258
259if __name__ == '__main__':
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100260 sys.exit(main())