blob: c93185322921373182ac4573ab66c6c3b830ec38 [file] [log] [blame]
oprypin7a2d8ca2017-02-06 07:53:41 -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.
Byoungchan Leec41093b2021-07-06 18:52:21 +090010"""WebRTC iOS XCFramework build script.
oprypin7a2d8ca2017-02-06 07:53:41 -080011Each architecture is compiled separately before being merged together.
12By default, the library is created in out_ios_libs/. (Change with -o.)
oprypin7a2d8ca2017-02-06 07:53:41 -080013"""
14
15import argparse
16import distutils.dir_util
17import logging
18import os
19import shutil
20import subprocess
21import sys
22
oprypin7a2d8ca2017-02-06 07:53:41 -080023os.environ['PATH'] = '/usr/libexec' + os.pathsep + os.environ['PATH']
24
25SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
Henrik Kjellanderec57e052017-10-17 21:36:01 +020026SRC_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, '..', '..'))
27sys.path.append(os.path.join(SRC_DIR, 'build'))
28import find_depot_tools
29
30SDK_OUTPUT_DIR = os.path.join(SRC_DIR, 'out_ios_libs')
oprypin7a2d8ca2017-02-06 07:53:41 -080031SDK_FRAMEWORK_NAME = 'WebRTC.framework'
Byoungchan Leec41093b2021-07-06 18:52:21 +090032SDK_DSYM_NAME = 'WebRTC.dSYM'
33SDK_XCFRAMEWORK_NAME = 'WebRTC.xcframework'
oprypin7a2d8ca2017-02-06 07:53:41 -080034
Byoungchan Leec41093b2021-07-06 18:52:21 +090035ENABLED_ARCHS = [
36 'device:arm64', 'simulator:arm64', 'simulator:x64',
37 'arm64', 'x64'
38]
39DEFAULT_ARCHS = [
40 'device:arm64', 'simulator:arm64', 'simulator:x64'
41]
Björn Terelius02768ae2021-07-02 16:19:32 +020042IOS_DEPLOYMENT_TARGET = '12.0'
oprypin7a2d8ca2017-02-06 07:53:41 -080043LIBVPX_BUILD_VP9 = False
oprypin7a2d8ca2017-02-06 07:53:41 -080044
sakal67e414c2017-09-05 00:16:15 -070045sys.path.append(os.path.join(SCRIPT_DIR, '..', 'libs'))
46from generate_licenses import LicenseBuilder
47
oprypin7a2d8ca2017-02-06 07:53:41 -080048
49def _ParseArgs():
Mirko Bonadei8cc66952020-10-30 10:13:45 +010050 parser = argparse.ArgumentParser(description=__doc__)
51 parser.add_argument('--build_config',
52 default='release',
53 choices=['debug', 'release'],
54 help='The build config. Can be "debug" or "release". '
55 'Defaults to "release".')
56 parser.add_argument(
57 '--arch',
58 nargs='+',
59 default=DEFAULT_ARCHS,
60 choices=ENABLED_ARCHS,
61 help='Architectures to build. Defaults to %(default)s.')
62 parser.add_argument(
63 '-c',
64 '--clean',
65 action='store_true',
66 default=False,
67 help='Removes the previously generated build output, if any.')
68 parser.add_argument(
69 '-p',
70 '--purify',
71 action='store_true',
72 default=False,
73 help='Purifies the previously generated build output by '
74 'removing the temporary results used when (re)building.')
75 parser.add_argument(
76 '-o',
77 '--output-dir',
Yura Yaroshevich686ad4f2021-02-10 22:32:20 +030078 type=os.path.abspath,
Mirko Bonadei8cc66952020-10-30 10:13:45 +010079 default=SDK_OUTPUT_DIR,
80 help='Specifies a directory to output the build artifacts to. '
81 'If specified together with -c, deletes the dir.')
82 parser.add_argument(
83 '-r',
84 '--revision',
85 type=int,
86 default=0,
87 help='Specifies a revision number to embed if building the framework.')
88 parser.add_argument('-e',
89 '--bitcode',
90 action='store_true',
91 default=False,
92 help='Compile with bitcode.')
93 parser.add_argument('--verbose',
94 action='store_true',
95 default=False,
96 help='Debug logging.')
97 parser.add_argument('--use-goma',
98 action='store_true',
99 default=False,
100 help='Use goma to build.')
101 parser.add_argument(
102 '--extra-gn-args',
103 default=[],
104 nargs='*',
105 help='Additional GN args to be used during Ninja generation.')
mbonadei585209b2017-02-13 04:59:27 -0800106
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100107 return parser.parse_args()
oprypin7a2d8ca2017-02-06 07:53:41 -0800108
109
110def _RunCommand(cmd):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100111 logging.debug('Running: %r', cmd)
112 subprocess.check_call(cmd, cwd=SRC_DIR)
oprypin7a2d8ca2017-02-06 07:53:41 -0800113
114
115def _CleanArtifacts(output_dir):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100116 if os.path.isdir(output_dir):
117 logging.info('Deleting %s', output_dir)
118 shutil.rmtree(output_dir)
oprypin7a2d8ca2017-02-06 07:53:41 -0800119
120
VladimirTechMan7b188e82017-03-14 03:12:35 -0700121def _CleanTemporary(output_dir, architectures):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100122 if os.path.isdir(output_dir):
123 logging.info('Removing temporary build files.')
124 for arch in architectures:
Byoungchan Leec41093b2021-07-06 18:52:21 +0900125 arch_lib_path = os.path.join(output_dir, arch)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100126 if os.path.isdir(arch_lib_path):
127 shutil.rmtree(arch_lib_path)
VladimirTechMan7b188e82017-03-14 03:12:35 -0700128
129
Byoungchan Leec41093b2021-07-06 18:52:21 +0900130def _ParseArchitecture(architectures):
131 result = dict()
132 for arch in architectures:
133 if ":" in arch:
134 target_environment, target_cpu = arch.split(":")
135 else:
136 logging.warning('The environment for build is not specified.')
137 logging.warning('It is assumed based on cpu type.')
138 logging.warning('See crbug.com/1138425 for more details.')
139 if arch == "x64":
140 target_environment = "simulator"
141 else:
142 target_environment = "device"
143 target_cpu = arch
144 archs = result.get(target_environment)
145 if archs is None:
146 result[target_environment] = {target_cpu}
147 else:
148 archs.add(target_cpu)
149
150 return result
151
152
153def BuildWebRTC(output_dir, target_environment, target_arch, flavor,
154 gn_target_name, ios_deployment_target, libvpx_build_vp9,
155 use_bitcode, use_goma, extra_gn_args):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100156 gn_args = [
157 'target_os="ios"', 'ios_enable_code_signing=false',
Mirko Bonadei58678a02020-12-01 10:54:40 +0100158 'use_xcode_clang=true', 'is_component_build=false',
159 'rtc_include_tests=false',
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100160 ]
oprypin7a2d8ca2017-02-06 07:53:41 -0800161
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100162 # Add flavor option.
163 if flavor == 'debug':
164 gn_args.append('is_debug=true')
165 elif flavor == 'release':
166 gn_args.append('is_debug=false')
167 else:
168 raise ValueError('Unexpected flavor type: %s' % flavor)
oprypin7a2d8ca2017-02-06 07:53:41 -0800169
Byoungchan Leec41093b2021-07-06 18:52:21 +0900170 gn_args.append('target_environment="%s"' % target_environment)
171
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100172 gn_args.append('target_cpu="%s"' % target_arch)
oprypin7a2d8ca2017-02-06 07:53:41 -0800173
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100174 gn_args.append('ios_deployment_target="%s"' % ios_deployment_target)
oprypin7a2d8ca2017-02-06 07:53:41 -0800175
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100176 gn_args.append('rtc_libvpx_build_vp9=' +
177 ('true' if libvpx_build_vp9 else 'false'))
oprypin7a2d8ca2017-02-06 07:53:41 -0800178
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100179 gn_args.append('enable_ios_bitcode=' +
180 ('true' if use_bitcode else 'false'))
181 gn_args.append('use_goma=' + ('true' if use_goma else 'false'))
Mirko Bonadeic1254e82020-12-17 15:56:13 +0100182 gn_args.append('rtc_enable_symbol_export=true')
oprypin7a2d8ca2017-02-06 07:53:41 -0800183
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100184 args_string = ' '.join(gn_args + extra_gn_args)
185 logging.info('Building WebRTC with args: %s', args_string)
mbonadei8714b8f2017-02-15 13:18:57 -0800186
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100187 cmd = [
188 sys.executable,
189 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
190 'gen',
191 output_dir,
192 '--args=' + args_string,
193 ]
194 _RunCommand(cmd)
195 logging.info('Building target: %s', gn_target_name)
mbonadei8714b8f2017-02-15 13:18:57 -0800196
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100197 cmd = [
198 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'ninja'),
199 '-C',
200 output_dir,
201 gn_target_name,
202 ]
203 if use_goma:
204 cmd.extend(['-j', '200'])
205 _RunCommand(cmd)
206
oprypin7a2d8ca2017-02-06 07:53:41 -0800207
oprypin7a2d8ca2017-02-06 07:53:41 -0800208def main():
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100209 args = _ParseArgs()
oprypin7a2d8ca2017-02-06 07:53:41 -0800210
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100211 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
oprypin7a2d8ca2017-02-06 07:53:41 -0800212
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100213 if args.clean:
214 _CleanArtifacts(args.output_dir)
215 return 0
oprypin7a2d8ca2017-02-06 07:53:41 -0800216
Byoungchan Leec41093b2021-07-06 18:52:21 +0900217 # architectures is typed as Dict[str, Set[str]],
218 # where key is for the environment (device or simulator)
219 # and value is for the cpu type.
220 architectures = _ParseArchitecture(args.arch)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100221 gn_args = args.extra_gn_args
VladimirTechMan7b188e82017-03-14 03:12:35 -0700222
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100223 if args.purify:
Byoungchan Leec41093b2021-07-06 18:52:21 +0900224 _CleanTemporary(args.output_dir, architectures.keys())
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100225 return 0
VladimirTechMan7b188e82017-03-14 03:12:35 -0700226
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100227 gn_target_name = 'framework_objc'
228 if not args.bitcode:
229 gn_args.append('enable_dsyms=true')
230 gn_args.append('enable_stripping=true')
Kári Tristan Helgason1edbda02017-06-13 10:45:42 +0200231
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100232 # Build all architectures.
Byoungchan Leec41093b2021-07-06 18:52:21 +0900233 framework_paths = []
234 all_lib_paths = []
235 for (environment, archs) in architectures.items():
236 framework_path = os.path.join(args.output_dir, environment)
237 framework_paths.append(framework_path)
238 lib_paths = []
239 for arch in archs:
240 lib_path = os.path.join(framework_path, arch + '_libs')
241 lib_paths.append(lib_path)
242 BuildWebRTC(lib_path, environment, arch, args.build_config,
243 gn_target_name, IOS_DEPLOYMENT_TARGET,
244 LIBVPX_BUILD_VP9, args.bitcode, args.use_goma, gn_args)
245 all_lib_paths.extend(lib_paths)
Kári Tristan Helgason1edbda02017-06-13 10:45:42 +0200246
Byoungchan Leec41093b2021-07-06 18:52:21 +0900247 # Combine the slices.
248 dylib_path = os.path.join(SDK_FRAMEWORK_NAME, 'WebRTC')
249 # Dylibs will be combined, all other files are the same across archs.
250 # Use distutils instead of shutil to support merging folders.
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100251 distutils.dir_util.copy_tree(
Byoungchan Leec41093b2021-07-06 18:52:21 +0900252 os.path.join(lib_paths[0], SDK_FRAMEWORK_NAME),
253 os.path.join(framework_path, SDK_FRAMEWORK_NAME))
254 logging.info('Merging framework slices for %s.', environment)
255 dylib_paths = [os.path.join(path, dylib_path) for path in lib_paths]
256 out_dylib_path = os.path.join(framework_path, dylib_path)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100257 try:
Byoungchan Leec41093b2021-07-06 18:52:21 +0900258 os.remove(out_dylib_path)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100259 except OSError:
260 pass
Byoungchan Leec41093b2021-07-06 18:52:21 +0900261 cmd = ['lipo'] + dylib_paths + ['-create', '-output', out_dylib_path]
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100262 _RunCommand(cmd)
263
Byoungchan Leec41093b2021-07-06 18:52:21 +0900264 # Merge the dSYM slices.
265 lib_dsym_dir_path = os.path.join(lib_paths[0], SDK_DSYM_NAME)
266 if os.path.isdir(lib_dsym_dir_path):
267 distutils.dir_util.copy_tree(
268 lib_dsym_dir_path, os.path.join(framework_path, SDK_DSYM_NAME))
269 logging.info('Merging dSYM slices.')
270 dsym_path = os.path.join(SDK_DSYM_NAME, 'Contents', 'Resources',
271 'DWARF', 'WebRTC')
272 lib_dsym_paths = [
273 os.path.join(path, dsym_path) for path in lib_paths
274 ]
275 out_dsym_path = os.path.join(framework_path, dsym_path)
276 try:
277 os.remove(out_dsym_path)
278 except OSError:
279 pass
280 cmd = ['lipo'
281 ] + lib_dsym_paths + ['-create', '-output', out_dsym_path]
282 _RunCommand(cmd)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100283
Byoungchan Leec41093b2021-07-06 18:52:21 +0900284 # Modify the version number.
285 # Format should be <Branch cut MXX>.<Hotfix #>.<Rev #>.
286 # e.g. 55.0.14986 means
287 # branch cut 55, no hotfixes, and revision 14986.
288 infoplist_path = os.path.join(framework_path, SDK_FRAMEWORK_NAME,
289 'Info.plist')
290 cmd = [
291 'PlistBuddy', '-c', 'Print :CFBundleShortVersionString',
292 infoplist_path
293 ]
294 major_minor = subprocess.check_output(cmd).strip()
295 version_number = '%s.%s' % (major_minor, args.revision)
296 logging.info('Substituting revision number: %s', version_number)
297 cmd = [
298 'PlistBuddy', '-c', 'Set :CFBundleVersion ' + version_number,
299 infoplist_path
300 ]
301 _RunCommand(cmd)
302 _RunCommand(['plutil', '-convert', 'binary1', infoplist_path])
303
304 xcframework_dir = os.path.join(args.output_dir, SDK_XCFRAMEWORK_NAME)
305 if os.path.isdir(xcframework_dir):
306 shutil.rmtree(xcframework_dir)
307
308 logging.info('Creating xcframework.')
309 cmd = ['xcodebuild', '-create-xcframework', '-output', xcframework_dir]
310
311 # Apparently, xcodebuild needs absolute paths for input arguments
312 for framework_path in framework_paths:
313 cmd += [
314 '-framework',
315 os.path.abspath(os.path.join(framework_path, SDK_FRAMEWORK_NAME)),
316 '-debug-symbols',
317 os.path.abspath(os.path.join(framework_path, SDK_DSYM_NAME))
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100318 ]
Byoungchan Leec41093b2021-07-06 18:52:21 +0900319
320 _RunCommand(cmd)
321
322 # Generate the license file.
323 logging.info('Generate license file.')
324 gn_target_full_name = '//sdk:' + gn_target_name
325 builder = LicenseBuilder(all_lib_paths, [gn_target_full_name])
326 builder.GenerateLicenseText(
327 os.path.join(args.output_dir, SDK_XCFRAMEWORK_NAME))
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100328
329 logging.info('Done.')
330 return 0
oprypin7a2d8ca2017-02-06 07:53:41 -0800331
332
333if __name__ == '__main__':
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100334 sys.exit(main())