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.) |
| 14 | The headers will be copied to out_ios_libs/include. |
| 15 | """ |
| 16 | |
| 17 | import argparse |
| 18 | import distutils.dir_util |
| 19 | import logging |
| 20 | import os |
| 21 | import shutil |
| 22 | import subprocess |
| 23 | import sys |
| 24 | |
| 25 | |
| 26 | os.environ['PATH'] = '/usr/libexec' + os.pathsep + os.environ['PATH'] |
| 27 | |
| 28 | SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 29 | WEBRTC_BASE_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, '..', '..')) |
| 30 | SDK_OUTPUT_DIR = os.path.join(WEBRTC_BASE_DIR, 'out_ios_libs') |
| 31 | SDK_LIB_NAME = 'librtc_sdk_objc.a' |
| 32 | SDK_FRAMEWORK_NAME = 'WebRTC.framework' |
| 33 | |
| 34 | ENABLED_ARCHITECTURES = ['arm', 'arm64', 'x64'] |
| 35 | IOS_DEPLOYMENT_TARGET = '8.0' |
| 36 | LIBVPX_BUILD_VP9 = False |
| 37 | CUSTOM_GN_OPTS = [] # example: ['some_option=foo bar', 'other_option=true'] |
| 38 | |
| 39 | |
| 40 | def _ParseArgs(): |
| 41 | parser = argparse.ArgumentParser(description=__doc__) |
| 42 | parser.add_argument('-b', '--build_type', default='framework', |
| 43 | choices=['framework', 'static_only'], |
| 44 | help='The build type. Can be "framework" or "static_only". ' |
| 45 | 'Defaults to "framework".') |
| 46 | parser.add_argument('--build_config', default='release', |
| 47 | choices=['debug', 'release'], |
| 48 | help='The build config. Can be "debug" or "release". ' |
| 49 | 'Defaults to "release".') |
| 50 | parser.add_argument('-c', '--clean', action='store_true', default=False, |
| 51 | help='Removes the previously generated build output, if any.') |
| 52 | parser.add_argument('-o', '--output-dir', default=SDK_OUTPUT_DIR, |
| 53 | help='Specifies a directory to output the build artifacts to. ' |
| 54 | 'If specified together with -c, deletes the dir.') |
| 55 | parser.add_argument('-r', '--revision', type=int, default=0, |
| 56 | help='Specifies a revision number to embed if building the framework.') |
| 57 | parser.add_argument('-e', '--bitcode', action='store_true', default=False, |
| 58 | help='Compile with bitcode.') |
| 59 | parser.add_argument('--verbose', action='store_true', default=False, |
| 60 | help='Debug logging.') |
| 61 | return parser.parse_args() |
| 62 | |
| 63 | |
| 64 | def _RunCommand(cmd): |
| 65 | logging.debug('Running: %r', cmd) |
| 66 | subprocess.check_call(cmd) |
| 67 | |
| 68 | |
| 69 | def _CleanArtifacts(output_dir): |
| 70 | if os.path.isdir(output_dir): |
| 71 | logging.info('Deleting %s', output_dir) |
| 72 | shutil.rmtree(output_dir) |
| 73 | |
| 74 | |
| 75 | def BuildWebRTC(output_dir, target_arch, flavor, build_type, |
| 76 | ios_deployment_target, libvpx_build_vp9, use_bitcode, |
| 77 | custom_gn_options=()): |
| 78 | output_dir = os.path.join(output_dir, target_arch + '_libs') |
| 79 | gn_args = ['target_os="ios"', 'ios_enable_code_signing=false', |
| 80 | 'use_xcode_clang=true', 'is_component_build=false'] |
| 81 | |
| 82 | # Add flavor option. |
| 83 | if flavor == 'debug': |
| 84 | gn_args.append('is_debug=true') |
| 85 | elif flavor == 'release': |
| 86 | gn_args.append('is_debug=false') |
| 87 | else: |
| 88 | raise ValueError('Unexpected flavor type: %s' % flavor) |
| 89 | |
| 90 | gn_args.append('target_cpu="%s"' % target_arch) |
| 91 | |
| 92 | gn_args.append('ios_deployment_target="%s"' % ios_deployment_target) |
| 93 | |
| 94 | gn_args.append('rtc_libvpx_build_vp9=' + |
| 95 | ('true' if libvpx_build_vp9 else 'false')) |
| 96 | |
| 97 | gn_args.append('enable_ios_bitcode=' + |
| 98 | ('true' if use_bitcode else 'false')) |
| 99 | |
| 100 | gn_args.extend(custom_gn_options) |
| 101 | |
| 102 | # Generate static or dynamic. |
| 103 | if build_type == 'static_only': |
| 104 | gn_target_name = 'rtc_sdk_objc' |
| 105 | elif build_type == 'framework': |
| 106 | gn_target_name = 'rtc_sdk_framework_objc' |
| 107 | gn_args.append('enable_dsyms=true') |
| 108 | gn_args.append('enable_stripping=true') |
| 109 | else: |
| 110 | raise ValueError('Build type "%s" is not supported.' % build_type) |
| 111 | |
| 112 | logging.info('Building WebRTC with args: %s', ' '.join(gn_args)) |
| 113 | cmd = ['gn', 'gen', output_dir, |
| 114 | '--args=' + ' '.join(gn_args)] |
| 115 | _RunCommand(cmd) |
| 116 | logging.info('Building target: %s', gn_target_name) |
| 117 | cmd = ['ninja', '-C', output_dir, gn_target_name] |
| 118 | _RunCommand(cmd) |
| 119 | |
| 120 | # Strip debug symbols to reduce size. |
| 121 | if build_type == 'static_only': |
| 122 | gn_target_path = os.path.join(output_dir, 'obj', 'webrtc', 'sdk', |
| 123 | 'lib%s.a' % gn_target_name) |
| 124 | cmd = ['strip', '-S', gn_target_path, '-o', |
| 125 | os.path.join(output_dir, 'lib%s.a' % gn_target_name)] |
| 126 | _RunCommand(cmd) |
| 127 | |
| 128 | |
| 129 | def main(): |
| 130 | args = _ParseArgs() |
| 131 | |
| 132 | logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO) |
| 133 | |
| 134 | if args.clean: |
| 135 | _CleanArtifacts(args.output_dir) |
| 136 | return 0 |
| 137 | |
| 138 | # Build all architectures. |
| 139 | for arch in ENABLED_ARCHITECTURES: |
| 140 | BuildWebRTC(args.output_dir, arch, args.build_config, args.build_type, |
| 141 | IOS_DEPLOYMENT_TARGET, LIBVPX_BUILD_VP9, args.bitcode, |
| 142 | CUSTOM_GN_OPTS) |
| 143 | |
| 144 | # Ignoring x86 except for static libraries for now because of a GN build issue |
| 145 | # where the generated dynamic framework has the wrong architectures. |
| 146 | |
| 147 | # Create FAT archive. |
| 148 | if args.build_type == 'static_only': |
| 149 | BuildWebRTC(args.output_dir, 'x86', args.build_config, args.build_type, |
| 150 | IOS_DEPLOYMENT_TARGET, LIBVPX_BUILD_VP9, args.bitcode, |
| 151 | CUSTOM_GN_OPTS) |
| 152 | |
| 153 | arm_lib_path = os.path.join(args.output_dir, 'arm_libs', SDK_LIB_NAME) |
| 154 | arm64_lib_path = os.path.join(args.output_dir, 'arm64_libs', SDK_LIB_NAME) |
| 155 | x64_lib_path = os.path.join(args.output_dir, 'x64_libs', SDK_LIB_NAME) |
| 156 | x86_lib_path = os.path.join(args.output_dir, 'x86_libs', SDK_LIB_NAME) |
| 157 | |
| 158 | # Combine the slices. |
| 159 | cmd = ['lipo', arm_lib_path, arm64_lib_path, x64_lib_path, x86_lib_path, |
| 160 | '-create', '-output', os.path.join(args.output_dir, SDK_LIB_NAME)] |
| 161 | _RunCommand(cmd) |
| 162 | |
| 163 | elif args.build_type == 'framework': |
| 164 | arm_lib_path = os.path.join(args.output_dir, 'arm_libs') |
| 165 | arm64_lib_path = os.path.join(args.output_dir, 'arm64_libs') |
| 166 | x64_lib_path = os.path.join(args.output_dir, 'x64_libs') |
| 167 | |
| 168 | # Combine the slices. |
| 169 | dylib_path = os.path.join(SDK_FRAMEWORK_NAME, 'WebRTC') |
| 170 | # Use distutils instead of shutil to support merging folders. |
| 171 | distutils.dir_util.copy_tree( |
| 172 | os.path.join(arm64_lib_path, SDK_FRAMEWORK_NAME), |
| 173 | os.path.join(args.output_dir, SDK_FRAMEWORK_NAME)) |
| 174 | try: |
| 175 | os.remove(os.path.join(args.output_dir, dylib_path)) |
| 176 | except OSError: |
| 177 | pass |
| 178 | logging.info('Merging framework slices.') |
| 179 | cmd = ['lipo', os.path.join(arm_lib_path, dylib_path), |
| 180 | os.path.join(arm64_lib_path, dylib_path), |
| 181 | os.path.join(x64_lib_path, dylib_path), |
| 182 | '-create', '-output', os.path.join(args.output_dir, dylib_path)] |
| 183 | _RunCommand(cmd) |
| 184 | |
| 185 | # Merge the dSYM slices. |
| 186 | dsym_path = os.path.join('WebRTC.dSYM', 'Contents', 'Resources', 'DWARF', |
| 187 | 'WebRTC') |
| 188 | distutils.dir_util.copy_tree(os.path.join(arm64_lib_path, 'WebRTC.dSYM'), |
| 189 | os.path.join(args.output_dir, 'WebRTC.dSYM')) |
| 190 | try: |
| 191 | os.remove(os.path.join(args.output_dir, dsym_path)) |
| 192 | except OSError: |
| 193 | pass |
| 194 | logging.info('Merging dSYM slices.') |
| 195 | cmd = ['lipo', os.path.join(arm_lib_path, dsym_path), |
| 196 | os.path.join(arm64_lib_path, dsym_path), |
| 197 | os.path.join(x64_lib_path, dsym_path), |
| 198 | '-create', '-output', os.path.join(args.output_dir, dsym_path)] |
| 199 | _RunCommand(cmd) |
| 200 | |
| 201 | # Modify the version number. |
| 202 | # Format should be <Branch cut MXX>.<Hotfix #>.<Rev #>. |
| 203 | # e.g. 55.0.14986 means branch cut 55, no hotfixes, and revision 14986. |
| 204 | infoplist_path = os.path.join(args.output_dir, SDK_FRAMEWORK_NAME, |
| 205 | 'Info.plist') |
| 206 | cmd = ['PlistBuddy', '-c', |
| 207 | 'Print :CFBundleShortVersionString', infoplist_path] |
| 208 | major_minor = subprocess.check_output(cmd).strip() |
| 209 | version_number = '%s.%s' % (major_minor, args.revision) |
| 210 | logging.info('Substituting revision number: %s', version_number) |
| 211 | cmd = ['PlistBuddy', '-c', |
| 212 | 'Set :CFBundleVersion ' + version_number, infoplist_path] |
| 213 | _RunCommand(cmd) |
| 214 | _RunCommand(['plutil', '-convert', 'binary1', infoplist_path]) |
| 215 | |
| 216 | logging.info('Done.') |
| 217 | return 0 |
| 218 | |
| 219 | |
| 220 | if __name__ == '__main__': |
| 221 | sys.exit(main()) |