oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 1 | #!/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. |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 10 | """WebRTC iOS FAT libraries build script. |
| 11 | Each architecture is compiled separately before being merged together. |
| 12 | By default, the library is created in out_ios_libs/. (Change with -o.) |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 13 | """ |
| 14 | |
| 15 | import argparse |
| 16 | import distutils.dir_util |
| 17 | import logging |
| 18 | import os |
| 19 | import shutil |
| 20 | import subprocess |
| 21 | import sys |
| 22 | |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 23 | os.environ['PATH'] = '/usr/libexec' + os.pathsep + os.environ['PATH'] |
| 24 | |
| 25 | SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
Henrik Kjellander | ec57e05 | 2017-10-17 21:36:01 +0200 | [diff] [blame] | 26 | SRC_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, '..', '..')) |
| 27 | sys.path.append(os.path.join(SRC_DIR, 'build')) |
| 28 | import find_depot_tools |
| 29 | |
| 30 | SDK_OUTPUT_DIR = os.path.join(SRC_DIR, 'out_ios_libs') |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 31 | SDK_FRAMEWORK_NAME = 'WebRTC.framework' |
| 32 | |
oprypin | 9b0dbd4 | 2017-02-13 08:30:01 -0800 | [diff] [blame] | 33 | DEFAULT_ARCHS = ENABLED_ARCHS = ['arm64', 'arm', 'x64', 'x86'] |
Kári Tristan Helgason | ffe9376 | 2019-03-07 13:14:32 +0100 | [diff] [blame] | 34 | IOS_DEPLOYMENT_TARGET = '10.0' |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 35 | LIBVPX_BUILD_VP9 = False |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 36 | |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 37 | sys.path.append(os.path.join(SCRIPT_DIR, '..', 'libs')) |
| 38 | from generate_licenses import LicenseBuilder |
| 39 | |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 40 | |
| 41 | def _ParseArgs(): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 42 | parser = argparse.ArgumentParser(description=__doc__) |
| 43 | parser.add_argument('--build_config', |
| 44 | default='release', |
| 45 | choices=['debug', 'release'], |
| 46 | help='The build config. Can be "debug" or "release". ' |
| 47 | 'Defaults to "release".') |
| 48 | parser.add_argument( |
| 49 | '--arch', |
| 50 | nargs='+', |
| 51 | default=DEFAULT_ARCHS, |
| 52 | choices=ENABLED_ARCHS, |
| 53 | help='Architectures to build. Defaults to %(default)s.') |
| 54 | parser.add_argument( |
| 55 | '-c', |
| 56 | '--clean', |
| 57 | action='store_true', |
| 58 | default=False, |
| 59 | help='Removes the previously generated build output, if any.') |
| 60 | parser.add_argument( |
| 61 | '-p', |
| 62 | '--purify', |
| 63 | action='store_true', |
| 64 | default=False, |
| 65 | help='Purifies the previously generated build output by ' |
| 66 | 'removing the temporary results used when (re)building.') |
| 67 | parser.add_argument( |
| 68 | '-o', |
| 69 | '--output-dir', |
Yura Yaroshevich | 686ad4f | 2021-02-10 22:32:20 +0300 | [diff] [blame] | 70 | type=os.path.abspath, |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 71 | default=SDK_OUTPUT_DIR, |
| 72 | help='Specifies a directory to output the build artifacts to. ' |
| 73 | 'If specified together with -c, deletes the dir.') |
| 74 | parser.add_argument( |
| 75 | '-r', |
| 76 | '--revision', |
| 77 | type=int, |
| 78 | default=0, |
| 79 | help='Specifies a revision number to embed if building the framework.') |
| 80 | parser.add_argument('-e', |
| 81 | '--bitcode', |
| 82 | action='store_true', |
| 83 | default=False, |
| 84 | help='Compile with bitcode.') |
| 85 | parser.add_argument('--verbose', |
| 86 | action='store_true', |
| 87 | default=False, |
| 88 | help='Debug logging.') |
| 89 | parser.add_argument('--use-goma', |
| 90 | action='store_true', |
| 91 | default=False, |
| 92 | help='Use goma to build.') |
| 93 | parser.add_argument( |
| 94 | '--extra-gn-args', |
| 95 | default=[], |
| 96 | nargs='*', |
| 97 | help='Additional GN args to be used during Ninja generation.') |
mbonadei | 585209b | 2017-02-13 04:59:27 -0800 | [diff] [blame] | 98 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 99 | return parser.parse_args() |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 100 | |
| 101 | |
| 102 | def _RunCommand(cmd): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 103 | logging.debug('Running: %r', cmd) |
| 104 | subprocess.check_call(cmd, cwd=SRC_DIR) |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 105 | |
| 106 | |
| 107 | def _CleanArtifacts(output_dir): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 108 | if os.path.isdir(output_dir): |
| 109 | logging.info('Deleting %s', output_dir) |
| 110 | shutil.rmtree(output_dir) |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 111 | |
| 112 | |
VladimirTechMan | 7b188e8 | 2017-03-14 03:12:35 -0700 | [diff] [blame] | 113 | def _CleanTemporary(output_dir, architectures): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 114 | if os.path.isdir(output_dir): |
| 115 | logging.info('Removing temporary build files.') |
| 116 | for arch in architectures: |
| 117 | arch_lib_path = os.path.join(output_dir, arch + '_libs') |
| 118 | if os.path.isdir(arch_lib_path): |
| 119 | shutil.rmtree(arch_lib_path) |
VladimirTechMan | 7b188e8 | 2017-03-14 03:12:35 -0700 | [diff] [blame] | 120 | |
| 121 | |
Kári Tristan Helgason | 1edbda0 | 2017-06-13 10:45:42 +0200 | [diff] [blame] | 122 | def BuildWebRTC(output_dir, target_arch, flavor, gn_target_name, |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 123 | ios_deployment_target, libvpx_build_vp9, use_bitcode, use_goma, |
| 124 | extra_gn_args): |
| 125 | output_dir = os.path.join(output_dir, target_arch + '_libs') |
| 126 | gn_args = [ |
| 127 | 'target_os="ios"', 'ios_enable_code_signing=false', |
Mirko Bonadei | 58678a0 | 2020-12-01 10:54:40 +0100 | [diff] [blame] | 128 | 'use_xcode_clang=true', 'is_component_build=false', |
| 129 | 'rtc_include_tests=false', |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 130 | ] |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 131 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 132 | # Add flavor option. |
| 133 | if flavor == 'debug': |
| 134 | gn_args.append('is_debug=true') |
| 135 | elif flavor == 'release': |
| 136 | gn_args.append('is_debug=false') |
| 137 | else: |
| 138 | raise ValueError('Unexpected flavor type: %s' % flavor) |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 139 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 140 | gn_args.append('target_cpu="%s"' % target_arch) |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 141 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 142 | gn_args.append('ios_deployment_target="%s"' % ios_deployment_target) |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 143 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 144 | gn_args.append('rtc_libvpx_build_vp9=' + |
| 145 | ('true' if libvpx_build_vp9 else 'false')) |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 146 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 147 | gn_args.append('enable_ios_bitcode=' + |
| 148 | ('true' if use_bitcode else 'false')) |
| 149 | gn_args.append('use_goma=' + ('true' if use_goma else 'false')) |
Mirko Bonadei | c1254e8 | 2020-12-17 15:56:13 +0100 | [diff] [blame] | 150 | gn_args.append('rtc_enable_symbol_export=true') |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 151 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 152 | args_string = ' '.join(gn_args + extra_gn_args) |
| 153 | logging.info('Building WebRTC with args: %s', args_string) |
mbonadei | 8714b8f | 2017-02-15 13:18:57 -0800 | [diff] [blame] | 154 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 155 | cmd = [ |
| 156 | sys.executable, |
| 157 | os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'), |
| 158 | 'gen', |
| 159 | output_dir, |
| 160 | '--args=' + args_string, |
| 161 | ] |
| 162 | _RunCommand(cmd) |
| 163 | logging.info('Building target: %s', gn_target_name) |
mbonadei | 8714b8f | 2017-02-15 13:18:57 -0800 | [diff] [blame] | 164 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 165 | cmd = [ |
| 166 | os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'ninja'), |
| 167 | '-C', |
| 168 | output_dir, |
| 169 | gn_target_name, |
| 170 | ] |
| 171 | if use_goma: |
| 172 | cmd.extend(['-j', '200']) |
| 173 | _RunCommand(cmd) |
| 174 | |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 175 | |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 176 | def main(): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 177 | args = _ParseArgs() |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 178 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 179 | logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO) |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 180 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 181 | if args.clean: |
| 182 | _CleanArtifacts(args.output_dir) |
| 183 | return 0 |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 184 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 185 | architectures = list(args.arch) |
| 186 | gn_args = args.extra_gn_args |
VladimirTechMan | 7b188e8 | 2017-03-14 03:12:35 -0700 | [diff] [blame] | 187 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 188 | if args.purify: |
| 189 | _CleanTemporary(args.output_dir, architectures) |
| 190 | return 0 |
VladimirTechMan | 7b188e8 | 2017-03-14 03:12:35 -0700 | [diff] [blame] | 191 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 192 | gn_target_name = 'framework_objc' |
| 193 | if not args.bitcode: |
| 194 | gn_args.append('enable_dsyms=true') |
| 195 | gn_args.append('enable_stripping=true') |
Kári Tristan Helgason | 1edbda0 | 2017-06-13 10:45:42 +0200 | [diff] [blame] | 196 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 197 | # Build all architectures. |
| 198 | for arch in architectures: |
| 199 | BuildWebRTC(args.output_dir, arch, args.build_config, gn_target_name, |
| 200 | IOS_DEPLOYMENT_TARGET, LIBVPX_BUILD_VP9, args.bitcode, |
| 201 | args.use_goma, gn_args) |
Kári Tristan Helgason | 1edbda0 | 2017-06-13 10:45:42 +0200 | [diff] [blame] | 202 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 203 | # Create FAT archive. |
| 204 | lib_paths = [ |
| 205 | os.path.join(args.output_dir, arch + '_libs') for arch in architectures |
| 206 | ] |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 207 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 208 | # Combine the slices. |
| 209 | dylib_path = os.path.join(SDK_FRAMEWORK_NAME, 'WebRTC') |
| 210 | # Dylibs will be combined, all other files are the same across archs. |
| 211 | # Use distutils instead of shutil to support merging folders. |
| 212 | distutils.dir_util.copy_tree( |
| 213 | os.path.join(lib_paths[0], SDK_FRAMEWORK_NAME), |
sakal | 67e414c | 2017-09-05 00:16:15 -0700 | [diff] [blame] | 214 | os.path.join(args.output_dir, SDK_FRAMEWORK_NAME)) |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 215 | logging.info('Merging framework slices.') |
| 216 | dylib_paths = [os.path.join(path, dylib_path) for path in lib_paths] |
| 217 | out_dylib_path = os.path.join(args.output_dir, dylib_path) |
| 218 | try: |
| 219 | os.remove(out_dylib_path) |
| 220 | except OSError: |
| 221 | pass |
| 222 | cmd = ['lipo'] + dylib_paths + ['-create', '-output', out_dylib_path] |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 223 | _RunCommand(cmd) |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 224 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 225 | # Merge the dSYM slices. |
| 226 | lib_dsym_dir_path = os.path.join(lib_paths[0], 'WebRTC.dSYM') |
| 227 | if os.path.isdir(lib_dsym_dir_path): |
| 228 | distutils.dir_util.copy_tree( |
| 229 | lib_dsym_dir_path, os.path.join(args.output_dir, 'WebRTC.dSYM')) |
| 230 | logging.info('Merging dSYM slices.') |
| 231 | dsym_path = os.path.join('WebRTC.dSYM', 'Contents', 'Resources', |
| 232 | 'DWARF', 'WebRTC') |
| 233 | lib_dsym_paths = [os.path.join(path, dsym_path) for path in lib_paths] |
| 234 | out_dsym_path = os.path.join(args.output_dir, dsym_path) |
| 235 | try: |
| 236 | os.remove(out_dsym_path) |
| 237 | except OSError: |
| 238 | pass |
| 239 | cmd = ['lipo'] + lib_dsym_paths + ['-create', '-output', out_dsym_path] |
| 240 | _RunCommand(cmd) |
| 241 | |
| 242 | # Generate the license file. |
| 243 | ninja_dirs = [ |
| 244 | os.path.join(args.output_dir, arch + '_libs') |
| 245 | for arch in architectures |
| 246 | ] |
| 247 | gn_target_full_name = '//sdk:' + gn_target_name |
| 248 | builder = LicenseBuilder(ninja_dirs, [gn_target_full_name]) |
| 249 | builder.GenerateLicenseText( |
| 250 | os.path.join(args.output_dir, SDK_FRAMEWORK_NAME)) |
| 251 | |
| 252 | # Modify the version number. |
| 253 | # Format should be <Branch cut MXX>.<Hotfix #>.<Rev #>. |
| 254 | # e.g. 55.0.14986 means branch cut 55, no hotfixes, and revision 14986. |
| 255 | infoplist_path = os.path.join(args.output_dir, SDK_FRAMEWORK_NAME, |
| 256 | 'Info.plist') |
| 257 | cmd = [ |
| 258 | 'PlistBuddy', '-c', 'Print :CFBundleShortVersionString', |
| 259 | infoplist_path |
| 260 | ] |
| 261 | major_minor = subprocess.check_output(cmd).strip() |
| 262 | version_number = '%s.%s' % (major_minor, args.revision) |
| 263 | logging.info('Substituting revision number: %s', version_number) |
| 264 | cmd = [ |
| 265 | 'PlistBuddy', '-c', 'Set :CFBundleVersion ' + version_number, |
| 266 | infoplist_path |
| 267 | ] |
| 268 | _RunCommand(cmd) |
| 269 | _RunCommand(['plutil', '-convert', 'binary1', infoplist_path]) |
| 270 | |
| 271 | logging.info('Done.') |
| 272 | return 0 |
oprypin | 7a2d8ca | 2017-02-06 07:53:41 -0800 | [diff] [blame] | 273 | |
| 274 | |
| 275 | if __name__ == '__main__': |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 276 | sys.exit(main()) |