blob: 01a95b08c4bd2163fcb4086011da308397b62346 [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.')
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010090 parser.add_argument('--verbose',
91 action='store_true',
92 default=False,
93 help='Debug logging.')
94 parser.add_argument('--use-goma',
95 action='store_true',
96 default=False,
97 help='Use goma to build.')
98 parser.add_argument(
99 '--extra-gn-args',
100 default=[],
101 nargs='*',
102 help='Additional GN args to be used during Ninja generation.')
mbonadei585209b2017-02-13 04:59:27 -0800103
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100104 return parser.parse_args()
oprypin7a2d8ca2017-02-06 07:53:41 -0800105
106
107def _RunCommand(cmd):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100108 logging.debug('Running: %r', cmd)
109 subprocess.check_call(cmd, cwd=SRC_DIR)
oprypin7a2d8ca2017-02-06 07:53:41 -0800110
111
112def _CleanArtifacts(output_dir):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100113 if os.path.isdir(output_dir):
114 logging.info('Deleting %s', output_dir)
115 shutil.rmtree(output_dir)
oprypin7a2d8ca2017-02-06 07:53:41 -0800116
117
VladimirTechMan7b188e82017-03-14 03:12:35 -0700118def _CleanTemporary(output_dir, architectures):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100119 if os.path.isdir(output_dir):
120 logging.info('Removing temporary build files.')
121 for arch in architectures:
122 arch_lib_path = os.path.join(output_dir, arch)
123 if os.path.isdir(arch_lib_path):
124 shutil.rmtree(arch_lib_path)
VladimirTechMan7b188e82017-03-14 03:12:35 -0700125
126
Byoungchan Leec41093b2021-07-06 18:52:21 +0900127def _ParseArchitecture(architectures):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100128 result = dict()
129 for arch in architectures:
130 if ":" in arch:
131 target_environment, target_cpu = arch.split(":")
132 else:
133 logging.warning('The environment for build is not specified.')
134 logging.warning('It is assumed based on cpu type.')
135 logging.warning('See crbug.com/1138425 for more details.')
136 if arch == "x64":
137 target_environment = "simulator"
138 else:
139 target_environment = "device"
140 target_cpu = arch
141 archs = result.get(target_environment)
142 if archs is None:
143 result[target_environment] = {target_cpu}
144 else:
145 archs.add(target_cpu)
Byoungchan Leec41093b2021-07-06 18:52:21 +0900146
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100147 return result
Byoungchan Leec41093b2021-07-06 18:52:21 +0900148
149
150def BuildWebRTC(output_dir, target_environment, target_arch, flavor,
151 gn_target_name, ios_deployment_target, libvpx_build_vp9,
Sylvain Defresnea5f267d2022-07-01 16:24:47 +0200152 use_goma, extra_gn_args):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100153 gn_args = [
154 'target_os="ios"',
155 'ios_enable_code_signing=false',
156 'is_component_build=false',
157 'rtc_include_tests=false',
158 ]
oprypin7a2d8ca2017-02-06 07:53:41 -0800159
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100160 # Add flavor option.
161 if flavor == 'debug':
162 gn_args.append('is_debug=true')
163 elif flavor == 'release':
164 gn_args.append('is_debug=false')
165 else:
166 raise ValueError('Unexpected flavor type: %s' % flavor)
oprypin7a2d8ca2017-02-06 07:53:41 -0800167
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100168 gn_args.append('target_environment="%s"' % target_environment)
Byoungchan Leec41093b2021-07-06 18:52:21 +0900169
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100170 gn_args.append('target_cpu="%s"' % target_arch)
oprypin7a2d8ca2017-02-06 07:53:41 -0800171
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100172 gn_args.append('ios_deployment_target="%s"' % ios_deployment_target)
oprypin7a2d8ca2017-02-06 07:53:41 -0800173
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100174 gn_args.append('rtc_libvpx_build_vp9=' +
175 ('true' if libvpx_build_vp9 else 'false'))
oprypin7a2d8ca2017-02-06 07:53:41 -0800176
Sylvain Defresnea5f267d2022-07-01 16:24:47 +0200177 gn_args.append('use_lld=true')
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100178 gn_args.append('use_goma=' + ('true' if use_goma else 'false'))
179 gn_args.append('rtc_enable_objc_symbol_export=true')
oprypin7a2d8ca2017-02-06 07:53:41 -0800180
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100181 args_string = ' '.join(gn_args + extra_gn_args)
182 logging.info('Building WebRTC with args: %s', args_string)
mbonadei8714b8f2017-02-15 13:18:57 -0800183
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100184 cmd = [
185 sys.executable,
186 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
187 'gen',
188 output_dir,
189 '--args=' + args_string,
190 ]
191 _RunCommand(cmd)
192 logging.info('Building target: %s', gn_target_name)
mbonadei8714b8f2017-02-15 13:18:57 -0800193
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100194 cmd = [
195 os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'ninja'),
196 '-C',
197 output_dir,
198 gn_target_name,
199 ]
200 if use_goma:
201 cmd.extend(['-j', '200'])
202 _RunCommand(cmd)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100203
oprypin7a2d8ca2017-02-06 07:53:41 -0800204
oprypin7a2d8ca2017-02-06 07:53:41 -0800205def main():
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100206 args = _ParseArgs()
oprypin7a2d8ca2017-02-06 07:53:41 -0800207
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100208 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
oprypin7a2d8ca2017-02-06 07:53:41 -0800209
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100210 if args.clean:
211 _CleanArtifacts(args.output_dir)
212 return 0
oprypin7a2d8ca2017-02-06 07:53:41 -0800213
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100214 # architectures is typed as Dict[str, Set[str]],
215 # where key is for the environment (device or simulator)
216 # and value is for the cpu type.
217 architectures = _ParseArchitecture(args.arch)
218 gn_args = args.extra_gn_args
VladimirTechMan7b188e82017-03-14 03:12:35 -0700219
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100220 if args.purify:
221 _CleanTemporary(args.output_dir, list(architectures.keys()))
222 return 0
VladimirTechMan7b188e82017-03-14 03:12:35 -0700223
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100224 gn_target_name = 'framework_objc'
Sylvain Defresnea5f267d2022-07-01 16:24:47 +0200225 gn_args.append('enable_dsyms=true')
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100226 gn_args.append('enable_stripping=true')
Kári Tristan Helgason1edbda02017-06-13 10:45:42 +0200227
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100228 # Build all architectures.
229 framework_paths = []
230 all_lib_paths = []
231 for (environment, archs) in list(architectures.items()):
232 framework_path = os.path.join(args.output_dir, environment)
233 framework_paths.append(framework_path)
234 lib_paths = []
235 for arch in archs:
236 lib_path = os.path.join(framework_path, arch + '_libs')
237 lib_paths.append(lib_path)
238 BuildWebRTC(lib_path, environment, arch, args.build_config,
239 gn_target_name, IOS_DEPLOYMENT_TARGET[environment],
Sylvain Defresnea5f267d2022-07-01 16:24:47 +0200240 LIBVPX_BUILD_VP9, args.use_goma, gn_args)
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100241 all_lib_paths.extend(lib_paths)
Kári Tristan Helgason1edbda02017-06-13 10:45:42 +0200242
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100243 # Combine the slices.
244 dylib_path = os.path.join(SDK_FRAMEWORK_NAME, 'WebRTC')
245 # Dylibs will be combined, all other files are the same across archs.
246 shutil.rmtree(os.path.join(framework_path, SDK_FRAMEWORK_NAME),
247 ignore_errors=True)
248 shutil.copytree(os.path.join(lib_paths[0], SDK_FRAMEWORK_NAME),
249 os.path.join(framework_path, SDK_FRAMEWORK_NAME),
250 symlinks=True)
251 logging.info('Merging framework slices for %s.', environment)
252 dylib_paths = [os.path.join(path, dylib_path) for path in lib_paths]
253 out_dylib_path = os.path.join(framework_path, dylib_path)
254 if os.path.islink(out_dylib_path):
255 out_dylib_path = os.path.join(os.path.dirname(out_dylib_path),
256 os.readlink(out_dylib_path))
257 try:
258 os.remove(out_dylib_path)
259 except OSError:
260 pass
261 cmd = ['lipo'] + dylib_paths + ['-create', '-output', out_dylib_path]
Byoungchan Leec41093b2021-07-06 18:52:21 +0900262 _RunCommand(cmd)
263
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100264 # 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 shutil.rmtree(os.path.join(framework_path, SDK_DSYM_NAME),
268 ignore_errors=True)
269 shutil.copytree(lib_dsym_dir_path,
270 os.path.join(framework_path, SDK_DSYM_NAME))
271 logging.info('Merging dSYM slices.')
272 dsym_path = os.path.join(SDK_DSYM_NAME, 'Contents', 'Resources', 'DWARF',
273 'WebRTC')
274 lib_dsym_paths = [os.path.join(path, dsym_path) for path in lib_paths]
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'] + lib_dsym_paths + ['-create', '-output', out_dsym_path]
281 _RunCommand(cmd)
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100282
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100283 # Check for Mac-style WebRTC.framework/Resources/ (for Catalyst)...
284 resources_dir = os.path.join(framework_path, SDK_FRAMEWORK_NAME,
285 'Resources')
286 if not os.path.exists(resources_dir):
287 # ...then fall back to iOS-style WebRTC.framework/
288 resources_dir = os.path.dirname(resources_dir)
289
290 # Modify the version number.
291 # Format should be <Branch cut MXX>.<Hotfix #>.<Rev #>.
292 # e.g. 55.0.14986 means
293 # branch cut 55, no hotfixes, and revision 14986.
294 infoplist_path = os.path.join(resources_dir, 'Info.plist')
295 cmd = [
296 'PlistBuddy', '-c', 'Print :CFBundleShortVersionString',
297 infoplist_path
298 ]
299 major_minor = subprocess.check_output(cmd).decode('utf-8').strip()
300 version_number = '%s.%s' % (major_minor, args.revision)
301 logging.info('Substituting revision number: %s', version_number)
302 cmd = [
303 'PlistBuddy', '-c', 'Set :CFBundleVersion ' + version_number,
304 infoplist_path
305 ]
306 _RunCommand(cmd)
307 _RunCommand(['plutil', '-convert', 'binary1', infoplist_path])
308
309 xcframework_dir = os.path.join(args.output_dir, SDK_XCFRAMEWORK_NAME)
310 if os.path.isdir(xcframework_dir):
311 shutil.rmtree(xcframework_dir)
312
313 logging.info('Creating xcframework.')
314 cmd = ['xcodebuild', '-create-xcframework', '-output', xcframework_dir]
315
316 # Apparently, xcodebuild needs absolute paths for input arguments
317 for framework_path in framework_paths:
318 cmd += [
319 '-framework',
320 os.path.abspath(os.path.join(framework_path, SDK_FRAMEWORK_NAME)),
321 ]
322 dsym_full_path = os.path.join(framework_path, SDK_DSYM_NAME)
323 if os.path.exists(dsym_full_path):
324 cmd += ['-debug-symbols', os.path.abspath(dsym_full_path)]
325
326 _RunCommand(cmd)
327
328 # Generate the license file.
329 logging.info('Generate license file.')
330 gn_target_full_name = '//sdk:' + gn_target_name
331 builder = LicenseBuilder(all_lib_paths, [gn_target_full_name])
332 builder.GenerateLicenseText(
333 os.path.join(args.output_dir, SDK_XCFRAMEWORK_NAME))
334
335 logging.info('Done.')
336 return 0
oprypin7a2d8ca2017-02-06 07:53:41 -0800337
338
339if __name__ == '__main__':
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100340 sys.exit(main())