blob: 15a912e4bac02d398f8bce865c9fc10d8c5f5e72 [file] [log] [blame]
Christoffer Jansson4e8a7732022-02-08 09:01:12 +01001#!/usr/bin/env vpython3
oprypin7a2d8ca2017-02-06 07:53:41 -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.
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():
Christoffer Jansson4e8a7732022-02-08 09:01:12 +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('--arch',
61 nargs='+',
62 default=DEFAULT_ARCHS,
63 choices=ENABLED_ARCHS,
64 help='Architectures to build. Defaults to %(default)s.')
65 parser.add_argument(
66 '-c',
67 '--clean',
68 action='store_true',
69 default=False,
70 help='Removes the previously generated build output, if any.')
71 parser.add_argument('-p',
72 '--purify',
73 action='store_true',
74 default=False,
75 help='Purifies the previously generated build output by '
76 'removing the temporary results used when (re)building.')
77 parser.add_argument(
78 '-o',
79 '--output-dir',
80 type=os.path.abspath,
81 default=SDK_OUTPUT_DIR,
82 help='Specifies a directory to output the build artifacts to. '
83 'If specified together with -c, deletes the dir.')
84 parser.add_argument(
85 '-r',
86 '--revision',
87 type=int,
88 default=0,
89 help='Specifies a revision number to embed if building the framework.')
90 parser.add_argument('-e',
91 '--bitcode',
92 action='store_true',
93 default=False,
94 help='Compile with bitcode.')
95 parser.add_argument('--verbose',
96 action='store_true',
97 default=False,
98 help='Debug logging.')
99 parser.add_argument('--use-goma',
100 action='store_true',
101 default=False,
102 help='Use goma to build.')
103 parser.add_argument(
104 '--extra-gn-args',
105 default=[],
106 nargs='*',
107 help='Additional GN args to be used during Ninja generation.')
mbonadei585209b2017-02-13 04:59:27 -0800108
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100109 return parser.parse_args()
oprypin7a2d8ca2017-02-06 07:53:41 -0800110
111
112def _RunCommand(cmd):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100113 logging.debug('Running: %r', cmd)
114 subprocess.check_call(cmd, cwd=SRC_DIR)
oprypin7a2d8ca2017-02-06 07:53:41 -0800115
116
117def _CleanArtifacts(output_dir):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100118 if os.path.isdir(output_dir):
119 logging.info('Deleting %s', output_dir)
120 shutil.rmtree(output_dir)
oprypin7a2d8ca2017-02-06 07:53:41 -0800121
122
VladimirTechMan7b188e82017-03-14 03:12:35 -0700123def _CleanTemporary(output_dir, architectures):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100124 if os.path.isdir(output_dir):
125 logging.info('Removing temporary build files.')
126 for arch in architectures:
127 arch_lib_path = os.path.join(output_dir, arch)
128 if os.path.isdir(arch_lib_path):
129 shutil.rmtree(arch_lib_path)
VladimirTechMan7b188e82017-03-14 03:12:35 -0700130
131
Byoungchan Leec41093b2021-07-06 18:52:21 +0900132def _ParseArchitecture(architectures):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100133 result = dict()
134 for arch in architectures:
135 if ":" in arch:
136 target_environment, target_cpu = arch.split(":")
137 else:
138 logging.warning('The environment for build is not specified.')
139 logging.warning('It is assumed based on cpu type.')
140 logging.warning('See crbug.com/1138425 for more details.')
141 if arch == "x64":
142 target_environment = "simulator"
143 else:
144 target_environment = "device"
145 target_cpu = arch
146 archs = result.get(target_environment)
147 if archs is None:
148 result[target_environment] = {target_cpu}
149 else:
150 archs.add(target_cpu)
Byoungchan Leec41093b2021-07-06 18:52:21 +0900151
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100152 return result
Byoungchan Leec41093b2021-07-06 18:52:21 +0900153
154
155def BuildWebRTC(output_dir, target_environment, target_arch, flavor,
156 gn_target_name, ios_deployment_target, libvpx_build_vp9,
157 use_bitcode, use_goma, extra_gn_args):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100158 gn_args = [
159 'target_os="ios"',
160 'ios_enable_code_signing=false',
161 'is_component_build=false',
162 'rtc_include_tests=false',
163 ]
oprypin7a2d8ca2017-02-06 07:53:41 -0800164
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100165 # Add flavor option.
166 if flavor == 'debug':
167 gn_args.append('is_debug=true')
168 elif flavor == 'release':
169 gn_args.append('is_debug=false')
170 else:
171 raise ValueError('Unexpected flavor type: %s' % flavor)
oprypin7a2d8ca2017-02-06 07:53:41 -0800172
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100173 gn_args.append('target_environment="%s"' % target_environment)
Byoungchan Leec41093b2021-07-06 18:52:21 +0900174
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100175 gn_args.append('target_cpu="%s"' % target_arch)
oprypin7a2d8ca2017-02-06 07:53:41 -0800176
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100177 gn_args.append('ios_deployment_target="%s"' % ios_deployment_target)
oprypin7a2d8ca2017-02-06 07:53:41 -0800178
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100179 gn_args.append('rtc_libvpx_build_vp9=' +
180 ('true' if libvpx_build_vp9 else 'false'))
oprypin7a2d8ca2017-02-06 07:53:41 -0800181
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100182 gn_args.append('enable_ios_bitcode=' + ('true' if use_bitcode else 'false'))
183 gn_args.append('use_goma=' + ('true' if use_goma else 'false'))
184 gn_args.append('rtc_enable_objc_symbol_export=true')
oprypin7a2d8ca2017-02-06 07:53:41 -0800185
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100186 args_string = ' '.join(gn_args + extra_gn_args)
187 logging.info('Building WebRTC with args: %s', args_string)
mbonadei8714b8f2017-02-15 13:18:57 -0800188
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100189 cmd = [
190 sys.executable,
191 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
192 'gen',
193 output_dir,
194 '--args=' + args_string,
195 ]
196 _RunCommand(cmd)
197 logging.info('Building target: %s', gn_target_name)
mbonadei8714b8f2017-02-15 13:18:57 -0800198
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100199 cmd = [
200 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'ninja'),
201 '-C',
202 output_dir,
203 gn_target_name,
204 ]
205 if use_goma:
206 cmd.extend(['-j', '200'])
207 _RunCommand(cmd)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100208
oprypin7a2d8ca2017-02-06 07:53:41 -0800209
oprypin7a2d8ca2017-02-06 07:53:41 -0800210def main():
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100211 args = _ParseArgs()
oprypin7a2d8ca2017-02-06 07:53:41 -0800212
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100213 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
oprypin7a2d8ca2017-02-06 07:53:41 -0800214
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100215 if args.clean:
216 _CleanArtifacts(args.output_dir)
217 return 0
oprypin7a2d8ca2017-02-06 07:53:41 -0800218
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100219 # architectures is typed as Dict[str, Set[str]],
220 # where key is for the environment (device or simulator)
221 # and value is for the cpu type.
222 architectures = _ParseArchitecture(args.arch)
223 gn_args = args.extra_gn_args
VladimirTechMan7b188e82017-03-14 03:12:35 -0700224
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100225 if args.purify:
226 _CleanTemporary(args.output_dir, list(architectures.keys()))
227 return 0
VladimirTechMan7b188e82017-03-14 03:12:35 -0700228
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100229 gn_target_name = 'framework_objc'
230 if not args.bitcode:
231 gn_args.append('enable_dsyms=true')
232 gn_args.append('enable_stripping=true')
Kári Tristan Helgason1edbda02017-06-13 10:45:42 +0200233
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100234 # Build all architectures.
235 framework_paths = []
236 all_lib_paths = []
237 for (environment, archs) in list(architectures.items()):
238 framework_path = os.path.join(args.output_dir, environment)
239 framework_paths.append(framework_path)
240 lib_paths = []
241 for arch in archs:
242 lib_path = os.path.join(framework_path, arch + '_libs')
243 lib_paths.append(lib_path)
244 BuildWebRTC(lib_path, environment, arch, args.build_config,
245 gn_target_name, IOS_DEPLOYMENT_TARGET[environment],
246 LIBVPX_BUILD_VP9, args.bitcode, args.use_goma, gn_args)
247 all_lib_paths.extend(lib_paths)
Kári Tristan Helgason1edbda02017-06-13 10:45:42 +0200248
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100249 # Combine the slices.
250 dylib_path = os.path.join(SDK_FRAMEWORK_NAME, 'WebRTC')
251 # Dylibs will be combined, all other files are the same across archs.
252 shutil.rmtree(os.path.join(framework_path, SDK_FRAMEWORK_NAME),
253 ignore_errors=True)
254 shutil.copytree(os.path.join(lib_paths[0], SDK_FRAMEWORK_NAME),
255 os.path.join(framework_path, SDK_FRAMEWORK_NAME),
256 symlinks=True)
257 logging.info('Merging framework slices for %s.', environment)
258 dylib_paths = [os.path.join(path, dylib_path) for path in lib_paths]
259 out_dylib_path = os.path.join(framework_path, dylib_path)
260 if os.path.islink(out_dylib_path):
261 out_dylib_path = os.path.join(os.path.dirname(out_dylib_path),
262 os.readlink(out_dylib_path))
263 try:
264 os.remove(out_dylib_path)
265 except OSError:
266 pass
267 cmd = ['lipo'] + dylib_paths + ['-create', '-output', out_dylib_path]
Byoungchan Leec41093b2021-07-06 18:52:21 +0900268 _RunCommand(cmd)
269
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100270 # Merge the dSYM slices.
271 lib_dsym_dir_path = os.path.join(lib_paths[0], SDK_DSYM_NAME)
272 if os.path.isdir(lib_dsym_dir_path):
273 shutil.rmtree(os.path.join(framework_path, SDK_DSYM_NAME),
274 ignore_errors=True)
275 shutil.copytree(lib_dsym_dir_path,
276 os.path.join(framework_path, SDK_DSYM_NAME))
277 logging.info('Merging dSYM slices.')
278 dsym_path = os.path.join(SDK_DSYM_NAME, 'Contents', 'Resources', 'DWARF',
279 'WebRTC')
280 lib_dsym_paths = [os.path.join(path, dsym_path) for path in lib_paths]
281 out_dsym_path = os.path.join(framework_path, dsym_path)
282 try:
283 os.remove(out_dsym_path)
284 except OSError:
285 pass
286 cmd = ['lipo'] + lib_dsym_paths + ['-create', '-output', out_dsym_path]
287 _RunCommand(cmd)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100288
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100289 # Check for Mac-style WebRTC.framework/Resources/ (for Catalyst)...
290 resources_dir = os.path.join(framework_path, SDK_FRAMEWORK_NAME,
291 'Resources')
292 if not os.path.exists(resources_dir):
293 # ...then fall back to iOS-style WebRTC.framework/
294 resources_dir = os.path.dirname(resources_dir)
295
296 # Modify the version number.
297 # Format should be <Branch cut MXX>.<Hotfix #>.<Rev #>.
298 # e.g. 55.0.14986 means
299 # branch cut 55, no hotfixes, and revision 14986.
300 infoplist_path = os.path.join(resources_dir, 'Info.plist')
301 cmd = [
302 'PlistBuddy', '-c', 'Print :CFBundleShortVersionString',
303 infoplist_path
304 ]
305 major_minor = subprocess.check_output(cmd).decode('utf-8').strip()
306 version_number = '%s.%s' % (major_minor, args.revision)
307 logging.info('Substituting revision number: %s', version_number)
308 cmd = [
309 'PlistBuddy', '-c', 'Set :CFBundleVersion ' + version_number,
310 infoplist_path
311 ]
312 _RunCommand(cmd)
313 _RunCommand(['plutil', '-convert', 'binary1', infoplist_path])
314
315 xcframework_dir = os.path.join(args.output_dir, SDK_XCFRAMEWORK_NAME)
316 if os.path.isdir(xcframework_dir):
317 shutil.rmtree(xcframework_dir)
318
319 logging.info('Creating xcframework.')
320 cmd = ['xcodebuild', '-create-xcframework', '-output', xcframework_dir]
321
322 # Apparently, xcodebuild needs absolute paths for input arguments
323 for framework_path in framework_paths:
324 cmd += [
325 '-framework',
326 os.path.abspath(os.path.join(framework_path, SDK_FRAMEWORK_NAME)),
327 ]
328 dsym_full_path = os.path.join(framework_path, SDK_DSYM_NAME)
329 if os.path.exists(dsym_full_path):
330 cmd += ['-debug-symbols', os.path.abspath(dsym_full_path)]
331
332 _RunCommand(cmd)
333
334 # Generate the license file.
335 logging.info('Generate license file.')
336 gn_target_full_name = '//sdk:' + gn_target_name
337 builder = LicenseBuilder(all_lib_paths, [gn_target_full_name])
338 builder.GenerateLicenseText(
339 os.path.join(args.output_dir, SDK_XCFRAMEWORK_NAME))
340
341 logging.info('Done.')
342 return 0
oprypin7a2d8ca2017-02-06 07:53:41 -0800343
344
345if __name__ == '__main__':
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100346 sys.exit(main())