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