blob: fb5b67ae22f0d55676ecf91ca982c3a42fcaf313 [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.')
71 parser.add_argument('--verbose',
72 action='store_true',
73 default=False,
74 help='Debug logging.')
75 parser.add_argument(
76 '--extra-gn-args',
77 default=[],
78 nargs='*',
79 help="""Additional GN arguments to be used during Ninja generation.
Yura Yaroshevichf517f112018-05-24 16:48:02 +030080 These are passed to gn inside `--args` switch and
81 applied after any other arguments and will
82 override any values defined by the script.
83 Example of building debug aar file:
84 build_aar.py --extra-gn-args='is_debug=true'""")
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010085 parser.add_argument(
86 '--extra-ninja-switches',
87 default=[],
88 nargs='*',
89 help="""Additional Ninja switches to be used during compilation.
Yura Yaroshevichf517f112018-05-24 16:48:02 +030090 These are applied after any other Ninja switches.
91 Example of enabling verbose Ninja output:
92 build_aar.py --extra-ninja-switches='-v'""")
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010093 parser.add_argument(
94 '--extra-gn-switches',
95 default=[],
96 nargs='*',
97 help="""Additional GN switches to be used during compilation.
Yura Yaroshevichf517f112018-05-24 16:48:02 +030098 These are applied after any other GN switches.
99 Example of enabling verbose GN output:
100 build_aar.py --extra-gn-switches='-v'""")
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100101 return parser.parse_args()
sakala4a75382017-01-24 01:25:50 -0800102
103
104def _RunGN(args):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100105 cmd = [
106 sys.executable,
107 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py')
108 ]
109 cmd.extend(args)
110 logging.debug('Running: %r', cmd)
111 subprocess.check_call(cmd)
sakala4a75382017-01-24 01:25:50 -0800112
113
114def _RunNinja(output_directory, args):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100115 cmd = [
116 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'ninja'), '-C',
117 output_directory
118 ]
119 cmd.extend(args)
120 logging.debug('Running: %r', cmd)
121 subprocess.check_call(cmd)
sakala4a75382017-01-24 01:25:50 -0800122
123
124def _EncodeForGN(value):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100125 """Encodes value as a GN literal."""
126 if isinstance(value, str):
127 return '"' + value + '"'
128 if isinstance(value, bool):
129 return repr(value).lower()
130 return repr(value)
sakala4a75382017-01-24 01:25:50 -0800131
132
korniltsev.anatoly0b510a92017-09-05 08:12:30 -0700133def _GetOutputDirectory(build_dir, arch):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100134 """Returns the GN output directory for the target architecture."""
135 return os.path.join(build_dir, arch)
sakala4a75382017-01-24 01:25:50 -0800136
137
138def _GetTargetCpu(arch):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100139 """Returns target_cpu for the GN build with the given architecture."""
140 if arch in ['armeabi', 'armeabi-v7a']:
141 return 'arm'
142 if arch == 'arm64-v8a':
143 return 'arm64'
144 if arch == 'x86':
145 return 'x86'
146 if arch == 'x86_64':
147 return 'x64'
148 raise Exception('Unknown arch: ' + arch)
sakala4a75382017-01-24 01:25:50 -0800149
150
151def _GetArmVersion(arch):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100152 """Returns arm_version for the GN build with the given architecture."""
153 if arch == 'armeabi':
154 return 6
155 if arch == 'armeabi-v7a':
156 return 7
157 if arch in ['arm64-v8a', 'x86', 'x86_64']:
158 return None
159 raise Exception('Unknown arch: ' + arch)
sakala4a75382017-01-24 01:25:50 -0800160
161
Yura Yaroshevichf517f112018-05-24 16:48:02 +0300162def Build(build_dir, arch, use_goma, extra_gn_args, extra_gn_switches,
163 extra_ninja_switches):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100164 """Generates target architecture using GN and builds it using ninja."""
165 logging.info('Building: %s', arch)
166 output_directory = _GetOutputDirectory(build_dir, arch)
167 gn_args = {
168 'target_os': 'android',
169 'is_debug': False,
170 'is_component_build': False,
171 'rtc_include_tests': False,
172 'target_cpu': _GetTargetCpu(arch),
173 'use_goma': use_goma
174 }
175 arm_version = _GetArmVersion(arch)
176 if arm_version:
177 gn_args['arm_version'] = arm_version
178 gn_args_str = '--args=' + ' '.join(
179 [k + '=' + _EncodeForGN(v) for k, v in gn_args.items()] + extra_gn_args)
sakala4a75382017-01-24 01:25:50 -0800180
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100181 gn_args_list = ['gen', output_directory, gn_args_str]
182 gn_args_list.extend(extra_gn_switches)
183 _RunGN(gn_args_list)
sakala4a75382017-01-24 01:25:50 -0800184
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100185 ninja_args = TARGETS[:]
186 if use_goma:
187 ninja_args.extend(['-j', '200'])
188 ninja_args.extend(extra_ninja_switches)
189 _RunNinja(output_directory, ninja_args)
sakala4a75382017-01-24 01:25:50 -0800190
191
korniltsev.anatoly0b510a92017-09-05 08:12:30 -0700192def CollectCommon(aar_file, build_dir, arch):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100193 """Collects architecture independent files into the .aar-archive."""
194 logging.info('Collecting common files.')
195 output_directory = _GetOutputDirectory(build_dir, arch)
196 aar_file.write(MANIFEST_FILE, 'AndroidManifest.xml')
197 aar_file.write(os.path.join(output_directory, JAR_FILE), 'classes.jar')
sakala4a75382017-01-24 01:25:50 -0800198
199
korniltsev.anatoly0b510a92017-09-05 08:12:30 -0700200def Collect(aar_file, build_dir, arch):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100201 """Collects architecture specific files into the .aar-archive."""
202 logging.info('Collecting: %s', arch)
203 output_directory = _GetOutputDirectory(build_dir, arch)
sakala4a75382017-01-24 01:25:50 -0800204
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100205 abi_dir = os.path.join('jni', arch)
206 for so_file in NEEDED_SO_FILES:
207 aar_file.write(os.path.join(output_directory, so_file),
208 os.path.join(abi_dir, so_file))
sakala4a75382017-01-24 01:25:50 -0800209
210
korniltsev.anatoly0b510a92017-09-05 08:12:30 -0700211def GenerateLicenses(output_dir, build_dir, archs):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100212 builder = LicenseBuilder(
213 [_GetOutputDirectory(build_dir, arch) for arch in archs], TARGETS)
214 builder.GenerateLicenseText(output_dir)
sakal67e414c2017-09-05 00:16:15 -0700215
216
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100217def BuildAar(archs,
218 output_file,
219 use_goma=False,
220 extra_gn_args=None,
221 ext_build_dir=None,
222 extra_gn_switches=None,
Yura Yaroshevichf517f112018-05-24 16:48:02 +0300223 extra_ninja_switches=None):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100224 extra_gn_args = extra_gn_args or []
225 extra_gn_switches = extra_gn_switches or []
226 extra_ninja_switches = extra_ninja_switches or []
227 build_dir = ext_build_dir if ext_build_dir else tempfile.mkdtemp()
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200228
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100229 for arch in archs:
230 Build(build_dir, arch, use_goma, extra_gn_args, extra_gn_switches,
231 extra_ninja_switches)
232
233 with zipfile.ZipFile(output_file, 'w') as aar_file:
234 # Architecture doesn't matter here, arbitrarily using the first one.
235 CollectCommon(aar_file, build_dir, archs[0])
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200236 for arch in archs:
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100237 Collect(aar_file, build_dir, arch)
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200238
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100239 license_dir = os.path.dirname(os.path.realpath(output_file))
240 GenerateLicenses(license_dir, build_dir, archs)
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200241
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100242 if not ext_build_dir:
243 shutil.rmtree(build_dir, True)
Sami Kalliomäkidbb15a72017-10-05 16:15:02 +0200244
245
sakala4a75382017-01-24 01:25:50 -0800246def main():
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100247 args = _ParseArgs()
248 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
sakala4a75382017-01-24 01:25:50 -0800249
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100250 BuildAar(args.arch, args.output, args.use_goma, args.extra_gn_args,
251 args.build_dir, args.extra_gn_switches, args.extra_ninja_switches)
sakala4a75382017-01-24 01:25:50 -0800252
253
254if __name__ == '__main__':
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100255 sys.exit(main())