blob: 3bc88743a72d5ec2517659da934946895a50ab27 [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
oprypin7a2d8ca2017-02-06 07:53:41 -080016import logging
17import os
18import shutil
19import subprocess
20import sys
21
oprypin7a2d8ca2017-02-06 07:53:41 -080022os.environ['PATH'] = '/usr/libexec' + os.pathsep + os.environ['PATH']
23
24SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
Henrik Kjellanderec57e052017-10-17 21:36:01 +020025SRC_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, '..', '..'))
26sys.path.append(os.path.join(SRC_DIR, 'build'))
27import find_depot_tools
28
29SDK_OUTPUT_DIR = os.path.join(SRC_DIR, 'out_ios_libs')
oprypin7a2d8ca2017-02-06 07:53:41 -080030SDK_FRAMEWORK_NAME = 'WebRTC.framework'
Byoungchan Leec41093b2021-07-06 18:52:21 +090031SDK_DSYM_NAME = 'WebRTC.dSYM'
32SDK_XCFRAMEWORK_NAME = 'WebRTC.xcframework'
oprypin7a2d8ca2017-02-06 07:53:41 -080033
Byoungchan Leec41093b2021-07-06 18:52:21 +090034ENABLED_ARCHS = [
35 'device:arm64', 'simulator:arm64', 'simulator:x64',
Jordan Rose4a3296d2021-07-23 15:06:19 -070036 'catalyst:arm64', 'catalyst:x64',
Byoungchan Leec41093b2021-07-06 18:52:21 +090037 'arm64', 'x64'
38]
39DEFAULT_ARCHS = [
40 'device:arm64', 'simulator:arm64', 'simulator:x64'
41]
Jordan Rose4a3296d2021-07-23 15:06:19 -070042IOS_DEPLOYMENT_TARGET = {
43 'device': '12.0',
44 'simulator': '12.0',
45 'catalyst': '14.0'
46}
oprypin7a2d8ca2017-02-06 07:53:41 -080047LIBVPX_BUILD_VP9 = False
oprypin7a2d8ca2017-02-06 07:53:41 -080048
sakal67e414c2017-09-05 00:16:15 -070049sys.path.append(os.path.join(SCRIPT_DIR, '..', 'libs'))
50from generate_licenses import LicenseBuilder
51
oprypin7a2d8ca2017-02-06 07:53:41 -080052
53def _ParseArgs():
Mirko Bonadei8cc66952020-10-30 10:13:45 +010054 parser = argparse.ArgumentParser(description=__doc__)
55 parser.add_argument('--build_config',
56 default='release',
57 choices=['debug', 'release'],
58 help='The build config. Can be "debug" or "release". '
59 'Defaults to "release".')
60 parser.add_argument(
61 '--arch',
62 nargs='+',
63 default=DEFAULT_ARCHS,
64 choices=ENABLED_ARCHS,
65 help='Architectures to build. Defaults to %(default)s.')
66 parser.add_argument(
67 '-c',
68 '--clean',
69 action='store_true',
70 default=False,
71 help='Removes the previously generated build output, if any.')
72 parser.add_argument(
73 '-p',
74 '--purify',
75 action='store_true',
76 default=False,
77 help='Purifies the previously generated build output by '
78 'removing the temporary results used when (re)building.')
79 parser.add_argument(
80 '-o',
81 '--output-dir',
Yura Yaroshevich686ad4f2021-02-10 22:32:20 +030082 type=os.path.abspath,
Mirko Bonadei8cc66952020-10-30 10:13:45 +010083 default=SDK_OUTPUT_DIR,
84 help='Specifies a directory to output the build artifacts to. '
85 'If specified together with -c, deletes the dir.')
86 parser.add_argument(
87 '-r',
88 '--revision',
89 type=int,
90 default=0,
91 help='Specifies a revision number to embed if building the framework.')
92 parser.add_argument('-e',
93 '--bitcode',
94 action='store_true',
95 default=False,
96 help='Compile with bitcode.')
97 parser.add_argument('--verbose',
98 action='store_true',
99 default=False,
100 help='Debug logging.')
101 parser.add_argument('--use-goma',
102 action='store_true',
103 default=False,
104 help='Use goma to build.')
105 parser.add_argument(
106 '--extra-gn-args',
107 default=[],
108 nargs='*',
109 help='Additional GN args to be used during Ninja generation.')
mbonadei585209b2017-02-13 04:59:27 -0800110
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100111 return parser.parse_args()
oprypin7a2d8ca2017-02-06 07:53:41 -0800112
113
114def _RunCommand(cmd):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100115 logging.debug('Running: %r', cmd)
116 subprocess.check_call(cmd, cwd=SRC_DIR)
oprypin7a2d8ca2017-02-06 07:53:41 -0800117
118
119def _CleanArtifacts(output_dir):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100120 if os.path.isdir(output_dir):
121 logging.info('Deleting %s', output_dir)
122 shutil.rmtree(output_dir)
oprypin7a2d8ca2017-02-06 07:53:41 -0800123
124
VladimirTechMan7b188e82017-03-14 03:12:35 -0700125def _CleanTemporary(output_dir, architectures):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100126 if os.path.isdir(output_dir):
127 logging.info('Removing temporary build files.')
128 for arch in architectures:
Byoungchan Leec41093b2021-07-06 18:52:21 +0900129 arch_lib_path = os.path.join(output_dir, arch)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100130 if os.path.isdir(arch_lib_path):
131 shutil.rmtree(arch_lib_path)
VladimirTechMan7b188e82017-03-14 03:12:35 -0700132
133
Byoungchan Leec41093b2021-07-06 18:52:21 +0900134def _ParseArchitecture(architectures):
135 result = dict()
136 for arch in architectures:
137 if ":" in arch:
138 target_environment, target_cpu = arch.split(":")
139 else:
140 logging.warning('The environment for build is not specified.')
141 logging.warning('It is assumed based on cpu type.')
142 logging.warning('See crbug.com/1138425 for more details.')
143 if arch == "x64":
144 target_environment = "simulator"
145 else:
146 target_environment = "device"
147 target_cpu = arch
148 archs = result.get(target_environment)
149 if archs is None:
150 result[target_environment] = {target_cpu}
151 else:
152 archs.add(target_cpu)
153
154 return result
155
156
157def BuildWebRTC(output_dir, target_environment, target_arch, flavor,
158 gn_target_name, ios_deployment_target, libvpx_build_vp9,
159 use_bitcode, use_goma, extra_gn_args):
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100160 gn_args = [
161 'target_os="ios"', 'ios_enable_code_signing=false',
Mirko Bonadei58678a02020-12-01 10:54:40 +0100162 'use_xcode_clang=true', 'is_component_build=false',
163 'rtc_include_tests=false',
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100164 ]
oprypin7a2d8ca2017-02-06 07:53:41 -0800165
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100166 # Add flavor option.
167 if flavor == 'debug':
168 gn_args.append('is_debug=true')
169 elif flavor == 'release':
170 gn_args.append('is_debug=false')
171 else:
172 raise ValueError('Unexpected flavor type: %s' % flavor)
oprypin7a2d8ca2017-02-06 07:53:41 -0800173
Byoungchan Leec41093b2021-07-06 18:52:21 +0900174 gn_args.append('target_environment="%s"' % target_environment)
175
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100176 gn_args.append('target_cpu="%s"' % target_arch)
oprypin7a2d8ca2017-02-06 07:53:41 -0800177
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100178 gn_args.append('ios_deployment_target="%s"' % ios_deployment_target)
oprypin7a2d8ca2017-02-06 07:53:41 -0800179
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100180 gn_args.append('rtc_libvpx_build_vp9=' +
181 ('true' if libvpx_build_vp9 else 'false'))
oprypin7a2d8ca2017-02-06 07:53:41 -0800182
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100183 gn_args.append('enable_ios_bitcode=' +
184 ('true' if use_bitcode else 'false'))
185 gn_args.append('use_goma=' + ('true' if use_goma else 'false'))
Mirko Bonadeic1254e82020-12-17 15:56:13 +0100186 gn_args.append('rtc_enable_symbol_export=true')
oprypin7a2d8ca2017-02-06 07:53:41 -0800187
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100188 args_string = ' '.join(gn_args + extra_gn_args)
189 logging.info('Building WebRTC with args: %s', args_string)
mbonadei8714b8f2017-02-15 13:18:57 -0800190
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100191 cmd = [
192 sys.executable,
193 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
194 'gen',
195 output_dir,
196 '--args=' + args_string,
197 ]
198 _RunCommand(cmd)
199 logging.info('Building target: %s', gn_target_name)
mbonadei8714b8f2017-02-15 13:18:57 -0800200
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100201 cmd = [
202 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'ninja'),
203 '-C',
204 output_dir,
205 gn_target_name,
206 ]
207 if use_goma:
208 cmd.extend(['-j', '200'])
209 _RunCommand(cmd)
210
oprypin7a2d8ca2017-02-06 07:53:41 -0800211
oprypin7a2d8ca2017-02-06 07:53:41 -0800212def main():
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100213 args = _ParseArgs()
oprypin7a2d8ca2017-02-06 07:53:41 -0800214
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100215 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
oprypin7a2d8ca2017-02-06 07:53:41 -0800216
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100217 if args.clean:
218 _CleanArtifacts(args.output_dir)
219 return 0
oprypin7a2d8ca2017-02-06 07:53:41 -0800220
Byoungchan Leec41093b2021-07-06 18:52:21 +0900221 # architectures is typed as Dict[str, Set[str]],
222 # where key is for the environment (device or simulator)
223 # and value is for the cpu type.
224 architectures = _ParseArchitecture(args.arch)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100225 gn_args = args.extra_gn_args
VladimirTechMan7b188e82017-03-14 03:12:35 -0700226
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100227 if args.purify:
Byoungchan Leec41093b2021-07-06 18:52:21 +0900228 _CleanTemporary(args.output_dir, architectures.keys())
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100229 return 0
VladimirTechMan7b188e82017-03-14 03:12:35 -0700230
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100231 gn_target_name = 'framework_objc'
232 if not args.bitcode:
233 gn_args.append('enable_dsyms=true')
234 gn_args.append('enable_stripping=true')
Kári Tristan Helgason1edbda02017-06-13 10:45:42 +0200235
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100236 # Build all architectures.
Byoungchan Leec41093b2021-07-06 18:52:21 +0900237 framework_paths = []
238 all_lib_paths = []
239 for (environment, archs) in architectures.items():
240 framework_path = os.path.join(args.output_dir, environment)
241 framework_paths.append(framework_path)
242 lib_paths = []
243 for arch in archs:
244 lib_path = os.path.join(framework_path, arch + '_libs')
245 lib_paths.append(lib_path)
246 BuildWebRTC(lib_path, environment, arch, args.build_config,
Jordan Rose4a3296d2021-07-23 15:06:19 -0700247 gn_target_name, IOS_DEPLOYMENT_TARGET[environment],
Byoungchan Leec41093b2021-07-06 18:52:21 +0900248 LIBVPX_BUILD_VP9, args.bitcode, args.use_goma, gn_args)
249 all_lib_paths.extend(lib_paths)
Kári Tristan Helgason1edbda02017-06-13 10:45:42 +0200250
Byoungchan Leec41093b2021-07-06 18:52:21 +0900251 # Combine the slices.
252 dylib_path = os.path.join(SDK_FRAMEWORK_NAME, 'WebRTC')
253 # Dylibs will be combined, all other files are the same across archs.
Jordan Rose4a3296d2021-07-23 15:06:19 -0700254 shutil.rmtree(
255 os.path.join(framework_path, SDK_FRAMEWORK_NAME),
256 ignore_errors=True)
257 shutil.copytree(
Byoungchan Leec41093b2021-07-06 18:52:21 +0900258 os.path.join(lib_paths[0], SDK_FRAMEWORK_NAME),
Jordan Rose4a3296d2021-07-23 15:06:19 -0700259 os.path.join(framework_path, SDK_FRAMEWORK_NAME),
260 symlinks=True)
Byoungchan Leec41093b2021-07-06 18:52:21 +0900261 logging.info('Merging framework slices for %s.', environment)
262 dylib_paths = [os.path.join(path, dylib_path) for path in lib_paths]
263 out_dylib_path = os.path.join(framework_path, dylib_path)
Jordan Rose4a3296d2021-07-23 15:06:19 -0700264 if os.path.islink(out_dylib_path):
265 out_dylib_path = os.path.join(os.path.dirname(out_dylib_path),
266 os.readlink(out_dylib_path))
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100267 try:
Byoungchan Leec41093b2021-07-06 18:52:21 +0900268 os.remove(out_dylib_path)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100269 except OSError:
270 pass
Byoungchan Leec41093b2021-07-06 18:52:21 +0900271 cmd = ['lipo'] + dylib_paths + ['-create', '-output', out_dylib_path]
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100272 _RunCommand(cmd)
273
Byoungchan Leec41093b2021-07-06 18:52:21 +0900274 # Merge the dSYM slices.
275 lib_dsym_dir_path = os.path.join(lib_paths[0], SDK_DSYM_NAME)
276 if os.path.isdir(lib_dsym_dir_path):
Jordan Rose4a3296d2021-07-23 15:06:19 -0700277 shutil.rmtree(
278 os.path.join(framework_path, SDK_DSYM_NAME),
279 ignore_errors=True)
280 shutil.copytree(
Byoungchan Leec41093b2021-07-06 18:52:21 +0900281 lib_dsym_dir_path, os.path.join(framework_path, SDK_DSYM_NAME))
282 logging.info('Merging dSYM slices.')
283 dsym_path = os.path.join(SDK_DSYM_NAME, 'Contents', 'Resources',
284 'DWARF', 'WebRTC')
285 lib_dsym_paths = [
286 os.path.join(path, dsym_path) for path in lib_paths
287 ]
288 out_dsym_path = os.path.join(framework_path, dsym_path)
289 try:
290 os.remove(out_dsym_path)
291 except OSError:
292 pass
293 cmd = ['lipo'
294 ] + lib_dsym_paths + ['-create', '-output', out_dsym_path]
295 _RunCommand(cmd)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100296
Jordan Rose4a3296d2021-07-23 15:06:19 -0700297 # Check for Mac-style WebRTC.framework/Resources/ (for Catalyst)...
298 resources_dir = os.path.join(framework_path, SDK_FRAMEWORK_NAME,
299 'Resources')
300 if not os.path.exists(resources_dir):
301 # ...then fall back to iOS-style WebRTC.framework/
302 resources_dir = os.path.dirname(resources_dir)
303
Byoungchan Leec41093b2021-07-06 18:52:21 +0900304 # Modify the version number.
305 # Format should be <Branch cut MXX>.<Hotfix #>.<Rev #>.
306 # e.g. 55.0.14986 means
307 # branch cut 55, no hotfixes, and revision 14986.
Jordan Rose4a3296d2021-07-23 15:06:19 -0700308 infoplist_path = os.path.join(resources_dir, 'Info.plist')
Byoungchan Leec41093b2021-07-06 18:52:21 +0900309 cmd = [
310 'PlistBuddy', '-c', 'Print :CFBundleShortVersionString',
311 infoplist_path
312 ]
313 major_minor = subprocess.check_output(cmd).strip()
314 version_number = '%s.%s' % (major_minor, args.revision)
315 logging.info('Substituting revision number: %s', version_number)
316 cmd = [
317 'PlistBuddy', '-c', 'Set :CFBundleVersion ' + version_number,
318 infoplist_path
319 ]
320 _RunCommand(cmd)
321 _RunCommand(['plutil', '-convert', 'binary1', infoplist_path])
322
323 xcframework_dir = os.path.join(args.output_dir, SDK_XCFRAMEWORK_NAME)
324 if os.path.isdir(xcframework_dir):
325 shutil.rmtree(xcframework_dir)
326
327 logging.info('Creating xcframework.')
328 cmd = ['xcodebuild', '-create-xcframework', '-output', xcframework_dir]
329
330 # Apparently, xcodebuild needs absolute paths for input arguments
331 for framework_path in framework_paths:
332 cmd += [
333 '-framework',
334 os.path.abspath(os.path.join(framework_path, SDK_FRAMEWORK_NAME)),
335 '-debug-symbols',
336 os.path.abspath(os.path.join(framework_path, SDK_DSYM_NAME))
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100337 ]
Byoungchan Leec41093b2021-07-06 18:52:21 +0900338
339 _RunCommand(cmd)
340
341 # Generate the license file.
342 logging.info('Generate license file.')
343 gn_target_full_name = '//sdk:' + gn_target_name
344 builder = LicenseBuilder(all_lib_paths, [gn_target_full_name])
345 builder.GenerateLicenseText(
346 os.path.join(args.output_dir, SDK_XCFRAMEWORK_NAME))
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100347
348 logging.info('Done.')
349 return 0
oprypin7a2d8ca2017-02-06 07:53:41 -0800350
351
352if __name__ == '__main__':
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100353 sys.exit(main())